Fix to Age, cancel navigation target if npc despawns

This commit is contained in:
fullwall 2012-09-10 19:16:37 +08:00
parent 6f211403c3
commit 3c5eb390a6
5 changed files with 28 additions and 21 deletions

View File

@ -172,7 +172,13 @@ public class CitizensNavigator implements Navigator {
}
public void update() {
if (!isNavigating() || !npc.isSpawned() || updateStationaryStatus())
if (!isNavigating())
return;
if (!npc.isSpawned()) {
stopNavigating(CancelReason.NPC_DESPAWNED);
return;
}
if (updateStationaryStatus())
return;
boolean finished = executing.update();
if (!finished)

View File

@ -31,6 +31,11 @@ public class MCNavigationStrategy implements PathStrategy {
cancelReason = CancelReason.STUCK;
}
@Override
public void clearCancelReason() {
cancelReason = null;
}
@Override
public CancelReason getCancelReason() {
return cancelReason;
@ -59,9 +64,4 @@ public class MCNavigationStrategy implements PathStrategy {
navigation.a(parameters.speed());
return navigation.f();
}
@Override
public void clearCancelReason() {
cancelReason = null;
}
}

View File

@ -39,6 +39,11 @@ public class MCTargetStrategy implements PathStrategy, EntityTarget {
&& distanceSquared() <= ATTACK_DISTANCE && handle.l(target);
}
@Override
public void clearCancelReason() {
cancelReason = null;
}
private double distanceSquared() {
return handle.getBukkitEntity().getLocation().distanceSquared(target.getBukkitEntity().getLocation());
}
@ -101,11 +106,6 @@ public class MCTargetStrategy implements PathStrategy, EntityTarget {
return false;
}
@Override
public void clearCancelReason() {
cancelReason = null;
}
private static final int ATTACK_DELAY_TICKS = 20;
private static final double ATTACK_DISTANCE = 1.75 * 1.75;
}

View File

@ -6,6 +6,8 @@ import net.citizensnpcs.api.ai.event.CancelReason;
import org.bukkit.Location;
public interface PathStrategy {
void clearCancelReason();
CancelReason getCancelReason();
Location getTargetAsLocation();
@ -15,6 +17,4 @@ public interface PathStrategy {
void stop();
boolean update();
void clearCancelReason();
}

View File

@ -11,7 +11,6 @@ import org.bukkit.entity.Ageable;
public class Age extends Trait implements Toggleable {
private int age = 0;
private boolean ageable = false;
private boolean locked = true;
public Age() {
@ -23,6 +22,10 @@ public class Age extends Trait implements Toggleable {
StringHelper.wrap(age), StringHelper.wrap(locked ? "is" : "isn't"));
}
private boolean isAgeable() {
return npc.getBukkitEntity() instanceof Ageable;
}
@Override
public void load(DataKey key) throws NPCLoadException {
if (npc.isSpawned() && !(npc.getBukkitEntity() instanceof Ageable))
@ -33,18 +36,16 @@ public class Age extends Trait implements Toggleable {
@Override
public void onSpawn() {
if (npc instanceof Ageable) {
if (isAgeable()) {
Ageable entity = (Ageable) npc.getBukkitEntity();
entity.setAge(age);
entity.setAgeLock(locked);
ageable = true;
} else
ageable = false;
}
}
@Override
public void run() {
if (!locked && ageable)
if (!locked && isAgeable())
age = ((Ageable) npc.getBukkitEntity()).getAge();
}
@ -56,14 +57,14 @@ public class Age extends Trait implements Toggleable {
public void setAge(int age) {
this.age = age;
if (ageable)
if (isAgeable())
((Ageable) npc.getBukkitEntity()).setAge(age);
}
@Override
public boolean toggle() {
locked = !locked;
if (ageable)
if (isAgeable())
((Ageable) npc.getBukkitEntity()).setAgeLock(locked);
return locked;
}