diff --git a/gradle/libs.versions.toml b/gradle/libs.versions.toml index 771624f9..1e1883ee 100644 --- a/gradle/libs.versions.toml +++ b/gradle/libs.versions.toml @@ -3,7 +3,7 @@ metadata.format.version = "1.1" [versions] adventure = "4.15.0" -opennbt = "1.6" +cloudburstnbt = "3.0.0.Final" mcauthlib = "6621fd0" math = "2.0" fastutil-maps = "8.5.3" @@ -22,7 +22,7 @@ lombok-plugin = "8.4" adventure-text-serializer-gson = { module = "net.kyori:adventure-text-serializer-gson", version.ref = "adventure" } adventure-text-serializer-json-legacy-impl = { module = "net.kyori:adventure-text-serializer-json-legacy-impl", version.ref = "adventure" } -opennbt = { module = "com.github.steveice10:opennbt", version.ref = "opennbt" } +cloudburstnbt = { module = "org.cloudburstmc:nbt", version.ref = "cloudburstnbt" } mcauthlib = { module = "com.github.GeyserMC:mcauthlib", version.ref = "mcauthlib" } math-api = { module = "org.cloudburstmc.math:api", version.ref = "math" } diff --git a/protocol/build.gradle.kts b/protocol/build.gradle.kts index afd8247b..01ba7f4b 100644 --- a/protocol/build.gradle.kts +++ b/protocol/build.gradle.kts @@ -7,7 +7,7 @@ description = "MCProtocolLib is a simple library for communicating with Minecraf dependencies { // Minecraft related libraries - api(libs.opennbt) + api(libs.cloudburstnbt) api(libs.mcauthlib) // Kyori adventure diff --git a/protocol/src/main/java/org/geysermc/mcprotocollib/protocol/MinecraftProtocol.java b/protocol/src/main/java/org/geysermc/mcprotocollib/protocol/MinecraftProtocol.java index 7c18f6fa..cbb0264f 100644 --- a/protocol/src/main/java/org/geysermc/mcprotocollib/protocol/MinecraftProtocol.java +++ b/protocol/src/main/java/org/geysermc/mcprotocollib/protocol/MinecraftProtocol.java @@ -1,13 +1,13 @@ package org.geysermc.mcprotocollib.protocol; import com.github.steveice10.mc.auth.data.GameProfile; +import org.cloudburstmc.nbt.NbtMap; +import org.cloudburstmc.nbt.NbtUtils; import org.geysermc.mcprotocollib.protocol.codec.MinecraftCodec; import org.geysermc.mcprotocollib.protocol.codec.MinecraftCodecHelper; import org.geysermc.mcprotocollib.protocol.codec.PacketCodec; import org.geysermc.mcprotocollib.protocol.codec.PacketStateCodec; import org.geysermc.mcprotocollib.protocol.data.ProtocolState; -import com.github.steveice10.opennbt.NBTIO; -import com.github.steveice10.opennbt.tag.builtin.CompoundTag; import org.geysermc.mcprotocollib.network.Server; import org.geysermc.mcprotocollib.network.Session; import org.geysermc.mcprotocollib.network.codec.PacketCodecHelper; @@ -23,14 +23,11 @@ import lombok.NonNull; import lombok.Setter; import org.checkerframework.checker.nullness.qual.Nullable; -import java.io.DataInput; -import java.io.DataInputStream; import java.io.InputStream; import java.security.GeneralSecurityException; import java.security.Key; import java.util.Objects; import java.util.UUID; -import java.util.zip.GZIPInputStream; /** * Implements the Minecraft protocol. @@ -43,7 +40,7 @@ public class MinecraftProtocol extends PacketProtocol { * if {@link #isUseDefaultListeners()} is true. */ @Nullable - private static CompoundTag DEFAULT_NETWORK_CODEC; + private static NbtMap DEFAULT_NETWORK_CODEC; /** * The codec used for the Minecraft protocol. @@ -251,10 +248,9 @@ public class MinecraftProtocol extends PacketProtocol { return this.stateCodec.getClientboundDefinition(id); } - public static CompoundTag loadNetworkCodec() { - try (InputStream inputStream = Objects.requireNonNull(MinecraftProtocol.class.getClassLoader().getResourceAsStream("networkCodec.nbt")) ; - DataInputStream stream = new DataInputStream(new GZIPInputStream(inputStream))) { - return (CompoundTag) NBTIO.readTag((DataInput) stream); + public static NbtMap loadNetworkCodec() { + try (InputStream inputStream = Objects.requireNonNull(MinecraftProtocol.class.getClassLoader().getResourceAsStream("networkCodec.nbt"))) { + return (NbtMap) NbtUtils.createGZIPReader(inputStream).readTag(); } catch (Exception e) { throw new AssertionError("Unable to load network codec.", e); } diff --git a/protocol/src/main/java/org/geysermc/mcprotocollib/protocol/ServerListener.java b/protocol/src/main/java/org/geysermc/mcprotocollib/protocol/ServerListener.java index 2de06986..8cf99163 100644 --- a/protocol/src/main/java/org/geysermc/mcprotocollib/protocol/ServerListener.java +++ b/protocol/src/main/java/org/geysermc/mcprotocollib/protocol/ServerListener.java @@ -3,6 +3,8 @@ package org.geysermc.mcprotocollib.protocol; import com.github.steveice10.mc.auth.data.GameProfile; import com.github.steveice10.mc.auth.exception.request.RequestException; import com.github.steveice10.mc.auth.service.SessionService; +import org.cloudburstmc.nbt.NbtMap; +import org.cloudburstmc.nbt.NbtType; import org.geysermc.mcprotocollib.protocol.data.ProtocolState; import org.geysermc.mcprotocollib.protocol.data.game.RegistryEntry; import org.geysermc.mcprotocollib.protocol.data.status.PlayerInfo; @@ -28,11 +30,6 @@ import org.geysermc.mcprotocollib.protocol.packet.status.clientbound.Clientbound import org.geysermc.mcprotocollib.protocol.packet.status.clientbound.ClientboundStatusResponsePacket; import org.geysermc.mcprotocollib.protocol.packet.status.serverbound.ServerboundPingRequestPacket; import org.geysermc.mcprotocollib.protocol.packet.status.serverbound.ServerboundStatusRequestPacket; -import com.github.steveice10.opennbt.tag.builtin.CompoundTag; -import com.github.steveice10.opennbt.tag.builtin.IntTag; -import com.github.steveice10.opennbt.tag.builtin.ListTag; -import com.github.steveice10.opennbt.tag.builtin.StringTag; -import com.github.steveice10.opennbt.tag.builtin.Tag; import org.geysermc.mcprotocollib.network.Session; import org.geysermc.mcprotocollib.network.event.session.ConnectedEvent; import org.geysermc.mcprotocollib.network.event.session.DisconnectingEvent; @@ -46,13 +43,7 @@ import java.security.KeyPair; import java.security.KeyPairGenerator; import java.security.NoSuchAlgorithmException; import java.security.PrivateKey; -import java.util.AbstractList; -import java.util.ArrayList; -import java.util.Arrays; -import java.util.List; -import java.util.Map; -import java.util.Random; -import java.util.UUID; +import java.util.*; /** * Handles initial login and status requests for servers. @@ -74,7 +65,7 @@ public class ServerListener extends SessionAdapter { } } - private final CompoundTag networkCodec; + private final NbtMap networkCodec; private final byte[] challenge = new byte[4]; private String username = ""; @@ -82,7 +73,7 @@ public class ServerListener extends SessionAdapter { private long lastPingTime = 0; private int lastPingId = 0; - public ServerListener(CompoundTag networkCodec) { + public ServerListener(NbtMap networkCodec) { this.networkCodec = networkCodec; new Random().nextBytes(this.challenge); } @@ -139,19 +130,18 @@ public class ServerListener extends SessionAdapter { protocol.setState(ProtocolState.CONFIGURATION); // Credit ViaVersion: https://github.com/ViaVersion/ViaVersion/blob/dev/common/src/main/java/com/viaversion/viaversion/protocols/protocol1_20_5to1_20_3/rewriter/EntityPacketRewriter1_20_5.java - for (Map.Entry entry : networkCodec.getValue().entrySet()) { - CompoundTag entryTag = (CompoundTag) entry.getValue(); - StringTag typeTag = entryTag.get("type"); - ListTag valueTag = entryTag.get("value"); + for (Map.Entry entry : networkCodec.entrySet()) { + NbtMap entryTag = (NbtMap) entry.getValue(); + String typeTag = entryTag.getString("type"); + List valueTag = entryTag.getList("value", NbtType.COMPOUND); List entries = new ArrayList<>(); - for (Tag tag : valueTag) { - CompoundTag compoundTag = (CompoundTag) tag; - StringTag nameTag = compoundTag.get("name"); - int id = ((IntTag) compoundTag.get("id")).getValue(); - entries.add(id, new RegistryEntry(nameTag.getValue(), compoundTag.get("element"))); + for (NbtMap compoundTag : valueTag) { + String nameTag = compoundTag.getString("name"); + int id = compoundTag.getInt("id"); + entries.add(id, new RegistryEntry(nameTag, compoundTag.getCompound("element"))); } - session.send(new ClientboundRegistryDataPacket(typeTag.getValue(), entries)); + session.send(new ClientboundRegistryDataPacket(typeTag, entries)); } session.send(new ClientboundFinishConfigurationPacket()); diff --git a/protocol/src/main/java/org/geysermc/mcprotocollib/protocol/codec/MinecraftCodecHelper.java b/protocol/src/main/java/org/geysermc/mcprotocollib/protocol/codec/MinecraftCodecHelper.java index 30d596f8..ce767cf9 100644 --- a/protocol/src/main/java/org/geysermc/mcprotocollib/protocol/codec/MinecraftCodecHelper.java +++ b/protocol/src/main/java/org/geysermc/mcprotocollib/protocol/codec/MinecraftCodecHelper.java @@ -1,6 +1,9 @@ package org.geysermc.mcprotocollib.protocol.codec; import com.github.steveice10.mc.auth.data.GameProfile; +import io.netty.buffer.ByteBufInputStream; +import io.netty.buffer.ByteBufOutputStream; +import org.cloudburstmc.nbt.*; import org.geysermc.mcprotocollib.protocol.data.DefaultComponentSerializer; import org.geysermc.mcprotocollib.protocol.data.game.Holder; import org.geysermc.mcprotocollib.protocol.data.game.Identifier; @@ -24,7 +27,6 @@ import org.geysermc.mcprotocollib.protocol.data.game.entity.attribute.ModifierOp import org.geysermc.mcprotocollib.protocol.data.game.entity.metadata.ArmadilloState; import org.geysermc.mcprotocollib.protocol.data.game.entity.metadata.EntityMetadata; import org.geysermc.mcprotocollib.protocol.data.game.entity.metadata.GlobalPos; -import org.geysermc.mcprotocollib.protocol.data.game.item.component.BannerPatternLayer; import org.geysermc.mcprotocollib.protocol.data.game.item.component.DataComponent; import org.geysermc.mcprotocollib.protocol.data.game.item.component.DataComponents; import org.geysermc.mcprotocollib.protocol.data.game.item.component.DataComponentType; @@ -66,9 +68,6 @@ import org.geysermc.mcprotocollib.protocol.data.game.level.sound.SoundCategory; import org.geysermc.mcprotocollib.protocol.data.game.recipe.Ingredient; import org.geysermc.mcprotocollib.protocol.data.game.statistic.StatisticCategory; import org.geysermc.mcprotocollib.protocol.packet.ingame.clientbound.ClientboundLoginPacket; -import com.github.steveice10.opennbt.NBTIO; -import com.github.steveice10.opennbt.tag.builtin.CompoundTag; -import com.github.steveice10.opennbt.tag.builtin.Tag; import org.geysermc.mcprotocollib.network.codec.BasePacketCodecHelper; import com.google.gson.JsonElement; import io.netty.buffer.ByteBuf; @@ -82,23 +81,16 @@ import org.cloudburstmc.math.vector.Vector4f; import org.checkerframework.checker.nullness.qual.Nullable; import org.jetbrains.annotations.NotNull; -import java.io.IOException; -import java.io.InputStream; -import java.io.OutputStream; -import java.util.ArrayList; -import java.util.Arrays; -import java.util.BitSet; -import java.util.EnumSet; -import java.util.HashMap; -import java.util.List; -import java.util.Map; -import java.util.UUID; +import java.io.*; +import java.util.*; import java.util.function.BiConsumer; import java.util.function.Function; import java.util.function.IntFunction; import java.util.function.ObjIntConsumer; import java.util.function.ToIntFunction; +import static org.cloudburstmc.nbt.NbtType.byClass; + @RequiredArgsConstructor public class MinecraftCodecHelper extends BasePacketCodecHelper { private static final int POSITION_X_SIZE = 38; @@ -110,7 +102,7 @@ public class MinecraftCodecHelper extends BasePacketCodecHelper { private final Int2ObjectMap levelEvents; private final Map soundNames; - protected CompoundTag registry; + protected NbtMap registry; @Nullable public T readNullable(ByteBuf buf, Function ifPresent) { @@ -211,52 +203,66 @@ public class MinecraftCodecHelper extends BasePacketCodecHelper { } @Nullable - public CompoundTag readAnyTag(ByteBuf buf) { - return readAnyTag(buf, CompoundTag.class); + public NbtMap readCompoundTag(ByteBuf buf) { + return readAnyTag(buf, NbtType.COMPOUND); } @NonNull - public CompoundTag readAnyTagOrThrow(ByteBuf buf) { - CompoundTag tag = readAnyTag(buf); + public NbtMap readCompoundTagOrThrow(ByteBuf buf) { + NbtMap tag = readCompoundTag(buf); if (tag == null) { - throw new IllegalArgumentException("Got end-tag when trying to read CompoundTag"); + throw new IllegalArgumentException("Got end-tag when trying to read NbtMap"); } return tag; } @Nullable - public T readAnyTag(ByteBuf buf, Class expected) { - Tag tag; - try { - tag = NBTIO.readAnyTag(new InputStream() { - @Override - public int read() { - return buf.readUnsignedByte(); - } - }); - } catch (IOException e) { - throw new IllegalArgumentException(e); - } + public T readAnyTag(ByteBuf buf, NbtType expected) { + Object tag = this.readAnyTag(buf); if (tag == null) { return null; } - if (!expected.isInstance(tag)) { - throw new IllegalArgumentException("Expected tag of type " + expected.getName() + " but got " + tag.getClass().getName()); + Class tagClass = expected.getTagClass(); + if (!tagClass.isInstance(tag)) { + throw new IllegalArgumentException("Expected tag of type " + tagClass.getName() + " but got " + tag.getClass().getName()); } - return expected.cast(tag); + return tagClass.cast(tag); } - public void writeAnyTag(ByteBuf buf, @Nullable T tag) { + @Nullable + public Object readAnyTag(ByteBuf buf) { try { - NBTIO.writeAnyTag(new OutputStream() { - @Override - public void write(int b) { - buf.writeByte(b); - } - }, tag); + ByteBufInputStream input = new ByteBufInputStream(buf); + + int typeId = input.readUnsignedByte(); + if (typeId == 0) { + return null; + } + + NbtType type = NbtType.byId(typeId); + + return new NBTInputStream(input).readValue(type); + } catch (IOException e) { + throw new IllegalArgumentException(e); + } + } + + public void writeAnyTag(ByteBuf buf, @Nullable Object tag) { + try { + ByteBufOutputStream output = new ByteBufOutputStream(buf); + + if (tag == null) { + output.writeByte(0); + return; + } + + NbtType type = NbtType.byClass(tag.getClass()); + output.writeByte(type.getId()); + + new NBTOutputStream(output).writeValue(tag); } catch (IOException e) { throw new IllegalArgumentException(e); } @@ -474,8 +480,8 @@ public class MinecraftCodecHelper extends BasePacketCodecHelper { } public Component readComponent(ByteBuf buf) { - // do not use CompoundTag, as mojang serializes a plaintext component as just a single StringTag - Tag tag = readAnyTag(buf, Tag.class); + // do not use NbtMap, as mojang serializes a plaintext component as just a single StringTag + Object tag = readAnyTag(buf); if (tag == null) { throw new IllegalArgumentException("Got end-tag when trying to read Component"); } @@ -485,7 +491,7 @@ public class MinecraftCodecHelper extends BasePacketCodecHelper { public void writeComponent(ByteBuf buf, Component component) { JsonElement json = DefaultComponentSerializer.get().serializeToTree(component); - Tag tag = NbtComponentSerializer.jsonComponentToTag(json); + Object tag = NbtComponentSerializer.jsonComponentToTag(json); writeAnyTag(buf, tag); } @@ -658,7 +664,7 @@ public class MinecraftCodecHelper extends BasePacketCodecHelper { int id = this.readVarInt(buf); return switch (id) { case 0 -> BlankFormat.INSTANCE; - case 1 -> new StyledFormat(this.readAnyTagOrThrow(buf)); + case 1 -> new StyledFormat(this.readCompoundTagOrThrow(buf)); case 2 -> new FixedFormat(this.readComponent(buf)); default -> throw new IllegalArgumentException("Unknown number format type: " + id); }; @@ -1022,11 +1028,11 @@ public class MinecraftCodecHelper extends BasePacketCodecHelper { * @return the game registry */ @Nullable - public CompoundTag getRegistry() { + public NbtMap getRegistry() { return this.registry; } - public void setRegistry(CompoundTag registry) { + public void setRegistry(NbtMap registry) { this.registry = registry; } } diff --git a/protocol/src/main/java/org/geysermc/mcprotocollib/protocol/codec/NbtComponentSerializer.java b/protocol/src/main/java/org/geysermc/mcprotocollib/protocol/codec/NbtComponentSerializer.java index 70deb8c1..621b9a36 100644 --- a/protocol/src/main/java/org/geysermc/mcprotocollib/protocol/codec/NbtComponentSerializer.java +++ b/protocol/src/main/java/org/geysermc/mcprotocollib/protocol/codec/NbtComponentSerializer.java @@ -1,18 +1,5 @@ package org.geysermc.mcprotocollib.protocol.codec; -import com.github.steveice10.opennbt.tag.builtin.ByteArrayTag; -import com.github.steveice10.opennbt.tag.builtin.ByteTag; -import com.github.steveice10.opennbt.tag.builtin.CompoundTag; -import com.github.steveice10.opennbt.tag.builtin.DoubleTag; -import com.github.steveice10.opennbt.tag.builtin.FloatTag; -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.builtin.LongArrayTag; -import com.github.steveice10.opennbt.tag.builtin.LongTag; -import com.github.steveice10.opennbt.tag.builtin.ShortTag; -import com.github.steveice10.opennbt.tag.builtin.StringTag; -import com.github.steveice10.opennbt.tag.builtin.Tag; import com.google.gson.JsonArray; import com.google.gson.JsonElement; import com.google.gson.JsonObject; @@ -20,6 +7,10 @@ import com.google.gson.JsonPrimitive; import com.google.gson.internal.LazilyParsedNumber; import lombok.AllArgsConstructor; import org.checkerframework.checker.nullness.qual.Nullable; +import org.cloudburstmc.nbt.NbtList; +import org.cloudburstmc.nbt.NbtMap; +import org.cloudburstmc.nbt.NbtMapBuilder; +import org.cloudburstmc.nbt.NbtType; import org.jetbrains.annotations.Contract; import java.util.Arrays; @@ -34,16 +25,16 @@ import java.util.UUID; */ public class NbtComponentSerializer { - private static final Set BOOLEAN_TYPES = new HashSet<>(Arrays.asList( + private static final Set BOOLEAN_TYPES = Set.of( "interpret", "bold", "italic", "underlined", "strikethrough", "obfuscated" - )); + ); // Order is important - private static final List> COMPONENT_TYPES = Arrays.asList( + private static final List> COMPONENT_TYPES = List.of( new Pair<>("text", "text"), new Pair<>("translatable", "translate"), new Pair<>("score", "score"), @@ -57,72 +48,86 @@ public class NbtComponentSerializer { } @Contract("null -> null") - public static JsonElement tagComponentToJson(@Nullable final Tag tag) { + public static JsonElement tagComponentToJson(@Nullable final Object tag) { return convertToJson(null, tag); } - public static @Nullable Tag jsonComponentToTag(@Nullable final JsonElement component) { - return convertToTag("", component); + public static @Nullable Object jsonComponentToTag(@Nullable final JsonElement component) { + return convertToTag(component); } - @Contract("_, null -> null") - private static Tag convertToTag(String name, final @Nullable JsonElement element) { + @Contract("null -> null") + private static Object convertToTag(final @Nullable JsonElement element) { if (element == null || element.isJsonNull()) { return null; } else if (element.isJsonObject()) { - final CompoundTag tag = new CompoundTag(name); + final NbtMapBuilder tag = NbtMap.builder(); final JsonObject jsonObject = element.getAsJsonObject(); for (final Map.Entry entry : jsonObject.entrySet()) { convertObjectEntry(entry.getKey(), entry.getValue(), tag); } addComponentType(jsonObject, tag); - return tag; + return tag.build(); } else if (element.isJsonArray()) { - return convertJsonArray(name, element.getAsJsonArray()); + return convertJsonArray(element.getAsJsonArray()); } else if (element.isJsonPrimitive()) { final JsonPrimitive primitive = element.getAsJsonPrimitive(); if (primitive.isString()) { - return new StringTag(name, primitive.getAsString()); + return primitive.getAsString(); } else if (primitive.isBoolean()) { - return new ByteTag(name, (byte) (primitive.getAsBoolean() ? 1 : 0)); + return (byte) (primitive.getAsBoolean() ? 1 : 0); } final Number number = primitive.getAsNumber(); if (number instanceof Integer) { - return new IntTag(name, number.intValue()); + return number.intValue(); } else if (number instanceof Byte) { - return new ByteTag(name, number.byteValue()); + return number.byteValue(); } else if (number instanceof Short) { - return new ShortTag(name, number.shortValue()); + return number.shortValue(); } else if (number instanceof Long) { - return new LongTag(name, number.longValue()); + return number.longValue(); } else if (number instanceof Double) { - return new DoubleTag(name, number.doubleValue()); + return number.doubleValue(); } else if (number instanceof Float) { - return new FloatTag(name, number.floatValue()); + return number.floatValue(); } else if (number instanceof LazilyParsedNumber) { // TODO: This might need better handling - return new IntTag(name, number.intValue()); + return number.intValue(); } - return new IntTag(name, number.intValue()); // ??? + return number.intValue(); // ??? } throw new IllegalArgumentException("Unhandled json type " + element.getClass().getSimpleName() + " with value " + element.getAsString()); } - private static ListTag convertJsonArray(String name, final JsonArray array) { + private static void addToListOrFail(final NbtList list, final Object tag) { + Class listClass = list.getType().getTagClass(); + + if (listClass.isInstance(tag)) { + list.add(listClass.cast(tag)); + } else { + throw new IllegalArgumentException("Cannot add " + tag.getClass().getSimpleName() + " to list of " + listClass.getSimpleName()); + } + } + + private static NbtList convertJsonArray(final JsonArray array) { // TODO Number arrays? - final ListTag listTag = new ListTag(name); + NbtList listTag = null; boolean singleType = true; for (final JsonElement entry : array) { - final Tag convertedEntryTag = convertToTag("", entry); - if (listTag.getElementType() != null && listTag.getElementType() != convertedEntryTag.getClass()) { + final Object convertedEntryTag = convertToTag(entry); + if (listTag == null) { + listTag = new NbtList<>(NbtType.byClass(convertedEntryTag.getClass())); + } + + if (listTag.getType() != NbtType.byClass(convertedEntryTag.getClass())) { singleType = false; break; } - listTag.add(convertedEntryTag); + addToListOrFail(listTag, convertedEntryTag); } if (singleType) { @@ -131,24 +136,24 @@ public class NbtComponentSerializer { // Generally, vanilla-esque serializers should not produce this format, so it should be rare // Lists are only used for lists of components ("extra" and "with") - final ListTag processedListTag = new ListTag(name); + final NbtList processedListTag = new NbtList<>(NbtType.COMPOUND); for (final JsonElement entry : array) { - final Tag convertedTag = convertToTag("", entry); - if (convertedTag instanceof CompoundTag) { - processedListTag.add(convertedTag); + final Object convertedTag = convertToTag(entry); + if (convertedTag instanceof NbtMap nbtMap) { + processedListTag.add(nbtMap); continue; } // Wrap all entries in compound tags, as lists can only consist of one type of tag - final CompoundTag compoundTag = new CompoundTag(""); - compoundTag.put(new StringTag("type", "text")); - if (convertedTag instanceof ListTag) { - compoundTag.put(new StringTag("text")); - compoundTag.put(new ListTag("extra", ((ListTag) convertedTag).getValue())); + final NbtMapBuilder compoundTag = NbtMap.builder(); + compoundTag.put("type", "text"); + if (convertedTag instanceof NbtList list) { + compoundTag.put("text", ""); + compoundTag.put("extra", list); } else { - compoundTag.put(new StringTag("text", stringValue(convertedTag))); + compoundTag.put("text", stringValue(convertedTag)); } - processedListTag.add(compoundTag); + processedListTag.add(compoundTag.build()); } return processedListTag; } @@ -160,7 +165,7 @@ public class NbtComponentSerializer { * @param value value of the entry * @param tag the resulting compound tag */ - private static void convertObjectEntry(final String key, final JsonElement value, final CompoundTag tag) { + private static void convertObjectEntry(final String key, final JsonElement value, final NbtMapBuilder tag) { if ((key.equals("contents")) && value.isJsonObject()) { // Store show_entity id as int array instead of uuid string // Not really required, but we might as well make it more compact @@ -170,17 +175,17 @@ public class NbtComponentSerializer { if (id != null && id.isJsonPrimitive() && (uuid = parseUUID(id.getAsString())) != null) { hoverEvent.remove("id"); - final CompoundTag convertedTag = (CompoundTag) convertToTag(key, value); - convertedTag.put(new IntArrayTag("id", toIntArray(uuid))); - tag.put(convertedTag); + final NbtMapBuilder convertedTag = ((NbtMap) convertToTag(value)).toBuilder(); + convertedTag.put("id", toIntArray(uuid)); + tag.put(key, convertedTag.build()); return; } } - tag.put(convertToTag(key, value)); + tag.put(key, convertToTag(value)); } - private static void addComponentType(final JsonObject object, final CompoundTag tag) { + private static void addComponentType(final JsonObject object, final NbtMapBuilder tag) { if (object.has("type")) { return; } @@ -188,54 +193,54 @@ public class NbtComponentSerializer { // Add the type to speed up deserialization and make DFU errors slightly more useful for (final Pair pair : COMPONENT_TYPES) { if (object.has(pair.value)) { - tag.put(new StringTag("type", pair.key)); + tag.put("type", pair.key); return; } } } - private static @Nullable JsonElement convertToJson(final @Nullable String key, final @Nullable Tag tag) { + private static @Nullable JsonElement convertToJson(final @Nullable String key, final @Nullable Object tag) { if (tag == null) { return null; - } else if (tag instanceof CompoundTag) { + } else if (tag instanceof NbtMap nbtMap) { final JsonObject object = new JsonObject(); if (!"value".equals(key)) { removeComponentType(object); } - for (final Tag entry : ((CompoundTag) tag)) { - convertCompoundTagEntry(entry.getName(), entry, object); + for (Map.Entry entry : nbtMap.entrySet()) { + convertNbtMapEntry(entry.getKey(), entry.getValue(), object); } return object; - } else if (tag instanceof ListTag list) { + } else if (tag instanceof NbtList list) { final JsonArray array = new JsonArray(); - for (final Tag listEntry : list) { + for (final Object listEntry : list) { array.add(convertToJson(null, listEntry)); } return array; - } else if (tag.getValue() instanceof Number number) { + } else if (tag instanceof Number number) { if (key != null && BOOLEAN_TYPES.contains(key)) { // Booleans don't have a direct representation in nbt return new JsonPrimitive(number.byteValue() != 0); } return new JsonPrimitive(number); - } else if (tag instanceof StringTag) { - return new JsonPrimitive(((StringTag) tag).getValue()); - } else if (tag instanceof ByteArrayTag arrayTag) { + } else if (tag instanceof String string) { + return new JsonPrimitive(string); + } else if (tag instanceof byte[] arrayTag) { final JsonArray array = new JsonArray(); - for (final byte num : arrayTag.getValue()) { + for (final byte num : arrayTag) { array.add(num); } return array; - } else if (tag instanceof IntArrayTag arrayTag) { + } else if (tag instanceof int[] arrayTag) { final JsonArray array = new JsonArray(); - for (final int num : arrayTag.getValue()) { + for (final int num : arrayTag) { array.add(num); } return array; - } else if (tag instanceof LongArrayTag arrayTag) { + } else if (tag instanceof long[] arrayTag) { final JsonArray array = new JsonArray(); - for (final long num : arrayTag.getValue()) { + for (final long num : arrayTag) { array.add(num); } return array; @@ -243,15 +248,13 @@ public class NbtComponentSerializer { throw new IllegalArgumentException("Unhandled tag type " + tag.getClass().getSimpleName()); } - private static void convertCompoundTagEntry(final String key, final Tag tag, final JsonObject object) { - if ((key.equals("contents")) && tag instanceof CompoundTag showEntity) { + private static void convertNbtMapEntry(final String key, final Object tag, final JsonObject object) { + if ((key.equals("contents")) && tag instanceof NbtMap showEntity) { // Back to a UUID string - final Tag idTag = showEntity.get("id"); - if (idTag instanceof IntArrayTag) { - showEntity.remove("id"); - + final Object idTag = showEntity.get("id"); + if (idTag instanceof int[] array) { final JsonObject convertedElement = (JsonObject) convertToJson(key, tag); - final UUID uuid = fromIntArray(((IntArrayTag) idTag).getValue()); + final UUID uuid = fromIntArray(array); convertedElement.addProperty("id", uuid.toString()); object.add(key, convertedElement); return; @@ -302,29 +305,29 @@ public class NbtComponentSerializer { } // Last adopted from https://github.com/ViaVersion/ViaNBT/commit/ad8ac024c48c2fc25e18dc689b3ca62602420ab9 - private static String stringValue(Tag tag) { - if (tag instanceof ByteArrayTag) { - return Arrays.toString(((ByteArrayTag) tag).getValue()); - } else if (tag instanceof ByteTag) { - return Byte.toString(((ByteTag) tag).getValue()); - } else if (tag instanceof DoubleTag) { - return Double.toString(((DoubleTag) tag).getValue()); - } else if (tag instanceof FloatTag) { - return Float.toString(((FloatTag) tag).getValue()); - } else if (tag instanceof IntArrayTag) { - return Arrays.toString(((IntArrayTag) tag).getValue()); - } else if (tag instanceof IntTag) { - return Integer.toString(((IntTag) tag).getValue()); - } else if (tag instanceof LongArrayTag) { - return Arrays.toString(((LongArrayTag) tag).getValue()); - } else if (tag instanceof LongTag) { - return Long.toString(((LongTag) tag).getValue()); - } else if (tag instanceof ShortTag) { - return Short.toString(((ShortTag) tag).getValue()); - } else if (tag instanceof StringTag) { - return ((StringTag) tag).getValue(); + private static String stringValue(Object tag) { + if (tag instanceof byte[] bytes) { + return Arrays.toString(bytes); + } else if (tag instanceof Byte byteTag) { + return Byte.toString(byteTag); + } else if (tag instanceof Double doubleTag) { + return Double.toString(doubleTag); + } else if (tag instanceof Float floatTag) { + return Float.toString(floatTag); + } else if (tag instanceof int[] intArray) { + return Arrays.toString(intArray); + } else if (tag instanceof Integer integer) { + return Integer.toString(integer); + } else if (tag instanceof long[] longs) { + return Arrays.toString(longs); + } else if (tag instanceof Long longTag) { + return Long.toString(longTag); + } else if (tag instanceof Short shortTag) { + return Short.toString(shortTag); + } else if (tag instanceof String string) { + return string; } else { - return tag.getValue().toString(); + return tag.toString(); } } diff --git a/protocol/src/main/java/org/geysermc/mcprotocollib/protocol/data/game/RegistryEntry.java b/protocol/src/main/java/org/geysermc/mcprotocollib/protocol/data/game/RegistryEntry.java index e5432522..55b3f772 100644 --- a/protocol/src/main/java/org/geysermc/mcprotocollib/protocol/data/game/RegistryEntry.java +++ b/protocol/src/main/java/org/geysermc/mcprotocollib/protocol/data/game/RegistryEntry.java @@ -1,13 +1,13 @@ package org.geysermc.mcprotocollib.protocol.data.game; -import com.github.steveice10.opennbt.tag.builtin.CompoundTag; import lombok.AllArgsConstructor; import lombok.Data; +import org.cloudburstmc.nbt.NbtMap; import org.jetbrains.annotations.Nullable; @Data @AllArgsConstructor public class RegistryEntry { private final String id; - private final @Nullable CompoundTag data; + private final @Nullable NbtMap data; } diff --git a/protocol/src/main/java/org/geysermc/mcprotocollib/protocol/data/game/chat/numbers/StyledFormat.java b/protocol/src/main/java/org/geysermc/mcprotocollib/protocol/data/game/chat/numbers/StyledFormat.java index 1e979748..ad7f9f3f 100644 --- a/protocol/src/main/java/org/geysermc/mcprotocollib/protocol/data/game/chat/numbers/StyledFormat.java +++ b/protocol/src/main/java/org/geysermc/mcprotocollib/protocol/data/game/chat/numbers/StyledFormat.java @@ -1,10 +1,10 @@ package org.geysermc.mcprotocollib.protocol.data.game.chat.numbers; -import com.github.steveice10.opennbt.tag.builtin.CompoundTag; import lombok.AllArgsConstructor; import lombok.Data; import lombok.NonNull; import net.kyori.adventure.text.format.Style; +import org.cloudburstmc.nbt.NbtMap; @Data @AllArgsConstructor @@ -13,5 +13,5 @@ public class StyledFormat implements NumberFormat { /** * Serialized {@link Style} */ - private final @NonNull CompoundTag style; + private final @NonNull NbtMap style; } diff --git a/protocol/src/main/java/org/geysermc/mcprotocollib/protocol/data/game/entity/metadata/MetadataType.java b/protocol/src/main/java/org/geysermc/mcprotocollib/protocol/data/game/entity/metadata/MetadataType.java index f6427638..4b5f789f 100644 --- a/protocol/src/main/java/org/geysermc/mcprotocollib/protocol/data/game/entity/metadata/MetadataType.java +++ b/protocol/src/main/java/org/geysermc/mcprotocollib/protocol/data/game/entity/metadata/MetadataType.java @@ -1,5 +1,6 @@ package org.geysermc.mcprotocollib.protocol.data.game.entity.metadata; +import org.cloudburstmc.nbt.NbtMap; import org.geysermc.mcprotocollib.protocol.codec.MinecraftCodecHelper; import org.geysermc.mcprotocollib.protocol.data.game.entity.metadata.type.BooleanEntityMetadata; import org.geysermc.mcprotocollib.protocol.data.game.entity.metadata.type.ByteEntityMetadata; @@ -10,9 +11,7 @@ import org.geysermc.mcprotocollib.protocol.data.game.entity.metadata.type.Object import org.geysermc.mcprotocollib.protocol.data.game.entity.object.Direction; import org.geysermc.mcprotocollib.protocol.data.game.entity.type.PaintingType; import org.geysermc.mcprotocollib.protocol.data.game.item.ItemStack; -import org.geysermc.mcprotocollib.protocol.data.game.item.component.DataComponentType; import org.geysermc.mcprotocollib.protocol.data.game.level.particle.Particle; -import com.github.steveice10.opennbt.tag.builtin.CompoundTag; import io.netty.buffer.ByteBuf; import lombok.Getter; import net.kyori.adventure.text.Component; @@ -45,7 +44,7 @@ public class MetadataType { public static final MetadataType> OPTIONAL_UUID = new MetadataType<>(optionalReader(MinecraftCodecHelper::readUUID), optionalWriter(MinecraftCodecHelper::writeUUID), ObjectEntityMetadata::new); public static final IntMetadataType BLOCK_STATE = new IntMetadataType(MinecraftCodecHelper::readVarInt, MinecraftCodecHelper::writeVarInt, IntEntityMetadata::new); public static final IntMetadataType OPTIONAL_BLOCK_STATE = new IntMetadataType(MinecraftCodecHelper::readVarInt, MinecraftCodecHelper::writeVarInt, IntEntityMetadata::new); - public static final MetadataType NBT_TAG = new MetadataType<>(MinecraftCodecHelper::readAnyTag, MinecraftCodecHelper::writeAnyTag, ObjectEntityMetadata::new); + public static final MetadataType NBT_TAG = new MetadataType<>(MinecraftCodecHelper::readCompoundTag, MinecraftCodecHelper::writeAnyTag, ObjectEntityMetadata::new); public static final MetadataType PARTICLE = new MetadataType<>(MinecraftCodecHelper::readParticle, MinecraftCodecHelper::writeParticle, ObjectEntityMetadata::new); public static final MetadataType> PARTICLES = new MetadataType<>(listReader(MinecraftCodecHelper::readParticle), listWriter(MinecraftCodecHelper::writeParticle), ObjectEntityMetadata::new); public static final MetadataType VILLAGER_DATA = new MetadataType<>(MinecraftCodecHelper::readVillagerData, MinecraftCodecHelper::writeVillagerData, ObjectEntityMetadata::new); diff --git a/protocol/src/main/java/org/geysermc/mcprotocollib/protocol/data/game/item/component/AdventureModePredicate.java b/protocol/src/main/java/org/geysermc/mcprotocollib/protocol/data/game/item/component/AdventureModePredicate.java index 9cdf5c62..4e14b4b2 100644 --- a/protocol/src/main/java/org/geysermc/mcprotocollib/protocol/data/game/item/component/AdventureModePredicate.java +++ b/protocol/src/main/java/org/geysermc/mcprotocollib/protocol/data/game/item/component/AdventureModePredicate.java @@ -1,8 +1,8 @@ package org.geysermc.mcprotocollib.protocol.data.game.item.component; -import com.github.steveice10.opennbt.tag.builtin.CompoundTag; import lombok.AllArgsConstructor; import lombok.Data; +import org.cloudburstmc.nbt.NbtMap; import org.jetbrains.annotations.Nullable; import java.util.List; @@ -19,7 +19,7 @@ public class AdventureModePredicate { private final @Nullable String location; private final int @Nullable[] holders; private final @Nullable List properties; - private final @Nullable CompoundTag nbt; + private final @Nullable NbtMap nbt; } @Data diff --git a/protocol/src/main/java/org/geysermc/mcprotocollib/protocol/data/game/item/component/BeehiveOccupant.java b/protocol/src/main/java/org/geysermc/mcprotocollib/protocol/data/game/item/component/BeehiveOccupant.java index d8392c7e..f8ffadaa 100644 --- a/protocol/src/main/java/org/geysermc/mcprotocollib/protocol/data/game/item/component/BeehiveOccupant.java +++ b/protocol/src/main/java/org/geysermc/mcprotocollib/protocol/data/game/item/component/BeehiveOccupant.java @@ -1,13 +1,13 @@ package org.geysermc.mcprotocollib.protocol.data.game.item.component; -import com.github.steveice10.opennbt.tag.builtin.CompoundTag; import lombok.AllArgsConstructor; import lombok.Data; +import org.cloudburstmc.nbt.NbtMap; @Data @AllArgsConstructor public class BeehiveOccupant { - private final CompoundTag entityData; + private final NbtMap entityData; private final int ticksInHive; private final int minTicksInHive; } diff --git a/protocol/src/main/java/org/geysermc/mcprotocollib/protocol/data/game/item/component/DataComponentType.java b/protocol/src/main/java/org/geysermc/mcprotocollib/protocol/data/game/item/component/DataComponentType.java index cdd645ba..28d5471f 100644 --- a/protocol/src/main/java/org/geysermc/mcprotocollib/protocol/data/game/item/component/DataComponentType.java +++ b/protocol/src/main/java/org/geysermc/mcprotocollib/protocol/data/game/item/component/DataComponentType.java @@ -1,15 +1,14 @@ package org.geysermc.mcprotocollib.protocol.data.game.item.component; import com.github.steveice10.mc.auth.data.GameProfile; +import org.cloudburstmc.nbt.NbtList; +import org.cloudburstmc.nbt.NbtMap; import org.geysermc.mcprotocollib.protocol.codec.MinecraftCodecHelper; import org.geysermc.mcprotocollib.protocol.data.game.Holder; import org.geysermc.mcprotocollib.protocol.data.game.item.ItemStack; import org.geysermc.mcprotocollib.protocol.data.game.item.component.type.BooleanDataComponent; import org.geysermc.mcprotocollib.protocol.data.game.item.component.type.IntDataComponent; import org.geysermc.mcprotocollib.protocol.data.game.item.component.type.ObjectDataComponent; -import com.github.steveice10.opennbt.tag.builtin.CompoundTag; -import com.github.steveice10.opennbt.tag.builtin.ListTag; -import com.github.steveice10.opennbt.tag.builtin.StringTag; import io.netty.buffer.ByteBuf; import lombok.Getter; import net.kyori.adventure.text.Component; @@ -21,7 +20,7 @@ import java.util.List; public class DataComponentType { private static final List> VALUES = new ArrayList<>(); - public static final DataComponentType CUSTOM_DATA = new DataComponentType<>(ItemCodecHelper::readAnyTag, ItemCodecHelper::writeAnyTag, ObjectDataComponent::new); + public static final DataComponentType CUSTOM_DATA = new DataComponentType<>(ItemCodecHelper::readCompoundTag, ItemCodecHelper::writeAnyTag, ObjectDataComponent::new); public static final IntComponentType MAX_STACK_SIZE = new IntComponentType(ItemCodecHelper::readVarInt, ItemCodecHelper::writeVarInt, IntDataComponent::new); public static final IntComponentType MAX_DAMAGE = new IntComponentType(ItemCodecHelper::readVarInt, ItemCodecHelper::writeVarInt, IntDataComponent::new); public static final IntComponentType DAMAGE = new IntComponentType(ItemCodecHelper::readVarInt, ItemCodecHelper::writeVarInt, IntDataComponent::new); @@ -40,7 +39,7 @@ public class DataComponentType { public static final IntComponentType REPAIR_COST = new IntComponentType(ItemCodecHelper::readVarInt, ItemCodecHelper::writeVarInt, IntDataComponent::new); public static final DataComponentType CREATIVE_SLOT_LOCK = new DataComponentType<>(unitReader(), unitWriter(), ObjectDataComponent::new); public static final BooleanComponentType ENCHANTMENT_GLINT_OVERRIDE = new BooleanComponentType(ByteBuf::readBoolean, ByteBuf::writeBoolean, BooleanDataComponent::new); - public static final DataComponentType INTANGIBLE_PROJECTILE = new DataComponentType<>(ItemCodecHelper::readAnyTag, ItemCodecHelper::writeAnyTag, ObjectDataComponent::new); + public static final DataComponentType INTANGIBLE_PROJECTILE = new DataComponentType<>(ItemCodecHelper::readCompoundTag, ItemCodecHelper::writeAnyTag, ObjectDataComponent::new); public static final DataComponentType FOOD = new DataComponentType<>(ItemCodecHelper::readFoodProperties, ItemCodecHelper::writeFoodProperties, ObjectDataComponent::new); public static final DataComponentType FIRE_RESISTANT = new DataComponentType<>(unitReader(), unitWriter(), ObjectDataComponent::new); public static final DataComponentType TOOL = new DataComponentType<>(ItemCodecHelper::readToolData, ItemCodecHelper::writeToolData, ObjectDataComponent::new); @@ -48,7 +47,7 @@ public class DataComponentType { public static final DataComponentType DYED_COLOR = new DataComponentType<>(ItemCodecHelper::readDyedItemColor, ItemCodecHelper::writeDyedItemColor, ObjectDataComponent::new); public static final IntComponentType MAP_COLOR = new IntComponentType((helper, input) -> input.readInt(), (helper, output, value) -> output.writeInt(value), IntDataComponent::new); public static final IntComponentType MAP_ID = new IntComponentType(ItemCodecHelper::readVarInt, ItemCodecHelper::writeVarInt, IntDataComponent::new); - public static final DataComponentType MAP_DECORATIONS = new DataComponentType<>(ItemCodecHelper::readAnyTag, ItemCodecHelper::writeAnyTag, ObjectDataComponent::new); + public static final DataComponentType MAP_DECORATIONS = new DataComponentType<>(ItemCodecHelper::readCompoundTag, ItemCodecHelper::writeAnyTag, ObjectDataComponent::new); public static final IntComponentType MAP_POST_PROCESSING = new IntComponentType(ItemCodecHelper::readVarInt, ItemCodecHelper::writeVarInt, IntDataComponent::new); public static final DataComponentType> CHARGED_PROJECTILES = new DataComponentType<>(listReader(ItemCodecHelper::readItemStack), listWriter(ItemCodecHelper::writeItemStack), ObjectDataComponent::new); public static final DataComponentType> BUNDLE_CONTENTS = new DataComponentType<>(listReader(ItemCodecHelper::readItemStack), listWriter(ItemCodecHelper::writeItemStack), ObjectDataComponent::new); @@ -57,13 +56,13 @@ public class DataComponentType { public static final DataComponentType WRITABLE_BOOK_CONTENT = new DataComponentType<>(ItemCodecHelper::readWritableBookContent, ItemCodecHelper::writeWritableBookContent, ObjectDataComponent::new); public static final DataComponentType WRITTEN_BOOK_CONTENT = new DataComponentType<>(ItemCodecHelper::readWrittenBookContent, ItemCodecHelper::writeWrittenBookContent, ObjectDataComponent::new); public static final DataComponentType TRIM = new DataComponentType<>(ItemCodecHelper::readArmorTrim, ItemCodecHelper::writeArmorTrim, ObjectDataComponent::new); - public static final DataComponentType DEBUG_STICK_STATE = new DataComponentType<>(ItemCodecHelper::readAnyTag, ItemCodecHelper::writeAnyTag, ObjectDataComponent::new); - public static final DataComponentType ENTITY_DATA = new DataComponentType<>(ItemCodecHelper::readAnyTag, ItemCodecHelper::writeAnyTag, ObjectDataComponent::new); - public static final DataComponentType BUCKET_ENTITY_DATA = new DataComponentType<>(ItemCodecHelper::readAnyTag, ItemCodecHelper::writeAnyTag, ObjectDataComponent::new); - public static final DataComponentType BLOCK_ENTITY_DATA = new DataComponentType<>(ItemCodecHelper::readAnyTag, ItemCodecHelper::writeAnyTag, ObjectDataComponent::new); + public static final DataComponentType DEBUG_STICK_STATE = new DataComponentType<>(ItemCodecHelper::readCompoundTag, ItemCodecHelper::writeAnyTag, ObjectDataComponent::new); + public static final DataComponentType ENTITY_DATA = new DataComponentType<>(ItemCodecHelper::readCompoundTag, ItemCodecHelper::writeAnyTag, ObjectDataComponent::new); + public static final DataComponentType BUCKET_ENTITY_DATA = new DataComponentType<>(ItemCodecHelper::readCompoundTag, ItemCodecHelper::writeAnyTag, ObjectDataComponent::new); + public static final DataComponentType BLOCK_ENTITY_DATA = new DataComponentType<>(ItemCodecHelper::readCompoundTag, ItemCodecHelper::writeAnyTag, ObjectDataComponent::new); public static final DataComponentType> INSTRUMENT = new DataComponentType<>(ItemCodecHelper::readInstrument, ItemCodecHelper::writeInstrument, ObjectDataComponent::new); public static final IntComponentType OMINOUS_BOTTLE_AMPLIFIER = new IntComponentType(ItemCodecHelper::readVarInt, ItemCodecHelper::writeVarInt, IntDataComponent::new); - public static final DataComponentType RECIPES = new DataComponentType<>(ItemCodecHelper::readRecipes, ItemCodecHelper::writeRecipes, ObjectDataComponent::new); + public static final DataComponentType> RECIPES = new DataComponentType<>(ItemCodecHelper::readRecipes, ItemCodecHelper::writeRecipes, ObjectDataComponent::new); public static final DataComponentType LODESTONE_TRACKER = new DataComponentType<>(ItemCodecHelper::readLodestoneTarget, ItemCodecHelper::writeLodestoneTarget, ObjectDataComponent::new); public static final DataComponentType FIREWORK_EXPLOSION = new DataComponentType<>(ItemCodecHelper::readFireworkExplosion, ItemCodecHelper::writeFireworkExplosion, ObjectDataComponent::new); public static final DataComponentType FIREWORKS = new DataComponentType<>(ItemCodecHelper::readFireworks, ItemCodecHelper::writeFireworks, ObjectDataComponent::new); @@ -75,8 +74,8 @@ public class DataComponentType { public static final DataComponentType> CONTAINER = new DataComponentType<>(listReader(ItemCodecHelper::readOptionalItemStack), listWriter(MinecraftCodecHelper::writeOptionalItemStack), ObjectDataComponent::new); public static final DataComponentType BLOCK_STATE = new DataComponentType<>(ItemCodecHelper::readBlockStateProperties, ItemCodecHelper::writeBlockStateProperties, ObjectDataComponent::new); public static final DataComponentType> BEES = new DataComponentType<>(listReader(ItemCodecHelper::readBeehiveOccupant), listWriter(ItemCodecHelper::writeBeehiveOccupant), ObjectDataComponent::new); - public static final DataComponentType LOCK = new DataComponentType<>(ItemCodecHelper::readLock, ItemCodecHelper::writeLock, ObjectDataComponent::new); - public static final DataComponentType CONTAINER_LOOT = new DataComponentType<>(ItemCodecHelper::readAnyTag, ItemCodecHelper::writeAnyTag, ObjectDataComponent::new); + public static final DataComponentType LOCK = new DataComponentType<>(ItemCodecHelper::readLock, ItemCodecHelper::writeLock, ObjectDataComponent::new); + public static final DataComponentType CONTAINER_LOOT = new DataComponentType<>(ItemCodecHelper::readCompoundTag, ItemCodecHelper::writeAnyTag, ObjectDataComponent::new); protected final int id; protected final Reader reader; diff --git a/protocol/src/main/java/org/geysermc/mcprotocollib/protocol/data/game/item/component/ItemCodecHelper.java b/protocol/src/main/java/org/geysermc/mcprotocollib/protocol/data/game/item/component/ItemCodecHelper.java index 2409c8b3..4919bd17 100644 --- a/protocol/src/main/java/org/geysermc/mcprotocollib/protocol/data/game/item/component/ItemCodecHelper.java +++ b/protocol/src/main/java/org/geysermc/mcprotocollib/protocol/data/game/item/component/ItemCodecHelper.java @@ -1,14 +1,14 @@ package org.geysermc.mcprotocollib.protocol.data.game.item.component; import com.github.steveice10.mc.auth.data.GameProfile; +import org.cloudburstmc.nbt.NbtList; +import org.cloudburstmc.nbt.NbtType; import org.geysermc.mcprotocollib.protocol.codec.MinecraftCodecHelper; import org.geysermc.mcprotocollib.protocol.data.game.Holder; import org.geysermc.mcprotocollib.protocol.data.game.entity.attribute.ModifierOperation; import org.geysermc.mcprotocollib.protocol.data.game.level.sound.BuiltinSound; import org.geysermc.mcprotocollib.protocol.data.game.level.sound.CustomSound; import org.geysermc.mcprotocollib.protocol.data.game.level.sound.Sound; -import com.github.steveice10.opennbt.tag.builtin.ListTag; -import com.github.steveice10.opennbt.tag.builtin.StringTag; import io.netty.buffer.ByteBuf; import it.unimi.dsi.fastutil.ints.Int2ObjectMap; import it.unimi.dsi.fastutil.ints.Int2ObjectMaps; @@ -120,7 +120,7 @@ public class ItemCodecHelper extends MinecraftCodecHelper { } } - return new AdventureModePredicate.BlockPredicate(location, holders, propertyMatchers, this.readNullable(buf, this::readAnyTag)); + return new AdventureModePredicate.BlockPredicate(location, holders, propertyMatchers, this.readNullable(buf, this::readCompoundTag)); } public void writeBlockPredicate(ByteBuf buf, AdventureModePredicate.BlockPredicate blockPredicate) { @@ -466,11 +466,11 @@ public class ItemCodecHelper extends MinecraftCodecHelper { }); } - public ListTag readRecipes(ByteBuf buf) { - return this.readAnyTag(buf, ListTag.class); + public NbtList readRecipes(ByteBuf buf) { + return this.readAnyTag(buf, NbtType.LIST); } - public void writeRecipes(ByteBuf buf, ListTag recipes) { + public void writeRecipes(ByteBuf buf, NbtList recipes) { this.writeAnyTag(buf, recipes); } @@ -601,7 +601,7 @@ public class ItemCodecHelper extends MinecraftCodecHelper { } public BeehiveOccupant readBeehiveOccupant(ByteBuf buf) { - return new BeehiveOccupant(this.readAnyTag(buf), this.readVarInt(buf), this.readVarInt(buf)); + return new BeehiveOccupant(this.readCompoundTag(buf), this.readVarInt(buf), this.readVarInt(buf)); } public void writeBeehiveOccupant(ByteBuf buf, BeehiveOccupant occupant) { @@ -610,11 +610,11 @@ public class ItemCodecHelper extends MinecraftCodecHelper { this.writeVarInt(buf, occupant.getMinTicksInHive()); } - public StringTag readLock(ByteBuf buf) { - return this.readAnyTag(buf, StringTag.class); + public String readLock(ByteBuf buf) { + return this.readAnyTag(buf, NbtType.STRING); } - public void writeLock(ByteBuf buf, StringTag key) { + public void writeLock(ByteBuf buf, String key) { this.writeAnyTag(buf, key); } } diff --git a/protocol/src/main/java/org/geysermc/mcprotocollib/protocol/data/game/level/block/BlockEntityInfo.java b/protocol/src/main/java/org/geysermc/mcprotocollib/protocol/data/game/level/block/BlockEntityInfo.java index 048731bc..7b934d07 100644 --- a/protocol/src/main/java/org/geysermc/mcprotocollib/protocol/data/game/level/block/BlockEntityInfo.java +++ b/protocol/src/main/java/org/geysermc/mcprotocollib/protocol/data/game/level/block/BlockEntityInfo.java @@ -1,9 +1,9 @@ package org.geysermc.mcprotocollib.protocol.data.game.level.block; -import com.github.steveice10.opennbt.tag.builtin.CompoundTag; import lombok.AllArgsConstructor; import lombok.Data; import org.checkerframework.checker.nullness.qual.Nullable; +import org.cloudburstmc.nbt.NbtMap; @Data @AllArgsConstructor @@ -12,5 +12,5 @@ public class BlockEntityInfo { private final int y; private final int z; private final BlockEntityType type; - private final @Nullable CompoundTag nbt; + private final @Nullable NbtMap nbt; } diff --git a/protocol/src/main/java/org/geysermc/mcprotocollib/protocol/packet/configuration/clientbound/ClientboundRegistryDataPacket.java b/protocol/src/main/java/org/geysermc/mcprotocollib/protocol/packet/configuration/clientbound/ClientboundRegistryDataPacket.java index 95c4fadc..23683215 100644 --- a/protocol/src/main/java/org/geysermc/mcprotocollib/protocol/packet/configuration/clientbound/ClientboundRegistryDataPacket.java +++ b/protocol/src/main/java/org/geysermc/mcprotocollib/protocol/packet/configuration/clientbound/ClientboundRegistryDataPacket.java @@ -24,7 +24,7 @@ public class ClientboundRegistryDataPacket implements MinecraftPacket { int entryCount = helper.readVarInt(in); for (int i = 0; i < entryCount; i++) { - this.entries.add(new RegistryEntry(helper.readResourceLocation(in), helper.readNullable(in, helper::readAnyTag))); + this.entries.add(new RegistryEntry(helper.readResourceLocation(in), helper.readNullable(in, helper::readCompoundTag))); } } diff --git a/protocol/src/main/java/org/geysermc/mcprotocollib/protocol/packet/ingame/clientbound/entity/ClientboundUpdateMobEffectPacket.java b/protocol/src/main/java/org/geysermc/mcprotocollib/protocol/packet/ingame/clientbound/entity/ClientboundUpdateMobEffectPacket.java index 3209bb7c..7118fbc2 100644 --- a/protocol/src/main/java/org/geysermc/mcprotocollib/protocol/packet/ingame/clientbound/entity/ClientboundUpdateMobEffectPacket.java +++ b/protocol/src/main/java/org/geysermc/mcprotocollib/protocol/packet/ingame/clientbound/entity/ClientboundUpdateMobEffectPacket.java @@ -3,13 +3,11 @@ package org.geysermc.mcprotocollib.protocol.packet.ingame.clientbound.entity; import org.geysermc.mcprotocollib.protocol.codec.MinecraftCodecHelper; import org.geysermc.mcprotocollib.protocol.codec.MinecraftPacket; import org.geysermc.mcprotocollib.protocol.data.game.entity.Effect; -import com.github.steveice10.opennbt.tag.builtin.CompoundTag; import io.netty.buffer.ByteBuf; import lombok.AllArgsConstructor; import lombok.Data; import lombok.NonNull; import lombok.With; -import org.checkerframework.checker.nullness.qual.Nullable; @Data @With diff --git a/protocol/src/main/java/org/geysermc/mcprotocollib/protocol/packet/ingame/clientbound/level/ClientboundBlockEntityDataPacket.java b/protocol/src/main/java/org/geysermc/mcprotocollib/protocol/packet/ingame/clientbound/level/ClientboundBlockEntityDataPacket.java index 2f688b6e..2249a692 100644 --- a/protocol/src/main/java/org/geysermc/mcprotocollib/protocol/packet/ingame/clientbound/level/ClientboundBlockEntityDataPacket.java +++ b/protocol/src/main/java/org/geysermc/mcprotocollib/protocol/packet/ingame/clientbound/level/ClientboundBlockEntityDataPacket.java @@ -1,9 +1,9 @@ package org.geysermc.mcprotocollib.protocol.packet.ingame.clientbound.level; +import org.cloudburstmc.nbt.NbtMap; import org.geysermc.mcprotocollib.protocol.codec.MinecraftCodecHelper; import org.geysermc.mcprotocollib.protocol.codec.MinecraftPacket; import org.geysermc.mcprotocollib.protocol.data.game.level.block.BlockEntityType; -import com.github.steveice10.opennbt.tag.builtin.CompoundTag; import io.netty.buffer.ByteBuf; import lombok.AllArgsConstructor; import lombok.Data; @@ -18,12 +18,12 @@ import org.checkerframework.checker.nullness.qual.Nullable; public class ClientboundBlockEntityDataPacket implements MinecraftPacket { private final @NonNull Vector3i position; private final @NonNull BlockEntityType type; - private final @Nullable CompoundTag nbt; + private final @Nullable NbtMap nbt; public ClientboundBlockEntityDataPacket(ByteBuf in, MinecraftCodecHelper helper) { this.position = helper.readPosition(in); this.type = helper.readBlockEntityType(in); - this.nbt = helper.readAnyTag(in); + this.nbt = helper.readCompoundTag(in); } @Override diff --git a/protocol/src/main/java/org/geysermc/mcprotocollib/protocol/packet/ingame/clientbound/level/ClientboundLevelChunkWithLightPacket.java b/protocol/src/main/java/org/geysermc/mcprotocollib/protocol/packet/ingame/clientbound/level/ClientboundLevelChunkWithLightPacket.java index a3e4565f..54a39771 100644 --- a/protocol/src/main/java/org/geysermc/mcprotocollib/protocol/packet/ingame/clientbound/level/ClientboundLevelChunkWithLightPacket.java +++ b/protocol/src/main/java/org/geysermc/mcprotocollib/protocol/packet/ingame/clientbound/level/ClientboundLevelChunkWithLightPacket.java @@ -1,11 +1,11 @@ package org.geysermc.mcprotocollib.protocol.packet.ingame.clientbound.level; +import org.cloudburstmc.nbt.NbtMap; import org.geysermc.mcprotocollib.protocol.codec.MinecraftCodecHelper; import org.geysermc.mcprotocollib.protocol.codec.MinecraftPacket; import org.geysermc.mcprotocollib.protocol.data.game.level.LightUpdateData; import org.geysermc.mcprotocollib.protocol.data.game.level.block.BlockEntityInfo; import org.geysermc.mcprotocollib.protocol.data.game.level.block.BlockEntityType; -import com.github.steveice10.opennbt.tag.builtin.CompoundTag; import io.netty.buffer.ByteBuf; import lombok.AllArgsConstructor; import lombok.Data; @@ -19,14 +19,14 @@ public class ClientboundLevelChunkWithLightPacket implements MinecraftPacket { private final int x; private final int z; private final byte @NonNull [] chunkData; - private final @NonNull CompoundTag heightMaps; + private final @NonNull NbtMap heightMaps; private final @NonNull BlockEntityInfo @NonNull [] blockEntities; private final @NonNull LightUpdateData lightData; public ClientboundLevelChunkWithLightPacket(ByteBuf in, MinecraftCodecHelper helper) { this.x = in.readInt(); this.z = in.readInt(); - this.heightMaps = helper.readAnyTagOrThrow(in); + this.heightMaps = helper.readCompoundTagOrThrow(in); this.chunkData = helper.readByteArray(in); this.blockEntities = new BlockEntityInfo[helper.readVarInt(in)]; @@ -36,7 +36,7 @@ public class ClientboundLevelChunkWithLightPacket implements MinecraftPacket { int blockEntityZ = xz & 15; int blockEntityY = in.readShort(); BlockEntityType type = helper.readBlockEntityType(in); - CompoundTag tag = helper.readAnyTag(in); + NbtMap tag = helper.readCompoundTag(in); this.blockEntities[i] = new BlockEntityInfo(blockEntityX, blockEntityY, blockEntityZ, type, tag); } diff --git a/protocol/src/main/java/org/geysermc/mcprotocollib/protocol/packet/ingame/clientbound/level/ClientboundTagQueryPacket.java b/protocol/src/main/java/org/geysermc/mcprotocollib/protocol/packet/ingame/clientbound/level/ClientboundTagQueryPacket.java index 556fd565..77e29217 100644 --- a/protocol/src/main/java/org/geysermc/mcprotocollib/protocol/packet/ingame/clientbound/level/ClientboundTagQueryPacket.java +++ b/protocol/src/main/java/org/geysermc/mcprotocollib/protocol/packet/ingame/clientbound/level/ClientboundTagQueryPacket.java @@ -1,8 +1,8 @@ package org.geysermc.mcprotocollib.protocol.packet.ingame.clientbound.level; +import org.cloudburstmc.nbt.NbtMap; import org.geysermc.mcprotocollib.protocol.codec.MinecraftCodecHelper; import org.geysermc.mcprotocollib.protocol.codec.MinecraftPacket; -import com.github.steveice10.opennbt.tag.builtin.CompoundTag; import io.netty.buffer.ByteBuf; import lombok.AllArgsConstructor; import lombok.Data; @@ -14,11 +14,11 @@ import org.checkerframework.checker.nullness.qual.Nullable; @AllArgsConstructor public class ClientboundTagQueryPacket implements MinecraftPacket { private final int transactionId; - private final @Nullable CompoundTag nbt; + private final @Nullable NbtMap nbt; public ClientboundTagQueryPacket(ByteBuf in, MinecraftCodecHelper helper) { this.transactionId = helper.readVarInt(in); - this.nbt = helper.readAnyTag(in); + this.nbt = helper.readCompoundTag(in); } @Override diff --git a/protocol/src/test/java/org/geysermc/mcprotocollib/protocol/packet/ingame/clientbound/level/ClientboundLevelChunkWithLightPacketTest.java b/protocol/src/test/java/org/geysermc/mcprotocollib/protocol/packet/ingame/clientbound/level/ClientboundLevelChunkWithLightPacketTest.java index 7ba596ad..104e12b9 100644 --- a/protocol/src/test/java/org/geysermc/mcprotocollib/protocol/packet/ingame/clientbound/level/ClientboundLevelChunkWithLightPacketTest.java +++ b/protocol/src/test/java/org/geysermc/mcprotocollib/protocol/packet/ingame/clientbound/level/ClientboundLevelChunkWithLightPacketTest.java @@ -1,10 +1,10 @@ package org.geysermc.mcprotocollib.protocol.packet.ingame.clientbound.level; +import org.cloudburstmc.nbt.NbtMap; import org.geysermc.mcprotocollib.protocol.data.game.level.LightUpdateData; import org.geysermc.mcprotocollib.protocol.data.game.level.block.BlockEntityInfo; import org.geysermc.mcprotocollib.protocol.data.game.level.block.BlockEntityType; import org.geysermc.mcprotocollib.protocol.packet.PacketTest; -import com.github.steveice10.opennbt.tag.builtin.CompoundTag; import org.junit.jupiter.api.BeforeEach; import java.util.BitSet; @@ -15,11 +15,11 @@ public class ClientboundLevelChunkWithLightPacketTest extends PacketTest { public void setup() { this.setPackets( new ClientboundLevelChunkWithLightPacket(0, 0, - new byte[0], new CompoundTag(""), new BlockEntityInfo[0], + new byte[0], NbtMap.EMPTY, new BlockEntityInfo[0], new LightUpdateData(new BitSet(), new BitSet(), new BitSet(), new BitSet(), Collections.emptyList(), Collections.emptyList()) ), new ClientboundLevelChunkWithLightPacket(1, 1, - new byte[256], new CompoundTag(""), new BlockEntityInfo[] { + new byte[256], NbtMap.EMPTY, new BlockEntityInfo[] { new BlockEntityInfo(1, 0, 1, BlockEntityType.CHEST, null) }, new LightUpdateData(new BitSet(), new BitSet(), new BitSet(), new BitSet(), Collections.emptyList(), Collections.emptyList()) )