Skip to content

Commit

Permalink
Remove waypoint provider goal when providers change
Browse files Browse the repository at this point in the history
  • Loading branch information
fullwall committed Oct 20, 2013
1 parent 01c7338 commit a7a6b54
Show file tree
Hide file tree
Showing 5 changed files with 31 additions and 6 deletions.
Expand Up @@ -138,6 +138,11 @@ public void load(DataKey key) {
rebuildTree();
}

@Override
public void onRemove() {
npc.getDefaultGoalController().removeGoal(currentGoal);
}

@Override
public void onSpawn(NPC npc) {
this.npc = npc;
Expand Down
Expand Up @@ -86,6 +86,11 @@ public void load(DataKey key) {
}
}

@Override
public void onRemove() {
npc.getDefaultGoalController().removeGoal(currentGoal);
}

@Override
public void onSpawn(NPC npc) {
this.npc = npc;
Expand Down
Expand Up @@ -12,6 +12,7 @@

public class WanderWaypointProvider implements WaypointProvider {
private Goal currentGoal;
private NPC npc;
private volatile boolean paused;
@Persist
private final int xrange = DEFAULT_XRANGE;
Expand Down Expand Up @@ -42,8 +43,14 @@ public boolean isPaused() {
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);
Expand All @@ -61,5 +68,6 @@ public void setPaused(boolean paused) {
}

private static final int DEFAULT_XRANGE = 3;

private static final int DEFAULT_YRANGE = 25;
}
Expand Up @@ -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.
*
Expand Down
14 changes: 8 additions & 6 deletions src/main/java/net/citizensnpcs/trait/waypoint/Waypoints.java
Expand Up @@ -81,8 +81,9 @@ public void load(DataKey key) throws NPCLoadException {

@Override
public void onSpawn() {
if (provider != null)
if (provider != null) {
provider.onSpawn(getNPC());
}
}

@Override
Expand All @@ -105,14 +106,15 @@ public void save(DataKey key) {
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;
}

Expand Down

0 comments on commit a7a6b54

Please sign in to comment.