Get if a player has an item with custom name in inv

Discussion in 'Plugin Development' started by HenkDeKipGaming, Nov 13, 2015.

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

    HenkDeKipGaming

    Hey guys,

    So I want to check if a player has a certain item with a custom name in his inv.
    Then I would like to remove 1 of them.

    How to do this?

    Thanks!
     
  2. Offline

    Zombie_Striker

    @HenkDeKipGaming
    • Check if the item in the player's hand is not null, and if it's not null, check if it's the same type as your item, and check if it has the same displayname (you should also check if it has itemmeta first)
    • Get the player's inventory, and use .remove(The itemstack in the player's hand) Note: do not use what you're testing for or else if the player has 1 more of that itemstack/ has a different lore, it will not remove. You need to get the instance that is in the player's hand and use that to remove it.
     
  3. Offline

    RoboticPlayer

    @Zombie_Striker The OP is asking about having it in the inventory, not just in the hand. @HenkDeKipGaming To do what you want, loop through the player's inventory, checking if each item is the one you want. If it is, get the number of items in that stack, Inventory#remove(ItemStack item) and then add back the number that they had -1.
     
  4. Offline

    CraftCreeper6

  5. Offline

    RoboticPlayer

    Yea I guess that does work too xD. Kind of forgot about the Inventory#contains() method though. Looping through the inventory does still work though.
     
  6. Offline

    HenkDeKipGaming

    You are talking about a for loop right?
    How would that for loop look like?
     
  7. Offline

    Scimiguy

    @HenkDeKipGaming
    Why don't you try and figure it out?
    We're not here to write code for you
     
  8. Offline

    RoboticPlayer

    Yes, I am talking about a for loop. All I will tell you is that you can loop through Inventory#getContents()
     
  9. Offline

    HenkDeKipGaming

    Ah thank you, that was the hint I needed :)

    Hm so I just tried this:

    Code:
     for(ItemStack m: player.getInventory().getContents()) {
                    if(m.equals(new ItemStack(Material.DIAMOND_BLOCK))) {
                        player.getInventory().removeItem(m);
                    }
                }
    
    This doesn't work.
    What am I doing wrong?
    Ps: This just testing, I want to check the name later on but when this works I think I can figure it out myself.

    EDIT by Moderator: merged posts, please use the edit button instead of double posting.
     
    Last edited by a moderator: Nov 14, 2015
  10. Offline

    RoboticPlayer

    @HenkDeKipGaming
    Don't compare ItemStacks unless you are looking for a specific item (i.e, one that has metadata, etc). Instead, check if ItemStack#getType() is equal to Material.DIAMOND_BLOCK.
     
  11. Offline

    HenkDeKipGaming

    So if I would check the item name like that in the for loop it will work?
    I just tried this to see if it would work
     
  12. Offline

    RoboticPlayer

    @HenkDeKipGaming Well are you trying to check for a specific diamond block or just diamond blocks in general?
     
  13. Offline

    HenkDeKipGaming

    specific, one with a custom name
     
  14. Offline

    RoboticPlayer

    @HenkDeKipGaming In this case, you would likely want to have a field which contains the ItemStack that the item is. Then compare the item in the inventory to the ItemStack field.
     
  15. Offline

    Scimiguy

    @henderry2019
    Incorrect.

    Use the getItemMeta() method, and getDisplayName() method to compare names
     
Thread Status:
Not open for further replies.

Share This Page