/
CompoundTag.java
555 lines (508 loc) · 17.1 KB
/
CompoundTag.java
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
/*
* WorldEdit, a Minecraft world manipulation toolkit
* Copyright (C) sk89q <http://www.sk89q.com>
* Copyright (C) WorldEdit team and contributors
*
* This program is free software: you can redistribute it and/or modify it
* under the terms of the GNU Lesser General Public License as published by the
* Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* This program is distributed in the hope that it will be useful, but WITHOUT
* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
* FITNESS FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public License
* for more details.
*
* You should have received a copy of the GNU Lesser General Public License
* along with this program. If not, see <http://www.gnu.org/licenses/>.
*/
package net.aufdemrand.denizen.utilities.jnbt;
import net.minecraft.server.v1_9_R1.*;
import java.util.*;
/**
* The {@code TAG_Compound} tag.
*/
public final class CompoundTag extends Tag {
private final Map<String, Tag> value;
public NBTTagCompound toNMSTag() {
NBTTagCompound tag = new NBTTagCompound();
for (Map.Entry<String, Tag> entry: value.entrySet()) {
if (entry.getValue() instanceof IntTag) {
tag.setInt(entry.getKey(), ((IntTag) entry.getValue()).getValue());
}
else if (entry.getValue() instanceof ByteTag) {
tag.setByte(entry.getKey(), ((ByteTag) entry.getValue()).getValue());
}
else if (entry.getValue() instanceof ByteArrayTag) {
tag.setByteArray(entry.getKey(), ((ByteArrayTag) entry.getValue()).getValue());
}
else if (entry.getValue() instanceof CompoundTag) {
tag.set(entry.getKey(), ((CompoundTag) entry.getValue()).toNMSTag());
}
else if (entry.getValue() instanceof DoubleTag) {
tag.setDouble(entry.getKey(), ((DoubleTag) entry.getValue()).getValue());
}
else if (entry.getValue() instanceof FloatTag) {
tag.setFloat(entry.getKey(), ((FloatTag) entry.getValue()).getValue());
}
else if (entry.getValue() instanceof IntArrayTag) {
tag.setIntArray(entry.getKey(), ((IntArrayTag) entry.getValue()).getValue());
}
else if (entry.getValue() instanceof ListTag) {
NBTTagList list = new NBTTagList();
List<Tag> tags = ((ListTag) entry.getValue()).getValue();
for (Tag btag : tags) {
HashMap<String, Tag> btags = new HashMap<String, Tag>();
btags.put("test", btag);
CompoundTag comp = new CompoundTag(btags);
list.add(comp.toNMSTag().get("test"));
}
tag.set(entry.getKey(), list);
}
else if (entry.getValue() instanceof LongTag) {
tag.setLong(entry.getKey(), ((LongTag) entry.getValue()).getValue());
}
else if (entry.getValue() instanceof ShortTag) {
tag.setShort(entry.getKey(), ((ShortTag) entry.getValue()).getValue());
}
else if (entry.getValue() instanceof StringTag) {
tag.setString(entry.getKey(), ((StringTag) entry.getValue()).getValue());
}
}
return tag;
}
public static CompoundTag fromNMSTag(NBTTagCompound tag) {
HashMap<String, Tag> tags = new HashMap<String, Tag>();
for (String key: tag.c()) {
NBTBase base = tag.get(key);
if (base instanceof NBTTagInt) {
tags.put(key, new IntTag(((NBTTagInt) base).d()));
}
else if (base instanceof NBTTagByte) {
tags.put(key, new ByteTag(((NBTTagByte) base).f()));
}
else if (base instanceof NBTTagFloat) {
tags.put(key, new FloatTag(((NBTTagFloat) base).h()));
}
else if (base instanceof NBTTagDouble) {
tags.put(key, new DoubleTag(((NBTTagDouble) base).g()));
}
else if (base instanceof NBTTagByteArray) {
tags.put(key, new ByteArrayTag(((NBTTagByteArray) base).c()));
}
else if (base instanceof NBTTagIntArray) {
tags.put(key, new IntArrayTag(((NBTTagIntArray) base).c()));
}
else if (base instanceof NBTTagCompound) {
tags.put(key, fromNMSTag(((NBTTagCompound) base)));
}
else if (base instanceof NBTTagEnd) {
tags.put(key, new EndTag());
}
else if (base instanceof NBTTagLong) {
tags.put(key, new LongTag(((NBTTagLong) base).c()));
}
else if (base instanceof NBTTagShort) {
tags.put(key, new ShortTag(((NBTTagShort) base).e()));
}
else if (base instanceof NBTTagString) {
tags.put(key, new StringTag(((NBTTagString) base).a_()));
}
else if (base instanceof NBTTagList) {
NBTTagList list = (NBTTagList) base;
if (list.size() > 0) {
NBTBase nbase = list.h(0);
NBTTagCompound comp = new NBTTagCompound();
comp.set("test", nbase);
ListTagBuilder ltb = new ListTagBuilder(fromNMSTag(comp).getValue().get("test").getClass());
for (int i = 0 ; i < list.size(); i++) {
NBTBase nbase2 = list.h(i);
NBTTagCompound comp2 = new NBTTagCompound();
comp2.set("test", nbase2);
ltb.add(fromNMSTag(comp2).getValue().get("test"));
}
tags.put(key, ltb.build());
}
}
}
return new CompoundTag(tags);
}
/**
* Creates the tag with an empty name.
*
* @param value the value of the tag
*/
public CompoundTag(Map<String, Tag> value) {
super();
this.value = Collections.unmodifiableMap(value);
}
/**
* Returns whether this compound tag contains the given key.
*
* @param key the given key
* @return true if the tag contains the given key
*/
public boolean containsKey(String key) {
return value.containsKey(key);
}
@Override
public Map<String, Tag> getValue() {
return value;
}
/**
* Return a new compound tag with the given values.
*
* @param value the value
* @return the new compound tag
*/
public CompoundTag setValue(Map<String, Tag> value) {
return new CompoundTag(value);
}
/**
* Create a compound tag builder.
*
* @return the builder
*/
public CompoundTagBuilder createBuilder() {
return new CompoundTagBuilder(new HashMap<String, Tag>(value));
}
/**
* Get a byte array named with the given key.
* <p/>
* <p>If the key does not exist or its value is not a byte array tag,
* then an empty byte array will be returned.</p>
*
* @param key the key
* @return a byte array
*/
public byte[] getByteArray(String key) {
Tag tag = value.get(key);
if (tag instanceof ByteArrayTag) {
return ((ByteArrayTag) tag).getValue();
}
else {
return new byte[0];
}
}
/**
* Get a byte named with the given key.
* <p/>
* <p>If the key does not exist or its value is not a byte tag,
* then {@code 0} will be returned.</p>
*
* @param key the key
* @return a byte
*/
public byte getByte(String key) {
Tag tag = value.get(key);
if (tag instanceof ByteTag) {
return ((ByteTag) tag).getValue();
}
else {
return (byte) 0;
}
}
/**
* Get a double named with the given key.
* <p/>
* <p>If the key does not exist or its value is not a double tag,
* then {@code 0} will be returned.</p>
*
* @param key the key
* @return a double
*/
public double getDouble(String key) {
Tag tag = value.get(key);
if (tag instanceof DoubleTag) {
return ((DoubleTag) tag).getValue();
}
else {
return 0;
}
}
/**
* Get a double named with the given key, even if it's another
* type of number.
* <p/>
* <p>If the key does not exist or its value is not a number,
* then {@code 0} will be returned.</p>
*
* @param key the key
* @return a double
*/
public double asDouble(String key) {
Tag tag = value.get(key);
if (tag instanceof ByteTag) {
return ((ByteTag) tag).getValue();
}
else if (tag instanceof ShortTag) {
return ((ShortTag) tag).getValue();
}
else if (tag instanceof IntTag) {
return ((IntTag) tag).getValue();
}
else if (tag instanceof LongTag) {
return ((LongTag) tag).getValue();
}
else if (tag instanceof FloatTag) {
return ((FloatTag) tag).getValue();
}
else if (tag instanceof DoubleTag) {
return ((DoubleTag) tag).getValue();
}
else {
return 0;
}
}
/**
* Get a float named with the given key.
* <p/>
* <p>If the key does not exist or its value is not a float tag,
* then {@code 0} will be returned.</p>
*
* @param key the key
* @return a float
*/
public float getFloat(String key) {
Tag tag = value.get(key);
if (tag instanceof FloatTag) {
return ((FloatTag) tag).getValue();
}
else {
return 0;
}
}
/**
* Get a {@code int[]} named with the given key.
* <p/>
* <p>If the key does not exist or its value is not an int array tag,
* then an empty array will be returned.</p>
*
* @param key the key
* @return an int array
*/
public int[] getIntArray(String key) {
Tag tag = value.get(key);
if (tag instanceof IntArrayTag) {
return ((IntArrayTag) tag).getValue();
}
else {
return new int[0];
}
}
/**
* Get an int named with the given key.
* <p/>
* <p>If the key does not exist or its value is not an int tag,
* then {@code 0} will be returned.</p>
*
* @param key the key
* @return an int
*/
public int getInt(String key) {
Tag tag = value.get(key);
if (tag instanceof IntTag) {
return ((IntTag) tag).getValue();
}
else {
return 0;
}
}
/**
* Get an int named with the given key, even if it's another
* type of number.
* <p/>
* <p>If the key does not exist or its value is not a number,
* then {@code 0} will be returned.</p>
*
* @param key the key
* @return an int
*/
public int asInt(String key) {
Tag tag = value.get(key);
if (tag instanceof ByteTag) {
return ((ByteTag) tag).getValue();
}
else if (tag instanceof ShortTag) {
return ((ShortTag) tag).getValue();
}
else if (tag instanceof IntTag) {
return ((IntTag) tag).getValue();
}
else if (tag instanceof LongTag) {
return ((LongTag) tag).getValue().intValue();
}
else if (tag instanceof FloatTag) {
return ((FloatTag) tag).getValue().intValue();
}
else if (tag instanceof DoubleTag) {
return ((DoubleTag) tag).getValue().intValue();
}
else {
return 0;
}
}
/**
* Get a list of tags named with the given key.
* <p/>
* <p>If the key does not exist or its value is not a list tag,
* then an empty list will be returned.</p>
*
* @param key the key
* @return a list of tags
*/
public List<Tag> getList(String key) {
Tag tag = value.get(key);
if (tag instanceof ListTag) {
return ((ListTag) tag).getValue();
}
else {
return Collections.emptyList();
}
}
/**
* Get a {@code TagList} named with the given key.
* <p/>
* <p>If the key does not exist or its value is not a list tag,
* then an empty tag list will be returned.</p>
*
* @param key the key
* @return a tag list instance
*/
public ListTag getListTag(String key) {
Tag tag = value.get(key);
if (tag instanceof ListTag) {
return (ListTag) tag;
}
else {
return new ListTag(StringTag.class, Collections.<Tag>emptyList());
}
}
/**
* Get a list of tags named with the given key.
* <p/>
* <p>If the key does not exist or its value is not a list tag,
* then an empty list will be returned. If the given key references
* a list but the list of of a different type, then an empty
* list will also be returned.</p>
*
* @param key the key
* @param listType the class of the contained type
* @param <T> the type of list
* @return a list of tags
*/
@SuppressWarnings("unchecked")
public <T extends Tag> List<T> getList(String key, Class<T> listType) {
Tag tag = value.get(key);
if (tag instanceof ListTag) {
ListTag listTag = (ListTag) tag;
if (listTag.getType().equals(listType)) {
return (List<T>) listTag.getValue();
}
else {
return Collections.emptyList();
}
}
else {
return Collections.emptyList();
}
}
/**
* Get a long named with the given key.
* <p/>
* <p>If the key does not exist or its value is not a long tag,
* then {@code 0} will be returned.</p>
*
* @param key the key
* @return a long
*/
public long getLong(String key) {
Tag tag = value.get(key);
if (tag instanceof LongTag) {
return ((LongTag) tag).getValue();
}
else {
return 0L;
}
}
/**
* Get a long named with the given key, even if it's another
* type of number.
* <p/>
* <p>If the key does not exist or its value is not a number,
* then {@code 0} will be returned.</p>
*
* @param key the key
* @return a long
*/
public long asLong(String key) {
Tag tag = value.get(key);
if (tag instanceof ByteTag) {
return ((ByteTag) tag).getValue();
}
else if (tag instanceof ShortTag) {
return ((ShortTag) tag).getValue();
}
else if (tag instanceof IntTag) {
return ((IntTag) tag).getValue();
}
else if (tag instanceof LongTag) {
return ((LongTag) tag).getValue();
}
else if (tag instanceof FloatTag) {
return ((FloatTag) tag).getValue().longValue();
}
else if (tag instanceof DoubleTag) {
return ((DoubleTag) tag).getValue().longValue();
}
else {
return 0L;
}
}
/**
* Get a short named with the given key.
* <p/>
* <p>If the key does not exist or its value is not a short tag,
* then {@code 0} will be returned.</p>
*
* @param key the key
* @return a short
*/
public short getShort(String key) {
Tag tag = value.get(key);
if (tag instanceof ShortTag) {
return ((ShortTag) tag).getValue();
}
else {
return 0;
}
}
/**
* Get a string named with the given key.
* <p/>
* <p>If the key does not exist or its value is not a string tag,
* then {@code ""} will be returned.</p>
*
* @param key the key
* @return a string
*/
public String getString(String key) {
Tag tag = value.get(key);
if (tag instanceof StringTag) {
return ((StringTag) tag).getValue();
}
else {
return "";
}
}
@Override
public String toString() {
StringBuilder bldr = new StringBuilder();
bldr.append("TAG_Compound").append(": ").append(value.size()).append(" entries\r\n{\r\n");
for (Map.Entry<String, Tag> entry : value.entrySet()) {
bldr.append(" ").append(entry.getValue().toString().replaceAll("\r\n", "\r\n ")).append("\r\n");
}
bldr.append("}");
return bldr.toString();
}
}