Explode MovementHelper methods into more fitting places

This commit is contained in:
Howard Stark 2018-08-06 14:32:54 -07:00
parent ee4ed0cc65
commit 1397b137ae
No known key found for this signature in database
GPG Key ID: 9FA4E350B33067F3
7 changed files with 94 additions and 88 deletions

View File

@ -36,7 +36,7 @@ public final class LookBehaviorUtils implements Helper {
}
public static Optional<Rotation> reachable(BlockPos pos) {
if (pos.equals(MovementHelper.whatAmILookingAt().orElse(null))) {
if (pos.equals(getSelectedBlock().orElse(null))) {
return Optional.of(new Rotation(mc.player.rotationYaw, mc.player.rotationPitch));
}
Optional<Rotation> possibleRotation = reachableCenter(pos);
@ -104,4 +104,17 @@ public final class LookBehaviorUtils implements Helper {
return Optional.empty();
}
/**
* The currently highlighted block.
* Updated once a tick by Minecraft.
*
* @return the position of the highlighted block
*/
public static Optional<BlockPos> getSelectedBlock() {
if (mc.objectMouseOver != null && mc.objectMouseOver.typeOfHit == RayTraceResult.Type.BLOCK) {
return Optional.of(mc.objectMouseOver.getBlockPos());
}
return Optional.empty();
}
}

View File

@ -2,6 +2,7 @@ package baritone.bot.chunk;
import baritone.bot.pathing.movement.MovementHelper;
import baritone.bot.pathing.util.PathingBlockType;
import baritone.bot.utils.BlockStateInterface;
import baritone.bot.utils.Helper;
import net.minecraft.block.Block;
import net.minecraft.block.BlockAir;
@ -43,7 +44,7 @@ public final class ChunkPacker implements Helper {
private static PathingBlockType getPathingBlockType(BlockPos pos, IBlockState state) {
Block block = state.getBlock();
if (MovementHelper.isWater(block)) {
if (BlockStateInterface.isWater(block)) {
return PathingBlockType.WATER;
}

View File

@ -1,5 +1,6 @@
package baritone.bot.pathing.movement;
import baritone.bot.behavior.impl.LookBehaviorUtils;
import baritone.bot.utils.BlockStateInterface;
import baritone.bot.utils.Helper;
import baritone.bot.utils.ToolSet;
@ -20,68 +21,17 @@ import java.util.Optional;
*/
public interface MovementHelper extends ActionCosts, Helper {
Block waterFlowing = Blocks.FLOWING_WATER;
Block waterStill = Blocks.WATER;
Block lavaFlowing = Blocks.FLOWING_LAVA;
Block lavaStill = Blocks.LAVA;
/**
* Returns whether or not the specified block is
* water, regardless of whether or not it is flowing.
*
* @param b The block
* @return Whether or not the block is water
*/
static boolean isWater(Block b) {
return waterFlowing.equals(b) || waterStill.equals(b);
}
/**
* Returns whether or not the block at the specified pos is
* water, regardless of whether or not it is flowing.
*
* @param bp The block pos
* @return Whether or not the block is water
*/
static boolean isWater(BlockPos bp) {
return isWater(BlockStateInterface.get(bp).getBlock());
}
/**
* Returns whether or not the specified block is any sort of liquid.
*
* @param b The block
* @return Whether or not the block is a liquid
*/
static boolean isLiquid(Block b) {
return b instanceof BlockLiquid;
}
static boolean isLiquid(BlockPos p) {
return isLiquid(BlockStateInterface.get(p).getBlock());
}
static boolean isFlowing(IBlockState state) {
return state.getBlock() instanceof BlockLiquid
&& state.getPropertyKeys().contains(BlockLiquid.LEVEL)
&& state.getValue(BlockLiquid.LEVEL) != 0;
}
static boolean isLava(Block b) {
return lavaFlowing.equals(b) || lavaStill.equals(b);
}
static boolean avoidBreaking(BlockPos pos) {
Block b = BlockStateInterface.get(pos).getBlock();
Block b = BlockStateInterface.getBlock(pos);
Block below = BlockStateInterface.get(new BlockPos(pos.getX(), pos.getY() - 1, pos.getZ())).getBlock();
return Blocks.ICE.equals(b) // ice becomes water, and water can mess up the path
|| b instanceof BlockSilverfish
|| isLiquid(new BlockPos(pos.getX(), pos.getY() + 1, pos.getZ()))//don't break anything touching liquid on any side
|| isLiquid(new BlockPos(pos.getX() + 1, pos.getY(), pos.getZ()))
|| isLiquid(new BlockPos(pos.getX() - 1, pos.getY(), pos.getZ()))
|| isLiquid(new BlockPos(pos.getX(), pos.getY(), pos.getZ() + 1))
|| isLiquid(new BlockPos(pos.getX(), pos.getY(), pos.getZ() - 1))
|| (!(b instanceof BlockLilyPad && isWater(below)) && isLiquid(below));//if it's a lilypad above water, it's ok to break, otherwise don't break if its liquid
|| BlockStateInterface.isLiquid(new BlockPos(pos.getX(), pos.getY() + 1, pos.getZ()))//don't break anything touching liquid on any side
|| BlockStateInterface.isLiquid(new BlockPos(pos.getX() + 1, pos.getY(), pos.getZ()))
|| BlockStateInterface.isLiquid(new BlockPos(pos.getX() - 1, pos.getY(), pos.getZ()))
|| BlockStateInterface.isLiquid(new BlockPos(pos.getX(), pos.getY(), pos.getZ() + 1))
|| BlockStateInterface.isLiquid(new BlockPos(pos.getX(), pos.getY(), pos.getZ() - 1))
|| (!(b instanceof BlockLilyPad && BlockStateInterface.isWater(below)) && BlockStateInterface.isLiquid(below));//if it's a lilypad above water, it's ok to break, otherwise don't break if its liquid
}
/**
@ -97,14 +47,14 @@ public interface MovementHelper extends ActionCosts, Helper {
|| block instanceof BlockTripWire) {//you can't actually walk through a lilypad from the side, and you shouldn't walk through fire
return false;
}
if (isFlowing(state) || isLiquid(pos.up())) {
if (BlockStateInterface.isFlowing(state) || BlockStateInterface.isLiquid(pos.up())) {
return false; // Don't walk through flowing liquids
}
return block.isPassable(Minecraft.getMinecraft().world, pos);
return block.isPassable(mc.world, pos);
}
static boolean avoidWalkingInto(Block block) {
return isLava(block)
return BlockStateInterface.isLava(block)
|| block instanceof BlockCactus
|| block instanceof BlockFire
|| block instanceof BlockEndPortal
@ -126,10 +76,10 @@ public interface MovementHelper extends ActionCosts, Helper {
if (block instanceof BlockAir) {
return false;
}
if (isWater(block)) {
return isWater(pos.up()); // You can only walk on water if there is water above it
if (BlockStateInterface.isWater(block)) {
return BlockStateInterface.isWater(pos.up()); // You can only walk on water if there is water above it
}
return state.isBlockNormalCube() && !isLava(block);
return state.isBlockNormalCube() && !BlockStateInterface.isLava(block);
}
@ -151,19 +101,6 @@ public interface MovementHelper extends ActionCosts, Helper {
return 0;
}
/**
* The currently highlighted block.
* Updated once a tick by Minecraft.
*
* @return the position of the highlighted block
*/
static Optional<BlockPos> whatAmILookingAt() {
if (mc.objectMouseOver != null && mc.objectMouseOver.typeOfHit == RayTraceResult.Type.BLOCK) {
return Optional.of(mc.objectMouseOver.getBlockPos());
}
return Optional.empty();
}
/**
* The entity the player is currently looking at
*
@ -180,7 +117,7 @@ public interface MovementHelper extends ActionCosts, Helper {
* AutoTool
*/
static void switchToBestTool() {
whatAmILookingAt().ifPresent(pos -> {
LookBehaviorUtils.getSelectedBlock().ifPresent(pos -> {
IBlockState state = BlockStateInterface.get(pos);
if (state.getBlock().equals(Blocks.AIR)) {
return;
@ -207,8 +144,4 @@ public interface MovementHelper extends ActionCosts, Helper {
static void switchToBestToolFor(IBlockState b, ToolSet ts) {
mc.player.inventory.currentItem = ts.getBestSlot(b);
}
static boolean isAir(BlockPos pos) {
return BlockStateInterface.get(pos).getBlock().equals(Blocks.AIR);
}
}

View File

@ -44,7 +44,7 @@ public class MovementAscend extends Movement {
@Override
protected double calculateCost(ToolSet ts) {
if (!MovementHelper.canWalkOn(positionsToPlace[0], BlockStateInterface.get(positionsToPlace[0]))) {
if (!MovementHelper.isAir(positionsToPlace[0]) && !MovementHelper.isWater(positionsToPlace[0])) {
if (!BlockStateInterface.isAir(positionsToPlace[0]) && !BlockStateInterface.isWater(positionsToPlace[0])) {
return COST_INF;
}
if (true) {

View File

@ -46,7 +46,7 @@ public class MovementTraverse extends Movement {
protected double calculateCost(ToolSet ts) {
IBlockState pb0 = BlockStateInterface.get(positionsToBreak[0]);
IBlockState pb1 = BlockStateInterface.get(positionsToBreak[1]);
double WC = MovementHelper.isWater(pb0.getBlock()) || MovementHelper.isWater(pb1.getBlock()) ? WALK_ONE_IN_WATER_COST : WALK_ONE_BLOCK_COST;
double WC = BlockStateInterface.isWater(pb0.getBlock()) || BlockStateInterface.isWater(pb1.getBlock()) ? WALK_ONE_IN_WATER_COST : WALK_ONE_BLOCK_COST;
if (MovementHelper.canWalkOn(positionsToPlace[0], BlockStateInterface.get(positionsToPlace[0]))) {//this is a walk, not a bridge
if (MovementHelper.canWalkThrough(positionsToBreak[0], pb0) && MovementHelper.canWalkThrough(positionsToBreak[1], pb1)) {
return WC;
@ -66,7 +66,7 @@ public class MovementTraverse extends Movement {
return COST_INF;
}
IBlockState pp0 = BlockStateInterface.get(positionsToPlace[0]);
if (pp0.getBlock().equals(Blocks.AIR) || (!MovementHelper.isWater(pp0.getBlock()) && pp0.getBlock().isReplaceable(Minecraft.getMinecraft().world, positionsToPlace[0]))) {
if (pp0.getBlock().equals(Blocks.AIR) || (!BlockStateInterface.isWater(pp0.getBlock()) && pp0.getBlock().isReplaceable(Minecraft.getMinecraft().world, positionsToPlace[0]))) {
for (BlockPos against1 : against) {
if (BlockStateInterface.get(against1).isBlockNormalCube()) {
return WC + PLACE_ONE_BLOCK_COST + getTotalHardnessOfBlocksToBreak(ts);

View File

@ -1,8 +1,10 @@
package baritone.bot.utils;
import net.minecraft.block.Block;
import net.minecraft.block.BlockLiquid;
import net.minecraft.block.state.IBlockState;
import net.minecraft.client.Minecraft;
import net.minecraft.init.Blocks;
import net.minecraft.util.math.BlockPos;
public class BlockStateInterface {
@ -13,4 +15,60 @@ public class BlockStateInterface {
public static Block getBlock(BlockPos pos) {
return get(pos).getBlock();
}
public static final Block waterFlowing = Blocks.FLOWING_WATER;
public static final Block waterStill = Blocks.WATER;
public static final Block lavaFlowing = Blocks.FLOWING_LAVA;
public static final Block lavaStill = Blocks.LAVA;
/**
* Returns whether or not the specified block is
* water, regardless of whether or not it is flowing.
*
* @param b The block
* @return Whether or not the block is water
*/
public static boolean isWater(Block b) {
return waterFlowing.equals(b) || waterStill.equals(b);
}
/**
* Returns whether or not the block at the specified pos is
* water, regardless of whether or not it is flowing.
*
* @param bp The block pos
* @return Whether or not the block is water
*/
public static boolean isWater(BlockPos bp) {
return isWater(BlockStateInterface.get(bp).getBlock());
}
public static boolean isLava(Block b) {
return lavaFlowing.equals(b) || lavaStill.equals(b);
}
/**
* Returns whether or not the specified block is any sort of liquid.
*
* @param b The block
* @return Whether or not the block is a liquid
*/
public static boolean isLiquid(Block b) {
return b instanceof BlockLiquid;
}
public static boolean isLiquid(BlockPos p) {
return isLiquid(BlockStateInterface.get(p).getBlock());
}
public static boolean isFlowing(IBlockState state) {
return state.getBlock() instanceof BlockLiquid
&& state.getPropertyKeys().contains(BlockLiquid.LEVEL)
&& state.getValue(BlockLiquid.LEVEL) != 0;
}
public static boolean isAir(BlockPos pos) {
return BlockStateInterface.get(pos).getBlock().equals(Blocks.AIR);
}
}

View File

@ -1,5 +1,6 @@
package baritone.bot.utils;
import net.minecraft.block.Block;
import net.minecraft.block.state.IBlockState;
import net.minecraft.util.math.AxisAlignedBB;
import net.minecraft.util.math.BlockPos;
@ -32,7 +33,7 @@ public final class Utils {
}
public static Vec3d calcCenterFromCoords(BlockPos orig, World world) {
IBlockState b = world.getBlockState(orig);
IBlockState b = BlockStateInterface.get(orig);
AxisAlignedBB bbox = b.getBoundingBox(world, orig);
double xDiff = (bbox.minX + bbox.maxX) / 2;
double yDiff = (bbox.minY + bbox.maxY) / 2;