EntityPickupItemEvent fixes

Fixes double firing of the event in PiglinAi

Fixes cancelling the event for piglins still triggering the
advancement trigger

Fires the event when a Raider tries to pick up a raid banner
to become raid leader.
This commit is contained in:
Jake Potrebic 2022-07-04 21:45:36 -07:00
parent 3f8bb2073a
commit 945f6ef6e6
3 changed files with 48 additions and 22 deletions

View File

@ -120,6 +120,15 @@
return flag && !flag1 ? true : (!flag && flag1 ? false : super.canReplaceCurrentItem(newStack, currentStack, slot)); return flag && !flag1 ? true : (!flag && flag1 ? false : super.canReplaceCurrentItem(newStack, currentStack, slot));
} }
@@ -410,7 +438,7 @@
@Override
protected void pickUpItem(ServerLevel world, ItemEntity itemEntity) {
- this.onItemPickup(itemEntity);
+ // this.onItemPickup(itemEntity); // Paper - EntityPickupItemEvent fixes; call in PiglinAi#pickUpItem after EntityPickupItemEvent is fired
PiglinAi.pickUpItem(world, this, itemEntity);
}
@@ -431,7 +459,7 @@ @@ -431,7 +459,7 @@
@Override @Override

View File

@ -50,19 +50,26 @@
Objects.requireNonNull(piglin); Objects.requireNonNull(piglin);
optional.ifPresent(piglin::makeSound); optional.ifPresent(piglin::makeSound);
@@ -235,23 +243,27 @@ @@ -235,23 +243,32 @@
PiglinAi.stopWalking(piglin); PiglinAi.stopWalking(piglin);
ItemStack itemstack; ItemStack itemstack;
- if (itemEntity.getItem().is(Items.GOLD_NUGGET)) { - if (itemEntity.getItem().is(Items.GOLD_NUGGET)) {
+ // CraftBukkit start - piglin.take(itemEntity, itemEntity.getItem().getCount());
+ if (itemEntity.getItem().is(Items.GOLD_NUGGET) && !org.bukkit.craftbukkit.event.CraftEventFactory.callEntityPickupItemEvent(piglin, itemEntity, 0, false).isCancelled()) { - itemstack = itemEntity.getItem();
piglin.take(itemEntity, itemEntity.getItem().getCount());
itemstack = itemEntity.getItem();
- itemEntity.discard(); - itemEntity.discard();
- } else { - } else {
+ // CraftBukkit start
+ // Paper start - EntityPickupItemEvent fixes; fix event firing twice
+ if (itemEntity.getItem().is(Items.GOLD_NUGGET)/* && !org.bukkit.craftbukkit.event.CraftEventFactory.callEntityPickupItemEvent(piglin, itemEntity, 0, false).isCancelled()*/) { // Paper
+ if (!org.bukkit.craftbukkit.event.CraftEventFactory.callEntityPickupItemEvent(piglin, itemEntity, 0, false).isCancelled()) return;
+ piglin.onItemPickup(itemEntity); // Paper - moved from Piglin#pickUpItem - call prior to item entity modification
+ // Paper end
+ piglin.take(itemEntity, itemEntity.getItem().getCount());
+ itemstack = itemEntity.getItem();
+ itemEntity.discard(EntityRemoveEvent.Cause.PICKUP); // CraftBukkit - add Bukkit remove cause + itemEntity.discard(EntityRemoveEvent.Cause.PICKUP); // CraftBukkit - add Bukkit remove cause
+ } else if (!org.bukkit.craftbukkit.event.CraftEventFactory.callEntityPickupItemEvent(piglin, itemEntity, itemEntity.getItem().getCount() - 1, false).isCancelled()) { + } else if (!org.bukkit.craftbukkit.event.CraftEventFactory.callEntityPickupItemEvent(piglin, itemEntity, itemEntity.getItem().getCount() - 1, false).isCancelled()) {
+ piglin.onItemPickup(itemEntity); // Paper - EntityPickupItemEvent fixes; moved from Piglin#pickUpItem - call prior to item entity modification
piglin.take(itemEntity, 1); piglin.take(itemEntity, 1);
itemstack = PiglinAi.removeOneItemFromItemEntity(itemEntity); itemstack = PiglinAi.removeOneItemFromItemEntity(itemEntity);
+ } else { + } else {
@ -79,11 +86,11 @@
PiglinAi.eat(piglin); PiglinAi.eat(piglin);
} else { } else {
- boolean flag = !piglin.equipItemIfPossible(world, itemstack).equals(ItemStack.EMPTY); - boolean flag = !piglin.equipItemIfPossible(world, itemstack).equals(ItemStack.EMPTY);
+ boolean flag = !piglin.equipItemIfPossible(world, itemstack, itemEntity).equals(ItemStack.EMPTY); // CraftBukkit + boolean flag = !piglin.equipItemIfPossible(world, itemstack, null).equals(ItemStack.EMPTY); // CraftBukkit // Paper - pass null item entity to prevent duplicate pickup item event call - called above.
if (!flag) { if (!flag) {
PiglinAi.putInInventory(piglin, itemstack); PiglinAi.putInInventory(piglin, itemstack);
@@ -261,7 +273,9 @@ @@ -261,7 +278,9 @@
private static void holdInOffhand(ServerLevel world, Piglin piglin, ItemStack stack) { private static void holdInOffhand(ServerLevel world, Piglin piglin, ItemStack stack) {
if (PiglinAi.isHoldingItemInOffHand(piglin)) { if (PiglinAi.isHoldingItemInOffHand(piglin)) {
@ -93,7 +100,7 @@
} }
piglin.holdInOffHand(stack); piglin.holdInOffHand(stack);
@@ -272,7 +286,7 @@ @@ -272,7 +291,7 @@
ItemStack itemstack1 = itemstack.split(1); ItemStack itemstack1 = itemstack.split(1);
if (itemstack.isEmpty()) { if (itemstack.isEmpty()) {
@ -102,7 +109,7 @@
} else { } else {
stack.setItem(itemstack); stack.setItem(itemstack);
} }
@@ -287,9 +301,14 @@ @@ -287,9 +306,14 @@
boolean flag1; boolean flag1;
if (piglin.isAdult()) { if (piglin.isAdult()) {
@ -119,7 +126,7 @@
} else if (!flag1) { } else if (!flag1) {
boolean flag2 = !piglin.equipItemIfPossible(world, itemstack).isEmpty(); boolean flag2 = !piglin.equipItemIfPossible(world, itemstack).isEmpty();
@@ -302,7 +321,7 @@ @@ -302,7 +326,7 @@
if (!flag1) { if (!flag1) {
ItemStack itemstack1 = piglin.getMainHandItem(); ItemStack itemstack1 = piglin.getMainHandItem();
@ -128,7 +135,7 @@
PiglinAi.putInInventory(piglin, itemstack1); PiglinAi.putInInventory(piglin, itemstack1);
} else { } else {
PiglinAi.throwItems(piglin, Collections.singletonList(itemstack1)); PiglinAi.throwItems(piglin, Collections.singletonList(itemstack1));
@@ -316,7 +335,9 @@ @@ -316,7 +340,9 @@
protected static void cancelAdmiring(ServerLevel world, Piglin piglin) { protected static void cancelAdmiring(ServerLevel world, Piglin piglin) {
if (PiglinAi.isAdmiringItem(piglin) && !piglin.getOffhandItem().isEmpty()) { if (PiglinAi.isAdmiringItem(piglin) && !piglin.getOffhandItem().isEmpty()) {
@ -138,7 +145,7 @@
piglin.setItemInHand(InteractionHand.OFF_HAND, ItemStack.EMPTY); piglin.setItemInHand(InteractionHand.OFF_HAND, ItemStack.EMPTY);
} }
@@ -379,15 +400,21 @@ @@ -379,15 +405,21 @@
return false; return false;
} else if (PiglinAi.isAdmiringDisabled(piglin) && piglin.getBrain().hasMemoryValue(MemoryModuleType.ATTACK_TARGET)) { } else if (PiglinAi.isAdmiringDisabled(piglin) && piglin.getBrain().hasMemoryValue(MemoryModuleType.ATTACK_TARGET)) {
return false; return false;
@ -162,7 +169,7 @@
protected static boolean isLovedItem(ItemStack stack) { protected static boolean isLovedItem(ItemStack stack) {
return stack.is(ItemTags.PIGLIN_LOVED); return stack.is(ItemTags.PIGLIN_LOVED);
} }
@@ -451,6 +478,7 @@ @@ -451,6 +483,7 @@
} }
public static void angerNearbyPiglins(ServerLevel world, Player player, boolean blockOpen) { public static void angerNearbyPiglins(ServerLevel world, Player player, boolean blockOpen) {
@ -170,7 +177,7 @@
List<Piglin> list = player.level().getEntitiesOfClass(Piglin.class, player.getBoundingBox().inflate(16.0D)); List<Piglin> list = player.level().getEntitiesOfClass(Piglin.class, player.getBoundingBox().inflate(16.0D));
list.stream().filter(PiglinAi::isIdle).filter((entitypiglin) -> { list.stream().filter(PiglinAi::isIdle).filter((entitypiglin) -> {
@@ -481,7 +509,7 @@ @@ -481,7 +514,7 @@
} }
protected static boolean canAdmire(Piglin piglin, ItemStack nearbyItems) { protected static boolean canAdmire(Piglin piglin, ItemStack nearbyItems) {
@ -179,7 +186,7 @@
} }
protected static void wasHurtBy(ServerLevel world, Piglin piglin, LivingEntity attacker) { protected static void wasHurtBy(ServerLevel world, Piglin piglin, LivingEntity attacker) {
@@ -735,6 +763,12 @@ @@ -735,6 +768,12 @@
return entity.getBrain().hasMemoryValue(MemoryModuleType.ADMIRING_ITEM); return entity.getBrain().hasMemoryValue(MemoryModuleType.ADMIRING_ITEM);
} }
@ -192,7 +199,7 @@
private static boolean isBarterCurrency(ItemStack stack) { private static boolean isBarterCurrency(ItemStack stack) {
return stack.is(PiglinAi.BARTERING_ITEM); return stack.is(PiglinAi.BARTERING_ITEM);
} }
@@ -772,7 +806,7 @@ @@ -772,7 +811,7 @@
} }
private static boolean isNotHoldingLovedItemInOffHand(Piglin piglin) { private static boolean isNotHoldingLovedItemInOffHand(Piglin piglin) {

View File

@ -10,7 +10,17 @@
public abstract class Raider extends PatrollingMonster { public abstract class Raider extends PatrollingMonster {
@@ -230,13 +233,15 @@ @@ -225,18 +228,25 @@
boolean flag = this.hasActiveRaid() && this.getCurrentRaid().getLeader(this.getWave()) != null;
if (this.hasActiveRaid() && !flag && ItemStack.matches(itemstack, Raid.getOminousBannerInstance(this.registryAccess().lookupOrThrow(Registries.BANNER_PATTERN)))) {
+ // Paper start - EntityPickupItemEvent fixes
+ if (org.bukkit.craftbukkit.event.CraftEventFactory.callEntityPickupItemEvent(this, itemEntity, 0, false).isCancelled()) {
+ return;
+ }
+ // Paper end - EntityPickupItemEvent fixes
EquipmentSlot enumitemslot = EquipmentSlot.HEAD;
ItemStack itemstack1 = this.getItemBySlot(enumitemslot);
double d0 = (double) this.getEquipmentDropChance(enumitemslot); double d0 = (double) this.getEquipmentDropChance(enumitemslot);
if (!itemstack1.isEmpty() && (double) Math.max(this.random.nextFloat() - 0.1F, 0.0F) < d0) { if (!itemstack1.isEmpty() && (double) Math.max(this.random.nextFloat() - 0.1F, 0.0F) < d0) {
@ -27,7 +37,7 @@
this.getCurrentRaid().setLeader(this.getWave(), this); this.getCurrentRaid().setLeader(this.getWave(), this);
this.setPatrolLeader(true); this.setPatrolLeader(true);
} else { } else {
@@ -290,7 +295,7 @@ @@ -290,7 +300,7 @@
@Nullable @Nullable
private ItemEntity pursuedBannerItemEntity; private ItemEntity pursuedBannerItemEntity;
@ -36,7 +46,7 @@
this.mob = entityraider; this.mob = entityraider;
this.setFlags(EnumSet.of(Goal.Flag.MOVE)); this.setFlags(EnumSet.of(Goal.Flag.MOVE));
} }
@@ -335,6 +340,7 @@ @@ -335,6 +345,7 @@
} }
private boolean cannotPickUpBanner() { private boolean cannotPickUpBanner() {
@ -44,7 +54,7 @@
if (!this.mob.hasActiveRaid()) { if (!this.mob.hasActiveRaid()) {
return true; return true;
} else if (this.mob.getCurrentRaid().isOver()) { } else if (this.mob.getCurrentRaid().isOver()) {
@@ -518,7 +524,7 @@ @@ -518,7 +529,7 @@
} }
} }
@ -53,7 +63,7 @@
private final Raider mob; private final Raider mob;
private final float hostileRadiusSqr; private final float hostileRadiusSqr;
@@ -547,7 +553,7 @@ @@ -547,7 +558,7 @@
while (iterator.hasNext()) { while (iterator.hasNext()) {
Raider entityraider = (Raider) iterator.next(); Raider entityraider = (Raider) iterator.next();
@ -62,7 +72,7 @@
} }
} }
@@ -564,7 +570,7 @@ @@ -564,7 +575,7 @@
while (iterator.hasNext()) { while (iterator.hasNext()) {
Raider entityraider = (Raider) iterator.next(); Raider entityraider = (Raider) iterator.next();