How can I check if player's inventory contains custom item?

Discussion in 'Plugin Development' started by jinhoftyu, Mar 27, 2021.

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

    jinhoftyu

    I want to know if inventory contains item which has specified displayname, but I have no idea.
    Inv.contains(new ItemStack(Material.PAPER).getItemMeta().getDisplayName() == "name") ??
     
  2. Online

    timtower Administrator Administrator Moderator

    @jinhoftyu Get all paper stacks, check the displayname.
     
  3. Offline

    KarimAKL

    @jinhoftyu You should compare strings with "String#equals(String)", not "String == String".
     
  4. Offline

    jinhoftyu

    How can I get 'all' paper stacks? Should I use PlayerInventory#getItem?
     
  5. Online

    timtower Administrator Administrator Moderator

  6. Offline

    Strahan

    You can also create an ItemStack representing said item and just call Player#getInventory#contains.

    PS if you do that, be sure you aren't doing something silly like actually creating it manually in the check apart from creating it when you issue it to the player. Make a method that returns the item so if you decide to tweak the config, you don't have to go and change it multiple places.
     
Thread Status:
Not open for further replies.

Share This Page