1.17.x Inventory item NBT tag as string

Discussion in 'Spigot Plugin Development' started by uncovery, Jul 13, 2021.

  1. What is the 1.17 recommended way to get the text of an inventory item NBT tag?
    Under 1.16, I used the following method to get the NBT of an inventory slot i:

    Code (Text):
    ItemStack itemStack = inv.getItem(i);
    net.minecraft.server.v1_16_R3.ItemStack CBStack = CraftItemStack.asNMSCopy(itemStack);
    NBTTagCompound itemTag = CBStack.getTag();
    String parsedString = MojangsonParser.parse(tagString).toString();
    String tagString = itemTag.toString();
    obviously net.minecraft.server.v1_16_R3.ItemStack does not work in 1.17, so how to do it then?

    thanks!
     
  2. change:
    Code (Java):
    net.minecraft.server.v1_16_R3.ItemStack
    to:
    Code (Java):
    org.bukkit.craftbukkit.v1_17_R1.inventory.CraftItemStack
    Or use Reflection (better in my opinion).
     
  3. Thanks a lot, tried it but I'm getting a

    Code (Text):
    incompatible types: net.minecraft.world.item.ItemStack cannot be converted to org.bukkit.craftbukkit.v1_17_R1.inventory.CraftItemStack
     
  4. Are there no errors in your code and the error only occurs at build time?
     
  5. lol you said wrong import
     
  6. use net.minecraft.world.item.ItemStack
     
    • Agree Agree x 1
  7. Code (Java):
    ItemStack spigotItem = new ItemStack(Material.DIRT);
    net.minecraft.world.item.ItemStack item = CraftItemStack.asNMSCopy(spigotItem);
    String nbt = item.getTag().toString();
     
  8. Oh, my bad...
     
  9. It works now, thanks a lot!