Solved [1.4.6] Setting a custom NBT Tag on item?

Discussion in 'Plugin Development' started by Shay Williams, Dec 22, 2012.

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

    Shay Williams

    Hey Bukkit,

    So I've been working hard over the last 24 hours porting all of my plugins for a private server to the new bukkit API in regards to NMS / NBT data.

    I'm wondering, is there still a way to set a custom NBT tag on an item? Before 1.4.6, this could easily be achieved by:

    net.minecraft.server.v1_4_6.ItemStack nms = ((CraftItemStack)i).getHandle();
    NBTTagCompound tag = nms.getTag();
    tag.setInt("DRD", (int)new_dur); 
    However I can't seem to port this to the new API. Here's what I've managed thus far:

    net.minecraft.server.v1_4_6.ItemStack nms = CraftItemStack.asNMSCopy(i);
    NBTTagCompound tag = nms.getTag();
    tag.setInt("DRD", (int)new_dur); 
    However this is merely using reflection to get the NMS data of the item and changing the reflected data; any way for me to update the original item's NBT collection after modifying the data in the reflection? Thanks!

    As a follow up, the same question applies to wanting to modify NBT's such as "CustomPotionEffects".

    EDIT by Moderator: merged posts, please use the edit button instead of double posting.
    Last edited by a moderator: May 30, 2016
  2. Offline


    After that, do CraftItemStack.asBukkitCopy(nms) to get the bukkit ItemStack version.
  3. Offline

    Shay Williams

    So the only way to pass the information is to create a new CraftItem stack with the new data? No way to just append the old one? Thanks!
  4. Offline


    Shay Williams likes this.
  5. Offline


    That makes a CraftItemStack mirror though.
  6. Offline


    You can still get a reference to the same underlying handle, but you will have to use reflection. Though, cloning the item back and forth is an option.

    In ProtocolLib, I use a custom class I call BukkitUnwrapper to get the underlying net.minecraft.server-reference for entities and item stacks. It's tied to a lot of other classes though, so I made a version (download here) that is much easier to use:
    1. for (ItemStack stack : target.getInventory()) {
    2. if (stack != null) {
    3. Object nmsStack = BukkitUnwrapper.unwrapItem(stack);
    4. System.out.println(nmsStack);
    5. }
    6. }
  7. Offline


    So CraftSomething.getHandle() no longer works in the new API?
  8. Offline


    CraftItemStack extends ItemStack, so you could use it in methods like player.getInventory().addItem()

    Nope, completely absent
Thread Status:
Not open for further replies.

Share This Page