Get total amount of items in a chest

Discussion in 'Plugin Development' started by boardinggamer, Aug 6, 2013.

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

    boardinggamer

    I need to get the total amount of items in a chest but I can't figure out the code for it. I need to figure out the title amount of let's say stone. I tried going through all the stacks and adding the stone ones together but I get a null pointer exception.

    Can someone please help me figure this out.
     
  2. Offline

    chasechocolate

    Do you want the total amount (total amount of each item) or the total count (number of ItemStacks with a specific type)? Just create an int variable called count, loop through the items in the chest, check if(item != null && item.getType() == Material.SOMETHING), and use count += item.getAmount().
     
  3. boardinggamer
    Code:
    public int getAmountOf(Material item, Inventory inventory) {
        int i = 0;
        for(ItemStack is : inventory.getContents()) {
            if(is == item) {
                i += is.getAmount();
            }
        }
        return i;
    }
    Untested.
     
  4. Offline

    boardinggamer

    I did that and it's giving me a null pointer when I get the stacks type
     
  5. Offline

    LucasEmanuel

    Assist
    You don't actually check if the material is the same as the material in the parameter, you just compare if the ItemStack-object in the current iteration is the same instance as the Material-object in the parameter. :)
     
  6. Offline

    dunbaratu

    In the code shown, the variable 'is' will be null for each empty slot in the inventory.
    So this bit:
    Code:
            if(is == item) {
                i += is.getAmount();
            }
    can be replaced with this instead:
    Code:
            if( is != null )
                if( is.getType() == item) {
                    i += is.getAmount();
                }
            }
    and that should work better.
     
  7. Offline

    boardinggamer

    I got it working. Thanks guys
     
  8. LucasEmanuel dunbaratu
    Offtopic, but does either one of you know if there's a mobile version of Eclipse, NetBeans etc? I'm very often on my phone, and there's no good way of checking if my code is correct, so somekind of tool would be handy. Any ideas?
     
  9. Offline

    dunbaratu

    *blink* *blink* *blink* gaaaaaa..... Eclipse is a fairly heavyweight program. The idea of trimming it down to where it would work on a smartphone is...hurting my brain.
     
    foodyling likes this.
  10. It was just an example :p
    I just need a way to check if my syntax is correct. In the code I posted above, I made a mistake by comparing itemstack and material, which you can't do. A way of checking stuff like that would be cool, and shouldn't be a big of a deal.. I think.
     
  11. Offline

    LucasEmanuel

    Assist
    There is no lightweight version of Eclipse that would work on a smartphone, but there are some pretty competent editors for Android. But I doubt any of them would fit your needs. :)
     
Thread Status:
Not open for further replies.

Share This Page