Remove waypoint provider goal when providers change

This commit is contained in:
fullwall 2013-10-21 00:13:03 +08:00
parent a0c6039e8e
commit 7d8389b736
5 changed files with 31 additions and 6 deletions

View File

@ -138,6 +138,11 @@ public class GuidedWaypointProvider implements WaypointProvider {
rebuildTree(); rebuildTree();
} }
@Override
public void onRemove() {
npc.getDefaultGoalController().removeGoal(currentGoal);
}
@Override @Override
public void onSpawn(NPC npc) { public void onSpawn(NPC npc) {
this.npc = npc; this.npc = npc;

View File

@ -86,6 +86,11 @@ public class LinearWaypointProvider implements WaypointProvider {
} }
} }
@Override
public void onRemove() {
npc.getDefaultGoalController().removeGoal(currentGoal);
}
@Override @Override
public void onSpawn(NPC npc) { public void onSpawn(NPC npc) {
this.npc = npc; this.npc = npc;

View File

@ -12,6 +12,7 @@ import org.bukkit.entity.Player;
public class WanderWaypointProvider implements WaypointProvider { public class WanderWaypointProvider implements WaypointProvider {
private Goal currentGoal; private Goal currentGoal;
private NPC npc;
private volatile boolean paused; private volatile boolean paused;
@Persist @Persist
private final int xrange = DEFAULT_XRANGE; private final int xrange = DEFAULT_XRANGE;
@ -42,8 +43,14 @@ public class WanderWaypointProvider implements WaypointProvider {
public void load(DataKey key) { public void load(DataKey key) {
} }
@Override
public void onRemove() {
npc.getDefaultGoalController().removeGoal(currentGoal);
}
@Override @Override
public void onSpawn(NPC npc) { public void onSpawn(NPC npc) {
this.npc = npc;
if (currentGoal == null) { if (currentGoal == null) {
currentGoal = WanderGoal.createWithNPCAndRange(npc, xrange, yrange); currentGoal = WanderGoal.createWithNPCAndRange(npc, xrange, yrange);
CitizensAPI.registerEvents(currentGoal); CitizensAPI.registerEvents(currentGoal);
@ -61,5 +68,6 @@ public class WanderWaypointProvider implements WaypointProvider {
} }
private static final int DEFAULT_XRANGE = 3; private static final int DEFAULT_XRANGE = 3;
private static final int DEFAULT_YRANGE = 25; private static final int DEFAULT_YRANGE = 25;
} }

View File

@ -33,6 +33,11 @@ public interface WaypointProvider {
*/ */
public void load(DataKey key); public void load(DataKey key);
/**
* Called when the provider is removed from the NPC.
*/
public void onRemove();
/** /**
* Called when the {@link NPC} attached to this provider is spawned. * Called when the {@link NPC} attached to this provider is spawned.
* *

View File

@ -81,8 +81,9 @@ public class Waypoints extends Trait {
@Override @Override
public void onSpawn() { public void onSpawn() {
if (provider != null) if (provider != null) {
provider.onSpawn(getNPC()); provider.onSpawn(getNPC());
}
} }
@Override @Override
@ -105,14 +106,15 @@ public class Waypoints extends Trait {
public boolean setWaypointProvider(String name) { public boolean setWaypointProvider(String name) {
name = name.toLowerCase(); name = name.toLowerCase();
Class<? extends WaypointProvider> clazz = providers.get(name); Class<? extends WaypointProvider> clazz = providers.get(name);
if (clazz == null) if (provider != null) {
return false; provider.onRemove();
provider = create(clazz); }
if (provider == null) if (clazz == null || (provider = create(clazz)) == null)
return false; return false;
providerName = name; providerName = name;
if (npc != null && npc.isSpawned()) if (npc != null && npc.isSpawned()) {
provider.onSpawn(npc); provider.onSpawn(npc);
}
return true; return true;
} }