|
19 | 19 |
|
20 | 20 | package com.sk89q.jnbt; |
21 | 21 |
|
| 22 | +import com.google.common.collect.Maps; |
| 23 | +import com.sk89q.worldedit.util.nbt.BinaryTagIO; |
| 24 | +import com.sk89q.worldedit.util.nbt.CompoundBinaryTag; |
| 25 | + |
22 | 26 | import java.io.Closeable; |
| 27 | +import java.io.DataOutput; |
23 | 28 | import java.io.DataOutputStream; |
24 | 29 | import java.io.IOException; |
25 | 30 | import java.io.OutputStream; |
@@ -70,236 +75,10 @@ public NBTOutputStream(OutputStream os) throws IOException { |
70 | 75 | * if an I/O error occurs. |
71 | 76 | */ |
72 | 77 | public void writeNamedTag(String name, Tag tag) throws IOException { |
73 | | - checkNotNull(name); |
74 | | - checkNotNull(tag); |
75 | | - |
76 | | - int type = NBTUtils.getTypeCode(tag.getClass()); |
77 | | - byte[] nameBytes = name.getBytes(NBTConstants.CHARSET); |
78 | | - |
79 | | - os.writeByte(type); |
80 | | - os.writeShort(nameBytes.length); |
81 | | - os.write(nameBytes); |
82 | | - |
83 | | - if (type == NBTConstants.TYPE_END) { |
84 | | - throw new IOException("Named TAG_End not permitted."); |
85 | | - } |
86 | | - |
87 | | - writeTagPayload(tag); |
88 | | - } |
89 | | - |
90 | | - /** |
91 | | - * Writes tag payload. |
92 | | - * |
93 | | - * @param tag |
94 | | - * The tag. |
95 | | - * @throws IOException |
96 | | - * if an I/O error occurs. |
97 | | - */ |
98 | | - private void writeTagPayload(Tag tag) throws IOException { |
99 | | - int type = NBTUtils.getTypeCode(tag.getClass()); |
100 | | - switch (type) { |
101 | | - case NBTConstants.TYPE_END: |
102 | | - writeEndTagPayload((EndTag) tag); |
103 | | - break; |
104 | | - case NBTConstants.TYPE_BYTE: |
105 | | - writeByteTagPayload((ByteTag) tag); |
106 | | - break; |
107 | | - case NBTConstants.TYPE_SHORT: |
108 | | - writeShortTagPayload((ShortTag) tag); |
109 | | - break; |
110 | | - case NBTConstants.TYPE_INT: |
111 | | - writeIntTagPayload((IntTag) tag); |
112 | | - break; |
113 | | - case NBTConstants.TYPE_LONG: |
114 | | - writeLongTagPayload((LongTag) tag); |
115 | | - break; |
116 | | - case NBTConstants.TYPE_FLOAT: |
117 | | - writeFloatTagPayload((FloatTag) tag); |
118 | | - break; |
119 | | - case NBTConstants.TYPE_DOUBLE: |
120 | | - writeDoubleTagPayload((DoubleTag) tag); |
121 | | - break; |
122 | | - case NBTConstants.TYPE_BYTE_ARRAY: |
123 | | - writeByteArrayTagPayload((ByteArrayTag) tag); |
124 | | - break; |
125 | | - case NBTConstants.TYPE_STRING: |
126 | | - writeStringTagPayload((StringTag) tag); |
127 | | - break; |
128 | | - case NBTConstants.TYPE_LIST: |
129 | | - writeListTagPayload((ListTag) tag); |
130 | | - break; |
131 | | - case NBTConstants.TYPE_COMPOUND: |
132 | | - writeCompoundTagPayload((CompoundTag) tag); |
133 | | - break; |
134 | | - case NBTConstants.TYPE_INT_ARRAY: |
135 | | - writeIntArrayTagPayload((IntArrayTag) tag); |
136 | | - break; |
137 | | - case NBTConstants.TYPE_LONG_ARRAY: |
138 | | - writeLongArrayTagPayload((LongArrayTag) tag); |
139 | | - break; |
140 | | - default: |
141 | | - throw new IOException("Invalid tag type: " + type + "."); |
142 | | - } |
143 | | - } |
144 | | - |
145 | | - /** |
146 | | - * Writes a {@code TAG_Byte} tag. |
147 | | - * |
148 | | - * @param tag |
149 | | - * The tag. |
150 | | - * @throws IOException |
151 | | - * if an I/O error occurs. |
152 | | - */ |
153 | | - private void writeByteTagPayload(ByteTag tag) throws IOException { |
154 | | - os.writeByte(tag.getValue()); |
155 | | - } |
156 | | - |
157 | | - /** |
158 | | - * Writes a {@code TAG_Byte_Array} tag. |
159 | | - * |
160 | | - * @param tag |
161 | | - * The tag. |
162 | | - * @throws IOException |
163 | | - * if an I/O error occurs. |
164 | | - */ |
165 | | - private void writeByteArrayTagPayload(ByteArrayTag tag) throws IOException { |
166 | | - byte[] bytes = tag.getValue(); |
167 | | - os.writeInt(bytes.length); |
168 | | - os.write(bytes); |
169 | | - } |
170 | | - |
171 | | - /** |
172 | | - * Writes a {@code TAG_Compound} tag. |
173 | | - * |
174 | | - * @param tag |
175 | | - * The tag. |
176 | | - * @throws IOException |
177 | | - * if an I/O error occurs. |
178 | | - */ |
179 | | - private void writeCompoundTagPayload(CompoundTag tag) throws IOException { |
180 | | - for (Map.Entry<String, Tag> entry : tag.getValue().entrySet()) { |
181 | | - writeNamedTag(entry.getKey(), entry.getValue()); |
182 | | - } |
183 | | - os.writeByte((byte) 0); // end tag - better way? |
184 | | - } |
185 | | - |
186 | | - /** |
187 | | - * Writes a {@code TAG_List} tag. |
188 | | - * |
189 | | - * @param tag |
190 | | - * The tag. |
191 | | - * @throws IOException |
192 | | - * if an I/O error occurs. |
193 | | - */ |
194 | | - private void writeListTagPayload(ListTag tag) throws IOException { |
195 | | - Class<? extends Tag> clazz = tag.getType(); |
196 | | - List<Tag> tags = tag.getValue(); |
197 | | - int size = tags.size(); |
198 | | - |
199 | | - os.writeByte(NBTUtils.getTypeCode(clazz)); |
200 | | - os.writeInt(size); |
201 | | - for (Tag tag1 : tags) { |
202 | | - writeTagPayload(tag1); |
203 | | - } |
204 | | - } |
205 | | - |
206 | | - /** |
207 | | - * Writes a {@code TAG_String} tag. |
208 | | - * |
209 | | - * @param tag |
210 | | - * The tag. |
211 | | - * @throws IOException |
212 | | - * if an I/O error occurs. |
213 | | - */ |
214 | | - private void writeStringTagPayload(StringTag tag) throws IOException { |
215 | | - byte[] bytes = tag.getValue().getBytes(NBTConstants.CHARSET); |
216 | | - os.writeShort(bytes.length); |
217 | | - os.write(bytes); |
218 | | - } |
219 | | - |
220 | | - /** |
221 | | - * Writes a {@code TAG_Double} tag. |
222 | | - * |
223 | | - * @param tag |
224 | | - * The tag. |
225 | | - * @throws IOException |
226 | | - * if an I/O error occurs. |
227 | | - */ |
228 | | - private void writeDoubleTagPayload(DoubleTag tag) throws IOException { |
229 | | - os.writeDouble(tag.getValue()); |
230 | | - } |
231 | | - |
232 | | - /** |
233 | | - * Writes a {@code TAG_Float} tag. |
234 | | - * |
235 | | - * @param tag |
236 | | - * The tag. |
237 | | - * @throws IOException |
238 | | - * if an I/O error occurs. |
239 | | - */ |
240 | | - private void writeFloatTagPayload(FloatTag tag) throws IOException { |
241 | | - os.writeFloat(tag.getValue()); |
242 | | - } |
243 | | - |
244 | | - /** |
245 | | - * Writes a {@code TAG_Long} tag. |
246 | | - * |
247 | | - * @param tag |
248 | | - * The tag. |
249 | | - * @throws IOException |
250 | | - * if an I/O error occurs. |
251 | | - */ |
252 | | - private void writeLongTagPayload(LongTag tag) throws IOException { |
253 | | - os.writeLong(tag.getValue()); |
254 | | - } |
255 | | - |
256 | | - /** |
257 | | - * Writes a {@code TAG_Int} tag. |
258 | | - * |
259 | | - * @param tag |
260 | | - * The tag. |
261 | | - * @throws IOException |
262 | | - * if an I/O error occurs. |
263 | | - */ |
264 | | - private void writeIntTagPayload(IntTag tag) throws IOException { |
265 | | - os.writeInt(tag.getValue()); |
266 | | - } |
267 | | - |
268 | | - /** |
269 | | - * Writes a {@code TAG_Short} tag. |
270 | | - * |
271 | | - * @param tag |
272 | | - * The tag. |
273 | | - * @throws IOException |
274 | | - * if an I/O error occurs. |
275 | | - */ |
276 | | - private void writeShortTagPayload(ShortTag tag) throws IOException { |
277 | | - os.writeShort(tag.getValue()); |
278 | | - } |
279 | | - |
280 | | - /** |
281 | | - * Writes a {@code TAG_Empty} tag. |
282 | | - * |
283 | | - * @param tag the tag |
284 | | - */ |
285 | | - private void writeEndTagPayload(EndTag tag) { |
286 | | - /* empty */ |
287 | | - } |
288 | | - |
289 | | - private void writeIntArrayTagPayload(IntArrayTag tag) throws IOException { |
290 | | - int[] data = tag.getValue(); |
291 | | - os.writeInt(data.length); |
292 | | - for (int aData : data) { |
293 | | - os.writeInt(aData); |
294 | | - } |
295 | | - } |
296 | | - |
297 | | - private void writeLongArrayTagPayload(LongArrayTag tag) throws IOException { |
298 | | - long[] data = tag.getValue(); |
299 | | - os.writeInt(data.length); |
300 | | - for (long aData : data) { |
301 | | - os.writeLong(aData); |
302 | | - } |
| 78 | + BinaryTagIO.writer().writeNamed( |
| 79 | + Maps.immutableEntry(name, (CompoundBinaryTag) tag.asBinaryTag()), |
| 80 | + (DataOutput) this.os |
| 81 | + ); |
303 | 82 | } |
304 | 83 |
|
305 | 84 | @Override |
|
0 commit comments