dmulloy2 / ProtocolLib

Provides read and write access to the Minecraft protocol with Bukkit.
GNU General Public License v2.0
1.04k stars 257 forks source link

How to convert Json Text to nms.nbt.CompoundTag ? #3273

Open Anarchick opened 1 month ago

Anarchick commented 1 month ago

Make sure you're doing the following

Describe the question I'm trying to convert a json string into NBT, may be used in PLAY_SERVER_TILE_ENTITY_DATA. The input json is given by the player so I need a deserializer.

API method(s) used I had try NbtTextSerializer.DEFAULT#deserialize and NbtFactory#ofCompound

Expected behavior Have a method that deserialize a complex json string into nms.nbt.CompoundTag

Code I tried something like this :

if (text == null || text.isEmpty()) {
                return NbtFactory.ofCompound("").getHandle();
            }

            byte[] bytes = text.getBytes(StandardCharsets.UTF_8);
            ByteArrayInputStream byteArrayInputStream = new ByteArrayInputStream(bytes);
            DataInputStream dataInput = new DataInputStream(byteArrayInputStream);
            NbtWrapper<Object> wrapper = NbtTextSerializer.DEFAULT.getBinarySerializer().deserialize(dataInput); // Issue here
            return wrapper.getHandle();

Additional context This is for Skript-Packet.