Solved Identifier for an ItemStack

Discussion in 'Spigot Plugin Development' started by Qhucy, Jan 16, 2020.

  1. TL;DR

    Is there any value I can retrieve from a given ItemStack to uniquely identify it if parts of it change down the line?

    ---------------------------------

    Hello,

    Whenever a player breaks a block, it will add a number to a HashMap that is essentially like a block counter. Every 30 seconds, the server will then run through this HashMap and add to the Block Counter found in the lore of their pickaxe.

    The problem is that I am not sure how to uniquely identify the ItemStack they use to break blocks on. Say a player breaks a block with a Diamond Pickaxe, what value would I store as an identifier in this HashMap so 30 seconds later I know which ItemStack to update the lore to inside of a player's inventory (if it's still there). Getting the hashCode from the ItemStack does not seem to do the job because it changes if the ItemStack is updated at all (i.e. name, lore, enchantment). Furthermore, tracking this ItemStack through it's name, material, enchantments, etc. would not be possible because all of these values COULD change within these 30 seconds.

    The reason I am not adding to the block counter the second after they break a block is because I believe this is an inefficient way to do this when tens of players are mining blocks with efficiency 100.
     
  2. ItemMeta#getPersistentDataContainer()
     
  3. Dont try to optimize if you are not sure if you need to.

    That said: You can just keep a reference to the ItemStack.
    Some idea would be something like that:

    Map<ItemStack, Consumer<ItemStack>> actions;

    Then you can just go through them piecewise.
    Just getting a reference of the ItemStack and storing it in a Collection
    lets you edit the ItemStack later on. You just have to make sure to also save
    the function you want to invoke on the ItemStack.

    A good class for this is Javas Consumer.
    This is another implementation
    Screenshot_10.png

    Then you can add actions like this:
    Screenshot_11.png


    Another idea would be creating some Object that can be called on later like this:
    Screenshot_6.png

    Or if you need a return type:
    Screenshot_7.png


    Then you can maintain some collection and work your way through somehow:
    Screenshot_8.png

    You can also be explicit about the type
    Screenshot_9.png
     
    #3 7smile7, Jan 16, 2020
    Last edited: Jan 16, 2020
  4. That is not a method that exists in 1.13.2 (not sure if it does for other versions). ItemMeta#getCustomTagContainer() does work though but it does not do what im asking for.
     
  5. md_5

    Administrator Developer

    Doesn't it? Just store a UUID on the item?
     
  6. After reading about the PersistentDataAPI, it is possible to add my own UUID for each item to find it later, but doing this would update the ItemStack which is what I was trying to avoid. However, I think I'll try this out anyway.

    Thank you for your replies