From 1397b137aec8e5c44bc4b56b0cb065f834af7770 Mon Sep 17 00:00:00 2001 From: Howard Stark Date: Mon, 6 Aug 2018 14:32:54 -0700 Subject: [PATCH] Explode MovementHelper methods into more fitting places --- .../bot/behavior/impl/LookBehaviorUtils.java | 15 ++- .../java/baritone/bot/chunk/ChunkPacker.java | 3 +- .../bot/pathing/movement/MovementHelper.java | 97 +++---------------- .../movement/movements/MovementAscend.java | 2 +- .../movement/movements/MovementTraverse.java | 4 +- .../bot/utils/BlockStateInterface.java | 58 +++++++++++ src/main/java/baritone/bot/utils/Utils.java | 3 +- 7 files changed, 94 insertions(+), 88 deletions(-) diff --git a/src/main/java/baritone/bot/behavior/impl/LookBehaviorUtils.java b/src/main/java/baritone/bot/behavior/impl/LookBehaviorUtils.java index 4b3750838..898fd5b92 100644 --- a/src/main/java/baritone/bot/behavior/impl/LookBehaviorUtils.java +++ b/src/main/java/baritone/bot/behavior/impl/LookBehaviorUtils.java @@ -36,7 +36,7 @@ public final class LookBehaviorUtils implements Helper { } public static Optional 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 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 getSelectedBlock() { + if (mc.objectMouseOver != null && mc.objectMouseOver.typeOfHit == RayTraceResult.Type.BLOCK) { + return Optional.of(mc.objectMouseOver.getBlockPos()); + } + return Optional.empty(); + } + } diff --git a/src/main/java/baritone/bot/chunk/ChunkPacker.java b/src/main/java/baritone/bot/chunk/ChunkPacker.java index 883a79696..5e4881316 100644 --- a/src/main/java/baritone/bot/chunk/ChunkPacker.java +++ b/src/main/java/baritone/bot/chunk/ChunkPacker.java @@ -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; } diff --git a/src/main/java/baritone/bot/pathing/movement/MovementHelper.java b/src/main/java/baritone/bot/pathing/movement/MovementHelper.java index 8baf94d1d..09ab12ac6 100644 --- a/src/main/java/baritone/bot/pathing/movement/MovementHelper.java +++ b/src/main/java/baritone/bot/pathing/movement/MovementHelper.java @@ -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 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); - } } diff --git a/src/main/java/baritone/bot/pathing/movement/movements/MovementAscend.java b/src/main/java/baritone/bot/pathing/movement/movements/MovementAscend.java index fd55ed7f3..d27c5222e 100644 --- a/src/main/java/baritone/bot/pathing/movement/movements/MovementAscend.java +++ b/src/main/java/baritone/bot/pathing/movement/movements/MovementAscend.java @@ -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) { diff --git a/src/main/java/baritone/bot/pathing/movement/movements/MovementTraverse.java b/src/main/java/baritone/bot/pathing/movement/movements/MovementTraverse.java index 286abfac5..3bc64d53b 100644 --- a/src/main/java/baritone/bot/pathing/movement/movements/MovementTraverse.java +++ b/src/main/java/baritone/bot/pathing/movement/movements/MovementTraverse.java @@ -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); diff --git a/src/main/java/baritone/bot/utils/BlockStateInterface.java b/src/main/java/baritone/bot/utils/BlockStateInterface.java index 4c7d88c32..9df8090d9 100644 --- a/src/main/java/baritone/bot/utils/BlockStateInterface.java +++ b/src/main/java/baritone/bot/utils/BlockStateInterface.java @@ -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); + } + } diff --git a/src/main/java/baritone/bot/utils/Utils.java b/src/main/java/baritone/bot/utils/Utils.java index 081e25993..3bec70862 100755 --- a/src/main/java/baritone/bot/utils/Utils.java +++ b/src/main/java/baritone/bot/utils/Utils.java @@ -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;