no more references to player() from pathing thread

This commit is contained in:
Leijurv 2018-11-09 17:21:02 -08:00
parent a2a60e9847
commit dd08b2c825
No known key found for this signature in database
GPG Key ID: 44A3EA646EADAC6A
9 changed files with 87 additions and 56 deletions

View File

@ -44,7 +44,7 @@ public final class AStarPathFinder extends AbstractNodeCostSearch implements Hel
private final CalculationContext calcContext; private final CalculationContext calcContext;
public AStarPathFinder(int startX, int startY, int startZ, Goal goal, Optional<HashSet<Long>> favoredPositions, CalculationContext context) { public AStarPathFinder(int startX, int startY, int startZ, Goal goal, Optional<HashSet<Long>> favoredPositions, CalculationContext context) {
super(startX, startY, startZ, goal); super(startX, startY, startZ, goal, context);
this.favoredPositions = favoredPositions; this.favoredPositions = favoredPositions;
this.calcContext = context; this.calcContext = context;
} }
@ -95,7 +95,7 @@ public final class AStarPathFinder extends AbstractNodeCostSearch implements Hel
numNodes++; numNodes++;
if (goal.isInGoal(currentNode.x, currentNode.y, currentNode.z)) { if (goal.isInGoal(currentNode.x, currentNode.y, currentNode.z)) {
logDebug("Took " + (System.nanoTime() / 1000000L - startTime) + "ms, " + numMovementsConsidered + " movements considered"); 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()) { for (Moves moves : Moves.values()) {
int newX = currentNode.x + moves.xOffset; 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("But I'm going to do it anyway, because yolo");
} }
System.out.println("Path goes for " + Math.sqrt(dist) + " blocks"); 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"); logDebug("Even with a cost coefficient of " + COEFFICIENTS[COEFFICIENTS.length - 1] + ", I couldn't get more than " + Math.sqrt(bestDist) + " blocks");

View File

@ -22,6 +22,7 @@ import baritone.api.pathing.calc.IPath;
import baritone.api.pathing.calc.IPathFinder; import baritone.api.pathing.calc.IPathFinder;
import baritone.api.pathing.goals.Goal; import baritone.api.pathing.goals.Goal;
import baritone.api.utils.PathCalculationResult; import baritone.api.utils.PathCalculationResult;
import baritone.pathing.movement.CalculationContext;
import it.unimi.dsi.fastutil.longs.Long2ObjectOpenHashMap; import it.unimi.dsi.fastutil.longs.Long2ObjectOpenHashMap;
import java.util.Optional; import java.util.Optional;
@ -44,6 +45,8 @@ public abstract class AbstractNodeCostSearch implements IPathFinder {
protected final Goal goal; protected final Goal goal;
private final CalculationContext context;
/** /**
* @see <a href="https://github.com/cabaletta/baritone/issues/107">Issue #107</a> * @see <a href="https://github.com/cabaletta/baritone/issues/107">Issue #107</a>
*/ */
@ -71,11 +74,12 @@ public abstract class AbstractNodeCostSearch implements IPathFinder {
*/ */
protected final static double MIN_DIST_PATH = 5; 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.startX = startX;
this.startY = startY; this.startY = startY;
this.startZ = startZ; this.startZ = startZ;
this.goal = goal; this.goal = goal;
this.context = context;
this.map = new Long2ObjectOpenHashMap<>(Baritone.settings().pathingMapDefaultSize.value, Baritone.settings().pathingMapLoadFactor.get()); this.map = new Long2ObjectOpenHashMap<>(Baritone.settings().pathingMapDefaultSize.value, Baritone.settings().pathingMapLoadFactor.get());
} }
@ -171,7 +175,7 @@ public abstract class AbstractNodeCostSearch implements IPathFinder {
@Override @Override
public Optional<IPath> pathToMostRecentNodeConsidered() { public Optional<IPath> pathToMostRecentNodeConsidered() {
try { 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) { } catch (IllegalStateException ex) {
System.out.println("Unable to construct path to render"); System.out.println("Unable to construct path to render");
return Optional.empty(); 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 if (getDistFromStartSq(bestSoFar[i]) > MIN_DIST_PATH * MIN_DIST_PATH) { // square the comparison since distFromStartSq is squared
try { 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) { } catch (IllegalStateException ex) {
System.out.println("Unable to construct path to render"); System.out.println("Unable to construct path to render");
return Optional.empty(); return Optional.empty();

View File

@ -21,6 +21,7 @@ import baritone.api.pathing.calc.IPath;
import baritone.api.pathing.goals.Goal; import baritone.api.pathing.goals.Goal;
import baritone.api.pathing.movement.IMovement; import baritone.api.pathing.movement.IMovement;
import baritone.api.utils.BetterBlockPos; import baritone.api.utils.BetterBlockPos;
import baritone.pathing.movement.CalculationContext;
import baritone.pathing.movement.Movement; import baritone.pathing.movement.Movement;
import baritone.pathing.movement.Moves; import baritone.pathing.movement.Moves;
import baritone.pathing.path.CutoffPath; import baritone.pathing.path.CutoffPath;
@ -63,9 +64,11 @@ class Path extends PathBase {
private final int numNodes; private final int numNodes;
private final CalculationContext context;
private volatile boolean verified; 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.start = new BetterBlockPos(start.x, start.y, start.z);
this.end = new BetterBlockPos(end.x, end.y, end.z); this.end = new BetterBlockPos(end.x, end.y, end.z);
this.numNodes = numNodes; this.numNodes = numNodes;
@ -73,6 +76,7 @@ class Path extends PathBase {
this.movements = new ArrayList<>(); this.movements = new ArrayList<>();
this.nodes = new ArrayList<>(); this.nodes = new ArrayList<>();
this.goal = goal; this.goal = goal;
this.context = context;
assemblePath(end); assemblePath(end);
} }
@ -123,9 +127,9 @@ class Path extends PathBase {
return false; 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()) { for (Moves moves : Moves.values()) {
Movement move = moves.apply0(src); Movement move = moves.apply0(context, src);
if (move.getDest().equals(dest)) { 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 // 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); move.override(cost);

View File

@ -22,19 +22,23 @@ import baritone.api.pathing.movement.ActionCosts;
import baritone.utils.Helper; import baritone.utils.Helper;
import baritone.utils.ToolSet; import baritone.utils.ToolSet;
import baritone.utils.pathing.BetterWorldBorder; import baritone.utils.pathing.BetterWorldBorder;
import net.minecraft.client.entity.EntityPlayerSP;
import net.minecraft.enchantment.EnchantmentHelper; import net.minecraft.enchantment.EnchantmentHelper;
import net.minecraft.entity.player.InventoryPlayer; import net.minecraft.entity.player.InventoryPlayer;
import net.minecraft.init.Items; import net.minecraft.init.Items;
import net.minecraft.item.ItemStack; import net.minecraft.item.ItemStack;
import net.minecraft.world.World;
/** /**
* @author Brady * @author Brady
* @since 8/7/2018 4:30 PM * @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 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 ToolSet toolSet;
private final boolean hasWaterBucket; private final boolean hasWaterBucket;
private final boolean hasThrowaway; private final boolean hasThrowaway;
@ -48,15 +52,17 @@ public class CalculationContext implements Helper {
private final BetterWorldBorder worldBorder; private final BetterWorldBorder worldBorder;
public CalculationContext() { 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.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.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.canSprint = Baritone.settings().allowSprint.get() && player.getFoodStats().getFoodLevel() > 6;
this.placeBlockCost = Baritone.settings().blockPlacementPenalty.get(); this.placeBlockCost = Baritone.settings().blockPlacementPenalty.get();
this.allowBreak = Baritone.settings().allowBreak.get(); this.allowBreak = Baritone.settings().allowBreak.get();
this.maxFallHeightNoWater = Baritone.settings().maxFallHeightNoWater.get(); this.maxFallHeightNoWater = Baritone.settings().maxFallHeightNoWater.get();
this.maxFallHeightBucket = Baritone.settings().maxFallHeightBucket.get(); this.maxFallHeightBucket = Baritone.settings().maxFallHeightBucket.get();
int depth = EnchantmentHelper.getDepthStriderModifier(player()); int depth = EnchantmentHelper.getDepthStriderModifier(player);
if (depth > 3) { if (depth > 3) {
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? // 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, // 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. // 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) { public boolean canPlaceThrowawayAt(int x, int y, int z) {
@ -91,6 +97,15 @@ public class CalculationContext implements Helper {
return false; return false;
} }
public World world() {
return world;
}
public EntityPlayerSP player() {
return player;
}
public ToolSet getToolSet() { public ToolSet getToolSet() {
return toolSet; return toolSet;
} }

View File

@ -383,7 +383,7 @@ public interface MovementHelper extends ActionCosts, Helper {
* @param b the blockstate to mine * @param b the blockstate to mine
*/ */
static void switchToBestToolFor(IBlockState b) { static void switchToBestToolFor(IBlockState b) {
switchToBestToolFor(b, new ToolSet()); switchToBestToolFor(b, new ToolSet(Helper.HELPER.player()));
} }
/** /**

View File

@ -30,7 +30,7 @@ import net.minecraft.util.EnumFacing;
public enum Moves { public enum Moves {
DOWNWARD(0, -1, 0) { DOWNWARD(0, -1, 0) {
@Override @Override
public Movement apply0(BetterBlockPos src) { public Movement apply0(CalculationContext context, BetterBlockPos src) {
return new MovementDownward(src, src.down()); return new MovementDownward(src, src.down());
} }
@ -42,7 +42,7 @@ public enum Moves {
PILLAR(0, +1, 0) { PILLAR(0, +1, 0) {
@Override @Override
public Movement apply0(BetterBlockPos src) { public Movement apply0(CalculationContext context, BetterBlockPos src) {
return new MovementPillar(src, src.up()); return new MovementPillar(src, src.up());
} }
@ -54,7 +54,7 @@ public enum Moves {
TRAVERSE_NORTH(0, 0, -1) { TRAVERSE_NORTH(0, 0, -1) {
@Override @Override
public Movement apply0(BetterBlockPos src) { public Movement apply0(CalculationContext context, BetterBlockPos src) {
return new MovementTraverse(src, src.north()); return new MovementTraverse(src, src.north());
} }
@ -66,7 +66,7 @@ public enum Moves {
TRAVERSE_SOUTH(0, 0, +1) { TRAVERSE_SOUTH(0, 0, +1) {
@Override @Override
public Movement apply0(BetterBlockPos src) { public Movement apply0(CalculationContext context, BetterBlockPos src) {
return new MovementTraverse(src, src.south()); return new MovementTraverse(src, src.south());
} }
@ -78,7 +78,7 @@ public enum Moves {
TRAVERSE_EAST(+1, 0, 0) { TRAVERSE_EAST(+1, 0, 0) {
@Override @Override
public Movement apply0(BetterBlockPos src) { public Movement apply0(CalculationContext context, BetterBlockPos src) {
return new MovementTraverse(src, src.east()); return new MovementTraverse(src, src.east());
} }
@ -90,7 +90,7 @@ public enum Moves {
TRAVERSE_WEST(-1, 0, 0) { TRAVERSE_WEST(-1, 0, 0) {
@Override @Override
public Movement apply0(BetterBlockPos src) { public Movement apply0(CalculationContext context, BetterBlockPos src) {
return new MovementTraverse(src, src.west()); return new MovementTraverse(src, src.west());
} }
@ -102,7 +102,7 @@ public enum Moves {
ASCEND_NORTH(0, +1, -1) { ASCEND_NORTH(0, +1, -1) {
@Override @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)); 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) { ASCEND_SOUTH(0, +1, +1) {
@Override @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)); 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) { ASCEND_EAST(+1, +1, 0) {
@Override @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)); 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) { ASCEND_WEST(-1, +1, 0) {
@Override @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)); 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) { DESCEND_EAST(+1, -1, 0, false, true) {
@Override @Override
public Movement apply0(BetterBlockPos src) { public Movement apply0(CalculationContext context, BetterBlockPos src) {
MutableMoveResult res = new MutableMoveResult(); 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) { if (res.y == src.y - 1) {
return new MovementDescend(src, new BetterBlockPos(res.x, res.y, res.z)); return new MovementDescend(src, new BetterBlockPos(res.x, res.y, res.z));
} else { } else {
@ -168,9 +168,9 @@ public enum Moves {
DESCEND_WEST(-1, -1, 0, false, true) { DESCEND_WEST(-1, -1, 0, false, true) {
@Override @Override
public Movement apply0(BetterBlockPos src) { public Movement apply0(CalculationContext context, BetterBlockPos src) {
MutableMoveResult res = new MutableMoveResult(); 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) { if (res.y == src.y - 1) {
return new MovementDescend(src, new BetterBlockPos(res.x, res.y, res.z)); return new MovementDescend(src, new BetterBlockPos(res.x, res.y, res.z));
} else { } else {
@ -186,9 +186,9 @@ public enum Moves {
DESCEND_NORTH(0, -1, -1, false, true) { DESCEND_NORTH(0, -1, -1, false, true) {
@Override @Override
public Movement apply0(BetterBlockPos src) { public Movement apply0(CalculationContext context, BetterBlockPos src) {
MutableMoveResult res = new MutableMoveResult(); 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) { if (res.y == src.y - 1) {
return new MovementDescend(src, new BetterBlockPos(res.x, res.y, res.z)); return new MovementDescend(src, new BetterBlockPos(res.x, res.y, res.z));
} else { } else {
@ -204,9 +204,9 @@ public enum Moves {
DESCEND_SOUTH(0, -1, +1, false, true) { DESCEND_SOUTH(0, -1, +1, false, true) {
@Override @Override
public Movement apply0(BetterBlockPos src) { public Movement apply0(CalculationContext context, BetterBlockPos src) {
MutableMoveResult res = new MutableMoveResult(); 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) { if (res.y == src.y - 1) {
return new MovementDescend(src, new BetterBlockPos(res.x, res.y, res.z)); return new MovementDescend(src, new BetterBlockPos(res.x, res.y, res.z));
} else { } else {
@ -222,7 +222,7 @@ public enum Moves {
DIAGONAL_NORTHEAST(+1, 0, -1) { DIAGONAL_NORTHEAST(+1, 0, -1) {
@Override @Override
public Movement apply0(BetterBlockPos src) { public Movement apply0(CalculationContext context, BetterBlockPos src) {
return new MovementDiagonal(src, EnumFacing.NORTH, EnumFacing.EAST); return new MovementDiagonal(src, EnumFacing.NORTH, EnumFacing.EAST);
} }
@ -234,7 +234,7 @@ public enum Moves {
DIAGONAL_NORTHWEST(-1, 0, -1) { DIAGONAL_NORTHWEST(-1, 0, -1) {
@Override @Override
public Movement apply0(BetterBlockPos src) { public Movement apply0(CalculationContext context, BetterBlockPos src) {
return new MovementDiagonal(src, EnumFacing.NORTH, EnumFacing.WEST); return new MovementDiagonal(src, EnumFacing.NORTH, EnumFacing.WEST);
} }
@ -246,7 +246,7 @@ public enum Moves {
DIAGONAL_SOUTHEAST(+1, 0, +1) { DIAGONAL_SOUTHEAST(+1, 0, +1) {
@Override @Override
public Movement apply0(BetterBlockPos src) { public Movement apply0(CalculationContext context, BetterBlockPos src) {
return new MovementDiagonal(src, EnumFacing.SOUTH, EnumFacing.EAST); return new MovementDiagonal(src, EnumFacing.SOUTH, EnumFacing.EAST);
} }
@ -258,7 +258,7 @@ public enum Moves {
DIAGONAL_SOUTHWEST(-1, 0, +1) { DIAGONAL_SOUTHWEST(-1, 0, +1) {
@Override @Override
public Movement apply0(BetterBlockPos src) { public Movement apply0(CalculationContext context, BetterBlockPos src) {
return new MovementDiagonal(src, EnumFacing.SOUTH, EnumFacing.WEST); return new MovementDiagonal(src, EnumFacing.SOUTH, EnumFacing.WEST);
} }
@ -270,8 +270,8 @@ public enum Moves {
PARKOUR_NORTH(0, 0, -4, true, false) { PARKOUR_NORTH(0, 0, -4, true, false) {
@Override @Override
public Movement apply0(BetterBlockPos src) { public Movement apply0(CalculationContext context, BetterBlockPos src) {
return MovementParkour.cost(new CalculationContext(), src, EnumFacing.NORTH); return MovementParkour.cost(context, src, EnumFacing.NORTH);
} }
@Override @Override
@ -282,8 +282,8 @@ public enum Moves {
PARKOUR_SOUTH(0, 0, +4, true, false) { PARKOUR_SOUTH(0, 0, +4, true, false) {
@Override @Override
public Movement apply0(BetterBlockPos src) { public Movement apply0(CalculationContext context, BetterBlockPos src) {
return MovementParkour.cost(new CalculationContext(), src, EnumFacing.SOUTH); return MovementParkour.cost(context, src, EnumFacing.SOUTH);
} }
@Override @Override
@ -294,8 +294,8 @@ public enum Moves {
PARKOUR_EAST(+4, 0, 0, true, false) { PARKOUR_EAST(+4, 0, 0, true, false) {
@Override @Override
public Movement apply0(BetterBlockPos src) { public Movement apply0(CalculationContext context, BetterBlockPos src) {
return MovementParkour.cost(new CalculationContext(), src, EnumFacing.EAST); return MovementParkour.cost(context, src, EnumFacing.EAST);
} }
@Override @Override
@ -306,8 +306,8 @@ public enum Moves {
PARKOUR_WEST(-4, 0, 0, true, false) { PARKOUR_WEST(-4, 0, 0, true, false) {
@Override @Override
public Movement apply0(BetterBlockPos src) { public Movement apply0(CalculationContext context, BetterBlockPos src) {
return MovementParkour.cost(new CalculationContext(), src, EnumFacing.WEST); return MovementParkour.cost(context, src, EnumFacing.WEST);
} }
@Override @Override
@ -335,7 +335,7 @@ public enum Moves {
this(x, y, z, false, false); 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) { public void apply(CalculationContext context, int x, int y, int z, MutableMoveResult result) {
if (dynamicXZ || dynamicY) { if (dynamicXZ || dynamicY) {

View File

@ -31,6 +31,7 @@ import baritone.cache.ChunkPacker;
import baritone.cache.Waypoint; import baritone.cache.Waypoint;
import baritone.cache.WorldProvider; import baritone.cache.WorldProvider;
import baritone.pathing.calc.AbstractNodeCostSearch; import baritone.pathing.calc.AbstractNodeCostSearch;
import baritone.pathing.movement.CalculationContext;
import baritone.pathing.movement.Movement; import baritone.pathing.movement.Movement;
import baritone.pathing.movement.Moves; import baritone.pathing.movement.Moves;
import baritone.process.CustomGoalProcess; import baritone.process.CustomGoalProcess;
@ -450,7 +451,7 @@ public class ExampleBaritoneControl extends Behavior implements Helper {
return true; return true;
} }
if (msg.equals("costs")) { if (msg.equals("costs")) {
List<Movement> moves = Stream.of(Moves.values()).map(x -> x.apply0(playerFeet())).collect(Collectors.toCollection(ArrayList::new)); List<Movement> moves = Stream.of(Moves.values()).map(x -> x.apply0(new CalculationContext(), playerFeet())).collect(Collectors.toCollection(ArrayList::new));
while (moves.contains(null)) { while (moves.contains(null)) {
moves.remove(null); moves.remove(null);
} }

View File

@ -51,6 +51,9 @@ public interface Helper {
Minecraft mc = Minecraft.getMinecraft(); Minecraft mc = Minecraft.getMinecraft();
default EntityPlayerSP player() { default EntityPlayerSP player() {
if (!mc.isCallingFromMinecraftThread()) {
throw new IllegalStateException("h00000000");
}
return mc.player; return mc.player;
} }

View File

@ -20,6 +20,7 @@ package baritone.utils;
import baritone.Baritone; import baritone.Baritone;
import net.minecraft.block.Block; import net.minecraft.block.Block;
import net.minecraft.block.state.IBlockState; import net.minecraft.block.state.IBlockState;
import net.minecraft.client.entity.EntityPlayerSP;
import net.minecraft.enchantment.EnchantmentHelper; import net.minecraft.enchantment.EnchantmentHelper;
import net.minecraft.init.Enchantments; import net.minecraft.init.Enchantments;
import net.minecraft.init.MobEffects; import net.minecraft.init.MobEffects;
@ -36,7 +37,7 @@ import java.util.function.Function;
* *
* @author Avery, Brady, leijurv * @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 * A cache mapping a {@link Block} to how long it will take to break
* with this toolset, given the optimum tool is used. * with this toolset, given the optimum tool is used.
@ -48,8 +49,11 @@ public class ToolSet implements Helper {
*/ */
private final Function<Block, Double> backendCalculation; private final Function<Block, Double> backendCalculation;
public ToolSet() { private final EntityPlayerSP player;
public ToolSet(EntityPlayerSP player) {
breakStrengthCache = new HashMap<>(); breakStrengthCache = new HashMap<>();
this.player = player;
if (Baritone.settings().considerPotionEffects.get()) { if (Baritone.settings().considerPotionEffects.get()) {
double amplifier = potionAmplifier(); double amplifier = potionAmplifier();
@ -98,7 +102,7 @@ public class ToolSet implements Helper {
int materialCost = Integer.MIN_VALUE; int materialCost = Integer.MIN_VALUE;
IBlockState blockState = b.getDefaultState(); IBlockState blockState = b.getDefaultState();
for (byte i = 0; i < 9; i++) { for (byte i = 0; i < 9; i++) {
ItemStack itemStack = player().inventory.getStackInSlot(i); ItemStack itemStack = player.inventory.getStackInSlot(i);
double v = calculateStrVsBlock(itemStack, blockState); double v = calculateStrVsBlock(itemStack, blockState);
if (v > value) { if (v > value) {
value = v; value = v;
@ -123,7 +127,7 @@ public class ToolSet implements Helper {
* @return A double containing the destruction ticks with the best tool * @return A double containing the destruction ticks with the best tool
*/ */
private double getBestDestructionTime(Block b) { private double getBestDestructionTime(Block b) {
ItemStack stack = player().inventory.getStackInSlot(getBestSlot(b)); ItemStack stack = player.inventory.getStackInSlot(getBestSlot(b));
return calculateStrVsBlock(stack, b.getDefaultState()); return calculateStrVsBlock(stack, b.getDefaultState());
} }
@ -164,11 +168,11 @@ public class ToolSet implements Helper {
*/ */
private double potionAmplifier() { private double potionAmplifier() {
double speed = 1; double speed = 1;
if (player().isPotionActive(MobEffects.HASTE)) { if (player.isPotionActive(MobEffects.HASTE)) {
speed *= 1 + (player().getActivePotionEffect(MobEffects.HASTE).getAmplifier() + 1) * 0.2; speed *= 1 + (player.getActivePotionEffect(MobEffects.HASTE).getAmplifier() + 1) * 0.2;
} }
if (player().isPotionActive(MobEffects.MINING_FATIGUE)) { if (player.isPotionActive(MobEffects.MINING_FATIGUE)) {
switch (player().getActivePotionEffect(MobEffects.MINING_FATIGUE).getAmplifier()) { switch (player.getActivePotionEffect(MobEffects.MINING_FATIGUE).getAmplifier()) {
case 0: case 0:
speed *= 0.3; speed *= 0.3;
break; break;