diff --git a/src/main/java/net/minestom/server/entity/ai/GoalSelector.java b/src/main/java/net/minestom/server/entity/ai/GoalSelector.java index 06bfd14fd..cc47581cf 100644 --- a/src/main/java/net/minestom/server/entity/ai/GoalSelector.java +++ b/src/main/java/net/minestom/server/entity/ai/GoalSelector.java @@ -60,23 +60,6 @@ public abstract class GoalSelector { return null; } - /** - * Finds a target for an entity. - * If the current one is not present, falls back to {@link GoalSelector#findTarget()} - * and updates the target inside the entity. - * - * @return the target entity, null if not found - */ - @Nullable - public Entity findAndUpdateTarget() { - Entity target = entityCreature.getTarget(); - if (target == null) { - target = findTarget(); - entityCreature.setTarget(target); - } - return target; - } - /** * Gets the entity behind the goal selector. * diff --git a/src/main/java/net/minestom/server/entity/ai/goal/MeleeAttackGoal.java b/src/main/java/net/minestom/server/entity/ai/goal/MeleeAttackGoal.java index 02e414f7f..2c46ec060 100644 --- a/src/main/java/net/minestom/server/entity/ai/goal/MeleeAttackGoal.java +++ b/src/main/java/net/minestom/server/entity/ai/goal/MeleeAttackGoal.java @@ -24,6 +24,7 @@ public class MeleeAttackGoal extends GoalSelector { private final int range; private boolean stop; + private Entity cachedTarget; /** * @param entityCreature the entity to add the goal to @@ -40,20 +41,25 @@ public class MeleeAttackGoal extends GoalSelector { @Override public boolean shouldStart() { - return findAndUpdateTarget() != null; + this.cachedTarget = findTarget(); + return this.cachedTarget != null; } @Override public void start() { - final Entity target = findAndUpdateTarget(); - Check.notNull(target, "The target is not expected to be null!"); - final Position targetPosition = target.getPosition(); + final Position targetPosition = this.cachedTarget.getPosition(); entityCreature.getNavigator().setPathTo(targetPosition); } @Override public void tick(long time) { - final Entity target = findAndUpdateTarget(); + Entity target; + if (this.cachedTarget != null) { + target = this.cachedTarget; + this.cachedTarget = null; + } else { + target = findTarget(); + } this.stop = target == null; diff --git a/src/main/java/net/minestom/server/entity/ai/goal/RangedAttackGoal.java b/src/main/java/net/minestom/server/entity/ai/goal/RangedAttackGoal.java index e8010d88d..ae3b979ae 100644 --- a/src/main/java/net/minestom/server/entity/ai/goal/RangedAttackGoal.java +++ b/src/main/java/net/minestom/server/entity/ai/goal/RangedAttackGoal.java @@ -28,6 +28,7 @@ public class RangedAttackGoal extends GoalSelector { private BiFunction projectileGenerator; private boolean stop; + private Entity cachedTarget; /** * @param entityCreature the entity to add the goal to. @@ -57,19 +58,24 @@ public class RangedAttackGoal extends GoalSelector { @Override public boolean shouldStart() { - return findAndUpdateTarget() != null; + this.cachedTarget = findTarget(); + return this.cachedTarget != null; } @Override public void start() { - Entity target = findAndUpdateTarget(); - Check.notNull(target, "The target is not expected to be null!"); - this.entityCreature.getNavigator().setPathTo(target.getPosition()); + this.entityCreature.getNavigator().setPathTo(this.cachedTarget.getPosition()); } @Override public void tick(long time) { - Entity target = findAndUpdateTarget(); + Entity target; + if (this.cachedTarget != null) { + target = this.cachedTarget; + this.cachedTarget = null; + } else { + target = findTarget(); + } if (target == null) { this.stop = true; return;