From f9a3a3b78bc0229f266745b3cbabd6e11040c4bb Mon Sep 17 00:00:00 2001 From: Leijurv Date: Sun, 25 Nov 2018 22:30:37 -0800 Subject: [PATCH] main thread bsi creation doesn't need to copy loaded chunks --- .../java/baritone/behavior/PathingBehavior.java | 2 +- .../pathing/movement/CalculationContext.java | 6 +++++- .../java/baritone/process/GetToBlockProcess.java | 2 +- src/main/java/baritone/process/MineProcess.java | 2 +- .../java/baritone/utils/BlockStateInterface.java | 15 ++++++++++++--- .../baritone/utils/ExampleBaritoneControl.java | 2 +- 6 files changed, 21 insertions(+), 8 deletions(-) diff --git a/src/main/java/baritone/behavior/PathingBehavior.java b/src/main/java/baritone/behavior/PathingBehavior.java index 3e8c3595..1a4b12b9 100644 --- a/src/main/java/baritone/behavior/PathingBehavior.java +++ b/src/main/java/baritone/behavior/PathingBehavior.java @@ -409,7 +409,7 @@ public final class PathingBehavior extends Behavior implements IPathingBehavior, primaryTimeout = Baritone.settings().planAheadPrimaryTimeoutMS.get(); failureTimeout = Baritone.settings().planAheadFailureTimeoutMS.get(); } - CalculationContext context = new CalculationContext(baritone); // not safe to create on the other thread, it looks up a lot of stuff in minecraft + CalculationContext context = new CalculationContext(baritone, true); // not safe to create on the other thread, it looks up a lot of stuff in minecraft AbstractNodeCostSearch pathfinder = createPathfinder(start, goal, current == null ? null : current.getPath(), context, true); if (!Objects.equals(pathfinder.getGoal(), goal)) { logDebug("Simplifying " + goal.getClass() + " to GoalXZ due to distance"); diff --git a/src/main/java/baritone/pathing/movement/CalculationContext.java b/src/main/java/baritone/pathing/movement/CalculationContext.java index cb4727cc..ace7670c 100644 --- a/src/main/java/baritone/pathing/movement/CalculationContext.java +++ b/src/main/java/baritone/pathing/movement/CalculationContext.java @@ -60,11 +60,15 @@ public class CalculationContext { private final BetterWorldBorder worldBorder; public CalculationContext(IBaritone baritone) { + this(baritone, false); + } + + public CalculationContext(IBaritone baritone, boolean forUseOnAnotherThread) { this.baritone = baritone; this.player = baritone.getPlayerContext().player(); this.world = baritone.getPlayerContext().world(); this.worldData = (WorldData) baritone.getWorldProvider().getCurrentWorld(); - this.bsi = new BlockStateInterface(world, worldData); // TODO TODO TODO + this.bsi = new BlockStateInterface(world, worldData, forUseOnAnotherThread); // TODO TODO TODO // new CalculationContext() needs to happen, can't add an argument (i'll beat you), can we get the world provider from currentlyTicking? this.toolSet = new ToolSet(player); this.hasThrowaway = Baritone.settings().allowPlace.get() && MovementHelper.throwaway(baritone.getPlayerContext(), false); diff --git a/src/main/java/baritone/process/GetToBlockProcess.java b/src/main/java/baritone/process/GetToBlockProcess.java index 95f2122b..920e5743 100644 --- a/src/main/java/baritone/process/GetToBlockProcess.java +++ b/src/main/java/baritone/process/GetToBlockProcess.java @@ -77,7 +77,7 @@ public class GetToBlockProcess extends BaritoneProcessHelper implements IGetToBl int mineGoalUpdateInterval = Baritone.settings().mineGoalUpdateInterval.get(); if (mineGoalUpdateInterval != 0 && tickCount++ % mineGoalUpdateInterval == 0) { // big brain List current = new ArrayList<>(knownLocations); - CalculationContext context = new CalculationContext(baritone); + CalculationContext context = new CalculationContext(baritone, true); Baritone.getExecutor().execute(() -> rescan(current, context)); } Goal goal = new GoalComposite(knownLocations.stream().map(GoalGetToBlock::new).toArray(Goal[]::new)); diff --git a/src/main/java/baritone/process/MineProcess.java b/src/main/java/baritone/process/MineProcess.java index 4ea75387..8b93d1cb 100644 --- a/src/main/java/baritone/process/MineProcess.java +++ b/src/main/java/baritone/process/MineProcess.java @@ -89,7 +89,7 @@ public final class MineProcess extends BaritoneProcessHelper implements IMinePro int mineGoalUpdateInterval = Baritone.settings().mineGoalUpdateInterval.get(); if (mineGoalUpdateInterval != 0 && tickCount++ % mineGoalUpdateInterval == 0) { // big brain List curr = new ArrayList<>(knownOreLocations); - CalculationContext context = new CalculationContext(baritone); + CalculationContext context = new CalculationContext(baritone, true); Baritone.getExecutor().execute(() -> rescan(curr, context)); } if (Baritone.settings().legitMine.get()) { diff --git a/src/main/java/baritone/utils/BlockStateInterface.java b/src/main/java/baritone/utils/BlockStateInterface.java index 96493e00..be49c97c 100644 --- a/src/main/java/baritone/utils/BlockStateInterface.java +++ b/src/main/java/baritone/utils/BlockStateInterface.java @@ -49,12 +49,21 @@ public class BlockStateInterface { private static final IBlockState AIR = Blocks.AIR.getDefaultState(); public BlockStateInterface(IPlayerContext ctx) { - this(ctx.world(), (WorldData) ctx.worldData()); + this(ctx, false); } - public BlockStateInterface(World world, WorldData worldData) { + public BlockStateInterface(IPlayerContext ctx, boolean copyLoadedChunks) { + this(ctx.world(), (WorldData) ctx.worldData(), copyLoadedChunks); + } + + public BlockStateInterface(World world, WorldData worldData, boolean copyLoadedChunks) { this.worldData = worldData; - this.loadedChunks = new Long2ObjectOpenHashMap<>(((IChunkProviderClient) world.getChunkProvider()).loadedChunks()); // make a copy that we can safely access from another thread + Long2ObjectMap worldLoaded = ((IChunkProviderClient) world.getChunkProvider()).loadedChunks(); + if (copyLoadedChunks) { + this.loadedChunks = new Long2ObjectOpenHashMap<>(worldLoaded); // make a copy that we can safely access from another thread + } else { + this.loadedChunks = worldLoaded; // this will only be used on the main thread + } if (!Minecraft.getMinecraft().isCallingFromMinecraftThread()) { throw new IllegalStateException(); } diff --git a/src/main/java/baritone/utils/ExampleBaritoneControl.java b/src/main/java/baritone/utils/ExampleBaritoneControl.java index 6b2c7448..2c112ee3 100644 --- a/src/main/java/baritone/utils/ExampleBaritoneControl.java +++ b/src/main/java/baritone/utils/ExampleBaritoneControl.java @@ -208,7 +208,7 @@ public class ExampleBaritoneControl extends Behavior implements Helper { logDirect("No goal."); } else { logDirect("Started segmented calculator"); - SegmentedCalculator.calculateSegmentsThreaded(pathingBehavior.pathStart(), pathingBehavior.getGoal(), new CalculationContext(baritone), ipath -> { + SegmentedCalculator.calculateSegmentsThreaded(pathingBehavior.pathStart(), pathingBehavior.getGoal(), new CalculationContext(baritone, true), ipath -> { logDirect("Found a path"); logDirect("Ends at " + ipath.getDest()); logDirect("Length " + ipath.length());