Add LinearWaypointsCompleteEvent

This commit is contained in:
fullwall 2012-11-02 17:32:11 +08:00
parent 6f1eb32445
commit 871259dcff
2 changed files with 61 additions and 15 deletions

View File

@ -322,8 +322,18 @@ public class LinearWaypointProvider implements WaypointProvider {
private GoalSelector selector;
private void ensureItr() {
if (itr == null || !itr.hasNext())
if (itr == null)
itr = waypoints.iterator();
else if (!itr.hasNext())
itr = getNewIterator();
}
private Iterator<Waypoint> getNewIterator() {
LinearWaypointsCompleteEvent event = new LinearWaypointsCompleteEvent(
LinearWaypointProvider.this, waypoints.iterator());
Bukkit.getPluginManager().callEvent(event);
Iterator<Waypoint> next = event.getNextWaypoints();
return next;
}
private Navigator getNavigator() {
@ -375,24 +385,20 @@ public class LinearWaypointProvider implements WaypointProvider {
@Override
public boolean shouldExecute(GoalSelector selector) {
if (paused || currentDestination != null || !npc.isSpawned() || getNavigator().isNavigating()
|| waypoints.size() == 0) {
return false;
}
if (waypoints.size() == 1) {
// avoid pathing to the same point repeatedly
Location dest = npc.getBukkitEntity().getLocation();
if (waypoints.get(0).getLocation().distanceSquared(dest) < 3)
if (paused || currentDestination != null || !npc.isSpawned() || getNavigator().isNavigating()) {
return false;
}
ensureItr();
boolean shouldExecute = itr.hasNext();
if (shouldExecute) {
if (!shouldExecute)
return false;
this.selector = selector;
currentDestination = itr.next();
Waypoint next = itr.next();
if (npc.getBukkitEntity().getLocation().distanceSquared(next.getLocation()) < 3)
return false;
currentDestination = next;
getNavigator().setTarget(currentDestination.getLocation());
}
return shouldExecute;
return true;
}
}
}

View File

@ -0,0 +1,40 @@
package net.citizensnpcs.trait.waypoint;
import java.util.Iterator;
import net.citizensnpcs.api.event.CitizensEvent;
import org.bukkit.event.HandlerList;
public class LinearWaypointsCompleteEvent extends CitizensEvent {
private Iterator<Waypoint> next;
private final WaypointProvider provider;
public LinearWaypointsCompleteEvent(WaypointProvider provider, Iterator<Waypoint> next) {
this.next = next;
this.provider = provider;
}
public WaypointProvider getWaypointProvider() {
return provider;
}
public Iterator<Waypoint> getNextWaypoints() {
return next;
}
public void setNextWaypoints(Iterator<Waypoint> waypoints) {
this.next = waypoints;
}
@Override
public HandlerList getHandlers() {
return handlers;
}
public static HandlerList getHandlerList() {
return handlers;
}
private static final HandlerList handlers = new HandlerList();
}