Chest content can be lost when ItemChestToCrateUpgrade is used
maxanier opened this issue ยท 0 comments
Chest content is lost when ItemChestToCrateUpgrade is used and the BlockEvent.PlaceEvent, which is fired during this process, is canceled.
See #752:
Main issue: The BlockPlace event is fired after Item#onItemUse
in ForgeHooks#onPlaceItemIntoWorld
the item placed the block. If the event is canceled by any mod Minecraft restores the Block and the TileEntity.
But since the inventory of the old tile was cleared/ the tile was removed before it was stored (during setBockState of the new chest), it's inventory is empty and all items are lost.
My suggestion:
If you move the code from Item#onItemUse (which is interpreted as onPlaceItemIntoWorld by Forge) to Item#onItemUseFirst.
This one is called very early when the item is right clicked and the action done here cannot be undone by canceling any event.This would solve the problem of the chest content being lost if any mod cancels the PlaceEvent.
I think this also affects Protection Plugins for SpongeForge or similar as they also cancel the PlaceEvent (as far as I can tell from SpongeForge's code).
If this code is moved to #onItemUseFirst they would not be able to prevent the block change, but it would prevent losing the items. (And if you are the owner of a protected chest on a server, you probably prefer a improved chest over losing your items).This solution is not perfect, but probably better than the current one.