Merge pull request #148 from RinesThaix/goals

Switched TargetSelectors result caching from EntityCreature field to local one in GoalSelectors
This commit is contained in:
TheMode 2021-02-22 15:15:18 +01:00 committed by GitHub
commit 7bd9283e17
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
3 changed files with 22 additions and 27 deletions

View File

@ -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.
*

View File

@ -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;

View File

@ -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;