1.14.4 Working with NBT Tags

Discussion in 'Spigot Plugin Development' started by FraterClub, Jan 21, 2020.

  1. I'm looking to add a NBT Tag to a item, but I can't find any mehtod in JavaDocs.
    In google the most that I found is this code:
    Code (Java):
            // This is the Bukkit ItemStack (org.bukkit.inventory.ItemStack)
            ItemStack item = new ItemStack(Material.DIAMOND); // Just an example item
            // This is the NMS ItemStack
            net.minecraft.server.v1_14_R1.ItemStack nmsItem = CraftItemStack.asNMSCopy(item);
            // Get the NBT tag
            NBTTagCompound tag = nmsItem.hasTag() ? nmsItem.getTag() : new NBTTagCompound();
            // I don't remember this method, just look through the methods and see which one is kinda like this
            tag.setString("key", "value");
            // Set the NBT tag to the modified version
            nmsItem.setTag(tag);
            // Get the Bukkit ItemStack after modification
            item = CraftItemStack.asBukkitCopy(nmsItem);
     
  2. Yes.
     
    • Useful Useful x 1
  3. Yes it changes it and items with different containers won't stack.
     
    • Like Like x 1
  4. But is being added in "BukkitPublicValues", but I want to store it in "NBT". How can I do it?
     
  5. What is wrong with the code you provided?
     
  6. It is with the NMS, but I don't want to use it, it's really dangerpur for the server
     
  7. Well... if you want To use NBT you will have to use NMS. The only other option is the mentioned PersistentDataHolder or you take some API. There is no Bukkit or Spigot-Method that can provide NBT...
     
  8. Ehh ? Why did you say NMS is dangerous ?!
    NMS is obfuscated and changes every version but it's not a nuclear bomb... Spigot uses indrectly NMS. Checks its source code ;)
     
  9. What nbt tag do you want to modify? Ever thought of creating a PR to add support for that?

    Most if not all things are covered through the Spigot-API.
     
  10. Or just accessing the NMS with Reflection for more versions support
     
  11. TeamBergerhealer

    Supporter

    Or including compiled code for different versions, or generating glue code at runtime. But stick to using an API like nbt-api because the version differences are too large to make it worth writing your own wrappers/glue code for.