Solved [1.8 - 1.15.x] How to get the player who holds Itemstack (NMS)

Discussion in 'Spigot Plugin Development' started by 6u5t4v, Jan 18, 2020.

  1. The reason I am using nms is because I need to store values in custom items, so the custom items can have a level, the can be increased by using it.
    I think it would be nice if I had an api for this plugin, and therefor I want to let people using the api be able to get the player holding the specific item.

    Method can be called with PetsAPI#getPetHolder(<item in players hand>)
    Code (Java):

        public Player getPetHolder(ItemStack pet) {

            return null;
    Notice the Itemstack have an nbttag
    #1 6u5t4v, Jan 18, 2020
    Last edited: Jan 18, 2020
  2. Why do you need to use NMS to store data, ItemMeta is a PersistentDataHolder
  3. 1.15 dont seem to have PersistenDataHolder
  4. becouse itemmeta is the PersistenDataHolder i think
  5. But if it is why dont I have access to the methods from PersistenDataHolder
  6. NMS is working fine for right, can anyone help me with my issue? :)
  7. I dont think the ItemStack itself saves the player who is holding it so you maybe could add a NBT Tag to save the information when a player swaps to that item?
  8. for-loop over all online players and check if they have the item in their inventory
  9. Trying not to do that
  10. What if it is in a chest
  11. Then you should just remove the value from the NBT Tag again
  12. Should I save the player, player's or player's uuid as value
  13. Choco


    because that's not the method name. Please actually take the time to look at the Javadocs that were linked (or even just use your IDE...) and realize that there's literally only one method added by PersistentDataHolder, #getPersistentDataContainer(). Use the API. You have no reason whatsoever to be using servers internals to be doing this.

    Also, no. ItemStacks are nothing more than information about a quantified item. They are not tied to a specific player nor inventory.
    • Agree Agree x 1
  14. I know thats not the name, I just hoped you would know what I meant, but I can see how that is very unclear to know. I found out I should need #getCustomContainer.
  15. I would use the UUID
  16. I switched over to PersistenDataContainer:

    This is my code for now, dont know if it works yet.
    Code (Java):

        public Player getPetHolder(ItemStack pet) {
            CustomItemTagContainer ctc = pet.getItemMeta().getCustomTagContainer();
            if(ctc.getCustomTag(plugin.wrapper.holderKey, ItemTagType.STRING) == null)
                return null;
            Player holder = Bukkit.getPlayer(ctc.getCustomTag(plugin.wrapper.holderKey, ItemTagType.STRING));
            return holder;
    Then I will create a custom event that checks if the pet is in a new inventory or something along the lines
  17. just fucking loop over players why are you doing this weird shit
  18. Your suggestion is pretty damn awful, if you have many players online you with generate a huge overhead. I am sure people like you are the reason why why a server with 80 plugins runs on 3 tps. Taking the easy way out, screw the performance of the server right? The last person I would ever want to comission something from is someone that shares your thought process. The little things add up for awful performance loss, dont just shrug over it thinking you will only lose a little tiny bit of performance.
    • Agree Agree x 1