Solved Trying to check if player has a item in his Inventory

Discussion in 'Spigot Plugin Development' started by Th3Wh1teG0d, Jan 9, 2020.

Thread Status:
Not open for further replies.
  1. Hello, im trying to check if a player has a special item in his inventory but my code dosent work. Can anyone help me ?

    Code:

    Code (Java):

    ItemStack ammoitem = new ItemStack(Material.SNOW_BALL);
    ItemMeta ammometa = ammoitem.getItemMeta();
    ammometa.setDisplayName("§8«§7Ammo §8/ §c0§7,§c25§7g§8»");
    ammoitem.setItemMeta(ammometa);
    if (player.getInventory().contains(ammoitem.getType())) {
     
    The problem is it dont check if the player has a regular snowball or this item in his inventory.
     
  2. Remove the getType
     
  3. drives_a_ford

    Moderator

    • Friendly Friendly x 1
  4. The problem is when i stack 2 of the items in one slot its not working
     
  5. Please read the docs yourself, the item also checks if the amount is the same which is something he does not want. I presented a code that should work in a previous post
     
  6. drives_a_ford

    Moderator

    If he asks for "a" item, then that's what I'll help him find.

    Then simply iterate over the contents with Inventory#getContents and use ItemStack#isSimilar. Bare in mind that as the javadocs describe, individual items in the contents may be null so you need to check for that.
     
    • Agree Agree x 1
  7. The only thing i do is changing .contains to .containsAtLeast ... :rolleyes:

    and it worked
     
Thread Status:
Not open for further replies.