Storing extra data in ItemStacks

Discussion in 'Plugin Development' started by The_Spaceman, Feb 19, 2020.

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


    I want to store some data in an ItemStack.

    What I want is that when a player r-clicks with this item a certain command will run. The arguments are displayed on the lore of the item. But I don't really want to extract the data for the command from the lore.

    I've tried creating my own MetaData, but this is has no access modifier, so I can't use it. When extending an existing MetaData class (like CraftMetaBook) it will throw an error when giving the metaData to an ItemStack. Because my class is not CraftMetaData.class...
    The same error when using MaterialData.

    I've also tried using a custom Enchantment, but the data is instantly lost.

    Is there a way to store custom data without using NMS?
    Last edited: Feb 19, 2020
  2. Offline


    You can create your own class to hold the information.

    Have a class called, say:

    Inside the class have something like:
    private ItemStack stack
    private string command
    Then initialize the class with a constructor. I'm not sure if this is particularly what you're wanting though.
  3. Offline


    I want to store the command in the ItemStack. So I don't have to keep track of it
    Just like adding tags to the ItemStack
    Last edited: Feb 19, 2020
  4. Offline


    If you're using spigot, look into persistent data container, allows you to easily store strings in items which I believe is what you're after.
  5. Offline


    You can use HashMap for that, <ItemStack, String>
    Then check if the hashmap contains the key of this itemstack when a player rightclicks
    and if it does, make the player perform the command value of the key item
  6. Offline


    yes, exactly. This way I can store the data in the item
    Thank you

    For people wanting the same: you can get the PersistentDataContainer from the ItemMeta, you should be able to figure the rest out on your own
Thread Status:
Not open for further replies.

Share This Page