From dd08b2c8255f63a43b5cf31e8121210d384b8891 Mon Sep 17 00:00:00 2001 From: Leijurv Date: Fri, 9 Nov 2018 17:21:02 -0800 Subject: [PATCH] no more references to player() from pathing thread --- .../pathing/calc/AStarPathFinder.java | 6 +- .../pathing/calc/AbstractNodeCostSearch.java | 10 ++- src/main/java/baritone/pathing/calc/Path.java | 10 ++- .../pathing/movement/CalculationContext.java | 27 ++++++-- .../pathing/movement/MovementHelper.java | 2 +- .../java/baritone/pathing/movement/Moves.java | 62 +++++++++---------- .../utils/ExampleBaritoneControl.java | 3 +- src/main/java/baritone/utils/Helper.java | 3 + src/main/java/baritone/utils/ToolSet.java | 20 +++--- 9 files changed, 87 insertions(+), 56 deletions(-) diff --git a/src/main/java/baritone/pathing/calc/AStarPathFinder.java b/src/main/java/baritone/pathing/calc/AStarPathFinder.java index 83830ac0..41c11ea5 100644 --- a/src/main/java/baritone/pathing/calc/AStarPathFinder.java +++ b/src/main/java/baritone/pathing/calc/AStarPathFinder.java @@ -44,7 +44,7 @@ public final class AStarPathFinder extends AbstractNodeCostSearch implements Hel private final CalculationContext calcContext; public AStarPathFinder(int startX, int startY, int startZ, Goal goal, Optional> favoredPositions, CalculationContext context) { - super(startX, startY, startZ, goal); + super(startX, startY, startZ, goal, context); this.favoredPositions = favoredPositions; this.calcContext = context; } @@ -95,7 +95,7 @@ public final class AStarPathFinder extends AbstractNodeCostSearch implements Hel numNodes++; if (goal.isInGoal(currentNode.x, currentNode.y, currentNode.z)) { logDebug("Took " + (System.nanoTime() / 1000000L - startTime) + "ms, " + numMovementsConsidered + " movements considered"); - return Optional.of(new Path(startNode, currentNode, numNodes, goal)); + return Optional.of(new Path(startNode, currentNode, numNodes, goal, calcContext)); } for (Moves moves : Moves.values()) { int newX = currentNode.x + moves.xOffset; @@ -198,7 +198,7 @@ public final class AStarPathFinder extends AbstractNodeCostSearch implements Hel System.out.println("But I'm going to do it anyway, because yolo"); } System.out.println("Path goes for " + Math.sqrt(dist) + " blocks"); - return Optional.of(new Path(startNode, bestSoFar[i], numNodes, goal)); + return Optional.of(new Path(startNode, bestSoFar[i], numNodes, goal, calcContext)); } } logDebug("Even with a cost coefficient of " + COEFFICIENTS[COEFFICIENTS.length - 1] + ", I couldn't get more than " + Math.sqrt(bestDist) + " blocks"); diff --git a/src/main/java/baritone/pathing/calc/AbstractNodeCostSearch.java b/src/main/java/baritone/pathing/calc/AbstractNodeCostSearch.java index 708b5ab0..79a56037 100644 --- a/src/main/java/baritone/pathing/calc/AbstractNodeCostSearch.java +++ b/src/main/java/baritone/pathing/calc/AbstractNodeCostSearch.java @@ -22,6 +22,7 @@ import baritone.api.pathing.calc.IPath; import baritone.api.pathing.calc.IPathFinder; import baritone.api.pathing.goals.Goal; import baritone.api.utils.PathCalculationResult; +import baritone.pathing.movement.CalculationContext; import it.unimi.dsi.fastutil.longs.Long2ObjectOpenHashMap; import java.util.Optional; @@ -44,6 +45,8 @@ public abstract class AbstractNodeCostSearch implements IPathFinder { protected final Goal goal; + private final CalculationContext context; + /** * @see Issue #107 */ @@ -71,11 +74,12 @@ public abstract class AbstractNodeCostSearch implements IPathFinder { */ protected final static double MIN_DIST_PATH = 5; - AbstractNodeCostSearch(int startX, int startY, int startZ, Goal goal) { + AbstractNodeCostSearch(int startX, int startY, int startZ, Goal goal, CalculationContext context) { this.startX = startX; this.startY = startY; this.startZ = startZ; this.goal = goal; + this.context = context; this.map = new Long2ObjectOpenHashMap<>(Baritone.settings().pathingMapDefaultSize.value, Baritone.settings().pathingMapLoadFactor.get()); } @@ -171,7 +175,7 @@ public abstract class AbstractNodeCostSearch implements IPathFinder { @Override public Optional pathToMostRecentNodeConsidered() { try { - return Optional.ofNullable(mostRecentConsidered).map(node -> new Path(startNode, node, 0, goal)); + return Optional.ofNullable(mostRecentConsidered).map(node -> new Path(startNode, node, 0, goal, context)); } catch (IllegalStateException ex) { System.out.println("Unable to construct path to render"); return Optional.empty(); @@ -193,7 +197,7 @@ public abstract class AbstractNodeCostSearch implements IPathFinder { } if (getDistFromStartSq(bestSoFar[i]) > MIN_DIST_PATH * MIN_DIST_PATH) { // square the comparison since distFromStartSq is squared try { - return Optional.of(new Path(startNode, bestSoFar[i], 0, goal)); + return Optional.of(new Path(startNode, bestSoFar[i], 0, goal, context)); } catch (IllegalStateException ex) { System.out.println("Unable to construct path to render"); return Optional.empty(); diff --git a/src/main/java/baritone/pathing/calc/Path.java b/src/main/java/baritone/pathing/calc/Path.java index 873fef64..58e1e661 100644 --- a/src/main/java/baritone/pathing/calc/Path.java +++ b/src/main/java/baritone/pathing/calc/Path.java @@ -21,6 +21,7 @@ import baritone.api.pathing.calc.IPath; import baritone.api.pathing.goals.Goal; import baritone.api.pathing.movement.IMovement; import baritone.api.utils.BetterBlockPos; +import baritone.pathing.movement.CalculationContext; import baritone.pathing.movement.Movement; import baritone.pathing.movement.Moves; import baritone.pathing.path.CutoffPath; @@ -63,9 +64,11 @@ class Path extends PathBase { private final int numNodes; + private final CalculationContext context; + private volatile boolean verified; - Path(PathNode start, PathNode end, int numNodes, Goal goal) { + Path(PathNode start, PathNode end, int numNodes, Goal goal, CalculationContext context) { this.start = new BetterBlockPos(start.x, start.y, start.z); this.end = new BetterBlockPos(end.x, end.y, end.z); this.numNodes = numNodes; @@ -73,6 +76,7 @@ class Path extends PathBase { this.movements = new ArrayList<>(); this.nodes = new ArrayList<>(); this.goal = goal; + this.context = context; assemblePath(end); } @@ -123,9 +127,9 @@ class Path extends PathBase { return false; } - private static Movement runBackwards(BetterBlockPos src, BetterBlockPos dest, double cost) { + private Movement runBackwards(BetterBlockPos src, BetterBlockPos dest, double cost) { for (Moves moves : Moves.values()) { - Movement move = moves.apply0(src); + Movement move = moves.apply0(context, src); if (move.getDest().equals(dest)) { // have to calculate the cost at calculation time so we can accurately judge whether a cost increase happened between cached calculation and real execution move.override(cost); diff --git a/src/main/java/baritone/pathing/movement/CalculationContext.java b/src/main/java/baritone/pathing/movement/CalculationContext.java index ca6936a5..10aa03c0 100644 --- a/src/main/java/baritone/pathing/movement/CalculationContext.java +++ b/src/main/java/baritone/pathing/movement/CalculationContext.java @@ -22,19 +22,23 @@ import baritone.api.pathing.movement.ActionCosts; import baritone.utils.Helper; import baritone.utils.ToolSet; import baritone.utils.pathing.BetterWorldBorder; +import net.minecraft.client.entity.EntityPlayerSP; import net.minecraft.enchantment.EnchantmentHelper; import net.minecraft.entity.player.InventoryPlayer; import net.minecraft.init.Items; import net.minecraft.item.ItemStack; +import net.minecraft.world.World; /** * @author Brady * @since 8/7/2018 4:30 PM */ -public class CalculationContext implements Helper { +public class CalculationContext { private static final ItemStack STACK_BUCKET_WATER = new ItemStack(Items.WATER_BUCKET); + private final EntityPlayerSP player; + private final World world; private final ToolSet toolSet; private final boolean hasWaterBucket; private final boolean hasThrowaway; @@ -48,15 +52,17 @@ public class CalculationContext implements Helper { private final BetterWorldBorder worldBorder; public CalculationContext() { - this.toolSet = new ToolSet(); + this.player = Helper.HELPER.player(); + this.world = Helper.HELPER.world(); + this.toolSet = new ToolSet(player); this.hasThrowaway = Baritone.settings().allowPlace.get() && MovementHelper.throwaway(false); - this.hasWaterBucket = Baritone.settings().allowWaterBucketFall.get() && InventoryPlayer.isHotbar(player().inventory.getSlotFor(STACK_BUCKET_WATER)) && !world().provider.isNether(); - this.canSprint = Baritone.settings().allowSprint.get() && player().getFoodStats().getFoodLevel() > 6; + this.hasWaterBucket = Baritone.settings().allowWaterBucketFall.get() && InventoryPlayer.isHotbar(player.inventory.getSlotFor(STACK_BUCKET_WATER)) && !world.provider.isNether(); + this.canSprint = Baritone.settings().allowSprint.get() && player.getFoodStats().getFoodLevel() > 6; this.placeBlockCost = Baritone.settings().blockPlacementPenalty.get(); this.allowBreak = Baritone.settings().allowBreak.get(); this.maxFallHeightNoWater = Baritone.settings().maxFallHeightNoWater.get(); this.maxFallHeightBucket = Baritone.settings().maxFallHeightBucket.get(); - int depth = EnchantmentHelper.getDepthStriderModifier(player()); + int depth = EnchantmentHelper.getDepthStriderModifier(player); if (depth > 3) { depth = 3; } @@ -66,7 +72,7 @@ public class CalculationContext implements Helper { // why cache these things here, why not let the movements just get directly from settings? // because if some movements are calculated one way and others are calculated another way, // then you get a wildly inconsistent path that isn't optimal for either scenario. - this.worldBorder = new BetterWorldBorder(world().getWorldBorder()); + this.worldBorder = new BetterWorldBorder(world.getWorldBorder()); } public boolean canPlaceThrowawayAt(int x, int y, int z) { @@ -91,6 +97,15 @@ public class CalculationContext implements Helper { return false; } + public World world() { + return world; + } + + public EntityPlayerSP player() { + return player; + } + + public ToolSet getToolSet() { return toolSet; } diff --git a/src/main/java/baritone/pathing/movement/MovementHelper.java b/src/main/java/baritone/pathing/movement/MovementHelper.java index 75361635..7f2f4e0d 100644 --- a/src/main/java/baritone/pathing/movement/MovementHelper.java +++ b/src/main/java/baritone/pathing/movement/MovementHelper.java @@ -383,7 +383,7 @@ public interface MovementHelper extends ActionCosts, Helper { * @param b the blockstate to mine */ static void switchToBestToolFor(IBlockState b) { - switchToBestToolFor(b, new ToolSet()); + switchToBestToolFor(b, new ToolSet(Helper.HELPER.player())); } /** diff --git a/src/main/java/baritone/pathing/movement/Moves.java b/src/main/java/baritone/pathing/movement/Moves.java index 80a6c4d5..340122b3 100644 --- a/src/main/java/baritone/pathing/movement/Moves.java +++ b/src/main/java/baritone/pathing/movement/Moves.java @@ -30,7 +30,7 @@ import net.minecraft.util.EnumFacing; public enum Moves { DOWNWARD(0, -1, 0) { @Override - public Movement apply0(BetterBlockPos src) { + public Movement apply0(CalculationContext context, BetterBlockPos src) { return new MovementDownward(src, src.down()); } @@ -42,7 +42,7 @@ public enum Moves { PILLAR(0, +1, 0) { @Override - public Movement apply0(BetterBlockPos src) { + public Movement apply0(CalculationContext context, BetterBlockPos src) { return new MovementPillar(src, src.up()); } @@ -54,7 +54,7 @@ public enum Moves { TRAVERSE_NORTH(0, 0, -1) { @Override - public Movement apply0(BetterBlockPos src) { + public Movement apply0(CalculationContext context, BetterBlockPos src) { return new MovementTraverse(src, src.north()); } @@ -66,7 +66,7 @@ public enum Moves { TRAVERSE_SOUTH(0, 0, +1) { @Override - public Movement apply0(BetterBlockPos src) { + public Movement apply0(CalculationContext context, BetterBlockPos src) { return new MovementTraverse(src, src.south()); } @@ -78,7 +78,7 @@ public enum Moves { TRAVERSE_EAST(+1, 0, 0) { @Override - public Movement apply0(BetterBlockPos src) { + public Movement apply0(CalculationContext context, BetterBlockPos src) { return new MovementTraverse(src, src.east()); } @@ -90,7 +90,7 @@ public enum Moves { TRAVERSE_WEST(-1, 0, 0) { @Override - public Movement apply0(BetterBlockPos src) { + public Movement apply0(CalculationContext context, BetterBlockPos src) { return new MovementTraverse(src, src.west()); } @@ -102,7 +102,7 @@ public enum Moves { ASCEND_NORTH(0, +1, -1) { @Override - public Movement apply0(BetterBlockPos src) { + public Movement apply0(CalculationContext context, BetterBlockPos src) { return new MovementAscend(src, new BetterBlockPos(src.x, src.y + 1, src.z - 1)); } @@ -114,7 +114,7 @@ public enum Moves { ASCEND_SOUTH(0, +1, +1) { @Override - public Movement apply0(BetterBlockPos src) { + public Movement apply0(CalculationContext context, BetterBlockPos src) { return new MovementAscend(src, new BetterBlockPos(src.x, src.y + 1, src.z + 1)); } @@ -126,7 +126,7 @@ public enum Moves { ASCEND_EAST(+1, +1, 0) { @Override - public Movement apply0(BetterBlockPos src) { + public Movement apply0(CalculationContext context, BetterBlockPos src) { return new MovementAscend(src, new BetterBlockPos(src.x + 1, src.y + 1, src.z)); } @@ -138,7 +138,7 @@ public enum Moves { ASCEND_WEST(-1, +1, 0) { @Override - public Movement apply0(BetterBlockPos src) { + public Movement apply0(CalculationContext context, BetterBlockPos src) { return new MovementAscend(src, new BetterBlockPos(src.x - 1, src.y + 1, src.z)); } @@ -150,9 +150,9 @@ public enum Moves { DESCEND_EAST(+1, -1, 0, false, true) { @Override - public Movement apply0(BetterBlockPos src) { + public Movement apply0(CalculationContext context, BetterBlockPos src) { MutableMoveResult res = new MutableMoveResult(); - apply(new CalculationContext(), src.x, src.y, src.z, res); + apply(context, src.x, src.y, src.z, res); if (res.y == src.y - 1) { return new MovementDescend(src, new BetterBlockPos(res.x, res.y, res.z)); } else { @@ -168,9 +168,9 @@ public enum Moves { DESCEND_WEST(-1, -1, 0, false, true) { @Override - public Movement apply0(BetterBlockPos src) { + public Movement apply0(CalculationContext context, BetterBlockPos src) { MutableMoveResult res = new MutableMoveResult(); - apply(new CalculationContext(), src.x, src.y, src.z, res); + apply(context, src.x, src.y, src.z, res); if (res.y == src.y - 1) { return new MovementDescend(src, new BetterBlockPos(res.x, res.y, res.z)); } else { @@ -186,9 +186,9 @@ public enum Moves { DESCEND_NORTH(0, -1, -1, false, true) { @Override - public Movement apply0(BetterBlockPos src) { + public Movement apply0(CalculationContext context, BetterBlockPos src) { MutableMoveResult res = new MutableMoveResult(); - apply(new CalculationContext(), src.x, src.y, src.z, res); + apply(context, src.x, src.y, src.z, res); if (res.y == src.y - 1) { return new MovementDescend(src, new BetterBlockPos(res.x, res.y, res.z)); } else { @@ -204,9 +204,9 @@ public enum Moves { DESCEND_SOUTH(0, -1, +1, false, true) { @Override - public Movement apply0(BetterBlockPos src) { + public Movement apply0(CalculationContext context, BetterBlockPos src) { MutableMoveResult res = new MutableMoveResult(); - apply(new CalculationContext(), src.x, src.y, src.z, res); + apply(context, src.x, src.y, src.z, res); if (res.y == src.y - 1) { return new MovementDescend(src, new BetterBlockPos(res.x, res.y, res.z)); } else { @@ -222,7 +222,7 @@ public enum Moves { DIAGONAL_NORTHEAST(+1, 0, -1) { @Override - public Movement apply0(BetterBlockPos src) { + public Movement apply0(CalculationContext context, BetterBlockPos src) { return new MovementDiagonal(src, EnumFacing.NORTH, EnumFacing.EAST); } @@ -234,7 +234,7 @@ public enum Moves { DIAGONAL_NORTHWEST(-1, 0, -1) { @Override - public Movement apply0(BetterBlockPos src) { + public Movement apply0(CalculationContext context, BetterBlockPos src) { return new MovementDiagonal(src, EnumFacing.NORTH, EnumFacing.WEST); } @@ -246,7 +246,7 @@ public enum Moves { DIAGONAL_SOUTHEAST(+1, 0, +1) { @Override - public Movement apply0(BetterBlockPos src) { + public Movement apply0(CalculationContext context, BetterBlockPos src) { return new MovementDiagonal(src, EnumFacing.SOUTH, EnumFacing.EAST); } @@ -258,7 +258,7 @@ public enum Moves { DIAGONAL_SOUTHWEST(-1, 0, +1) { @Override - public Movement apply0(BetterBlockPos src) { + public Movement apply0(CalculationContext context, BetterBlockPos src) { return new MovementDiagonal(src, EnumFacing.SOUTH, EnumFacing.WEST); } @@ -270,8 +270,8 @@ public enum Moves { PARKOUR_NORTH(0, 0, -4, true, false) { @Override - public Movement apply0(BetterBlockPos src) { - return MovementParkour.cost(new CalculationContext(), src, EnumFacing.NORTH); + public Movement apply0(CalculationContext context, BetterBlockPos src) { + return MovementParkour.cost(context, src, EnumFacing.NORTH); } @Override @@ -282,8 +282,8 @@ public enum Moves { PARKOUR_SOUTH(0, 0, +4, true, false) { @Override - public Movement apply0(BetterBlockPos src) { - return MovementParkour.cost(new CalculationContext(), src, EnumFacing.SOUTH); + public Movement apply0(CalculationContext context, BetterBlockPos src) { + return MovementParkour.cost(context, src, EnumFacing.SOUTH); } @Override @@ -294,8 +294,8 @@ public enum Moves { PARKOUR_EAST(+4, 0, 0, true, false) { @Override - public Movement apply0(BetterBlockPos src) { - return MovementParkour.cost(new CalculationContext(), src, EnumFacing.EAST); + public Movement apply0(CalculationContext context, BetterBlockPos src) { + return MovementParkour.cost(context, src, EnumFacing.EAST); } @Override @@ -306,8 +306,8 @@ public enum Moves { PARKOUR_WEST(-4, 0, 0, true, false) { @Override - public Movement apply0(BetterBlockPos src) { - return MovementParkour.cost(new CalculationContext(), src, EnumFacing.WEST); + public Movement apply0(CalculationContext context, BetterBlockPos src) { + return MovementParkour.cost(context, src, EnumFacing.WEST); } @Override @@ -335,7 +335,7 @@ public enum Moves { this(x, y, z, false, false); } - public abstract Movement apply0(BetterBlockPos src); + public abstract Movement apply0(CalculationContext context, BetterBlockPos src); public void apply(CalculationContext context, int x, int y, int z, MutableMoveResult result) { if (dynamicXZ || dynamicY) { diff --git a/src/main/java/baritone/utils/ExampleBaritoneControl.java b/src/main/java/baritone/utils/ExampleBaritoneControl.java index f4defbfd..2fbf05f7 100644 --- a/src/main/java/baritone/utils/ExampleBaritoneControl.java +++ b/src/main/java/baritone/utils/ExampleBaritoneControl.java @@ -31,6 +31,7 @@ import baritone.cache.ChunkPacker; import baritone.cache.Waypoint; import baritone.cache.WorldProvider; import baritone.pathing.calc.AbstractNodeCostSearch; +import baritone.pathing.movement.CalculationContext; import baritone.pathing.movement.Movement; import baritone.pathing.movement.Moves; import baritone.process.CustomGoalProcess; @@ -450,7 +451,7 @@ public class ExampleBaritoneControl extends Behavior implements Helper { return true; } if (msg.equals("costs")) { - List moves = Stream.of(Moves.values()).map(x -> x.apply0(playerFeet())).collect(Collectors.toCollection(ArrayList::new)); + List moves = Stream.of(Moves.values()).map(x -> x.apply0(new CalculationContext(), playerFeet())).collect(Collectors.toCollection(ArrayList::new)); while (moves.contains(null)) { moves.remove(null); } diff --git a/src/main/java/baritone/utils/Helper.java b/src/main/java/baritone/utils/Helper.java index 2a34ecc0..a80e5660 100755 --- a/src/main/java/baritone/utils/Helper.java +++ b/src/main/java/baritone/utils/Helper.java @@ -51,6 +51,9 @@ public interface Helper { Minecraft mc = Minecraft.getMinecraft(); default EntityPlayerSP player() { + if (!mc.isCallingFromMinecraftThread()) { + throw new IllegalStateException("h00000000"); + } return mc.player; } diff --git a/src/main/java/baritone/utils/ToolSet.java b/src/main/java/baritone/utils/ToolSet.java index 0c618294..026ec199 100644 --- a/src/main/java/baritone/utils/ToolSet.java +++ b/src/main/java/baritone/utils/ToolSet.java @@ -20,6 +20,7 @@ package baritone.utils; import baritone.Baritone; import net.minecraft.block.Block; import net.minecraft.block.state.IBlockState; +import net.minecraft.client.entity.EntityPlayerSP; import net.minecraft.enchantment.EnchantmentHelper; import net.minecraft.init.Enchantments; import net.minecraft.init.MobEffects; @@ -36,7 +37,7 @@ import java.util.function.Function; * * @author Avery, Brady, leijurv */ -public class ToolSet implements Helper { +public class ToolSet { /** * A cache mapping a {@link Block} to how long it will take to break * with this toolset, given the optimum tool is used. @@ -48,8 +49,11 @@ public class ToolSet implements Helper { */ private final Function backendCalculation; - public ToolSet() { + private final EntityPlayerSP player; + + public ToolSet(EntityPlayerSP player) { breakStrengthCache = new HashMap<>(); + this.player = player; if (Baritone.settings().considerPotionEffects.get()) { double amplifier = potionAmplifier(); @@ -98,7 +102,7 @@ public class ToolSet implements Helper { int materialCost = Integer.MIN_VALUE; IBlockState blockState = b.getDefaultState(); for (byte i = 0; i < 9; i++) { - ItemStack itemStack = player().inventory.getStackInSlot(i); + ItemStack itemStack = player.inventory.getStackInSlot(i); double v = calculateStrVsBlock(itemStack, blockState); if (v > value) { value = v; @@ -123,7 +127,7 @@ public class ToolSet implements Helper { * @return A double containing the destruction ticks with the best tool */ private double getBestDestructionTime(Block b) { - ItemStack stack = player().inventory.getStackInSlot(getBestSlot(b)); + ItemStack stack = player.inventory.getStackInSlot(getBestSlot(b)); return calculateStrVsBlock(stack, b.getDefaultState()); } @@ -164,11 +168,11 @@ public class ToolSet implements Helper { */ private double potionAmplifier() { double speed = 1; - if (player().isPotionActive(MobEffects.HASTE)) { - speed *= 1 + (player().getActivePotionEffect(MobEffects.HASTE).getAmplifier() + 1) * 0.2; + if (player.isPotionActive(MobEffects.HASTE)) { + speed *= 1 + (player.getActivePotionEffect(MobEffects.HASTE).getAmplifier() + 1) * 0.2; } - if (player().isPotionActive(MobEffects.MINING_FATIGUE)) { - switch (player().getActivePotionEffect(MobEffects.MINING_FATIGUE).getAmplifier()) { + if (player.isPotionActive(MobEffects.MINING_FATIGUE)) { + switch (player.getActivePotionEffect(MobEffects.MINING_FATIGUE).getAmplifier()) { case 0: speed *= 0.3; break;