mirror of
https://github.com/Minestom/Minestom.git
synced 2025-02-10 01:11:25 +01:00
Switched TargetSelectors result caching from EntityCreature field to local one in GoalSelectors
This commit is contained in:
parent
1053cb33b1
commit
fbddffec32
@ -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.
|
||||
*
|
||||
|
@ -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;
|
||||
|
||||
|
@ -28,6 +28,7 @@ public class RangedAttackGoal extends GoalSelector {
|
||||
private BiFunction<Entity, Position, Projectile> 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;
|
||||
|
Loading…
Reference in New Issue
Block a user