mirror of
https://github.com/CitizensDev/Citizens2.git
synced 2025-01-15 12:41:20 +01:00
Weak references for NavigationCallbacks
This commit is contained in:
parent
185b2a05b0
commit
2f6d5d8ec3
@ -1,7 +1,6 @@
|
|||||||
package net.citizensnpcs;
|
package net.citizensnpcs;
|
||||||
|
|
||||||
import net.citizensnpcs.api.npc.NPC;
|
import net.citizensnpcs.api.npc.NPC;
|
||||||
import net.citizensnpcs.npc.CitizensNPC;
|
|
||||||
import net.citizensnpcs.npc.CitizensNPCManager;
|
import net.citizensnpcs.npc.CitizensNPCManager;
|
||||||
|
|
||||||
public class NPCUpdater implements Runnable {
|
public class NPCUpdater implements Runnable {
|
||||||
@ -16,7 +15,7 @@ public class NPCUpdater implements Runnable {
|
|||||||
for (NPC npc : npcManager) {
|
for (NPC npc : npcManager) {
|
||||||
if (!npc.isSpawned())
|
if (!npc.isSpawned())
|
||||||
continue;
|
continue;
|
||||||
((CitizensNPC) npc).update();
|
npc.update();
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
@ -1,6 +1,13 @@
|
|||||||
package net.citizensnpcs.editor;
|
package net.citizensnpcs.editor;
|
||||||
|
|
||||||
|
import org.bukkit.entity.Player;
|
||||||
|
|
||||||
public class PathEditor implements Editor {
|
public class PathEditor implements Editor {
|
||||||
|
private final Player player;
|
||||||
|
|
||||||
|
public PathEditor(Player player) {
|
||||||
|
this.player = player;
|
||||||
|
}
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
public void begin() {
|
public void begin() {
|
||||||
|
@ -1,5 +1,6 @@
|
|||||||
package net.citizensnpcs.npc.ai;
|
package net.citizensnpcs.npc.ai;
|
||||||
|
|
||||||
|
import java.lang.ref.WeakReference;
|
||||||
import java.util.Collections;
|
import java.util.Collections;
|
||||||
import java.util.Iterator;
|
import java.util.Iterator;
|
||||||
import java.util.List;
|
import java.util.List;
|
||||||
@ -19,7 +20,7 @@ import com.google.common.collect.Lists;
|
|||||||
public class CitizensAI implements AI {
|
public class CitizensAI implements AI {
|
||||||
private Runnable ai;
|
private Runnable ai;
|
||||||
private boolean paused;
|
private boolean paused;
|
||||||
private final List<NavigationCallback> callbacks = Lists.newArrayList();
|
private final List<WeakReference<NavigationCallback>> callbacks = Lists.newArrayList();
|
||||||
private PathStrategy executing;
|
private PathStrategy executing;
|
||||||
private final List<GoalEntry> executingGoals = Lists.newArrayList();
|
private final List<GoalEntry> executingGoals = Lists.newArrayList();
|
||||||
private final List<GoalEntry> goals = Lists.newArrayList();
|
private final List<GoalEntry> goals = Lists.newArrayList();
|
||||||
@ -56,7 +57,7 @@ public class CitizensAI implements AI {
|
|||||||
@Override
|
@Override
|
||||||
public void registerNavigationCallback(NavigationCallback callback) {
|
public void registerNavigationCallback(NavigationCallback callback) {
|
||||||
if (!callbacks.contains(callback))
|
if (!callbacks.contains(callback))
|
||||||
callbacks.add(callback);
|
callbacks.add(new WeakReference<NavigationCallback>(callback));
|
||||||
}
|
}
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
@ -67,9 +68,10 @@ public class CitizensAI implements AI {
|
|||||||
@Override
|
@Override
|
||||||
public void setDestination(Location destination) {
|
public void setDestination(Location destination) {
|
||||||
if (executing != null) {
|
if (executing != null) {
|
||||||
Iterator<NavigationCallback> itr = callbacks.iterator();
|
Iterator<WeakReference<NavigationCallback>> itr = callbacks.iterator();
|
||||||
while (itr.hasNext()) {
|
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();
|
itr.remove();
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
@ -80,17 +82,19 @@ public class CitizensAI implements AI {
|
|||||||
@Override
|
@Override
|
||||||
public void setTarget(LivingEntity target, boolean aggressive) {
|
public void setTarget(LivingEntity target, boolean aggressive) {
|
||||||
if (executing != null) {
|
if (executing != null) {
|
||||||
Iterator<NavigationCallback> itr = callbacks.iterator();
|
Iterator<WeakReference<NavigationCallback>> itr = callbacks.iterator();
|
||||||
while (itr.hasNext()) {
|
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();
|
itr.remove();
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
executing = new TargetStrategy(npc, target, aggressive);
|
executing = new TargetStrategy(npc, target, aggressive);
|
||||||
Iterator<NavigationCallback> itr = callbacks.iterator();
|
Iterator<WeakReference<NavigationCallback>> itr = callbacks.iterator();
|
||||||
while (itr.hasNext()) {
|
while (itr.hasNext()) {
|
||||||
if (itr.next().onBegin(this)) {
|
NavigationCallback next = itr.next().get();
|
||||||
|
if (next == null || next.onBegin(this)) {
|
||||||
itr.remove();
|
itr.remove();
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
@ -108,9 +112,10 @@ public class CitizensAI implements AI {
|
|||||||
if (paused)
|
if (paused)
|
||||||
return;
|
return;
|
||||||
if (executing != null && executing.update()) {
|
if (executing != null && executing.update()) {
|
||||||
Iterator<NavigationCallback> itr = callbacks.iterator();
|
Iterator<WeakReference<NavigationCallback>> itr = callbacks.iterator();
|
||||||
while (itr.hasNext()) {
|
while (itr.hasNext()) {
|
||||||
if (itr.next().onCompletion(this)) {
|
NavigationCallback next = itr.next().get();
|
||||||
|
if (next == null || next.onCompletion(this)) {
|
||||||
itr.remove();
|
itr.remove();
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
11
src/net/citizensnpcs/trait/Waypoint.java
Normal file
11
src/net/citizensnpcs/trait/Waypoint.java
Normal 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;
|
||||||
|
}
|
||||||
|
}
|
14
src/net/citizensnpcs/trait/WaypointProvider.java
Normal file
14
src/net/citizensnpcs/trait/WaypointProvider.java
Normal 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();
|
||||||
|
}
|
26
src/net/citizensnpcs/trait/Waypoints.java
Normal file
26
src/net/citizensnpcs/trait/Waypoints.java
Normal 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
|
||||||
|
}
|
||||||
|
}
|
Loading…
Reference in New Issue
Block a user