checking if a chest is full

Discussion in 'Plugin Development' started by CaiusTSM, Dec 24, 2011.

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

    CaiusTSM

    Hello, I was wondering how I could check to see if a chest is full or not. Thank you for any help.
     
  2. Offline

    bergerkiller

    Code:
            Block b = event.getBlock();
            if (b.getType() == Material.CHEST) {
                Chest chest = (Chest) b.getState();
                Inventory inv = chest.getInventory();
                //method a: if it contains empty slots
                boolean hasEmptySlot = false;
                for (ItemStack stack : inv.getContents()) {
                    if (stack == null) {
                        hasEmptySlot = true;
                        break;
                    }
                }
                //method b: if it contains room for any sort of item
                ItemStack itemToAdd = new ItemStack(Material.WOOD, 4);
                int foundcount = itemToAdd.getAmount();
                for (ItemStack stack : inv.getContents()) {
                    if (stack == null) foundcount -= itemToAdd.getMaxStackSize();
                    if (stack.getType() == itemToAdd.getType()) {
                        if (stack.getDurability() == itemToAdd.getDurability()) {
                            foundcount -= itemToAdd.getMaxStackSize() - stack.getAmount();
                        }
                    }
                }
                boolean canContainitem = foundcount <= 0;
            }
    I decided to just show both examples: one to check for a 100% empty slot and one to check if an item can be added to the chest.
     
    Mike724 likes this.
  3. Offline

    CaiusTSM

    thank you!:)
     
Thread Status:
Not open for further replies.

Share This Page