From 8b711f86a3fc01ea9012efc47cc9b8c44d6d0775 Mon Sep 17 00:00:00 2001 From: Bjarne Koll Date: Fri, 17 Nov 2023 07:43:25 +0100 Subject: [PATCH] Call LivingEntity#onItemPickup before mutation (#9948) The existing EntityPickupItemEvent fixes patch moves the call to LivingEntity#onItemPickup for piglins after the respective EntityPickupItemEvent calls, which is correct. However the patch moved the call so far down the line that the existing logic already mutated the picked up item entity, leading to faulty state being passed to the onItemPickup method. To prevent logic in LivingEntity#onItemPickup to read from an ItemEntity that was already mutated, this commit moves the calls prior to the two respective mutations (either gold_nugget or rest). This was chosen above taking a copy of the original item and restoring state later on to avoid a full item stack clone. --- patches/server/EntityPickupItemEvent-fixes.patch | 12 +++++------- 1 file changed, 5 insertions(+), 7 deletions(-) diff --git a/patches/server/EntityPickupItemEvent-fixes.patch b/patches/server/EntityPickupItemEvent-fixes.patch index 8e20053376..a51631d5ef 100644 --- a/patches/server/EntityPickupItemEvent-fixes.patch +++ b/patches/server/EntityPickupItemEvent-fixes.patch @@ -36,18 +36,16 @@ index 0000000000000000000000000000000000000000..00000000000000000000000000000000 + // Paper start - fix event firing twice + if (drop.getItem().is(Items.GOLD_NUGGET) /* && !org.bukkit.craftbukkit.event.CraftEventFactory.callEntityPickupItemEvent(piglin, drop, 0, false).isCancelled() */) { + if (org.bukkit.craftbukkit.event.CraftEventFactory.callEntityPickupItemEvent(piglin, drop, 0, false).isCancelled()) return; ++ piglin.onItemPickup(drop); // Paper - moved from Piglin#pickUpItem - call prior to item entity modification + // Paper end piglin.take(drop, drop.getItem().getCount()); itemstack = drop.getItem(); drop.discard(); -@@ -0,0 +0,0 @@ public class PiglinAi { + } else if (!org.bukkit.craftbukkit.event.CraftEventFactory.callEntityPickupItemEvent(piglin, drop, drop.getItem().getCount() - 1, false).isCancelled()) { ++ piglin.onItemPickup(drop); // Paper - moved from Piglin#pickUpItem - call prior to item entity modification + piglin.take(drop, 1); + itemstack = PiglinAi.removeOneItemFromItemEntity(drop); } else { - return; - } -+ piglin.onItemPickup(drop); // Paper - moved from Piglin#pickUpItem - // CraftBukkit end - - if (PiglinAi.isLovedItem(itemstack, piglin)) { // CraftBukkit - Changes to allow for custom payment in bartering diff --git a/src/main/java/net/minecraft/world/entity/raid/Raider.java b/src/main/java/net/minecraft/world/entity/raid/Raider.java index 0000000000000000000000000000000000000000..0000000000000000000000000000000000000000 100644 --- a/src/main/java/net/minecraft/world/entity/raid/Raider.java