diff --git a/src/main/java/baritone/behavior/impl/PathingBehavior.java b/src/main/java/baritone/behavior/impl/PathingBehavior.java index 8b43d2f6..25eb89ef 100644 --- a/src/main/java/baritone/behavior/impl/PathingBehavior.java +++ b/src/main/java/baritone/behavior/impl/PathingBehavior.java @@ -18,11 +18,11 @@ package baritone.behavior.impl; import baritone.Baritone; -import baritone.behavior.Behavior; import baritone.api.event.events.PathEvent; import baritone.api.event.events.PlayerUpdateEvent; import baritone.api.event.events.RenderEvent; import baritone.api.event.events.TickEvent; +import baritone.behavior.Behavior; import baritone.pathing.calc.AStarPathFinder; import baritone.pathing.calc.AbstractNodeCostSearch; import baritone.pathing.calc.IPathFinder; @@ -194,6 +194,7 @@ public class PathingBehavior extends Behavior { current = null; next = null; Baritone.INSTANCE.getInputOverrideHandler().clearAllKeys(); + AbstractNodeCostSearch.getCurrentlyRunning().ifPresent(AbstractNodeCostSearch::cancel); } public void path() { diff --git a/src/main/java/baritone/pathing/calc/AStarPathFinder.java b/src/main/java/baritone/pathing/calc/AStarPathFinder.java index dc31e1c4..2dca7bb8 100644 --- a/src/main/java/baritone/pathing/calc/AStarPathFinder.java +++ b/src/main/java/baritone/pathing/calc/AStarPathFinder.java @@ -80,7 +80,7 @@ public class AStarPathFinder extends AbstractNodeCostSearch implements Helper { int pathingMaxChunkBorderFetch = Baritone.settings().pathingMaxChunkBorderFetch.get(); // grab all settings beforehand so that changing settings during pathing doesn't cause a crash or unpredictable behavior double favorCoeff = Baritone.settings().backtrackCostFavoringCoefficient.get(); boolean minimumImprovementRepropagation = Baritone.settings().minimumImprovementRepropagation.get(); - while (!openSet.isEmpty() && numEmptyChunk < pathingMaxChunkBorderFetch && System.currentTimeMillis() < timeoutTime) { + while (!openSet.isEmpty() && numEmptyChunk < pathingMaxChunkBorderFetch && System.currentTimeMillis() < timeoutTime && !cancelRequested) { if (slowPath) { try { Thread.sleep(Baritone.settings().slowPathTimeDelayMS.get()); @@ -169,6 +169,10 @@ public class AStarPathFinder extends AbstractNodeCostSearch implements Helper { } } } + if (cancelRequested) { + currentlyRunning = null; + return Optional.empty(); + } double bestDist = 0; for (int i = 0; i < bestSoFar.length; i++) { if (bestSoFar[i] == null) { diff --git a/src/main/java/baritone/pathing/calc/AbstractNodeCostSearch.java b/src/main/java/baritone/pathing/calc/AbstractNodeCostSearch.java index 5414f89c..18b8050f 100644 --- a/src/main/java/baritone/pathing/calc/AbstractNodeCostSearch.java +++ b/src/main/java/baritone/pathing/calc/AbstractNodeCostSearch.java @@ -52,6 +52,8 @@ public abstract class AbstractNodeCostSearch implements IPathFinder { private volatile boolean isFinished; + protected boolean cancelRequested; + /** * This is really complicated and hard to explain. I wrote a comment in the old version of MineBot but it was so * long it was easier as a Google Doc (because I could insert charts). @@ -70,10 +72,15 @@ public abstract class AbstractNodeCostSearch implements IPathFinder { this.map = new HashMap<>(); } + public void cancel() { + cancelRequested = true; + } + public synchronized Optional calculate() { if (isFinished) { throw new IllegalStateException("Path Finder is currently in use, and cannot be reused!"); } + this.cancelRequested = false; Optional path = calculate0(); isFinished = true; return path;