Checking how many of X is in a player's inventory

Discussion in 'Plugin Development' started by Starfire1337, Jul 30, 2014.

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

    Starfire1337

    I need to find out how many items X is in a player's inventory - So far, I've used essentials to get an itemstack from the sign, returned as {ITEM x 64}, and I'm using this code:

    Code:
    public static int getAmount(Player player, ItemStack i)
    {
            PlayerInventory inventory = player.getInventory();
            ItemStack[] items = inventory.getContents();
            int has = 0;
            for (ItemStack item : items)
            {
                if ((item != null) && (item == i) && (item.getAmount() > 0))
                {
                    has += item.getAmount();
                }
            }
            return has;
        }
    However - it is returning 0 every time. Any thoughts on how to fix this?
     
  2. Offline

    mythbusterma

    Starfire1337

    Because an ItemStack cannot equal an int. (item == 1) is a logical fallacy.
     
  3. Offline

    Starfire1337

  4. Offline

    mythbusterma

    Starfire1337

    Are you sure the ItemStack you're comparing it to is exactly equal? You should instead check that they are the same material.
     
  5. Offline

    Starfire1337

    mythbusterma
    YES! That's what I'm looking for - how would I do that?
     
  6. Offline

    mythbusterma

    Starfire1337

    item.getType().equals(i.getType()) should do it
     
  7. Offline

    Starfire1337

    Yup - works perfectly :)
     
Thread Status:
Not open for further replies.

Share This Page