diff --git a/pom.xml b/pom.xml index f67d8e9..8eac94b 100644 --- a/pom.xml +++ b/pom.xml @@ -21,7 +21,7 @@ unknown 1.20-R0.2-SNAPSHOT - 3.2.0-SNAPSHOT + 3.3.0-SNAPSHOT 1.11.13 2.3.3 diff --git a/protocolize-velocity/pom.xml b/protocolize-velocity/pom.xml index e2dd459..81ad1f6 100644 --- a/protocolize-velocity/pom.xml +++ b/protocolize-velocity/pom.xml @@ -58,6 +58,11 @@ ${bytebuddy.version} compile + + net.kyori + adventure-nbt + 4.16.0 + diff --git a/protocolize-velocity/src/main/java/dev/simplix/protocolize/velocity/providers/VelocityComponentConverterProvider.java b/protocolize-velocity/src/main/java/dev/simplix/protocolize/velocity/providers/VelocityComponentConverterProvider.java index dd45504..6e8cf80 100644 --- a/protocolize-velocity/src/main/java/dev/simplix/protocolize/velocity/providers/VelocityComponentConverterProvider.java +++ b/protocolize-velocity/src/main/java/dev/simplix/protocolize/velocity/providers/VelocityComponentConverterProvider.java @@ -1,7 +1,11 @@ package dev.simplix.protocolize.velocity.providers; +import com.velocitypowered.api.network.ProtocolVersion; +import com.velocitypowered.proxy.protocol.packet.chat.ComponentHolder; import dev.simplix.protocolize.api.ComponentConverter; import dev.simplix.protocolize.api.providers.ComponentConverterProvider; +import dev.simplix.protocolize.velocity.util.AdventureNbtToQuerzNbtMapper; +import net.kyori.adventure.nbt.BinaryTag; import net.kyori.adventure.text.Component; import net.kyori.adventure.text.format.TextDecoration; import net.kyori.adventure.text.serializer.gson.GsonComponentSerializer; @@ -44,7 +48,8 @@ public String toJson(Component component) { @Override public Tag toNbt(Component component) { - throw new UnsupportedOperationException("Adventure does not support nbt serialization yet"); + BinaryTag adventureNbtTag = new ComponentHolder(ProtocolVersion.MINECRAFT_1_20_3, component).getBinaryTag(); + return AdventureNbtToQuerzNbtMapper.adventureToQuerz(adventureNbtTag); } @Override @@ -59,7 +64,8 @@ public Component fromJson(String json) { @Override public Component fromNbt(Tag tag) { - throw new UnsupportedOperationException("Adventure does not support nbt deserialization yet"); + BinaryTag adventureNbtTag = AdventureNbtToQuerzNbtMapper.querzToAdventure(tag); + return new ComponentHolder(ProtocolVersion.MINECRAFT_1_20_3, adventureNbtTag).getComponent(); } @Override diff --git a/protocolize-velocity/src/main/java/dev/simplix/protocolize/velocity/util/AdventureNbtToQuerzNbtMapper.java b/protocolize-velocity/src/main/java/dev/simplix/protocolize/velocity/util/AdventureNbtToQuerzNbtMapper.java new file mode 100644 index 0000000..63ccc2c --- /dev/null +++ b/protocolize-velocity/src/main/java/dev/simplix/protocolize/velocity/util/AdventureNbtToQuerzNbtMapper.java @@ -0,0 +1,180 @@ +package dev.simplix.protocolize.velocity.util; + +import java.util.ArrayList; +import java.util.List; +import java.util.Map; + +import net.kyori.adventure.nbt.ArrayBinaryTag; +import net.kyori.adventure.nbt.BinaryTag; +import net.kyori.adventure.nbt.BinaryTagType; +import net.kyori.adventure.nbt.BinaryTagTypes; +import net.kyori.adventure.nbt.ByteArrayBinaryTag; +import net.kyori.adventure.nbt.ByteBinaryTag; +import net.kyori.adventure.nbt.CompoundBinaryTag; +import net.kyori.adventure.nbt.DoubleBinaryTag; +import net.kyori.adventure.nbt.FloatBinaryTag; +import net.kyori.adventure.nbt.IntArrayBinaryTag; +import net.kyori.adventure.nbt.IntBinaryTag; +import net.kyori.adventure.nbt.ListBinaryTag; +import net.kyori.adventure.nbt.LongArrayBinaryTag; +import net.kyori.adventure.nbt.LongBinaryTag; +import net.kyori.adventure.nbt.ShortBinaryTag; +import net.kyori.adventure.nbt.StringBinaryTag; +import net.querz.nbt.tag.ByteArrayTag; +import net.querz.nbt.tag.ByteTag; +import net.querz.nbt.tag.CompoundTag; +import net.querz.nbt.tag.DoubleTag; +import net.querz.nbt.tag.FloatTag; +import net.querz.nbt.tag.IntArrayTag; +import net.querz.nbt.tag.IntTag; +import net.querz.nbt.tag.ListTag; +import net.querz.nbt.tag.LongArrayTag; +import net.querz.nbt.tag.LongTag; +import net.querz.nbt.tag.ShortTag; +import net.querz.nbt.tag.StringTag; +import net.querz.nbt.tag.Tag; + +public class AdventureNbtToQuerzNbtMapper { + + private static BinaryTagType querzTagTypeToAdventure(Class type) { + if (type == ByteArrayTag.class) + return BinaryTagTypes.BYTE_ARRAY; + else if (type == ByteTag.class) + return BinaryTagTypes.BYTE; + else if (type == CompoundTag.class) + return BinaryTagTypes.COMPOUND; + else if (type == DoubleTag.class) + return BinaryTagTypes.DOUBLE; + else if (type == FloatTag.class) + return BinaryTagTypes.FLOAT; + else if (type == IntArrayTag.class) + return BinaryTagTypes.INT_ARRAY; + else if (type == IntTag.class) + return BinaryTagTypes.INT; + else if (type == ListTag.class) + return BinaryTagTypes.LIST; + else if (type == LongArrayTag.class) + return BinaryTagTypes.LONG_ARRAY; + else if (type == LongTag.class) + return BinaryTagTypes.LONG; + else if (type == ShortTag.class) + return BinaryTagTypes.SHORT; + else if (type == StringTag.class) + return BinaryTagTypes.STRING; + else + return null; + } + + private static Class adventureTagTypeToQuerz(BinaryTagType type) { + if (type == BinaryTagTypes.BYTE_ARRAY) { + return ByteArrayTag.class; + } else if (type == BinaryTagTypes.BYTE) { + return ByteTag.class; + } else if (type == BinaryTagTypes.COMPOUND) { + return CompoundTag.class; + } else if (type == BinaryTagTypes.DOUBLE) { + return DoubleTag.class; + } else if (type == BinaryTagTypes.FLOAT) { + return FloatTag.class; + } else if (type == BinaryTagTypes.INT_ARRAY) { + return IntArrayTag.class; + } else if (type == BinaryTagTypes.INT) { + return IntTag.class; + } else if (type == BinaryTagTypes.LIST) { + return ListTag.class; + } else if (type == BinaryTagTypes.LONG_ARRAY) { + return LongArrayTag.class; + } else if (type == BinaryTagTypes.LONG) { + return LongTag.class; + } else if (type == BinaryTagTypes.SHORT) { + return ShortTag.class; + } else if (type == BinaryTagTypes.STRING) { + return StringTag.class; + } + + return null; + } + + public static BinaryTag querzToAdventure(Tag tag) { + if (tag instanceof CompoundTag) { + CompoundTag compoundTag = (CompoundTag) tag; + Map entries = Map.of(); + for (Map.Entry> entry : compoundTag.entrySet()) { + entries.put(entry.getKey(), querzToAdventure(entry.getValue())); + } + return CompoundBinaryTag.from(entries); + } else if (tag instanceof ListTag) { + ListTag> list = (ListTag>) tag; + List items = new ArrayList<>(list.size()); + list.iterator().forEachRemaining((item) -> items.add(querzToAdventure(item))); + return ListBinaryTag.listBinaryTag(querzTagTypeToAdventure(list.getTypeClass()), items); + } else if (tag instanceof ArrayBinaryTag) { + if (tag instanceof ByteArrayTag) { + return ByteArrayBinaryTag.byteArrayBinaryTag(((ByteArrayTag) tag).getValue()); + } else if (tag instanceof IntArrayTag) { + return IntArrayBinaryTag.intArrayBinaryTag(((IntArrayTag) tag).getValue()); + } else if (tag instanceof LongArrayTag) { + return LongArrayBinaryTag.longArrayBinaryTag(((LongArrayTag) tag).getValue()); + } + } else if (tag instanceof ByteTag) { + return ByteBinaryTag.byteBinaryTag(((ByteTag) tag).asByte()); + } else if (tag instanceof DoubleTag) { + return DoubleBinaryTag.doubleBinaryTag(((DoubleTag) tag).asDouble()); + } else if (tag instanceof FloatTag) { + return FloatBinaryTag.floatBinaryTag(((FloatTag) tag).asFloat()); + } else if (tag instanceof IntTag) { + return IntBinaryTag.intBinaryTag(((IntTag) tag).asInt()); + } else if (tag instanceof LongTag) { + return LongBinaryTag.longBinaryTag(((LongTag) tag).asLong()); + } else if (tag instanceof ShortTag) { + return ShortBinaryTag.shortBinaryTag(((ShortTag) tag).asShort()); + } else if (tag instanceof StringTag) { + return StringBinaryTag.stringBinaryTag(((StringTag) tag).getValue()); + } + + throw new RuntimeException("Unexpected NBT tag type while converting: " + tag.getClass().getName()); + } + + public static Tag adventureToQuerz(BinaryTag tag) { + if (tag instanceof CompoundBinaryTag) { + CompoundTag compound = new CompoundTag(); + ((CompoundBinaryTag) tag).iterator().forEachRemaining((entry) -> { + compound.put(entry.getKey(), adventureToQuerz(entry.getValue())); + }); + return compound; + } else if (tag instanceof ListBinaryTag) { + ListBinaryTag list = (ListBinaryTag) tag; + ListTag listTag = new ListTag(adventureTagTypeToQuerz(list.elementType())); + + List> items = new ArrayList<>(list.size()); + list.iterator().forEachRemaining((item) -> items.add(adventureToQuerz(item))); + + listTag.addAll(items); + return listTag; + } else if (tag instanceof ArrayBinaryTag) { + if (tag instanceof ByteArrayBinaryTag) { + return new ByteArrayTag(((ByteArrayBinaryTag) tag).value()); + } else if (tag instanceof IntArrayBinaryTag) { + return new IntArrayTag(((IntArrayBinaryTag) tag).value()); + } else if (tag instanceof LongArrayBinaryTag) { + return new LongArrayTag(((LongArrayBinaryTag) tag).value()); + } + } else if (tag instanceof ByteBinaryTag) { + return new ByteTag(((ByteBinaryTag) tag).value()); + } else if (tag instanceof DoubleBinaryTag) { + return new DoubleTag(((DoubleBinaryTag) tag).value()); + } else if (tag instanceof FloatBinaryTag) { + return new FloatTag(((FloatBinaryTag) tag).value()); + } else if (tag instanceof IntBinaryTag) { + return new IntTag(((IntBinaryTag) tag).value()); + } else if (tag instanceof LongBinaryTag) { + return new LongTag(((LongBinaryTag) tag).value()); + } else if (tag instanceof ShortBinaryTag) { + return new ShortTag(((ShortBinaryTag) tag).value()); + } else if (tag instanceof StringBinaryTag) { + return new StringTag(((StringBinaryTag) tag).value()); + } + + throw new RuntimeException("Unexpected NBT tag type while converting: " + tag.getClass().getName()); + } +}