-
-
Notifications
You must be signed in to change notification settings - Fork 103
/
CompoundTag.java
468 lines (420 loc) · 12.7 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
/*
* 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 com.denizenscript.denizen.nms.util.jnbt;
import com.denizenscript.denizen.nms.NMSHandler;
import java.util.Collections;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
/**
* The {@code TAG_Compound} tag.
*/
public abstract class CompoundTag extends Tag {
protected final Map<String, Tag> value;
/**
* 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);
}
public boolean isEmpty() {
return getValue().isEmpty();
}
public int size() {
return getValue().size();
}
@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 NMSHandler.instance.createCompoundTag(value);
}
/**
* Create a compound tag builder.
*
* @return the builder
*/
public CompoundTagBuilder createBuilder() {
return new CompoundTagBuilder(new HashMap<>(value));
}
public CompoundTag getCompound(String key) {
return value.get(key) instanceof CompoundTag compoundTag ? compoundTag : null;
}
/**
* 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;
}
}
public boolean getBoolean(String key) {
return getByte(key) != 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 JNBTListTag) {
return ((JNBTListTag) 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 JNBTListTag getListTag(String key) {
Tag tag = value.get(key);
if (tag instanceof JNBTListTag) {
return (JNBTListTag) tag;
}
else {
return new JNBTListTag(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 JNBTListTag) {
JNBTListTag listTag = (JNBTListTag) 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();
}
}