Check if inventory contains item with a data but custom displayname

Discussion in 'Plugin Development' started by FisheyLP, Jun 9, 2014.

Thread Status:
Not open for further replies.
  1. I want to check if the inventory contains new ItemStack(Material.INK_SACK, 1, (byte) 2);. The itemstack can have a custom displayname and custom amount. How can i do this?
  2. Offline


    Try if it contains material.inksack, then make a itemstack that gets the inksac from player inv, then get its amount and compare it to a ont :) hope I helped...
  3. Can you give me a example? :D
  4. Offline

    Go Hard

    1. public ItemStack next = (HELLO(new ItemStack(Material.INK_SACK, (byte) 2)));

    1. private ItemStack HELLO(ItemStack is) {
    2. List<String> name = new ArrayList<String>();
    3. name.add("");
    4. name.add(ChatColor.RED + "ITEM LORE");
    5. ItemMeta im = is.getItemMeta();
    6. im.setDisplayName(ChatColor.AQUA + "ITEM NAME");
    7. im.setLore(name);
    8. is.setItemMeta(im);
    9. return is;
    10. }

    Then you would do something like
    1. if(p.getInventory().contains(next) {
    2. //do something
    3. }

    This is a similar way i do it. Don't know if this works haven't tested it.
  5. ehh. no. I want to detect if the inventory contains the itemstack Material.INK_SACK, (byte) 2. It can have a random / custom displayname or it can not have a display name
  6. Offline

    Go Hard

    FisheyLP Oh read it wrong. Just check if the players inventory contains the ink sack and if the ink sack has item meta. Then get the display name.
  7. You read it this time too wrong ._. i explain it you again: The item CAN have a displayname, but it CAN too don't have a displayname. It's not a special displayname like "ยง6trololo inksack". It can be everything.
    EDIT: I dont want to check if the item have a displayname. I want to check if the inventory CONTAINS the item
  8. Offline


    Would something like this work?
    1. for (ItemStack item : player.getInventory().getContents()){
    2. if (item.getType() == Material.INK_SACK && item.getAmount() == 1 && item.getDurability() == 2) return true;
    3. }
    4. return false;
    Garris0n likes this.
  9. Offline

    Go Hard

    FisheyLP That's what you said.... Even the tilte of this thread says it... I get that you wanna check if the player CONTAINS the item. But you want to said it can have a custom display name or not. So just check if the players inventory contains an ink sack.
  10. bump

    but i want to check if it contains a ink sack with data 2. if i do it with p.getInventory().contains(new ItemStack(Material.INK_SACK, 1, (byte) 2)){ then it doesn't works with a itemstack with displayname

    EDIT by Moderator: merged posts, please use the edit button instead of double posting.
    Last edited by a moderator: Jun 8, 2016
  11. Offline


  12. Offline


    It would be nice if you explained what to do instead of spoonfeeding, but with all the misinformation on this thread, at least somebody posted an answer that made logical sense.
    Axe2760 likes this.
  13. oups. I didn't said that i need to remove the specified amount of Items in the inventory :D
  14. Offline


    Then use setItem() and/or item.setAmount()?
  15. Lets say i have 2 stacks ink_sack with data 2 in my inventory. One stack with a displayname and the other without. I want to check if the inventory contains at least 1 stack + 32 items. Now i want to remove 1 stack + 32 items. How can i do this?
  16. Offline


    Entirely remove the first stack (setItem(index, null). Set the amount of the second stack (stack.setAmount(32)).
Thread Status:
Not open for further replies.

Share This Page