Player Prevent DropItem problem


#1

I don’t want the player to discard anything.
I canceled with the DropItemEvent.Dispense listener, but the thing disappeared after the box was destroyed
Another problem is that when you open the GUI again, if you close it on the mouse, it will still drop.
How can I intercept the event


#2

So this is due to the timings of the events. With DropItemEvent.Despense, the item has been taken out of the inventory and ready to be dispensed out, by cancelling this event you are cancelling the despense of the item however you are not cancelling the item being removed from the inventory.

I believe the order of events are
Pre (this is where the server knows that a drop from a inventory is going to happen but no action has yet happened.
Desruct (this is where the item will be removed from the inventory)
Dispense (already said) / Close (close is where an item is connected to your cursor and you close the inventory. It happens at the same time as dispense, it just depends on the action)

I can not work out exactly what you want, so i hope that helps. Please describe in as much detail as possible what your attempting to do if you want my help, or ask any question.


#3

Not that you’re wrong, but such behavior would be nonsensical - ClickInventoryEvent.Drop extends DropItemEvent.Dispense, so it should be caught by the same.


#4

Closing your inventory is not the same as Dispensing an Item.
I added DropItemEvent.Close in https://github.com/SpongePowered/SpongeAPI/commit/81ce267dc845e37caf3bed3f3fdc9432a8b14bf6
Before that it was DropItemEvent.Custom grouped together with other unknown drops (e.g. from mods).

All of those events extend the base DropItemEvent so you can use that too.

Note that canceling DropItemEvent.Close will delete the dropped item.