0

Im currently creating a plugin which utilizes dispensers. I have a while loop during a listener that should remove one piece of coal from the inventory of the dispenser until there is none left on which the loop will break. It loops fine and registers the counting down of a integer used to reprosent the coal quatity. However the inventory of the dispenser doesnt not update and any help would be much appreciated. Here is the code contained in the while loop

Block temp;
            int coal;
            BlockState state = block.getState();
            Inventory sourceInv = ((InventoryHolder)          state).getInventory();
            while (sourceInv.contains(Material.COAL))
            {
                state.update();
                temp =     drillSpenser.getBlock().getRelative(BlockFace.DOWN);
                temp.breakNaturally();
                int index = sourceInv.first(Material.COAL);
                ItemStack stack = sourceInv.getItem(index);
                coal = stack.getAmount();
                System.out.println(coal);
                if (coal == 0)
                {
                    sourceInv.remove(Material.COAL);
                    break;
                }
                coal--;
                ItemStack newItem = new ItemStack(Material.COAL, coal);
                sourceInv.remove(Material.COAL);
                sourceInv.addItem(newItem);

                state.update();
                block.getState().update();
}

            }
CarbonAssassin
  • 855
  • 12
  • 24

1 Answers1

1

try this

ItemStack itemStack = new ItemStack(Material.COLA, 1);
sourceInv.remove(itemStack);

or

ItemStack itemStack = new ItemStack(Material.COLA, 1);
sourceInv.removeItem(itemStack);

instead of

ItemStack newItem = new ItemStack(Material.COAL, coal);
sourceInv.remove(Material.COAL);
sourceInv.addItem(newItem);

This should remove one coal even from a stack that is larger than 1.

bw2801
  • 321
  • 2
  • 8
  • 15