Util Slick way of setting entity NBTs

Discussion in 'Resources' started by Orcane, Jul 30, 2018.

Thread Status:
Not open for further replies.
  1. Offline

    Orcane

    Hi there,

    I was working on a project for a client where I found myself needing to save and load entities, I already wrote a serialization library for other projects, so was going to use that, but thought it would be nice to load entities using that JSON NBT tag you would use in vanilla commands such as /entitydata or /summon. I've done a lot of research about entity and item NBTs, and everyone seems to always set up NBTTagCompounds manually, which is a lot of work! Especially if you want to extract one, change it, and add it back to an item or entity. So I started snooping around in the NMS source for a better solution, and found the class that Minecraft itself actually uses for parsing the JSON for NBTs. It makes NBT managing so much easier as you can easily convert a JSON string into a NBTTagCompound without any hassle. Sadly I was developing for 1.8, so I'm not sure how things have changed in the newer versions.

    The class is called MojangsonParser, it has a static method in it called parse which takes in a string, and returns a NBTTagCompound.

    This example is for making an item Unbreakable.
    Code:
    NBTTagCompound tag = nmsItem.a("key", true);
    tag.a(MojangsonParser.parse("{Unbreakable:1}"));
    nmsItem.setTag(tag);
    I was pretty happy with this discovery, and didn't see it anywhere else on Bukkit, so I thought I would share.
     
    Chr0mosom3 and Zombie_Striker like this.
Thread Status:
Not open for further replies.

Share This Page