Util NBT - Util - Save data on Itemstacks

Discussion in 'Resources' started by I Al Istannen, Aug 24, 2016.

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

    I Al Istannen


    I have seen a few questions about how to save data invisible on items. While there are a few creative ways, the on I would prefer is NBT - data.

    Problem is, it is in the NMS package. This means you will need to use reflection to make it version compatible, which inflates the code by a lot and makes it hard to read.

    So I decided to write some utility classes to solve that. They are named exactly like their NMS counterpart and their function was as closely modelled as I could (without any feedback :p)).
    You can use them just like you would use any of the NBTTagXXX classes, and can move it over to the NMS classes by using INBTBase#toNBT(). To create one from NBT use INBTBase#fromNBT(Object).

    I have made a small class which allows the saving of data on an itemstack, as well as getting it back. Which was mentioned in the title ;)

    Paging @Lordloss because you used a different system :p
    And @Benjamint22 as it hopefully answers your thread about how to save data.


    Since they were to big to fit in this post, I created a small gist. Probably not the best idea, but let's stay optimistic. Hope you can somehow make changes to it, never really used it before :p
    EDIT: I added that to the BukkitUtil, implemented equals and hashcode for the Wrappers, added support for modifying Entities and added a Wrapper for the MojangsenParser to convert a String to NBT. Here.


    Using it is quite simple. You can instantiate any of these classes as you wish.

    Here is a simple demonstration. Just copy this code in an Event listener. It saves the player who first left/right clicked with the item and outputs it after that. Quite simple, isn't it?
    1. @EventHandler
    2. public void onInteractEvent(PlayerInteractEvent event) {
    3. if(!event.hasItem()) {
    4. return;
    5. }
    6. ItemStack item = event.getItem();
    7. NBTWrappers.NBTTagCompound compound = ItemNBTUtil.getTag(item);
    9. if(compound.hasKey("ownerName")) {
    10. event.getPlayer().sendMessage("The owner of this item is: " + compound.getString("ownerName"));
    11. }
    12. else {
    13. compound.setString("ownerName", event.getPlayer().getName().toString());
    14. item = ItemNBTUtil.setNBTTag(compound, item);
    15. event.getPlayer().setItemInHand(item);
    16. event.getPlayer().sendMessage("Made you the owner of the item.");
    17. }
    18. }

    Now you should be able to make your own storage with ease (or modify NBT tags, as needed for custom heads for example). I think it looks much nicer and is much easier to do than just using reflection.

    Important Notes

    It should work on 1.8.8+, maybe even lower. Before that all custom NBT data was cleared from any item, god only knows why. Maybe there are some quirks with other versions I am not aware of, so feel free to report them.

    Though I am away for 10 days straight (vacations :p) and have to wake up again in 7 hours. So I will only be able to fix things then. I apologize for that, but I wanted to get this out to you, to hopefully help you and to find bugs and suggestions. Hope you understand that :)

    Hope I didn't miss anything... I am notorious for that :p

    • 24.8.16
      • Init
    • 11.09.16
      • Added a Wrapper for the MojangsonParser (String to NBT)
      • Added an EntityNbtUtil to modify Entity NBT
      • Added equals and hashcode to the NbtWrappers
    Last edited: Sep 11, 2016
  2. Offline

    I Al Istannen

    Bump. Any suggestions? If not, I will let it get buried in the "Resources" graveyard :p
  3. @I Al Istannen

    Could you make a method that converts a string into NBT and backwards?

    I would suggest you replace the (ALT + 2 + 1) with '&' as I get some errors every now and then with essentials. Also do the ChatColor.translateAlternateColorCodes() method for that, so I could write it into a string and save it into a config and the other way round.

    (If you did it then sorry Im on a phone)
    Last edited: Aug 30, 2016
  4. Offline

    I Al Istannen

    Thanks for the suggestions :)

    I haven't used it anywhere, as far as I know.
    What do you do to get the essentials error?

    NBTWrappers.NBTTagString nbtTagString = new NBTWrappers.NBTTagString("test");
    Object stringNBT = nbtTagString.toNBT();
    To make that to a String, you would need to convert the bytes to base64, as it is a (Notch) Binary-Tag format. It is not made for being saved as a String, but as binary instead.

    What is the usecase? If you save it to a file yourself (so no data to an ItemStack or Entity), JSON or YAML suit you probably better.
  5. Essentialy something changes the § to an A with a line other it, which screws up all the plugin work (checking for items and stuff)
    I'll look into the conversion btw

    Attached Files:

  6. @TheEnderCrafter9
    That has to do with file encodings, since § is not part of standard ascii and will mess up with improper file encodings. However, if you don't hard code the '§' symbol, and instead use ChatColor.BLUE (or another colour, of course), that will never display as "Å"
    MCMastery and I Al Istannen like this.
  7. Offline

    I Al Istannen

    May I see your code for setting this name and modifying the itemstack?
  8. :D Im currently in the proccess, will update once done. But first I have to right a kit wrapper and a kit config wrapper
    Last edited: Aug 31, 2016
    I Al Istannen likes this.
  9. Offline

    I Al Istannen

    I just added support for modifying Entity NBT as well as a Wrapper for the MojangsonParser to convert a String to NBT (and equals and hashcode for the wrappers *whistles*).

    Does anybody have time to test that (they changed the save method return type or a suggestion for it? Else I think this is probably finished and hopefully working. Or is there anything else that can save NBT?
  10. Offline

    I Al Istannen

    Good point :p I will have a look. Thanks!

    EDIT: Done. Anything else?
    Last edited: Sep 11, 2016
Thread Status:
Not open for further replies.

Share This Page