Inventory counter help

Discussion in 'Plugin Development' started by rob4001, Sep 26, 2011.

Thread Status:
Not open for further replies.
  1. Is there a way of counting the amount of say red wool in a persons inventory Spesifically items with data values which does not throw null pointers if the block doesnt have a data vlaue

    Current code:
    Code:
        public static int getAmount(Player player, int id, Byte itemdata){
            PlayerInventory inventory = player.getInventory();
            ItemStack[] items = inventory.getContents();
            int has = 0;
            for (ItemStack item : items) {
                if ((item != null) && (item.getTypeId() == id) && (item.getAmount() > 0)&&(item.getData().getData()==itemdata)) {
                    has += item.getAmount();
                }
            }
            return has;
        }
     
  2. Offline

    vildaberper

    Check if the data is null I guess, like you did with item.
     
  3. Offline

    wwsean08

    in my experience when i tried to check if the data was null it threw an error at me, but i'm fighting through a similar situation rob
     
  4. Offline

    vildaberper

    If everything else fails, just use try/catch.
    Try comparing the data in the try block, if it fails itll only compare the ids in the catch block.
     
  5. item.getDurability() would do if you don't mind the same effect on damaged tools.

    Other than that, to avoid a too complex if statement, put another one in the first one where you sperately check for data if there is any.
     
Thread Status:
Not open for further replies.

Share This Page