Count How Many Of A Particular Item Is In A Player's Inventory

Discussion in 'Plugin Development' started by Jamie Sage, Jul 7, 2012.

Thread Status:
Not open for further replies.
  1. How would i count how many of a particular item is in a players inventory?

    Been looking for a while now but i haven't found a solution.

    And again, any help will be appreciated :)
     
  2. Offline

    Squish000

    I think the only way it would be to use a loop on player.getInventory().getContents(), then for every itemstack with the same id as the one your searching for add the amount of the itemstack to the counter.
     
  3. Offline

    lol768

    Use this function. Example usage:
    Code:
    int i = getAmount(event.getPlayer(),7);
    //Counts bedrock in player's inventory
    PHP:
    public static int getAmount(Player playerint id)
    {
            
    PlayerInventory inventory player.getInventory();
            
    ItemStack[] items inventory.getContents();
            
    int has 0;
            for (
    ItemStack item items)
            {
                if ((
    item != null) && (item.getTypeId() == id) && (item.getAmount() > 0))
                {
                    
    has += item.getAmount();
                }
            }
            return 
    has;
        }
     
    legostarwarszach likes this.
  4. Thanks!

    As soon as you posted it i created it myself, thanks for your help anyway :)
     
  5. Offline

    lol768

    Glad you got it sorted. Been working on something similar for an economy system. Mind if I ask what you're using it for (just curious :))?
     
  6. Hehe, decided to re-create my old minecraft server. I didnt really like how most servers can be put up within a few minutes; nothings original anymore.

    Thats why im trying to create everything myself (well not everything, just the 'main' features).

    At the moment im working on the Economy system, going to be item based (as you probably guested). Trying to create it differently to other Economy plugins, but its getting hard to think of ideas which dont already exist / are over used by most servers.
     
Thread Status:
Not open for further replies.

Share This Page