Add linear waypoint cycling

This commit is contained in:
fullwall 2020-06-12 19:29:28 +08:00
parent 67510c43a5
commit daa4d3cc25

View File

@ -55,6 +55,8 @@ public class LinearWaypointProvider implements EnumerableWaypointProvider {
@Persist @Persist
private boolean cachePaths = Setting.DEFAULT_CACHE_WAYPOINT_PATHS.asBoolean(); private boolean cachePaths = Setting.DEFAULT_CACHE_WAYPOINT_PATHS.asBoolean();
private LinearWaypointGoal currentGoal; private LinearWaypointGoal currentGoal;
@Persist
private boolean cycle = false;
private NPC npc; private NPC npc;
private final List<Waypoint> waypoints = Lists.newArrayList(); private final List<Waypoint> waypoints = Lists.newArrayList();
@ -287,6 +289,14 @@ public class LinearWaypointProvider implements EnumerableWaypointProvider {
togglePath(); togglePath();
} }
}); });
} else if (message.equalsIgnoreCase("cycle")) {
event.setCancelled(true);
Bukkit.getScheduler().scheduleSyncDelayedTask(CitizensAPI.getPlugin(), new Runnable() {
@Override
public void run() {
cycle = !cycle;
}
});
} }
} }
@ -394,6 +404,7 @@ public class LinearWaypointProvider implements EnumerableWaypointProvider {
} }
private class LinearWaypointGoal implements Goal { private class LinearWaypointGoal implements Goal {
private boolean ascending = true;
private final Location cachedLocation = new Location(null, 0, 0, 0); private final Location cachedLocation = new Location(null, 0, 0, 0);
private Waypoint currentDestination; private Waypoint currentDestination;
private Iterator<Waypoint> itr; private Iterator<Waypoint> itr;
@ -421,6 +432,29 @@ public class LinearWaypointProvider implements EnumerableWaypointProvider {
} }
private Iterator<Waypoint> getUnsafeIterator() { private Iterator<Waypoint> getUnsafeIterator() {
if (cycle && ascending) {
ascending = false;
return new Iterator<Waypoint>() {
int idx = waypoints.size() - 1;
@Override
public boolean hasNext() {
return idx >= 0 && idx < waypoints.size();
}
@Override
public Waypoint next() {
System.out.println(idx);
return waypoints.get(idx--);
}
@Override
public void remove() {
waypoints.remove(Math.max(0, idx - 1));
}
};
} else {
ascending = true;
return new Iterator<Waypoint>() { return new Iterator<Waypoint>() {
int idx = 0; int idx = 0;
@ -431,6 +465,7 @@ public class LinearWaypointProvider implements EnumerableWaypointProvider {
@Override @Override
public Waypoint next() { public Waypoint next() {
System.out.println(idx);
return waypoints.get(idx++); return waypoints.get(idx++);
} }
@ -440,6 +475,7 @@ public class LinearWaypointProvider implements EnumerableWaypointProvider {
} }
}; };
} }
}
public boolean isPaused() { public boolean isPaused() {
return paused; return paused;