main thread bsi creation doesn't need to copy loaded chunks

This commit is contained in:
Leijurv 2018-11-25 22:30:37 -08:00
parent 2ba0e6ada6
commit f9a3a3b78b
No known key found for this signature in database
GPG Key ID: 44A3EA646EADAC6A
6 changed files with 21 additions and 8 deletions

View File

@ -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");

View File

@ -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);

View File

@ -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<BlockPos> 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));

View File

@ -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<BlockPos> 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()) {

View File

@ -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<Chunk> 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();
}

View File

@ -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());