Solved Getting the amount of Items in a inventory

Discussion in 'Plugin Development' started by SnelleFrikandel, Jul 12, 2016.

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

    SnelleFrikandel

    Hey,

    Im trying to make something that counts the amount of items of a Inventory if you close it:

    Code:
    ItemStack[] items = e.getInventory().getStorageContents();
                  for (ItemStack item : items) {
                      if (item.getType() == Material.GOLD_INGOT) {
                          balance += item.getAmount();
                      }
                     if (item.getType() == Material.GOLD_BLOCK) {
                          balance += item.getAmount()*64;
                      }
                     if (item.getType() == Material.LAVA_BUCKET) {
                          balance += item.getAmount()*4096;
                      }
            }
    But when I run it it shows a NullpointerException on:

    Code:
    if (item.getType() == Material.GOLD_INGOT) {
    Regards,

    Joey
     
  2. Offline

    Lordloss

    check if the itemStack is null first.
     
  3. Do not use == to compare objects. Instead use .equals() or .equalsIgnoreCase() if you're comparing strings.
     
  4. Offline

    ArsenArsen

    Use == if you compare primitives or enums, or if you need to see do two variables point to the samem memory adress.

    Everything is an object.
     
  5. Offline

    Jakeeeee

    @CodePlaysMinecraft He is not comparing a string. He is in fact comparing an enum and allows ==.
     
    Lordloss likes this.
  6. Offline

    SnelleFrikandel

    Code:
        @EventHandler
        public void onVaultClose(InventoryCloseEvent e) {
            if(e.getInventory().getName().equalsIgnoreCase("Vault")) {
                int goldingot=0;
                  int goldblock=0;
                  int moltengold=0;
                  int balance = 0;
                  ItemStack[] items = e.getInventory().getContents();
                  for (ItemStack item : items) {
                      if(item == null) {
                          e.getPlayer().sendMessage("is null");
                      }else {
                      if (item.getType().equals(Material.GOLD_INGOT)) {
                          balance += item.getAmount();
                      }
                     if (item.getType().equals(Material.GOLD_BLOCK)) {
                          balance += item.getAmount()*64;
                      }
                     if (item.getType().equals(Material.LAVA_BUCKET)) {
                          balance += item.getAmount()*4096;
                      }
                      }
                  }
            balance = (goldingot) + (goldblock*64) + (moltengold * 4096);
              e.getPlayer().sendMessage("" + balance);
              Vault.setBalance(e.getPlayer().getName(), balance);
            }
        }
    If I run this with 5 items in the inventory I get 4 times the message "is null" and 1 time the balance message which is 0.
     
  7. Offline

    SnelleFrikandel

    Can anyone help me out?
     
  8. Offline

    ArsenArsen

    int count is 0
    for Every Item in Inventory.getContent
    if item is not null
    count incremented
    end if
    end loop

    There you have it.
     
  9. Offline

    Lordloss

    Yes i can. You add the amount of items to the balance, and at the end you set balance to the sum of the other variables, goldingot etc. which you never touched.
     
  10. Offline

    SnelleFrikandel

    Really? How stupid of me.
    Sometimes you cant see your own mistakes,

    Thanks for pointing it out @Lordloss
     
Thread Status:
Not open for further replies.

Share This Page