Prevent keep inventory deaths from dropping open container/cursor items
When a player has an open inventory—such as with a merchant, anvil or their own crafting grid—and places items in it, or has an item on their cursor, closing the inventory typically returns those items to the player's main inventory. However, if the player dies with the inventory still open, the items are dropped onto the ground.
This usually isn't a problem, as their entire inventory would be dropped anyway. But when keep inventory is enabled, these items should return directly to the player's inventory to prevent valuable items from being accidentally dropped—especially in spawn areas where keep inventory is active.
This patch makes the items return directly to the inventory if they die with keep inventory.
Before: Dying with item in merchant inventory while in inventory protected area (item drops): https://github.com/user-attachments/assets/bf14d5a0-6e20-4a7d-b240-578d98d2074e
Dying with item on cursor while in inventory protected area (item drops): https://github.com/user-attachments/assets/f9da24f3-6128-4d32-b580-e6fd22a4f13e
Dying with item in anvil inventory while in inventory protected area (item drops): https://github.com/user-attachments/assets/05013e61-ab6e-4831-a3a4-a4698aa18af5
Dying with item in crafting grid while in inventory protected area (item drops): https://github.com/user-attachments/assets/bc11af09-a1c9-478c-a726-c8799adecd45
After (with config option enabled)
Dying with item in merchant inventory while in inventory protected area (item returned to inventory): https://github.com/user-attachments/assets/9d32951f-1a79-4c95-8d41-05864c89c27c
Dying with item on cursor while in inventory protected area (item returned to inventory): https://github.com/user-attachments/assets/a692f718-9817-4c69-9c44-8b1dc9d1169e
Dying with item in anvil inventory while in inventory protected area (item returned to inventory): https://github.com/user-attachments/assets/d453f48e-b47a-48cd-879b-c23896ef470e
Dying with item in crafting grid while in inventory protected area (item returned to inventory): https://github.com/user-attachments/assets/25a9485a-e574-484f-9a69-bad76c7434d1
The only inventory this doesn't apply to is beacons, because they never return items to inventory and always drop (see: https://bugs.mojang.com/browse/MC-177476)
@Override // where is placeItemBackInInventory here?? MC-177476
public void removed(Player player) {
super.removed(player);
if (!player.level().isClientSide) {
ItemStack itemstack = this.paymentSlot.remove(this.paymentSlot.getMaxStackSize());
if (!itemstack.isEmpty()) {
player.drop(itemstack, false);
}
}
}
I could easily add a fix for this to the patch and add keep inventory support, what do you think?
ye this would be helpful
I have just seem the changes for 1.21.3 and this PR patch will definitely need rewritten, gonna wait a couple weeks for 1.21.3 to be stable.
Fun side fact, this is now reversed in 1.21.3. Still needs addressing tho, just this PR approach is not applicable until the bug (originating with spigot not correctly removing server players on death/respawn etc) is fixed to actually drop the items.