How to get item nbt

Discussion in 'Plugin Development' started by Cat 700, Jun 4, 2019.

  1. Offline

    Cat 700

    Hi so I am trying to make a wand plugin but I want the item to only work if it has a nbt tag
     
  2. Offline

    KarimAKL

    @Cat 700 It's been a while since i've used it but i think something like this works:
    Code:Java
    1. // This is the Bukkit ItemStack (org.bukkit.inventory.ItemStack)
    2. ItemStack item = new ItemStack(Material.DIAMOND); // Just an example item
    3. // This is the NMS ItemStack
    4. net.minecraft.server./*version*/.ItemStack nmsItem = CraftItemStack.asNMSCopy(item);
    5. // Get the NBT tag
    6. NBTTagCompound tag = nmsItem.hasTag() ? nmsItem.getTag() : new NBTTagCompound();
    7. // I don't remember this method, just look through the methods and see which one is kinda like this
    8. tag.setString("key", "value");
    9. // Set the NBT tag to the modified version
    10. nmsItem.setTag(tag);
    11. // Get the Bukkit ItemStack after modification
    12. item = CraftItemStack.asBukkitCopy(nmsItem);
     
  3. Offline

    Cat 700

    I am sorry I am not the best with plugin code so where would I put the nbt?
    also how would I edit the nbt I feel like it is in your code but I don't understand most of it other than the itemstack I don't do a lot of plugin coding
     
    Last edited: Jun 5, 2019
  4. Offline

    KarimAKL

    @Cat 700 You set it using the 'setString(key, value)' but as i said i don't remember if that's the exact method, just look at the possible methods for that. I commented the code to briefly explain it.
     
  5. Offline

    Cat 700

    @KarimAKL ok I understand but some of that code has errors it might be because I am using 1.14.2 but IDK
     
    Last edited: Jun 5, 2019
  6. Offline

    KarimAKL

    @Cat 700 Then show your current code.
     
  7. Offline

    Cat 700

    @KarimAKL the errors that I am getting are from the nmsItem.setTag(tag); and tag.setString("key", "value"); the nmsitem one is saying identifier expected and then the tag.setstring is saying quite a few things also my code is
    Code:Java
    1. package me.cat700.MythicalArmorWands;
    2.  
    3. import org.bukkit.Material;
    4. import org.bukkit.craftbukkit.v1_14_R1.inventory.CraftItemStack;
    5. import org.bukkit.inventory.ItemStack;
    6.  
    7. import net.minecraft.server.v1_14_R1.NBTTagCompound;
    8.  
    9. public class MythicalArmorWands {
    10. // This is the Bukkit ItemStack (org.bukkit.inventory.ItemStack)
    11. ItemStack item = new ItemStack(Material.DIAMOND); // Just an example item
    12. // This is the NMS ItemStack
    13. net.minecraft.server.v1_14_R1.ItemStack nmsItem = CraftItemStack.asNMSCopy(item);
    14. // Get the NBT tag
    15. NBTTagCompound tag = nmsItem.hasTag() ? nmsItem.getTag() : new NBTTagCompound();
    16. // I don't remember this method, just look through the methods and see which one is kinda like this
    17. tag.setString("key", "value");
    18. // Set the NBT tag to the modified version
    19. nmsItem.setTag(tag);
    20. // Get the Bukkit ItemStack after modification
    21. item = CraftItemStack.asBukkitCopy(nmsItem);
    22. }


    I found this forum with this code
    Code:java
    1. public NBTTagList getLore(ItemStack i)
    2. {
    3. net.minecraft.server.ItemStack nms = ((CraftItemStack)i).getHandle();
    4. NBTTagCompound tag = nms.getTag()
    5. if (!tag.hasKey("display"))
    6. {
    7. return new NBTTagList();
    8. }
    9. NBTTagCompound display = (NBTTagCompound)tag.get("display");
    10. NBTTagList lore = display.getList("Lore");
    11. if (lore == null)
    12. {
    13. // returns empty list
    14. return new NBTTagList();
    15. }
    16. return lore;
    17. }public void setLore(String lore, ItemStack i)
    18. {
    19. net.minecraft.server.ItemStack nms = ((CraftItemStack)i).getHandle();
    20. NBTTagCompound tag = nms.getTag()
    21. if (!tag.hasKey("display"))
    22. {
    23. this.addDisplay();
    24. }
    25. NBTTagCompound display = this.getDisplay();
    26. NBTTagList l = new NBTTagList();
    27. // Added multiline lore support
    28. // Splits the lines
    29. String[] loreLines = lore.split("\n");
    30. // Add each line
    31. for(String line : loreLines)
    32. {
    33. l.add(new NBTTagString("", line));
    34. }
    35. // Set the lore
    36. display.set("Lore", l);
    37. }

    but I do not know how I would use this for what I want

    @KarimAKL I also found this forum but it doesn't say how to test if it has a nbt tag

    EDIT by Moderator: merged posts, please use the edit button instead of double posting.
     
    Last edited: Jun 6, 2019
  8. Offline

    Cat 700

    @KarimAKL I have tried many things but I can't find much I tried to do code in this forum but that didn't work I tried replacing the tag.setstring with if(tag.hasKey) but that doesn't work IDK
     

Share This Page