From b9b33b5351cabdb0a372018fda4c596791c89728 Mon Sep 17 00:00:00 2001 From: Leijurv Date: Mon, 29 Oct 2018 18:58:52 -0700 Subject: [PATCH] move calculation context construction to main thread --- src/main/java/baritone/behavior/PathingBehavior.java | 8 +++++--- src/main/java/baritone/pathing/calc/AStarPathFinder.java | 5 +++-- 2 files changed, 8 insertions(+), 5 deletions(-) diff --git a/src/main/java/baritone/behavior/PathingBehavior.java b/src/main/java/baritone/behavior/PathingBehavior.java index 65fa6cd5..d8bf9a1d 100644 --- a/src/main/java/baritone/behavior/PathingBehavior.java +++ b/src/main/java/baritone/behavior/PathingBehavior.java @@ -32,6 +32,7 @@ import baritone.api.utils.interfaces.IGoalRenderPos; import baritone.pathing.calc.AStarPathFinder; import baritone.pathing.calc.AbstractNodeCostSearch; import baritone.pathing.calc.CutoffPath; +import baritone.pathing.movement.CalculationContext; import baritone.pathing.movement.MovementHelper; import baritone.pathing.path.PathExecutor; import baritone.utils.BlockBreakHelper; @@ -325,12 +326,13 @@ public final class PathingBehavior extends Behavior implements IPathingBehavior, } isPathCalcInProgress = true; } + CalculationContext context = new CalculationContext(); // not safe to create on the other thread, it looks up a lot of stuff in minecraft Baritone.INSTANCE.getExecutor().execute(() -> { if (talkAboutIt) { logDebug("Starting to search for path from " + start + " to " + goal); } - Optional path = findPath(start, previous); + Optional path = findPath(start, previous, context); if (Baritone.settings().cutoffAtLoadBoundary.get()) { path = path.map(p -> { IPath result = p.cutoffAtLoadedChunks(); @@ -398,7 +400,7 @@ public final class PathingBehavior extends Behavior implements IPathingBehavior, * @param start * @return */ - private Optional findPath(BlockPos start, Optional previous) { + private Optional findPath(BlockPos start, Optional previous, CalculationContext context) { Goal goal = this.goal; if (goal == null) { logDebug("no goal"); @@ -428,7 +430,7 @@ public final class PathingBehavior extends Behavior implements IPathingBehavior, favoredPositions = previous.map(IPath::positions).map(Collection::stream).map(x -> x.map(BetterBlockPos::longHash)).map(x -> x.collect(Collectors.toList())).map(HashSet::new); // <-- okay this is EPIC } try { - IPathFinder pf = new AStarPathFinder(start.getX(), start.getY(), start.getZ(), goal, favoredPositions); + IPathFinder pf = new AStarPathFinder(start.getX(), start.getY(), start.getZ(), goal, favoredPositions, context); return pf.calculate(timeout); } catch (Exception e) { logDebug("Pathing exception: " + e); diff --git a/src/main/java/baritone/pathing/calc/AStarPathFinder.java b/src/main/java/baritone/pathing/calc/AStarPathFinder.java index dfecae2e..f9a15e36 100644 --- a/src/main/java/baritone/pathing/calc/AStarPathFinder.java +++ b/src/main/java/baritone/pathing/calc/AStarPathFinder.java @@ -41,10 +41,12 @@ import java.util.Optional; public final class AStarPathFinder extends AbstractNodeCostSearch implements Helper { private final Optional> favoredPositions; + private final CalculationContext calcContext; - public AStarPathFinder(int startX, int startY, int startZ, Goal goal, Optional> favoredPositions) { + public AStarPathFinder(int startX, int startY, int startZ, Goal goal, Optional> favoredPositions, CalculationContext context) { super(startX, startY, startZ, goal); this.favoredPositions = favoredPositions; + this.calcContext = context; } @Override @@ -61,7 +63,6 @@ public final class AStarPathFinder extends AbstractNodeCostSearch implements Hel bestHeuristicSoFar[i] = startNode.estimatedCostToGoal; bestSoFar[i] = startNode; } - CalculationContext calcContext = new CalculationContext(); MutableMoveResult res = new MutableMoveResult(); HashSet favored = favoredPositions.orElse(null); BetterWorldBorder worldBorder = new BetterWorldBorder(world().getWorldBorder());