From f6c49555bf5f64fccf9eb29660f976800316edce Mon Sep 17 00:00:00 2001 From: Nassim Jahnke Date: Sun, 31 Dec 2023 13:38:14 +0100 Subject: [PATCH] Update ViaNBT --- .../viaversion/api/data/MappingDataLoader.java | 10 ++++++---- .../api/type/types/misc/CompoundTagType.java | 5 +++-- .../type/types/misc/NamedCompoundTagType.java | 12 +++++------- .../viaversion/api/type/types/misc/TagType.java | 9 ++++----- .../kotlin/via.shadow-conventions.gradle.kts | 1 - .../packets/InventoryPackets.java | 4 ++-- .../packets/InventoryPackets.java | 2 +- .../protocol1_16_2to1_16_1/data/MappingData.java | 12 ++---------- .../packets/EntityPackets.java | 2 +- .../packets/EntityPackets.java | 2 +- .../Protocol1_19_1To1_19.java | 2 +- .../protocol1_19_4to1_19_3/data/MappingData.java | 10 ++-------- .../protocol1_19to1_18_2/data/MappingData.java | 16 +++++----------- .../packets/EntityPackets.java | 4 ++-- .../chunks/FakeTileEntity.java | 2 +- .../storage/CommandBlockStorage.java | 2 +- gradle/libs.versions.toml | 2 +- 17 files changed, 38 insertions(+), 59 deletions(-) diff --git a/api/src/main/java/com/viaversion/viaversion/api/data/MappingDataLoader.java b/api/src/main/java/com/viaversion/viaversion/api/data/MappingDataLoader.java index 8f31af942..c6dc0e731 100644 --- a/api/src/main/java/com/viaversion/viaversion/api/data/MappingDataLoader.java +++ b/api/src/main/java/com/viaversion/viaversion/api/data/MappingDataLoader.java @@ -22,12 +22,13 @@ */ package com.viaversion.viaversion.api.data; -import com.github.steveice10.opennbt.NBTIO; import com.github.steveice10.opennbt.tag.builtin.ByteTag; import com.github.steveice10.opennbt.tag.builtin.CompoundTag; import com.github.steveice10.opennbt.tag.builtin.IntArrayTag; import com.github.steveice10.opennbt.tag.builtin.IntTag; import com.github.steveice10.opennbt.tag.builtin.ListTag; +import com.github.steveice10.opennbt.tag.io.NBTIO; +import com.github.steveice10.opennbt.tag.io.TagReader; import com.google.common.annotations.Beta; import com.google.gson.JsonArray; import com.google.gson.JsonElement; @@ -51,11 +52,12 @@ import org.checkerframework.checker.nullness.qual.Nullable; public final class MappingDataLoader { + private static final Map MAPPINGS_CACHE = new HashMap<>(); + private static final TagReader MAPPINGS_READER = NBTIO.reader(CompoundTag.class).named(); private static final byte DIRECT_ID = 0; private static final byte SHIFTS_ID = 1; private static final byte CHANGES_ID = 2; private static final byte IDENTITY_ID = 3; - private static final Map MAPPINGS_CACHE = new HashMap<>(); private static boolean cacheValid = true; @Deprecated/*(forRemoval = true)*/ @@ -131,14 +133,14 @@ public final class MappingDataLoader { return loadNBT(name, false); } - private static @Nullable CompoundTag loadNBTFromFile(final String name) { + public static @Nullable CompoundTag loadNBTFromFile(final String name) { final InputStream resource = getResource(name); if (resource == null) { return null; } try (final InputStream stream = resource) { - return NBTIO.readTag(stream); + return MAPPINGS_READER.read(stream); } catch (final IOException e) { throw new RuntimeException(e); } diff --git a/api/src/main/java/com/viaversion/viaversion/api/type/types/misc/CompoundTagType.java b/api/src/main/java/com/viaversion/viaversion/api/type/types/misc/CompoundTagType.java index c20f7621e..da7f0f316 100644 --- a/api/src/main/java/com/viaversion/viaversion/api/type/types/misc/CompoundTagType.java +++ b/api/src/main/java/com/viaversion/viaversion/api/type/types/misc/CompoundTagType.java @@ -26,6 +26,7 @@ import com.github.steveice10.opennbt.tag.builtin.CompoundTag; import com.viaversion.viaversion.api.type.OptionalType; import com.viaversion.viaversion.api.type.Type; import io.netty.buffer.ByteBuf; +import java.io.IOException; /** @@ -41,12 +42,12 @@ public class CompoundTagType extends Type { } @Override - public CompoundTag read(final ByteBuf buffer) throws Exception { + public CompoundTag read(final ByteBuf buffer) throws IOException { return NamedCompoundTagType.read(buffer, false); } @Override - public void write(final ByteBuf buffer, final CompoundTag object) throws Exception { + public void write(final ByteBuf buffer, final CompoundTag object) throws IOException { NamedCompoundTagType.write(buffer, object, null); } diff --git a/api/src/main/java/com/viaversion/viaversion/api/type/types/misc/NamedCompoundTagType.java b/api/src/main/java/com/viaversion/viaversion/api/type/types/misc/NamedCompoundTagType.java index 15dd03c27..25ac7b53c 100644 --- a/api/src/main/java/com/viaversion/viaversion/api/type/types/misc/NamedCompoundTagType.java +++ b/api/src/main/java/com/viaversion/viaversion/api/type/types/misc/NamedCompoundTagType.java @@ -42,16 +42,16 @@ public class NamedCompoundTagType extends Type { } @Override - public CompoundTag read(final ByteBuf buffer) throws Exception { + public CompoundTag read(final ByteBuf buffer) throws IOException { return read(buffer, true); } @Override - public void write(final ByteBuf buffer, final CompoundTag object) throws Exception { + public void write(final ByteBuf buffer, final CompoundTag object) throws IOException { write(buffer, object, ""); } - public static CompoundTag read(final ByteBuf buffer, final boolean readName) throws Exception { + public static CompoundTag read(final ByteBuf buffer, final boolean readName) throws IOException { final byte id = buffer.readByte(); if (id == 0) { return null; @@ -65,12 +65,10 @@ public class NamedCompoundTagType extends Type { } final TagLimiter tagLimiter = TagLimiter.create(MAX_NBT_BYTES, MAX_NESTING_LEVEL); - final CompoundTag tag = new CompoundTag(); - tag.read(new ByteBufInputStream(buffer), tagLimiter); - return tag; + return CompoundTag.read(new ByteBufInputStream(buffer), tagLimiter, 0); } - public static void write(final ByteBuf buffer, final Tag tag, final @Nullable String name) throws Exception { + public static void write(final ByteBuf buffer, final Tag tag, final @Nullable String name) throws IOException { if (tag == null) { buffer.writeByte(0); return; diff --git a/api/src/main/java/com/viaversion/viaversion/api/type/types/misc/TagType.java b/api/src/main/java/com/viaversion/viaversion/api/type/types/misc/TagType.java index 47d9d038d..3e8689488 100644 --- a/api/src/main/java/com/viaversion/viaversion/api/type/types/misc/TagType.java +++ b/api/src/main/java/com/viaversion/viaversion/api/type/types/misc/TagType.java @@ -29,6 +29,7 @@ import com.viaversion.viaversion.api.type.OptionalType; import com.viaversion.viaversion.api.type.Type; import io.netty.buffer.ByteBuf; import io.netty.buffer.ByteBufInputStream; +import java.io.IOException; public class TagType extends Type { @@ -37,20 +38,18 @@ public class TagType extends Type { } @Override - public Tag read(final ByteBuf buffer) throws Exception { + public Tag read(final ByteBuf buffer) throws IOException { final byte id = buffer.readByte(); if (id == 0) { return null; } final TagLimiter tagLimiter = TagLimiter.create(NamedCompoundTagType.MAX_NBT_BYTES, NamedCompoundTagType.MAX_NESTING_LEVEL); - final Tag tag = TagRegistry.createInstance(id); - tag.read(new ByteBufInputStream(buffer), tagLimiter); - return tag; + return TagRegistry.read(id, new ByteBufInputStream(buffer), tagLimiter, 0); } @Override - public void write(final ByteBuf buffer, final Tag tag) throws Exception { + public void write(final ByteBuf buffer, final Tag tag) throws IOException { NamedCompoundTagType.write(buffer, tag, null); } diff --git a/build-logic/src/main/kotlin/via.shadow-conventions.gradle.kts b/build-logic/src/main/kotlin/via.shadow-conventions.gradle.kts index a820b6d0e..21ca9a760 100644 --- a/build-logic/src/main/kotlin/via.shadow-conventions.gradle.kts +++ b/build-logic/src/main/kotlin/via.shadow-conventions.gradle.kts @@ -49,7 +49,6 @@ fun ShadowJar.configureExcludes() { exclude("it/unimi/dsi/fastutil/*/*Big*") exclude("it/unimi/dsi/fastutil/*/*Synchronized*") exclude("it/unimi/dsi/fastutil/*/*Unmodifiable*") - exclude("it/unimi/dsi/fastutil/io/*") // Flare - only need int maps exclude("space/vectrix/flare/fastutil/*Double*") exclude("space/vectrix/flare/fastutil/*Float*") diff --git a/common/src/main/java/com/viaversion/viaversion/protocols/protocol1_13to1_12_2/packets/InventoryPackets.java b/common/src/main/java/com/viaversion/viaversion/protocols/protocol1_13to1_12_2/packets/InventoryPackets.java index 7c5353eed..d8dcab56c 100644 --- a/common/src/main/java/com/viaversion/viaversion/protocols/protocol1_13to1_12_2/packets/InventoryPackets.java +++ b/common/src/main/java/com/viaversion/viaversion/protocols/protocol1_13to1_12_2/packets/InventoryPackets.java @@ -361,7 +361,7 @@ public class InventoryPackets extends ItemRewriter dimensionDataMap = new HashMap<>(); @@ -40,11 +36,7 @@ public class MappingData extends MappingDataBase { @Override public void loadExtras(final CompoundTag data) { - try { - dimensionRegistry = NBTIO.readTag(MappingDataLoader.getResource("dimension-registry-1.16.2.nbt")); - } catch (final IOException e) { - Via.getPlatform().getLogger().log(Level.SEVERE, "Error loading dimension registry:", e); - } + dimensionRegistry = MappingDataLoader.loadNBTFromFile("dimension-registry-1.16.2.nbt"); // Data of each dimension final ListTag dimensions = ((CompoundTag) dimensionRegistry.get("minecraft:dimension_type")).get("value"); @@ -61,6 +53,6 @@ public class MappingData extends MappingDataBase { } public CompoundTag getDimensionRegistry() { - return dimensionRegistry.clone(); + return dimensionRegistry.copy(); } } diff --git a/common/src/main/java/com/viaversion/viaversion/protocols/protocol1_16_2to1_16_1/packets/EntityPackets.java b/common/src/main/java/com/viaversion/viaversion/protocols/protocol1_16_2to1_16_1/packets/EntityPackets.java index a4a5d714a..579b40d64 100644 --- a/common/src/main/java/com/viaversion/viaversion/protocols/protocol1_16_2to1_16_1/packets/EntityPackets.java +++ b/common/src/main/java/com/viaversion/viaversion/protocols/protocol1_16_2to1_16_1/packets/EntityPackets.java @@ -77,6 +77,6 @@ public class EntityPackets { Via.getPlatform().getLogger().severe("Could not get dimension data of " + dimensionType); throw new NullPointerException("Dimension data for " + dimensionType + " is null!"); } - return tag.clone(); + return tag.copy(); } } diff --git a/common/src/main/java/com/viaversion/viaversion/protocols/protocol1_16to1_15_2/packets/EntityPackets.java b/common/src/main/java/com/viaversion/viaversion/protocols/protocol1_16to1_15_2/packets/EntityPackets.java index 1895f5eb2..d52db0dd1 100644 --- a/common/src/main/java/com/viaversion/viaversion/protocols/protocol1_16to1_15_2/packets/EntityPackets.java +++ b/common/src/main/java/com/viaversion/viaversion/protocols/protocol1_16to1_15_2/packets/EntityPackets.java @@ -210,7 +210,7 @@ public class EntityPackets { handler(wrapper -> { wrapper.write(Type.BYTE, (byte) -1); // Previous gamemode, set to none wrapper.write(Type.STRING_ARRAY, Arrays.copyOf(WORLD_NAMES, WORLD_NAMES.length)); // World list - only used for command completion - wrapper.write(Type.NAMED_COMPOUND_TAG, DIMENSIONS_TAG.clone()); // Dimension registry + wrapper.write(Type.NAMED_COMPOUND_TAG, DIMENSIONS_TAG.copy()); // Dimension registry }); handler(DIMENSION_HANDLER); // Dimension map(Type.LONG); // Seed diff --git a/common/src/main/java/com/viaversion/viaversion/protocols/protocol1_19_1to1_19/Protocol1_19_1To1_19.java b/common/src/main/java/com/viaversion/viaversion/protocols/protocol1_19_1to1_19/Protocol1_19_1To1_19.java index f33d72048..d40026a7e 100644 --- a/common/src/main/java/com/viaversion/viaversion/protocols/protocol1_19_1to1_19/Protocol1_19_1To1_19.java +++ b/common/src/main/java/com/viaversion/viaversion/protocols/protocol1_19_1to1_19/Protocol1_19_1To1_19.java @@ -230,7 +230,7 @@ public final class Protocol1_19_1To1_19 extends AbstractProtocol