#Inventory#removeItem() problem

1 messages · Page 1 of 1 (latest)

lean sequoia
#

Hey, when a dispenser dispenses an item, the BlockDispenseEvent triggers.
From their i get the dispenser block with.

Dispenser dispenser = (Dispesner) event.getBlock().getState();

From there, i wanna cancel the event but removing the item from the inventory too.
So i wrote following:

HashMap<Integer, ItemStack> items = dispenser.getInventory().removeItem(event.getItem());
if (items.size() == 0) event.setCancelled(true);

So the problem is now, that its only working when the dispensed itemstack has an amount that is not equal to 1 or 2. Why is it the case?

sand ice
#

if you cancel the event the remove will have no effect

lean sequoia
#

but its working for itemstacks with higher amount of 2

sand ice
#

if you want to modify the items you need to schedule for 1 tick later

sand ice
#

you are not cancelling it when the stack size is greater

lean sequoia
#

the hashmap returns items that where unable to remove

#

so why are they unable to remove

sand ice
#

you are also workgin on a BlockState so you must call update()

lean sequoia
#

i did

sand ice
#

I can only comment on teh code you have shown

lean sequoia
#

my bad

#

but the problem is that the inventory is unable to remove an itemstack of amount 1 and amount 2. The rest is removeable

#

thats what i dont understand

sand ice
#

ok from the beginning. If you are cancelign the event you can;t trust any modification of the inventory inside the event.

#

if you want to modify the inventory you shoudl do it all in a scheduled task 1 tick delayed

lean sequoia
#

but its working with itemstacks that amount is bigger than 2 AND with the cancel ;-;

lean sequoia
#

but why do i have to wait a tick for the itemstacks amount of 1 and 2 and why is it working for amount from 3 to 64????? im confused

#

but its working with 1 tick delay

sand ice
#

its because some events actually fire after they happen so it undoes what it did if cancelled

#

BlockPlace is the worst offender