Solved Getting amount of an itemstack in player's inventory

Discussion in 'Plugin Development' started by Pik0, Apr 9, 2014.

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

    Pik0

    Hi, i was trying to get an integer of the player's material amount and i came out with this:
    The material i want to check is Skull.

    Code:java
    1. int size = p.getInventory().all(Material.SKULL_ITEM).size();


    but however that doesnt work very well. I wanted to know if you guys can help me xD
     
  2. Offline

    St3venAU

    Inventory.all returns a Map of type <Integer,ItemStack>, so we can get all of those itemstacks with .values() and add up all of the amounts of those itemstacks.

    Code:java
    1. int amount = 0;
    2. for(ItemStack is : p.getInventory().all(Material.SKULL_ITEM).values())
    3. {
    4. amount=amount+is.getAmount();
    5. }
     
  3. Offline

    rfsantos1996

    Code:java
    1. int amount = 0;
    2. for(Map.Entry<Integer, ? extends ItemStack> set : p.getInventory().all(Material.SKULL_ITEM).entrySet()) {
    3. amount += set.getValue().getAmount();
    4. }

    The .size() on .all(material) will return the size of the hashmap given by all. (how many slots and not how many items)

    @EDIT: and oh, yeah, there's .values() on HashMaps, I forgot that and used entrySet. St3venAU is right lol
     
  4. Offline

    Pik0

Thread Status:
Not open for further replies.

Share This Page