Collect all nbt tag from item

Discussion in 'Spigot Plugin Development' started by 6ap6oc, Sep 11, 2019.

  1. I seem to reinvent the wheel -_-
    The next task is to save the complete data about all things in the player’s inventory in the json line (which will be later saved to the database, and if necessary, the player’s inventory will be restored using this information). If there are no problems with the inventory itself (you can simply skip it through the loop and get all the items), then what about the items? an ordinary ItemStack does not give a complete picture of an item, because an item can store attributes in itself, or be a shalker box, which will also store other items, or even have non-standard tags.
    At the same time, NBTTagCompound does not have a method like getTags (), which can return a collection of all available tags. With it, you can get the tag only if you have the name of the tag.
    Alternatively, knowing the structure of the subject (for example, take information from a wiki), manually get all the tags:

    Code (Java):
    net.minecraft.server.v1_12_R1.ItemStack nmsStack = CraftItemStack.asNMSCopy(item);
    NBTTagCompound nmsCompound = (nmsStack.hasTag()) ? nmsStack.getTag() : new NBTTagCompound();  
    nmsCompound.getString("some tag");
    and then also put it in a string after processing. This method will turn out to be very cumbersome, but working (and in which case this json string can be turned back into an item for the player).
    But how much will it be right?
    Most likely this has already been done before me, just tell me.
    Thanks in advance.
  2. I would use the NBTItemApi dependency created by tr7zw, you can compile it inside your plugin or have it as seperate if you want an external dependency, but i usually prefer the former,
    I actually answered this question pretty in depth on how to but the poster refused to use a library, not very sure why, there are reasons but this is the best option you have (to my knowledge) without a bunch of work that could be messy and hard to maintain.
    Heres a link to the thread:
    • Agree Agree x 2
  3. NBTTagCompound.getKeys()
  4. Are you kidding me?


    If you do not know, do not write, otherwise your post looks nonsense.
    • Optimistic Optimistic x 1
  5. The method in question should be called c() in 1.12, if I'm not mistaken.
  6. [​IMG]

    If you do not know, do not write, otherwise your post looks like nonsense.

    (y) Easily found by just reading all the methods that NBTTagCompound has.
    • Funny Funny x 1
  7. plz, read the first post on this topic