From daa4d3cc25e0741555b29efda85a82b7093970a8 Mon Sep 17 00:00:00 2001 From: fullwall Date: Fri, 12 Jun 2020 19:29:28 +0800 Subject: [PATCH] Add linear waypoint cycling --- .../waypoint/LinearWaypointProvider.java | 66 ++++++++++++++----- 1 file changed, 51 insertions(+), 15 deletions(-) diff --git a/main/src/main/java/net/citizensnpcs/trait/waypoint/LinearWaypointProvider.java b/main/src/main/java/net/citizensnpcs/trait/waypoint/LinearWaypointProvider.java index 6b43f2b2b..7558e6257 100644 --- a/main/src/main/java/net/citizensnpcs/trait/waypoint/LinearWaypointProvider.java +++ b/main/src/main/java/net/citizensnpcs/trait/waypoint/LinearWaypointProvider.java @@ -55,6 +55,8 @@ public class LinearWaypointProvider implements EnumerableWaypointProvider { @Persist private boolean cachePaths = Setting.DEFAULT_CACHE_WAYPOINT_PATHS.asBoolean(); private LinearWaypointGoal currentGoal; + @Persist + private boolean cycle = false; private NPC npc; private final List waypoints = Lists.newArrayList(); @@ -287,6 +289,14 @@ public class LinearWaypointProvider implements EnumerableWaypointProvider { 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 boolean ascending = true; private final Location cachedLocation = new Location(null, 0, 0, 0); private Waypoint currentDestination; private Iterator itr; @@ -421,24 +432,49 @@ public class LinearWaypointProvider implements EnumerableWaypointProvider { } private Iterator getUnsafeIterator() { - return new Iterator() { - int idx = 0; + if (cycle && ascending) { + ascending = false; + return new Iterator() { + int idx = waypoints.size() - 1; - @Override - public boolean hasNext() { - return idx < waypoints.size(); - } + @Override + public boolean hasNext() { + return idx >= 0 && idx < waypoints.size(); + } - @Override - public Waypoint next() { - return waypoints.get(idx++); - } + @Override + public Waypoint next() { + System.out.println(idx); + return waypoints.get(idx--); + } - @Override - public void remove() { - waypoints.remove(Math.max(0, idx - 1)); - } - }; + @Override + public void remove() { + waypoints.remove(Math.max(0, idx - 1)); + } + }; + } else { + ascending = true; + return new Iterator() { + int idx = 0; + + @Override + public boolean hasNext() { + return 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)); + } + }; + } } public boolean isPaused() {