ListTag .java

net.minecraft.nbt.ListTag (class): members: WRAPPER_MARKER, compoundStream, addAndUnwrap, getCompoundOrEmpty, getListOrEmpty, getLongArray, getCompound.

net.minecraft.nbtclass438 linescommonextends AbstractList<Tag>10.7 KB

Implements

API Surface (44)

ListTag load(final DataInput input, final NbtAccounter accounter)
StreamTagVisitor.ValueResult parse(final DataInput input, final StreamTagVisitor output, final NbtAccounter accounter)
void skip(final DataInput input, final NbtAccounter accounter)
String getName()
String getPrettyName()
void write(final DataOutput output)
void addAndUnwrap(final Tag tag)
int sizeInBytes()
byte getId()
TagType<ListTag> getType()
String toString()
Tag remove(final int index)
boolean isEmpty()
Optional<CompoundTag> getCompound(final int index)
CompoundTag getCompoundOrEmpty(final int index)
Optional<ListTag> getList(final int index)
ListTag getListOrEmpty(final int index)
Optional<Short> getShort(final int index)
short getShortOr(final int index, final short defaultValue)
Optional<Integer> getInt(final int index)
int getIntOr(final int index, final int defaultValue)
Optional<int[]> getIntArray(final int index)
Optional<long[]> getLongArray(final int index)
Optional<Double> getDouble(final int index)
double getDoubleOr(final int index, final double defaultValue)
Optional<Float> getFloat(final int index)
float getFloatOr(final int index, final float defaultValue)
Optional<String> getString(final int index)
String getStringOr(final int index, final String defaultValue)
int size()
Tag get(final int index)
Tag set(final int index, final Tag tag)
void add(final int index, final Tag tag)
boolean setTag(final int index, final Tag tag)
boolean addTag(final int index, final Tag tag)
ListTag copy()
Optional<ListTag> asList()
boolean equals(final Object obj)
int hashCode()
Stream<Tag> stream()
Stream<CompoundTag> compoundStream()
void accept(final TagVisitor visitor)
void clear()
StreamTagVisitor.ValueResult accept(final StreamTagVisitor visitor)

Fields & Constants (4)

WRAPPER_MARKER
SELF_SIZE_IN_BYTES
TYPE
list

String literals (4)

"Missing type on ListTag"
"ListTag length cannot be negative: "
"LIST"
"TAG_List"

Code Graph: Dependencies (8)

Edges (calls / imports / inherits / references) resolved by graphify from the AST. Every chip opens the class source.

Full Source

ListTag.java438 lines · Minecraft 26.3 · verbatim
1
package net.minecraft.nbt;
2
3
import com.google.common.annotations.VisibleForTesting;
4
import java.io.DataInput;
5
import java.io.DataOutput;
6
import java.io.IOException;
7
import java.util.AbstractList;
8
import java.util.ArrayList;
9
import java.util.List;
10
import java.util.Map;
11
import java.util.Objects;
12
import java.util.Optional;
13
import java.util.stream.Stream;
14
import org.jspecify.annotations.Nullable;
15
16
public final class ListTag extends AbstractList<Tag> implements CollectionTag {
17
	private static final String WRAPPER_MARKER = "";
18
	private static final int SELF_SIZE_IN_BYTES = 36;
19
	public static final TagType<ListTag> TYPE = new TagType.VariableSize<ListTag>() {
20
		public ListTag load(final DataInput input, final NbtAccounter accounter) throws IOException {
21
			accounter.pushDepth();
22
23
			try {
24
				return loadList(input, accounter);
25
			} finally {
26
				accounter.popDepth();
27
			}
28
		}
29
30
		private static ListTag loadList(final DataInput input, final NbtAccounter accounter) throws IOException {
31
			accounter.accountBytes(36L);
32
			byte typeId = input.readByte();
33
			int count = readListCount(input);
34
			if (typeId == 0 && count > 0) {
35
				throw new NbtFormatException("Missing type on ListTag");
36
			}
37
38
			accounter.accountBytes(4L, count);
39
			TagType<?> type = TagTypes.getType(typeId);
40
			ListTag list = new ListTag(new ArrayList<>(count));
41
42
			for (int i = 0; i < count; i++) {
43
				list.addAndUnwrap(type.load(input, accounter));
44
			}
45
46
			return list;
47
		}
48
49
		@Override
50
		public StreamTagVisitor.ValueResult parse(final DataInput input, final StreamTagVisitor output, final NbtAccounter accounter) throws IOException {
51
			accounter.pushDepth();
52
53
			try {
54
				return parseList(input, output, accounter);
55
			} finally {
56
				accounter.popDepth();
57
			}
58
		}
59
60
		private static StreamTagVisitor.ValueResult parseList(final DataInput input, final StreamTagVisitor output, final NbtAccounter accounter) throws IOException {
61
			accounter.accountBytes(36L);
62
			TagType<?> elementType = TagTypes.getType(input.readByte());
63
			int count = readListCount(input);
64
			switch (output.visitList(elementType, count)) {
65
				case HALT:
66
					return StreamTagVisitor.ValueResult.HALT;
67
				case BREAK:
68
					elementType.skip(input, count, accounter);
69
					return output.visitContainerEnd();
70
				default:
71
					accounter.accountBytes(4L, count);
72
					int i = 0;
73
74
					while (true) {
75
						label41: {
76
							if (i < count) {
77
								switch (output.visitElement(elementType, i)) {
78
									case HALT:
79
										return StreamTagVisitor.ValueResult.HALT;
80
									case BREAK:
81
										elementType.skip(input, accounter);
82
										break;
83
									case SKIP:
84
										elementType.skip(input, accounter);
85
										break label41;
86
									default:
87
										switch (elementType.parse(input, output, accounter)) {
88
											case HALT:
89
												return StreamTagVisitor.ValueResult.HALT;
90
											case BREAK:
91
												break;
92
											default:
93
												break label41;
94
										}
95
								}
96
							}
97
98
							int amountToSkip = count - 1 - i;
99
							if (amountToSkip > 0) {
100
								elementType.skip(input, amountToSkip, accounter);
101
							}
102
103
							return output.visitContainerEnd();
104
						}
105
106
						i++;
107
					}
108
			}
109
		}
110
111
		private static int readListCount(final DataInput input) throws IOException {
112
			int count = input.readInt();
113
			if (count < 0) {
114
				throw new NbtFormatException("ListTag length cannot be negative: " + count);
115
			} else {
116
				return count;
117
			}
118
		}
119
120
		@Override
121
		public void skip(final DataInput input, final NbtAccounter accounter) throws IOException {
122
			accounter.pushDepth();
123
124
			try {
125
				TagType<?> type = TagTypes.getType(input.readByte());
126
				int count = input.readInt();
127
				type.skip(input, count, accounter);
128
			} finally {
129
				accounter.popDepth();
130
			}
131
		}
132
133
		@Override
134
		public String getName() {
135
			return "LIST";
136
		}
137
138
		@Override
139
		public String getPrettyName() {
140
			return "TAG_List";
141
		}
142
	};
143
	private final List<Tag> list;
144
145
	public ListTag() {
146
		this(new ArrayList<>());
147
	}
148
149
	ListTag(final List<Tag> list) {
150
		this.list = list;
151
	}
152
153
	private static Tag tryUnwrap(final CompoundTag tag) {
154
		if (tag.size() == 1) {
155
			Tag value = tag.get("");
156
			if (value != null) {
157
				return value;
158
			}
159
		}
160
161
		return tag;
162
	}
163
164
	private static boolean isWrapper(final CompoundTag tag) {
165
		return tag.size() == 1 && tag.contains("");
166
	}
167
168
	private static Tag wrapIfNeeded(final byte elementType, final Tag tag) {
169
		if (elementType != 10) {
170
			return tag;
171
		} else {
172
			return tag instanceof CompoundTag compoundTag && !isWrapper(compoundTag) ? compoundTag : wrapElement(tag);
173
		}
174
	}
175
176
	private static CompoundTag wrapElement(final Tag tag) {
177
		return new CompoundTag(Map.of("", tag));
178
	}
179
180
	@Override
181
	public void write(final DataOutput output) throws IOException {
182
		byte elementType = this.identifyRawElementType();
183
		output.writeByte(elementType);
184
		output.writeInt(this.list.size());
185
186
		for (Tag element : this.list) {
187
			wrapIfNeeded(elementType, element).write(output);
188
		}
189
	}
190
191
	@VisibleForTesting
192
	byte identifyRawElementType() {
193
		byte homogenousType = 0;
194
195
		for (Tag element : this.list) {
196
			byte elementType = element.getId();
197
			if (homogenousType == 0) {
198
				homogenousType = elementType;
199
			} else if (homogenousType != elementType) {
200
				return 10;
201
			}
202
		}
203
204
		return homogenousType;
205
	}
206
207
	public void addAndUnwrap(final Tag tag) {
208
		if (tag instanceof CompoundTag compound) {
209
			this.add(tryUnwrap(compound));
210
		} else {
211
			this.add(tag);
212
		}
213
	}
214
215
	@Override
216
	public int sizeInBytes() {
217
		int size = 36;
218
		size += 4 * this.list.size();
219
220
		for (Tag child : this.list) {
221
			size += child.sizeInBytes();
222
		}
223
224
		return size;
225
	}
226
227
	@Override
228
	public byte getId() {
229
		return 9;
230
	}
231
232
	@Override
233
	public TagType<ListTag> getType() {
234
		return TYPE;
235
	}
236
237
	@Override
238
	public String toString() {
239
		StringTagVisitor visitor = new StringTagVisitor();
240
		visitor.visitList(this);
241
		return visitor.build();
242
	}
243
244
	@Override
245
	public Tag remove(final int index) {
246
		return this.list.remove(index);
247
	}
248
249
	@Override
250
	public boolean isEmpty() {
251
		return this.list.isEmpty();
252
	}
253
254
	public Optional<CompoundTag> getCompound(final int index) {
255
		return this.getNullable(index) instanceof CompoundTag tag ? Optional.of(tag) : Optional.empty();
256
	}
257
258
	public CompoundTag getCompoundOrEmpty(final int index) {
259
		return this.getCompound(index).orElseGet(CompoundTag::new);
260
	}
261
262
	public Optional<ListTag> getList(final int index) {
263
		return this.getNullable(index) instanceof ListTag tag ? Optional.of(tag) : Optional.empty();
264
	}
265
266
	public ListTag getListOrEmpty(final int index) {
267
		return this.getList(index).orElseGet(ListTag::new);
268
	}
269
270
	public Optional<Short> getShort(final int index) {
271
		return this.getOptional(index).flatMap(Tag::asShort);
272
	}
273
274
	public short getShortOr(final int index, final short defaultValue) {
275
		return this.getNullable(index) instanceof NumericTag tag ? tag.shortValue() : defaultValue;
276
	}
277
278
	public Optional<Integer> getInt(final int index) {
279
		return this.getOptional(index).flatMap(Tag::asInt);
280
	}
281
282
	public int getIntOr(final int index, final int defaultValue) {
283
		return this.getNullable(index) instanceof NumericTag tag ? tag.intValue() : defaultValue;
284
	}
285
286
	public Optional<int[]> getIntArray(final int index) {
287
		return this.getNullable(index) instanceof IntArrayTag tag ? Optional.of(tag.getAsIntArray()) : Optional.empty();
288
	}
289
290
	public Optional<long[]> getLongArray(final int index) {
291
		return this.getNullable(index) instanceof LongArrayTag tag ? Optional.of(tag.getAsLongArray()) : Optional.empty();
292
	}
293
294
	public Optional<Double> getDouble(final int index) {
295
		return this.getOptional(index).flatMap(Tag::asDouble);
296
	}
297
298
	public double getDoubleOr(final int index, final double defaultValue) {
299
		return this.getNullable(index) instanceof NumericTag tag ? tag.doubleValue() : defaultValue;
300
	}
301
302
	public Optional<Float> getFloat(final int index) {
303
		return this.getOptional(index).flatMap(Tag::asFloat);
304
	}
305
306
	public float getFloatOr(final int index, final float defaultValue) {
307
		return this.getNullable(index) instanceof NumericTag tag ? tag.floatValue() : defaultValue;
308
	}
309
310
	public Optional<String> getString(final int index) {
311
		return this.getOptional(index).flatMap(Tag::asString);
312
	}
313
314
	public String getStringOr(final int index, final String defaultValue) {
315
		return this.getNullable(index) instanceof StringTag(String var8) ? var8 : defaultValue;
316
	}
317
318
	private @Nullable Tag getNullable(final int index) {
319
		return index >= 0 && index < this.list.size() ? this.list.get(index) : null;
320
	}
321
322
	private Optional<Tag> getOptional(final int index) {
323
		return Optional.ofNullable(this.getNullable(index));
324
	}
325
326
	@Override
327
	public int size() {
328
		return this.list.size();
329
	}
330
331
	@Override
332
	public Tag get(final int index) {
333
		return this.list.get(index);
334
	}
335
336
	public Tag set(final int index, final Tag tag) {
337
		return this.list.set(index, tag);
338
	}
339
340
	public void add(final int index, final Tag tag) {
341
		this.list.add(index, tag);
342
	}
343
344
	@Override
345
	public boolean setTag(final int index, final Tag tag) {
346
		this.list.set(index, tag);
347
		return true;
348
	}
349
350
	@Override
351
	public boolean addTag(final int index, final Tag tag) {
352
		this.list.add(index, tag);
353
		return true;
354
	}
355
356
	public ListTag copy() {
357
		List<Tag> copy = new ArrayList<>(this.list.size());
358
359
		for (Tag tag : this.list) {
360
			copy.add(tag.copy());
361
		}
362
363
		return new ListTag(copy);
364
	}
365
366
	@Override
367
	public Optional<ListTag> asList() {
368
		return Optional.of(this);
369
	}
370
371
	@Override
372
	public boolean equals(final Object obj) {
373
		return this == obj ? true : obj instanceof ListTag listTag && Objects.equals(this.list, listTag.list);
374
	}
375
376
	@Override
377
	public int hashCode() {
378
		return this.list.hashCode();
379
	}
380
381
	@Override
382
	public Stream<Tag> stream() {
383
		return super.stream();
384
	}
385
386
	public Stream<CompoundTag> compoundStream() {
387
		return this.stream().mapMulti((tag, output) -> {
388
			if (tag instanceof CompoundTag compound) {
389
				output.accept(compound);
390
			}
391
		});
392
	}
393
394
	@Override
395
	public void accept(final TagVisitor visitor) {
396
		visitor.visitList(this);
397
	}
398
399
	@Override
400
	public void clear() {
401
		this.list.clear();
402
	}
403
404
	@Override
405
	public StreamTagVisitor.ValueResult accept(final StreamTagVisitor visitor) {
406
		byte elementType = this.identifyRawElementType();
407
		switch (visitor.visitList(TagTypes.getType(elementType), this.list.size())) {
408
			case HALT:
409
				return StreamTagVisitor.ValueResult.HALT;
410
			case BREAK:
411
				return visitor.visitContainerEnd();
412
			default:
413
				int i = 0;
414
415
				while (i < this.list.size()) {
416
					Tag tag = wrapIfNeeded(elementType, this.list.get(i));
417
					switch (visitor.visitElement(tag.getType(), i)) {
418
						case HALT:
419
							return StreamTagVisitor.ValueResult.HALT;
420
						case BREAK:
421
							return visitor.visitContainerEnd();
422
						default:
423
							switch (tag.accept(visitor)) {
424
								case HALT:
425
									return StreamTagVisitor.ValueResult.HALT;
426
								case BREAK:
427
									return visitor.visitContainerEnd();
428
							}
429
						case SKIP:
430
							i++;
431
					}
432
				}
433
434
				return visitor.visitContainerEnd();
435
		}
436
	}
437
}
438

Decompiled Minecraft 26.3 source of ListTag (net/minecraft/nbt/ListTag.java) with Mojang official mappings, stored verbatim on the site.