Skip to content

Commit

Permalink
Only erase allay memory on non-item targets (#9570)
Browse files Browse the repository at this point in the history
* Only erase allay memory on non-item targets

Spigot incorrectly instanceOf checks the EntityTargetEvent#getTarget
against the internal ItemEntity type and removes the nearest wanted item
memory if said instanceOf check fails, (which is always the case)
causing allays to behave differently as they constantly loose their
target item.

This commit fixes the faulty behaviour by instance performing a check
against the CraftItem type.

* Reduce diff

* fix typo

---------

Co-authored-by: Jake Potrebic <jake.m.potrebic@gmail.com>
  • Loading branch information
lynxplay and Machine-Maker authored Aug 6, 2023
1 parent 2fa8efc commit 508a295
Show file tree
Hide file tree
Showing 2 changed files with 31 additions and 2 deletions.
4 changes: 2 additions & 2 deletions patches/server/0707-Remove-streams-for-villager-AI.patch
Original file line number Diff line number Diff line change
Expand Up @@ -112,15 +112,15 @@ index 731ef21dbbd25d6924717de42f4569a9b5935643..fe3ab3d388f0481fb0db06b7f730f868
private final boolean isUnsafe; // Paper

diff --git a/src/main/java/net/minecraft/world/entity/ai/sensing/NearestItemSensor.java b/src/main/java/net/minecraft/world/entity/ai/sensing/NearestItemSensor.java
index 1dfcc5cba6ffb463acf161a23fff1ca452184290..2c4517850a9692f1c2b1332b58e8312fe1166772 100644
index 1dfcc5cba6ffb463acf161a23fff1ca452184290..61a164c5bfc86faa3f4d04a66e0257016cfd937d 100644
--- a/src/main/java/net/minecraft/world/entity/ai/sensing/NearestItemSensor.java
+++ b/src/main/java/net/minecraft/world/entity/ai/sensing/NearestItemSensor.java
@@ -25,13 +25,16 @@ public class NearestItemSensor extends Sensor<Mob> {
protected void doTick(ServerLevel world, Mob entity) {
Brain<?> brain = entity.getBrain();
List<ItemEntity> list = world.getEntitiesOfClass(ItemEntity.class, entity.getBoundingBox().inflate(32.0D, 16.0D, 32.0D), (itemEntity) -> {
- return true;
+ return itemEntity.closerThan(entity, 9.0D) && entity.wantsToPickUp(itemEntity.getItem()); // Paper - move predicate into getEntities
+ return itemEntity.closerThan(entity, MAX_DISTANCE_TO_WANTED_ITEM) && entity.wantsToPickUp(itemEntity.getItem()); // Paper - move predicate into getEntities
});
- list.sort(Comparator.comparingDouble(entity::distanceToSqr));
+ list.sort((e1, e2) -> Double.compare(entity.distanceToSqr(e1), entity.distanceToSqr(e2))); // better to take the sort perf hit than using line of sight more than we need to.
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
From 0000000000000000000000000000000000000000 Mon Sep 17 00:00:00 2001
From: Bjarne Koll <lynxplay101@gmail.com>
Date: Fri, 4 Aug 2023 15:53:36 +0200
Subject: [PATCH] Only erase allay memory on non-item targets

Spigot incorrectly instanceOf checks the EntityTargetEvent#getTarget
against the internal ItemEntity type and removes the nearest wanted item
memory if said instanceOf check fails, (which is always the case)
causing allays to behave differently as they constantly loose their
target item.

This commit fixes the faulty behaviour by instance performing a check
against the CraftItem type.

diff --git a/src/main/java/net/minecraft/world/entity/ai/behavior/GoToWantedItem.java b/src/main/java/net/minecraft/world/entity/ai/behavior/GoToWantedItem.java
index bd9aa4a5443da862be3403c1941113373741a87c..2f92eb39cde7b30a894db347ca85a506d880411e 100644
--- a/src/main/java/net/minecraft/world/entity/ai/behavior/GoToWantedItem.java
+++ b/src/main/java/net/minecraft/world/entity/ai/behavior/GoToWantedItem.java
@@ -35,8 +35,9 @@ public class GoToWantedItem {
if (event.isCancelled()) {
return false;
}
- if (!(event.getTarget() instanceof ItemEntity)) {
+ if (!(event.getTarget() instanceof org.bukkit.craftbukkit.entity.CraftItem)) { // Paper - only erase allay memory on non-item targets
memoryaccessor2.erase();
+ return false; // Paper - only erase allay memory on non-item targets
}

entityitem = (ItemEntity) ((org.bukkit.craftbukkit.entity.CraftEntity) event.getTarget()).getHandle();

0 comments on commit 508a295

Please sign in to comment.