Getting ItemStack/LivingEntity NBT

Discussion in 'Plugin Development' started by Orcane, Feb 11, 2017.

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



    I've being struggling with trying to find out how to get an item's NBT tag for a while now, I'm pretty terrible at Java at the moment. So if anyone could write out a the code to say, get the NBT tag of the item in your hand with an explanation of what is happening, it would be very greatly appreciated.

    ~ Orcane
  2. Code:
    CraftItemStack is essentially a wrapper or utility class that brings us away from the safe-space that is version independent(ish) coding with the Bukkit API. Using it requires that you update your imports (as the package name changes each update of the .jar file) every time a new version of the API is released and you want to use it on your server. The above method is a utility method that they do all the hard work for you in, and simply return you the NMS (net.minecraft.server) ItemStack, which has the data that you're requesting (net.minecraft.server.ItemStack#getTag() returns NBTTagCompound)
    Orcane likes this.
  3. Offline


    Wow, It's that simple. You have no idea how long I've been trying to figure this out. Thank you very much, I appreciate it! ^-^

    It appears getting LivingEntity NBTs is not done the same way, so how would you go about getting them?
    Last edited: Feb 11, 2017
  4. @Orcane
    What NBT tag do you need? For which entity?
  5. Offline


    The NBT tag for any org.bukkit.LivingEntity
  6. Offline


    I have a pretty good idea of what NBTs are, what what I want to edit. I just don't know how to do it with Java.
  7. @Orcane you mean the Bukkit API, but alright.
    net.minecraft.server.v1_11_R1.ItemStack a = CraftItemStack.asNMSCopy(itemHere);
    NBTTagCompound tag = a.getTag();
    tag.set("ench", new NBTTagList());
    a.setTag(tag); //Update the NBT Tag.
    itemHere = CraftItemStack.asBukkitCopy(a); //OR set the item in the inventory manually, depends on what you want. You could also return itemHere and that'd be the modified stack.
    I believe this code would erase all enchants on the item, and perhaps make it just the glow (the NBT system has become smarter with Minecraft nowadays, though. NBTTagCompound and NBTTagList both extend NBTBase. You should just try getting the variable and typing a period after it and seeing what comes up. Once you get an error is when you should be asking for a bit more help.
Thread Status:
Not open for further replies.

Share This Page