| 1 | package net.minecraft.nbt;
|
| 2 |
|
| 3 | import com.google.common.collect.Maps;
|
| 4 | import com.mojang.logging.LogUtils;
|
| 5 | import com.mojang.serialization.Codec;
|
| 6 | import com.mojang.serialization.DataResult;
|
| 7 | import com.mojang.serialization.Dynamic;
|
| 8 | import com.mojang.serialization.DynamicOps;
|
| 9 | import com.mojang.serialization.MapCodec;
|
| 10 | import java.io.DataInput;
|
| 11 | import java.io.DataOutput;
|
| 12 | import java.io.IOException;
|
| 13 | import java.util.Collection;
|
| 14 | import java.util.HashMap;
|
| 15 | import java.util.Map;
|
| 16 | import java.util.Objects;
|
| 17 | import java.util.Optional;
|
| 18 | import java.util.Set;
|
| 19 | import java.util.Map.Entry;
|
| 20 | import java.util.function.BiConsumer;
|
| 21 | import net.minecraft.CrashReport;
|
| 22 | import net.minecraft.CrashReportCategory;
|
| 23 | import org.jspecify.annotations.Nullable;
|
| 24 | import org.slf4j.Logger;
|
| 25 |
|
| 26 | public final class CompoundTag implements Tag {
|
| 27 | private static final Logger LOGGER = LogUtils.getLogger();
|
| 28 | public static final Codec<CompoundTag> CODEC = Codec.PASSTHROUGH
|
| 29 | .comapFlatMap(
|
| 30 | t -> {
|
| 31 | Tag tag = t.convert(NbtOps.INSTANCE).getValue();
|
| 32 | return tag instanceof CompoundTag compoundTag
|
| 33 | ? DataResult.success(compoundTag == t.getValue() ? compoundTag.copy() : compoundTag)
|
| 34 | : DataResult.error(() -> "Not a compound tag: " + tag);
|
| 35 | },
|
| 36 | t -> new Dynamic<>(NbtOps.INSTANCE, t.copy())
|
| 37 | );
|
| 38 | private static final int SELF_SIZE_IN_BYTES = 48;
|
| 39 | private static final int MAP_ENTRY_SIZE_IN_BYTES = 32;
|
| 40 | public static final TagType<CompoundTag> TYPE = new TagType.VariableSize<CompoundTag>() {
|
| 41 | public CompoundTag load(final DataInput input, final NbtAccounter accounter) throws IOException {
|
| 42 | accounter.pushDepth();
|
| 43 |
|
| 44 | try {
|
| 45 | return loadCompound(input, accounter);
|
| 46 | } finally {
|
| 47 | accounter.popDepth();
|
| 48 | }
|
| 49 | }
|
| 50 |
|
| 51 | private static CompoundTag loadCompound(final DataInput input, final NbtAccounter accounter) throws IOException {
|
| 52 | accounter.accountBytes(48L);
|
| 53 | Map<String, Tag> values = Maps.newHashMap();
|
| 54 |
|
| 55 | byte tagType;
|
| 56 | while ((tagType = input.readByte()) != 0) {
|
| 57 | String key = readString(input, accounter);
|
| 58 | Tag tag = CompoundTag.readNamedTagData(TagTypes.getType(tagType), key, input, accounter);
|
| 59 | if (values.put(key, tag) == null) {
|
| 60 | accounter.accountBytes(36L);
|
| 61 | }
|
| 62 | }
|
| 63 |
|
| 64 | return new CompoundTag(values);
|
| 65 | }
|
| 66 |
|
| 67 | @Override
|
| 68 | public StreamTagVisitor.ValueResult parse(final DataInput input, final StreamTagVisitor output, final NbtAccounter accounter) throws IOException {
|
| 69 | accounter.pushDepth();
|
| 70 |
|
| 71 | try {
|
| 72 | return parseCompound(input, output, accounter);
|
| 73 | } finally {
|
| 74 | accounter.popDepth();
|
| 75 | }
|
| 76 | }
|
| 77 |
|
| 78 | private static StreamTagVisitor.ValueResult parseCompound(final DataInput input, final StreamTagVisitor output, final NbtAccounter accounter) throws IOException {
|
| 79 | accounter.accountBytes(48L);
|
| 80 |
|
| 81 | byte tagTypeId;
|
| 82 | label35:
|
| 83 | while ((tagTypeId = input.readByte()) != 0) {
|
| 84 | TagType<?> tagType = TagTypes.getType(tagTypeId);
|
| 85 | switch (output.visitEntry(tagType)) {
|
| 86 | case HALT:
|
| 87 | return StreamTagVisitor.ValueResult.HALT;
|
| 88 | case BREAK:
|
| 89 | StringTag.skipString(input);
|
| 90 | tagType.skip(input, accounter);
|
| 91 | break label35;
|
| 92 | case SKIP:
|
| 93 | StringTag.skipString(input);
|
| 94 | tagType.skip(input, accounter);
|
| 95 | break;
|
| 96 | default:
|
| 97 | String key = readString(input, accounter);
|
| 98 | switch (output.visitEntry(tagType, key)) {
|
| 99 | case HALT:
|
| 100 | return StreamTagVisitor.ValueResult.HALT;
|
| 101 | case BREAK:
|
| 102 | tagType.skip(input, accounter);
|
| 103 | break label35;
|
| 104 | case SKIP:
|
| 105 | tagType.skip(input, accounter);
|
| 106 | break;
|
| 107 | default:
|
| 108 | accounter.accountBytes(36L);
|
| 109 | switch (tagType.parse(input, output, accounter)) {
|
| 110 | case HALT:
|
| 111 | return StreamTagVisitor.ValueResult.HALT;
|
| 112 | case BREAK:
|
| 113 | }
|
| 114 | }
|
| 115 | }
|
| 116 | }
|
| 117 |
|
| 118 | if (tagTypeId != 0) {
|
| 119 | while ((tagTypeId = input.readByte()) != 0) {
|
| 120 | StringTag.skipString(input);
|
| 121 | TagTypes.getType(tagTypeId).skip(input, accounter);
|
| 122 | }
|
| 123 | }
|
| 124 |
|
| 125 | return output.visitContainerEnd();
|
| 126 | }
|
| 127 |
|
| 128 | private static String readString(final DataInput input, final NbtAccounter accounter) throws IOException {
|
| 129 | String key = input.readUTF();
|
| 130 | accounter.accountBytes(28L);
|
| 131 | accounter.accountBytes(2L, key.length());
|
| 132 | return key;
|
| 133 | }
|
| 134 |
|
| 135 | @Override
|
| 136 | public void skip(final DataInput input, final NbtAccounter accounter) throws IOException {
|
| 137 | accounter.pushDepth();
|
| 138 |
|
| 139 | byte tagTypeId;
|
| 140 | try {
|
| 141 | while ((tagTypeId = input.readByte()) != 0) {
|
| 142 | StringTag.skipString(input);
|
| 143 | TagTypes.getType(tagTypeId).skip(input, accounter);
|
| 144 | }
|
| 145 | } finally {
|
| 146 | accounter.popDepth();
|
| 147 | }
|
| 148 | }
|
| 149 |
|
| 150 | @Override
|
| 151 | public String getName() {
|
| 152 | return "COMPOUND";
|
| 153 | }
|
| 154 |
|
| 155 | @Override
|
| 156 | public String getPrettyName() {
|
| 157 | return "TAG_Compound";
|
| 158 | }
|
| 159 | };
|
| 160 | private final Map<String, Tag> tags;
|
| 161 |
|
| 162 | CompoundTag(final Map<String, Tag> tags) {
|
| 163 | this.tags = tags;
|
| 164 | }
|
| 165 |
|
| 166 | public CompoundTag() {
|
| 167 | this(new HashMap<>());
|
| 168 | }
|
| 169 |
|
| 170 | @Override
|
| 171 | public void write(final DataOutput output) throws IOException {
|
| 172 | for (String key : this.tags.keySet()) {
|
| 173 | Tag tag = this.tags.get(key);
|
| 174 | writeNamedTag(key, tag, output);
|
| 175 | }
|
| 176 |
|
| 177 | output.writeByte(0);
|
| 178 | }
|
| 179 |
|
| 180 | @Override
|
| 181 | public int sizeInBytes() {
|
| 182 | int size = 48;
|
| 183 |
|
| 184 | for (Entry<String, Tag> entry : this.tags.entrySet()) {
|
| 185 | size += 28 + 2 * entry.getKey().length();
|
| 186 | size += 36;
|
| 187 | size += entry.getValue().sizeInBytes();
|
| 188 | }
|
| 189 |
|
| 190 | return size;
|
| 191 | }
|
| 192 |
|
| 193 | public Set<String> keySet() {
|
| 194 | return this.tags.keySet();
|
| 195 | }
|
| 196 |
|
| 197 | public Set<Entry<String, Tag>> entrySet() {
|
| 198 | return this.tags.entrySet();
|
| 199 | }
|
| 200 |
|
| 201 | public Collection<Tag> values() {
|
| 202 | return this.tags.values();
|
| 203 | }
|
| 204 |
|
| 205 | public void forEach(final BiConsumer<String, Tag> consumer) {
|
| 206 | this.tags.forEach(consumer);
|
| 207 | }
|
| 208 |
|
| 209 | @Override
|
| 210 | public byte getId() {
|
| 211 | return 10;
|
| 212 | }
|
| 213 |
|
| 214 | @Override
|
| 215 | public TagType<CompoundTag> getType() {
|
| 216 | return TYPE;
|
| 217 | }
|
| 218 |
|
| 219 | public int size() {
|
| 220 | return this.tags.size();
|
| 221 | }
|
| 222 |
|
| 223 | public @Nullable Tag put(final String name, final Tag tag) {
|
| 224 | return this.tags.put(name, tag);
|
| 225 | }
|
| 226 |
|
| 227 | public void putByte(final String name, final byte value) {
|
| 228 | this.tags.put(name, ByteTag.valueOf(value));
|
| 229 | }
|
| 230 |
|
| 231 | public void putShort(final String name, final short value) {
|
| 232 | this.tags.put(name, ShortTag.valueOf(value));
|
| 233 | }
|
| 234 |
|
| 235 | public void putInt(final String name, final int value) {
|
| 236 | this.tags.put(name, IntTag.valueOf(value));
|
| 237 | }
|
| 238 |
|
| 239 | public void putLong(final String name, final long value) {
|
| 240 | this.tags.put(name, LongTag.valueOf(value));
|
| 241 | }
|
| 242 |
|
| 243 | public void putFloat(final String name, final float value) {
|
| 244 | this.tags.put(name, FloatTag.valueOf(value));
|
| 245 | }
|
| 246 |
|
| 247 | public void putDouble(final String name, final double value) {
|
| 248 | this.tags.put(name, DoubleTag.valueOf(value));
|
| 249 | }
|
| 250 |
|
| 251 | public void putString(final String name, final String value) {
|
| 252 | this.tags.put(name, StringTag.valueOf(value));
|
| 253 | }
|
| 254 |
|
| 255 | public void putByteArray(final String name, final byte[] value) {
|
| 256 | this.tags.put(name, new ByteArrayTag(value));
|
| 257 | }
|
| 258 |
|
| 259 | public void putIntArray(final String name, final int[] value) {
|
| 260 | this.tags.put(name, new IntArrayTag(value));
|
| 261 | }
|
| 262 |
|
| 263 | public void putLongArray(final String name, final long[] value) {
|
| 264 | this.tags.put(name, new LongArrayTag(value));
|
| 265 | }
|
| 266 |
|
| 267 | public void putBoolean(final String name, final boolean value) {
|
| 268 | this.tags.put(name, ByteTag.valueOf(value));
|
| 269 | }
|
| 270 |
|
| 271 | public @Nullable Tag get(final String name) {
|
| 272 | return this.tags.get(name);
|
| 273 | }
|
| 274 |
|
| 275 | public boolean contains(final String name) {
|
| 276 | return this.tags.containsKey(name);
|
| 277 | }
|
| 278 |
|
| 279 | private Optional<Tag> getOptional(final String name) {
|
| 280 | return Optional.ofNullable(this.tags.get(name));
|
| 281 | }
|
| 282 |
|
| 283 | public Optional<Byte> getByte(final String name) {
|
| 284 | return this.getOptional(name).flatMap(Tag::asByte);
|
| 285 | }
|
| 286 |
|
| 287 | public byte getByteOr(final String name, final byte defaultValue) {
|
| 288 | return this.tags.get(name) instanceof NumericTag tag ? tag.byteValue() : defaultValue;
|
| 289 | }
|
| 290 |
|
| 291 | public Optional<Short> getShort(final String name) {
|
| 292 | return this.getOptional(name).flatMap(Tag::asShort);
|
| 293 | }
|
| 294 |
|
| 295 | public short getShortOr(final String name, final short defaultValue) {
|
| 296 | return this.tags.get(name) instanceof NumericTag tag ? tag.shortValue() : defaultValue;
|
| 297 | }
|
| 298 |
|
| 299 | public Optional<Integer> getInt(final String name) {
|
| 300 | return this.getOptional(name).flatMap(Tag::asInt);
|
| 301 | }
|
| 302 |
|
| 303 | public int getIntOr(final String name, final int defaultValue) {
|
| 304 | return this.tags.get(name) instanceof NumericTag tag ? tag.intValue() : defaultValue;
|
| 305 | }
|
| 306 |
|
| 307 | public Optional<Long> getLong(final String name) {
|
| 308 | return this.getOptional(name).flatMap(Tag::asLong);
|
| 309 | }
|
| 310 |
|
| 311 | public long getLongOr(final String name, final long defaultValue) {
|
| 312 | return this.tags.get(name) instanceof NumericTag tag ? tag.longValue() : defaultValue;
|
| 313 | }
|
| 314 |
|
| 315 | public Optional<Float> getFloat(final String name) {
|
| 316 | return this.getOptional(name).flatMap(Tag::asFloat);
|
| 317 | }
|
| 318 |
|
| 319 | public float getFloatOr(final String name, final float defaultValue) {
|
| 320 | return this.tags.get(name) instanceof NumericTag tag ? tag.floatValue() : defaultValue;
|
| 321 | }
|
| 322 |
|
| 323 | public Optional<Double> getDouble(final String name) {
|
| 324 | return this.getOptional(name).flatMap(Tag::asDouble);
|
| 325 | }
|
| 326 |
|
| 327 | public double getDoubleOr(final String name, final double defaultValue) {
|
| 328 | return this.tags.get(name) instanceof NumericTag tag ? tag.doubleValue() : defaultValue;
|
| 329 | }
|
| 330 |
|
| 331 | public Optional<String> getString(final String name) {
|
| 332 | return this.getOptional(name).flatMap(Tag::asString);
|
| 333 | }
|
| 334 |
|
| 335 | public String getStringOr(final String name, final String defaultValue) {
|
| 336 | return this.tags.get(name) instanceof StringTag(String var8) ? var8 : defaultValue;
|
| 337 | }
|
| 338 |
|
| 339 | public Optional<byte[]> getByteArray(final String name) {
|
| 340 | return this.tags.get(name) instanceof ByteArrayTag tag ? Optional.of(tag.getAsByteArray()) : Optional.empty();
|
| 341 | }
|
| 342 |
|
| 343 | public Optional<int[]> getIntArray(final String name) {
|
| 344 | return this.tags.get(name) instanceof IntArrayTag tag ? Optional.of(tag.getAsIntArray()) : Optional.empty();
|
| 345 | }
|
| 346 |
|
| 347 | public Optional<long[]> getLongArray(final String name) {
|
| 348 | return this.tags.get(name) instanceof LongArrayTag tag ? Optional.of(tag.getAsLongArray()) : Optional.empty();
|
| 349 | }
|
| 350 |
|
| 351 | public Optional<CompoundTag> getCompound(final String name) {
|
| 352 | return this.tags.get(name) instanceof CompoundTag tag ? Optional.of(tag) : Optional.empty();
|
| 353 | }
|
| 354 |
|
| 355 | public CompoundTag getCompoundOrEmpty(final String name) {
|
| 356 | return this.getCompound(name).orElseGet(CompoundTag::new);
|
| 357 | }
|
| 358 |
|
| 359 | public Optional<ListTag> getList(final String name) {
|
| 360 | return this.tags.get(name) instanceof ListTag tag ? Optional.of(tag) : Optional.empty();
|
| 361 | }
|
| 362 |
|
| 363 | public ListTag getListOrEmpty(final String name) {
|
| 364 | return this.getList(name).orElseGet(ListTag::new);
|
| 365 | }
|
| 366 |
|
| 367 | public Optional<Boolean> getBoolean(final String name) {
|
| 368 | return this.getOptional(name).flatMap(Tag::asBoolean);
|
| 369 | }
|
| 370 |
|
| 371 | public boolean getBooleanOr(final String string, final boolean defaultValue) {
|
| 372 | return this.getByteOr(string, (byte)(defaultValue ? 1 : 0)) != 0;
|
| 373 | }
|
| 374 |
|
| 375 | public @Nullable Tag remove(final String name) {
|
| 376 | return this.tags.remove(name);
|
| 377 | }
|
| 378 |
|
| 379 | @Override
|
| 380 | public String toString() {
|
| 381 | StringTagVisitor visitor = new StringTagVisitor();
|
| 382 | visitor.visitCompound(this);
|
| 383 | return visitor.build();
|
| 384 | }
|
| 385 |
|
| 386 | public boolean isEmpty() {
|
| 387 | return this.tags.isEmpty();
|
| 388 | }
|
| 389 |
|
| 390 | CompoundTag shallowCopy() {
|
| 391 | return new CompoundTag(new HashMap<>(this.tags));
|
| 392 | }
|
| 393 |
|
| 394 | public CompoundTag copy() {
|
| 395 | HashMap<String, Tag> newTags = new HashMap<>();
|
| 396 | this.tags.forEach((key, tag) -> newTags.put(key, tag.copy()));
|
| 397 | return new CompoundTag(newTags);
|
| 398 | }
|
| 399 |
|
| 400 | @Override
|
| 401 | public Optional<CompoundTag> asCompound() {
|
| 402 | return Optional.of(this);
|
| 403 | }
|
| 404 |
|
| 405 | @Override
|
| 406 | public boolean equals(final Object obj) {
|
| 407 | return this == obj ? true : obj instanceof CompoundTag compoundTag && Objects.equals(this.tags, compoundTag.tags);
|
| 408 | }
|
| 409 |
|
| 410 | @Override
|
| 411 | public int hashCode() {
|
| 412 | return this.tags.hashCode();
|
| 413 | }
|
| 414 |
|
| 415 | private static void writeNamedTag(final String name, final Tag tag, final DataOutput output) throws IOException {
|
| 416 | output.writeByte(tag.getId());
|
| 417 | if (tag.getId() != 0) {
|
| 418 | output.writeUTF(name);
|
| 419 | tag.write(output);
|
| 420 | }
|
| 421 | }
|
| 422 |
|
| 423 | private static Tag readNamedTagData(final TagType<?> type, final String name, final DataInput input, final NbtAccounter accounter) {
|
| 424 | try {
|
| 425 | return type.load(input, accounter);
|
| 426 | } catch (IOException e) {
|
| 427 | CrashReport report = CrashReport.forThrowable(e, "Loading NBT data");
|
| 428 | CrashReportCategory category = report.addCategory("NBT Tag");
|
| 429 | category.setDetail("Tag name", name);
|
| 430 | category.setDetail("Tag type", type.getName());
|
| 431 | throw new ReportedNbtException(report);
|
| 432 | }
|
| 433 | }
|
| 434 |
|
| 435 | public CompoundTag merge(final CompoundTag other) {
|
| 436 | for (String tagName : other.tags.keySet()) {
|
| 437 | Tag otherTag = other.tags.get(tagName);
|
| 438 | if (otherTag instanceof CompoundTag otherCompound && this.tags.get(tagName) instanceof CompoundTag selfCompound) {
|
| 439 | selfCompound.merge(otherCompound);
|
| 440 | } else {
|
| 441 | this.put(tagName, otherTag.copy());
|
| 442 | }
|
| 443 | }
|
| 444 |
|
| 445 | return this;
|
| 446 | }
|
| 447 |
|
| 448 | @Override
|
| 449 | public void accept(final TagVisitor visitor) {
|
| 450 | visitor.visitCompound(this);
|
| 451 | }
|
| 452 |
|
| 453 | @Override
|
| 454 | public StreamTagVisitor.ValueResult accept(final StreamTagVisitor visitor) {
|
| 455 | for (Entry<String, Tag> entry : this.tags.entrySet()) {
|
| 456 | Tag value = entry.getValue();
|
| 457 | TagType<?> type = value.getType();
|
| 458 | StreamTagVisitor.EntryResult entryParseResult = visitor.visitEntry(type);
|
| 459 | switch (entryParseResult) {
|
| 460 | case HALT:
|
| 461 | return StreamTagVisitor.ValueResult.HALT;
|
| 462 | case BREAK:
|
| 463 | return visitor.visitContainerEnd();
|
| 464 | case SKIP:
|
| 465 | break;
|
| 466 | default:
|
| 467 | entryParseResult = visitor.visitEntry(type, entry.getKey());
|
| 468 | switch (entryParseResult) {
|
| 469 | case HALT:
|
| 470 | return StreamTagVisitor.ValueResult.HALT;
|
| 471 | case BREAK:
|
| 472 | return visitor.visitContainerEnd();
|
| 473 | case SKIP:
|
| 474 | break;
|
| 475 | default:
|
| 476 | StreamTagVisitor.ValueResult valueResult = value.accept(visitor);
|
| 477 | switch (valueResult) {
|
| 478 | case HALT:
|
| 479 | return StreamTagVisitor.ValueResult.HALT;
|
| 480 | case BREAK:
|
| 481 | return visitor.visitContainerEnd();
|
| 482 | }
|
| 483 | }
|
| 484 | }
|
| 485 | }
|
| 486 |
|
| 487 | return visitor.visitContainerEnd();
|
| 488 | }
|
| 489 |
|
| 490 | public <T> void store(final String name, final Codec<T> codec, final T value) {
|
| 491 | this.store(name, codec, NbtOps.INSTANCE, value);
|
| 492 | }
|
| 493 |
|
| 494 | public <T> void storeNullable(final String name, final Codec<T> codec, final @Nullable T value) {
|
| 495 | if (value != null) {
|
| 496 | this.store(name, codec, value);
|
| 497 | }
|
| 498 | }
|
| 499 |
|
| 500 | public <T> void store(final String name, final Codec<T> codec, final DynamicOps<Tag> ops, final T value) {
|
| 501 | this.put(name, codec.encodeStart(ops, value).getOrThrow());
|
| 502 | }
|
| 503 |
|
| 504 | public <T> void storeNullable(final String name, final Codec<T> codec, final DynamicOps<Tag> ops, final @Nullable T value) {
|
| 505 | if (value != null) {
|
| 506 | this.store(name, codec, ops, value);
|
| 507 | }
|
| 508 | }
|
| 509 |
|
| 510 | public <T> void store(final MapCodec<T> codec, final T value) {
|
| 511 | this.store(codec, NbtOps.INSTANCE, value);
|
| 512 | }
|
| 513 |
|
| 514 | public <T> void store(final MapCodec<T> codec, final DynamicOps<Tag> ops, final T value) {
|
| 515 | this.merge((CompoundTag)codec.encoder().encodeStart(ops, value).getOrThrow());
|
| 516 | }
|
| 517 |
|
| 518 | public <T> Optional<T> read(final String name, final Codec<T> codec) {
|
| 519 | return this.read(name, codec, NbtOps.INSTANCE);
|
| 520 | }
|
| 521 |
|
| 522 | public <T> Optional<T> read(final String name, final Codec<T> codec, final DynamicOps<Tag> ops) {
|
| 523 | Tag tag = this.get(name);
|
| 524 | return tag == null ? Optional.empty() : codec.parse(ops, tag).resultOrPartial(error -> LOGGER.error("Failed to read field ({}={}): {}", name, tag, error));
|
| 525 | }
|
| 526 |
|
| 527 | public <T> Optional<T> read(final MapCodec<T> codec) {
|
| 528 | return this.read(codec, NbtOps.INSTANCE);
|
| 529 | }
|
| 530 |
|
| 531 | public <T> Optional<T> read(final MapCodec<T> codec, final DynamicOps<Tag> ops) {
|
| 532 | return codec.decode(ops, ops.getMap(this).getOrThrow()).resultOrPartial(error -> LOGGER.error("Failed to read value ({}): {}", this, error));
|
| 533 | }
|
| 534 | }
|
| 535 |
|