Remove waypoint provider goal when providers change

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

View File

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

View File

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

View File

@ -33,6 +33,11 @@ public interface WaypointProvider {
*/
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.
*

View File

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