Weak references for NavigationCallbacks

This commit is contained in:
fullwall 2012-02-22 22:05:28 +08:00
parent 185b2a05b0
commit 2f6d5d8ec3
6 changed files with 74 additions and 12 deletions

View File

@ -1,7 +1,6 @@
package net.citizensnpcs;
import net.citizensnpcs.api.npc.NPC;
import net.citizensnpcs.npc.CitizensNPC;
import net.citizensnpcs.npc.CitizensNPCManager;
public class NPCUpdater implements Runnable {
@ -16,7 +15,7 @@ public class NPCUpdater implements Runnable {
for (NPC npc : npcManager) {
if (!npc.isSpawned())
continue;
((CitizensNPC) npc).update();
npc.update();
}
}
}

View File

@ -1,6 +1,13 @@
package net.citizensnpcs.editor;
import org.bukkit.entity.Player;
public class PathEditor implements Editor {
private final Player player;
public PathEditor(Player player) {
this.player = player;
}
@Override
public void begin() {

View File

@ -1,5 +1,6 @@
package net.citizensnpcs.npc.ai;
import java.lang.ref.WeakReference;
import java.util.Collections;
import java.util.Iterator;
import java.util.List;
@ -19,7 +20,7 @@ import com.google.common.collect.Lists;
public class CitizensAI implements AI {
private Runnable ai;
private boolean paused;
private final List<NavigationCallback> callbacks = Lists.newArrayList();
private final List<WeakReference<NavigationCallback>> callbacks = Lists.newArrayList();
private PathStrategy executing;
private final List<GoalEntry> executingGoals = Lists.newArrayList();
private final List<GoalEntry> goals = Lists.newArrayList();
@ -56,7 +57,7 @@ public class CitizensAI implements AI {
@Override
public void registerNavigationCallback(NavigationCallback callback) {
if (!callbacks.contains(callback))
callbacks.add(callback);
callbacks.add(new WeakReference<NavigationCallback>(callback));
}
@Override
@ -67,9 +68,10 @@ public class CitizensAI implements AI {
@Override
public void setDestination(Location destination) {
if (executing != null) {
Iterator<NavigationCallback> itr = callbacks.iterator();
Iterator<WeakReference<NavigationCallback>> itr = callbacks.iterator();
while (itr.hasNext()) {
if (itr.next().onCancel(this, PathCancelReason.PLUGIN)) {
NavigationCallback next = itr.next().get();
if (next == null || next.onCancel(this, PathCancelReason.PLUGIN)) {
itr.remove();
}
}
@ -80,17 +82,19 @@ public class CitizensAI implements AI {
@Override
public void setTarget(LivingEntity target, boolean aggressive) {
if (executing != null) {
Iterator<NavigationCallback> itr = callbacks.iterator();
Iterator<WeakReference<NavigationCallback>> itr = callbacks.iterator();
while (itr.hasNext()) {
if (itr.next().onCancel(this, PathCancelReason.PLUGIN)) {
NavigationCallback next = itr.next().get();
if (next == null || next.onCancel(this, PathCancelReason.PLUGIN)) {
itr.remove();
}
}
}
executing = new TargetStrategy(npc, target, aggressive);
Iterator<NavigationCallback> itr = callbacks.iterator();
Iterator<WeakReference<NavigationCallback>> itr = callbacks.iterator();
while (itr.hasNext()) {
if (itr.next().onBegin(this)) {
NavigationCallback next = itr.next().get();
if (next == null || next.onBegin(this)) {
itr.remove();
}
}
@ -108,9 +112,10 @@ public class CitizensAI implements AI {
if (paused)
return;
if (executing != null && executing.update()) {
Iterator<NavigationCallback> itr = callbacks.iterator();
Iterator<WeakReference<NavigationCallback>> itr = callbacks.iterator();
while (itr.hasNext()) {
if (itr.next().onCompletion(this)) {
NavigationCallback next = itr.next().get();
if (next == null || next.onCompletion(this)) {
itr.remove();
}
}

View File

@ -0,0 +1,11 @@
package net.citizensnpcs.trait;
import org.bukkit.Location;
public class Waypoint {
private final Location location;
public Waypoint(Location location) {
this.location = location;
}
}

View File

@ -0,0 +1,14 @@
package net.citizensnpcs.trait;
import net.citizensnpcs.api.npc.ai.NavigationCallback;
import net.citizensnpcs.editor.Editor;
import org.bukkit.entity.Player;
public interface WaypointProvider {
public Editor createEditor(Player player);
public void addWaypoint(Waypoint waypoint);
public NavigationCallback getCallback();
}

View File

@ -0,0 +1,26 @@
package net.citizensnpcs.trait;
import net.citizensnpcs.api.exception.NPCLoadException;
import net.citizensnpcs.api.npc.NPC;
import net.citizensnpcs.api.npc.trait.SaveId;
import net.citizensnpcs.api.npc.trait.Trait;
import net.citizensnpcs.api.util.DataKey;
@SaveId("waypoints")
public class Waypoints extends Trait {
private final NPC npc;
private WaypointProvider provider;
public Waypoints(NPC npc) {
this.npc = npc;
}
@Override
public void load(DataKey key) throws NPCLoadException {
}
@Override
public void save(DataKey key) {
// TODO Auto-generated method stub
}
}