Best way to track Items

Discussion in 'Spigot Plugin Development' started by H4x0r1337, Jun 14, 2019.

  1. So I've got a specific command. The user can choose an item and action. The action is assigned to the item. It should be possible to assign two different actions to two different diamond hoes, for example. There is no way, to set some Metadata on an itemstack. So I need to be able to do the following:
    1. Track the Item in the player inventory
    2. Distinguish between multiple items
    (3. Track the item outside of the player inventory)
    (4. Track when the Item is destroyed, might be needed to prevent memory leaks)
  2. SteelPhoenix


    Just put nbt tags on them to distinguish between the items.
  3. Last time I tried this on 1.8, and it was horrible to work with NMS, and then Reflection and stuff. Plus, the NBT data is Client side too, so I have to replace the item every single time I modify the properties of my action.

    PS: Now I'm on 1.14, but not aware or any non-NMS NBT API.
  4. Choco


    Latest version of Bukkit (1.14+) has a proper API for custom NBT tags. The reason you have to replace the item isn't due to client-side restrictions, it's because ItemStack objects are frequently cloned. When pulled from the inventory, it's a clone of what's in the player's inventory. You have to update it.
    • Agree Agree x 1
  5. Can you send a link to some documentation? I'm not afraid of updating the inventory itself, but I don't want to send a packet to the player. The data should be server side.
  6. This really seems like a nice tool. Is it possible to store store any object in it? Does the data stay after restart? If so, can I save the data of my object and load them back?
  7. #9 H4x0r1337, Jun 14, 2019
    Last edited: Jun 14, 2019
  8. Choco


  9. You aren't much of a leet haxor, are you?
    Otherwise, if this API sucks, I dare you to write a better one replacing this custom NBT one.
    But keep in mind:
    • If you change the object, the item automatically updates as well.
    • If you get a new instance of the item, you always get the same object that was attached to the original item.
    • If you update anything on the object, it gets auto-synchronized with the client of course.
    • The client can't access the NBT data, but can still use it.
    • Oh and don't forget all this need to be very easy to use, readable and accessible via the official API.
    Good luck.
    Meanwhile, I am happily using this new API.

    Cheers ~
  10. How exactly are you planning on storing data persistently if not using NBT values ? The metadata API you know from spigot simply stores all of your values in a giant key value map :/ Yes the client cannot read from it nor is the item updated. But items are not entities. They are not identified by uuid. It is really up to you to build a framework around the persistent data API to reduce item updates etc.
  11. I'm just uncreative with my names.
    Bukkit just doesn't let me, and I don't intend to fork/modify it. There are not the proper events (When item is deleted / unloaded) and the item doesn't contain some unique id. ItemStack also gets cloned so I can't check if the reference is equal.
    May I see your code? I had to manually replace it in the inventory of the player. And this is a hacky, because of main / off hand.
    this contradicts the previous point.
    But it shouldn't. This results in a wiered replacing animation and the client shouldn't even know about the data.
    Hacked/Modded clients can read the NBT tags and see the data.
    Yes, it is, but the results are bad (for my project).
  12. I plan to assign some random id once for every item (the only NBT). This is useful to determine which item invoked the action and prevents stacking incompatible items. Then I would need prevent cloning the Item in creative and assigning a new id. I store the id and action in a hashmap. I also would need an event if the item is destroyed to remove the data from my map and free memory. If I need to save them, then I just serialize my map and store it on disk. My previous answer answered some things like the client read thing. That's hard, because bukkit doesn't provide such event, so I expected the official API to do something like this.
  13. Firestar311


    Why does it matter if a hacked/modded client can see the data anyways? They can only view said data, they cannot modify it as it is stored on the server. And only your plugin will use it anyways, unless you provide an API for other plugin to use it