[1.18] How to get NBT of an ItemStack

Discussion in 'Plugin Development' started by Wonkers, May 26, 2022.

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


    Title is self-explanatory, but basically, I just want to get the NBT data of an ItemStack. I want the whole nbt, not just a tag. Raw, Pure NBT. I searched through some forum posts for this, but they're all old, and use NMS methods such as .getTag() which no longer exist in 1.18.

    Please help.
  2. Offline


    Sorry for the necro but for the people that get confused when they want to use NBT on the newer version here's how I do it.
    So from 1.14+ there is PersistentDataContainer. An example on how to use it
    NamespacedKey key= new NamespacedKey(plugin, "yourKey");
    ItemMeta meta = ...
    meta.getPersistentDataContainer().set(key, PersistentDataType.STRING, "CoolString");
    String coolString = meta.getPersistentDataContainer().get(key, PersistentDataType.STRING);
    It's really easy and you no longer need to stress about version support if you use any version from 1.14 and up.

    Also here is a more in depth tutorial which explains things really nicely: https://www.spigotmc.org/threads/a-guide-to-1-14-persistentdataholder-api.371200/
  3. Offline


    I could be wrong, but I believe PersistentDataContainer to be an alternative to NBT, which would mean that it doesn't help in this case.
    gochi9 likes this.
  4. Offline


    Hmm, well to be honest I'm not sure. If you add a tag with persistent data container to an item and then you go in minecraft and hover over the item (This is if you have CTRL + h activated) it will say NBT: 1 (Or however many tags you have).
  5. Offline


    This is a piece of my code. I hope it can help you.
    ItemStack MapDraw = new ItemStack(Material.FILLED_MAP);
                            net.minecraft.world.item.ItemStack NmsItem = CraftItemStack.asNMSCopy(MapDraw);
                            NBTTagCompound nbttagcompound = new NBTTagCompound();
                            nbttagcompound.setShort("map", (short)Data.MapsFile.getInt(event.getCurrentItem().getItemMeta().getDisplayName()));
                            ItemStack BukkitItemMap = CraftItemStack.asBukkitCopy(NmsItem);
    Attention! This is for 1.17.1, so the method name probably is not the same as 1.18
Thread Status:
Not open for further replies.

Share This Page