[Tutorial] Attaching Metadata to Itemstacks

Discussion in 'Resources' started by Traks, Jan 18, 2014.

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

    Traks

    I wrote a little class for myself to edit metadata to itemstacks without accessing CraftBukkit or NMS classes directly. But since I couldn't find any on the interwebs I decided to make it public.
    After Comphenix informed me about general meta data on item stacks not being persistent at all, I redid the whole thing. There are now 6 classes instead of 1 (not counting the Reflection class) and it has a feature to add persistent meta data.

    Code can be found on Github:
    https://github.com/TraksAG/ItemMetaData

    Examples:
    Adding non-persistent meta data:
    Code:java
    1. ItemStack is = new ItemStack(Material.APPLE, 1);
    2. ItemMetaData imd = new ItemMetaData(is);
    3.  
    4. imd.put("key", "value");
    5. imd.put("values can also be numbers", 1337)
    6.  
    7. TagList list = new TagList();
    8.  
    9. list.add("a string");
    10. list.add(9001); // Impossible: you can only add objects of 1 type to a list (it's the way how Bukkit handles it)
    11.  
    12. imd.put("values can also be lists", list);
    13.  
    14. TagCompound compound = new TagComound();
    15.  
    16. compound.put("a compound value", "a compound key"); // Used the same way as ItemMetaData because it extends TagCompound
    17.  
    18. imd.put("values can also be compounds", compound);
    19.  
    20. is = imd.getItemStack(); // Trivial
    21. // Do something with the item stack...

    Adding (nearly) persistent meta data
    Code:java
    1. ItemStack is = new ItemStack(Material.APPLE, 1);
    2. ItemMetaData imd = new ItemMetaData(is);
    3.  
    4. TagAttribute attribute = new TagAttribute("custom name", 0, 0, UUID.randomUUID(), AttributeName.ATTACK_DAMAGE);
    5. TagList list = new TagList();
    6.  
    7. list.add(attribute);
    8.  
    9. imd.put("AttributeModifiers", list);
    10. // For attributes to work you need to create a list, add attributes to it and add the list to the ItemMetaData with key 'AttributeModifiers'
    11.  
    12. is = imd.getItemStack(); // Trivial
    13. // Do something with the item stack...

    Feel free to use it in any of your plugins or other projects. You don't need to give credit, but it'd be appreciated if you did. If you need any other examples or need extra methods or encounter bugs just Tahg me.
    Have fun!
     
  2. Offline

    Comphenix

    Ah, so this is an NBT library? I've written one myself too.

    Unfortunately, you can't use custom NBT tags (such as "customline") to store data reliably for a number of reasons:
    • Any use of ItemMeta on the item stack, either from Bukkit or a plugin, will clear out the custom tags.
    • This also occurs whenever a creative player swaps items in their inventory (try it).
    • Serialized and deserialized item stacks (through the Configuration API) will also lose their tags.
    I recommend taking a look at AttributeStorage if you want to persist item data reliably.
     
  3. Offline

    Traks

    Well that sucks...

    BTW, I took a look at your AttributeStorage and I don't quite understand what it does and how it works.
     
  4. Offline

    Comphenix

  5. Offline

    Traks

    Comphenix it supports (nearly) persistent meta data now!
     
Thread Status:
Not open for further replies.

Share This Page