From 8b307f296a4e063ab9f2b82d8e824fa08ffa69b7 Mon Sep 17 00:00:00 2001 From: Leijurv Date: Fri, 21 Sep 2018 22:14:18 -0700 Subject: [PATCH] fix very rare null pointer exception --- .../java/baritone/pathing/calc/AStarPathFinder.java | 2 ++ .../pathing/calc/AbstractNodeCostSearch.java | 13 ++++++++++++- 2 files changed, 14 insertions(+), 1 deletion(-) diff --git a/src/main/java/baritone/pathing/calc/AStarPathFinder.java b/src/main/java/baritone/pathing/calc/AStarPathFinder.java index 73ca28f6..d9f3ba1a 100644 --- a/src/main/java/baritone/pathing/calc/AStarPathFinder.java +++ b/src/main/java/baritone/pathing/calc/AStarPathFinder.java @@ -90,6 +90,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(); + loopBegin(); while (!openSet.isEmpty() && numEmptyChunk < pathingMaxChunkBorderFetch && System.nanoTime() / 1000000L - timeoutTime < 0 && !cancelRequested) { if (slowPath) { try { @@ -180,6 +181,7 @@ public class AStarPathFinder extends AbstractNodeCostSearch implements Helper { } System.out.println(numMovementsConsidered + " movements considered"); System.out.println("Open set size: " + openSet.size()); + System.out.println("PathNode map size: " + mapSize()); System.out.println((int) (numNodes * 1.0 / ((System.nanoTime() / 1000000L - startTime) / 1000F)) + " nodes per second"); double bestDist = 0; for (int i = 0; i < bestSoFar.length; i++) { diff --git a/src/main/java/baritone/pathing/calc/AbstractNodeCostSearch.java b/src/main/java/baritone/pathing/calc/AbstractNodeCostSearch.java index 20dce981..ee2d0e24 100644 --- a/src/main/java/baritone/pathing/calc/AbstractNodeCostSearch.java +++ b/src/main/java/baritone/pathing/calc/AbstractNodeCostSearch.java @@ -85,7 +85,6 @@ public abstract class AbstractNodeCostSearch implements IPathFinder { } this.cancelRequested = false; try { - currentlyRunning = this; Optional path = calculate0(timeout); path.ifPresent(IPath::postprocess); isFinished = true; @@ -97,6 +96,14 @@ public abstract class AbstractNodeCostSearch implements IPathFinder { } } + /** + * Don't set currentlyRunning to this until everything is all ready to go, and we're about to enter the main loop. + * For example, bestSoFar is null so bestPathSoFar (which gets bestSoFar[0]) could NPE if we set currentlyRunning before calculate0 + */ + protected void loopBegin() { + currentlyRunning = this; + } + protected abstract Optional calculate0(long timeout); /** @@ -143,6 +150,10 @@ public abstract class AbstractNodeCostSearch implements IPathFinder { return Optional.ofNullable(mostRecentConsidered).map(node -> new Path(startNode, node, 0, goal)); } + protected int mapSize() { + return map.size(); + } + @Override public Optional bestPathSoFar() { if (startNode == null || bestSoFar[0] == null) {