1.17.x Converting an Item's NBT to an ItemStack

Discussion in 'Spigot Plugin Development' started by GamerCoder215, Jun 28, 2021.

?

Is this Possible?

  1. No this is not possible

    0 vote(s)
    0.0%
  2. I will explain below

    42.9%
  3. Go learn java hehehehehehahahahahshhehhehehehhe

    57.1%
  1. So I own a semi-private SMP that me and a couple other people work on. While I use ItemStacks for the custom items, the other people use /give @p <item> {nbt}. I was hoping if there was a way to convert the item to an ItemStack through this way. It would help a lot since I need the item as an ItemStack for things like trades, item information, etc. Is this possible/is there any efficient method?

    For example:
    Code (Java):
    public ItemStack getItemStackFromGive(String givecmd) { // The Give Command

      // do stuff
    }
     
  2. Code (Java):
    CraftItemStack.asBukkitCopy(new net.minecraft.server.v1_12_R1.ItemStack(nbtTagCompoundRoot));
    (you need to change the 1_12_R1 to your plugin version)
    Basically you create an NMS ItemStack and then convert it into a Spigot ItemStack. nbtTagCompound root is, as the name suggests, the NBTTagCompound of the whole item
     
    • Useful Useful x 1
  3. Its not efficient but it is nice, you could use the NBTapi to do it quite neatly!

    NBTAPI
     
  4. You create an item from provided material using material registry (o.b.Registry#Material), and then apply nbt using bukkit usafe values util (o.b.UnsafeValues#modifyItemStack).
     
  5. How do I get the NBTTagCompound? New to NMS :p
     
  6. So you want to get the itemstack from a the /minecraft:give command? That command appears to drop the itemStack at the player's location, so you could probably listen for the command, then get the next item the player picks up using EntityPickUpItemEvent, skipping the NBT conversion altogether.
    If that isn't what you want, can you be more specific? Do you just want to make a function that takes in NBT and outputs and itemstack?
     
  7. I should have been more specific:

    I would like a way to input NBT as a string or something like that and output an ItemStack.

    For example:

    Code (Java):
    public ItemStack parseNBT(String s) {
    // stuff
    }

    // Usage:
    parseNBT("{id: \"minecraft:gold_block\", Count: 1b, tag: {HideFlags: 1, display: {Name: '{\"text\":\"Enchanted Gold Block\",\"color\":\"gold\",\"bold\":true,\"italic\":false}'}, Enchantments: [{lvl: 1s, id: \"minecraft:protection\"}]}}")

     
     
    #7 GamerCoder215, Jun 28, 2021
    Last edited: Jun 28, 2021
    • Like Like x 1
  8. Simple as this
    Code (Java):
    String nbtStr = "{id: \"minecraft:gold_block\", Count: 1b, tag: {HideFlags: 1, display: {Name: '{\"text\":\"Enchanted Gold Block\",\"color\":\"gold\",\"bold\":true,\"italic\":false}'}, Enchantments: [{lvl: 1s, id: \"minecraft:protection\"}]}}";
    NBTTagCompound nbt =  MojangsonParser.parse(nbtStr);
    net.minecraft.world.item.ItemStack nmsItem = net.minecraft.world.item.ItemStack.a(nbt);
    org.bukkit.inventory.ItemStack item = CraftItemStack.asBukkitCopy(nmsItem);
    receiver.getInventory().addItem(item);
     
    • Informative Informative x 1
  9. Thank you!
    Testing currently
     
    • Like Like x 1