ProtocolLib icon indicating copy to clipboard operation
ProtocolLib copied to clipboard

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

Open Anarchick opened this issue 1 year ago • 0 comments

Make sure you're doing the following

  • [x] You're using the latest build for your server version
  • [x] This isn't an issue caused by another plugin
  • [x] You've checked for duplicate issues
  • [x] You didn't use /reload

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.

Anarchick avatar Oct 12 '24 15:10 Anarchick