From 933b295c4062f6deba8d51ea3a35258674e8b50d Mon Sep 17 00:00:00 2001 From: Leijurv Date: Wed, 14 Nov 2018 13:46:16 -0800 Subject: [PATCH] context objectMouseOver --- .../baritone/api/utils/IPlayerContext.java | 31 +++++++++++++++++++ .../baritone/api/utils/RayTraceUtils.java | 30 ------------------ .../baritone/api/utils/RotationUtils.java | 8 +++-- .../baritone/pathing/movement/Movement.java | 2 +- .../movement/movements/MovementAscend.java | 6 ++-- .../movement/movements/MovementParkour.java | 5 ++- .../movement/movements/MovementTraverse.java | 12 ++++--- .../utils/ExampleBaritoneControl.java | 3 +- .../java/baritone/utils/PathRenderer.java | 5 ++- .../utils/player/LocalPlayerContext.java | 6 ++++ 10 files changed, 58 insertions(+), 50 deletions(-) diff --git a/src/api/java/baritone/api/utils/IPlayerContext.java b/src/api/java/baritone/api/utils/IPlayerContext.java index c7e7e24a..cd80a7d9 100644 --- a/src/api/java/baritone/api/utils/IPlayerContext.java +++ b/src/api/java/baritone/api/utils/IPlayerContext.java @@ -21,9 +21,14 @@ import baritone.api.cache.IWorldData; import net.minecraft.block.BlockSlab; import net.minecraft.client.entity.EntityPlayerSP; import net.minecraft.client.multiplayer.PlayerControllerMP; +import net.minecraft.entity.Entity; +import net.minecraft.util.math.BlockPos; +import net.minecraft.util.math.RayTraceResult; import net.minecraft.util.math.Vec3d; import net.minecraft.world.World; +import java.util.Optional; + /** * @author Brady * @since 11/12/2018 @@ -38,6 +43,8 @@ public interface IPlayerContext { IWorldData worldData(); + RayTraceResult objectMouseOver(); + default BetterBlockPos playerFeet() { // TODO find a better way to deal with soul sand!!!!! BetterBlockPos feet = new BetterBlockPos(player().posX, player().posY + 0.1251, player().posZ); @@ -58,4 +65,28 @@ public interface IPlayerContext { default Rotation playerRotations() { return new Rotation(player().rotationYaw, player().rotationPitch); } + + /** + * Returns the block that the crosshair is currently placed over. Updated once per tick. + * + * @return The position of the highlighted block + */ + default Optional getSelectedBlock() { + if (objectMouseOver() != null && objectMouseOver().typeOfHit == RayTraceResult.Type.BLOCK) { + return Optional.of(objectMouseOver().getBlockPos()); + } + return Optional.empty(); + } + + /** + * Returns the entity that the crosshair is currently placed over. Updated once per tick. + * + * @return The entity + */ + default Optional getSelectedEntity() { + if (objectMouseOver() != null && objectMouseOver().typeOfHit == RayTraceResult.Type.ENTITY) { + return Optional.of(objectMouseOver().entityHit); + } + return Optional.empty(); + } } diff --git a/src/api/java/baritone/api/utils/RayTraceUtils.java b/src/api/java/baritone/api/utils/RayTraceUtils.java index 13f1081b..61086f3d 100644 --- a/src/api/java/baritone/api/utils/RayTraceUtils.java +++ b/src/api/java/baritone/api/utils/RayTraceUtils.java @@ -17,22 +17,16 @@ package baritone.api.utils; -import net.minecraft.client.Minecraft; import net.minecraft.entity.Entity; -import net.minecraft.util.math.BlockPos; import net.minecraft.util.math.RayTraceResult; import net.minecraft.util.math.Vec3d; -import java.util.Optional; - /** * @author Brady * @since 8/25/2018 */ public final class RayTraceUtils { - private static final Minecraft mc = Minecraft.getMinecraft(); - private RayTraceUtils() {} /** @@ -53,28 +47,4 @@ public final class RayTraceUtils { ); return entity.world.rayTraceBlocks(start, end, false, false, true); } - - /** - * Returns the block that the crosshair is currently placed over. Updated once per render tick. - * - * @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(); - } - - /** - * Returns the entity that the crosshair is currently placed over. Updated once per render tick. - * - * @return The entity - */ - public static Optional getSelectedEntity() { - if (mc.objectMouseOver != null && mc.objectMouseOver.typeOfHit == RayTraceResult.Type.ENTITY) { - return Optional.of(mc.objectMouseOver.entityHit); - } - return Optional.empty(); - } } diff --git a/src/api/java/baritone/api/utils/RotationUtils.java b/src/api/java/baritone/api/utils/RotationUtils.java index b527287e..20cb0dde 100644 --- a/src/api/java/baritone/api/utils/RotationUtils.java +++ b/src/api/java/baritone/api/utils/RotationUtils.java @@ -17,8 +17,11 @@ package baritone.api.utils; +import baritone.api.BaritoneAPI; +import baritone.api.IBaritone; import net.minecraft.block.BlockFire; import net.minecraft.block.state.IBlockState; +import net.minecraft.client.entity.EntityPlayerSP; import net.minecraft.entity.Entity; import net.minecraft.util.math.*; @@ -135,8 +138,9 @@ public final class RotationUtils { * @param pos The target block position * @return The optional rotation */ - public static Optional reachable(Entity entity, BlockPos pos, double blockReachDistance) { - if (pos.equals(RayTraceUtils.getSelectedBlock().orElse(null))) { + public static Optional reachable(EntityPlayerSP entity, BlockPos pos, double blockReachDistance) { + IBaritone baritone = BaritoneAPI.getProvider().getBaritoneForPlayer(entity); + if (pos.equals(baritone.getPlayerContext().getSelectedBlock().orElse(null))) { /* * why add 0.0001? * to indicate that we actually have a desired pitch diff --git a/src/main/java/baritone/pathing/movement/Movement.java b/src/main/java/baritone/pathing/movement/Movement.java index c1b4e351..a3e841c6 100644 --- a/src/main/java/baritone/pathing/movement/Movement.java +++ b/src/main/java/baritone/pathing/movement/Movement.java @@ -153,7 +153,7 @@ public abstract class Movement implements IMovement, MovementHelper { if (reachable.isPresent()) { MovementHelper.switchToBestToolFor(ctx, BlockStateInterface.get(ctx, blockPos)); state.setTarget(new MovementState.MovementTarget(reachable.get(), true)); - if (Objects.equals(RayTraceUtils.getSelectedBlock().orElse(null), blockPos)) { + if (Objects.equals(ctx.getSelectedBlock().orElse(null), blockPos)) { state.setInput(Input.CLICK_LEFT, true); } return false; diff --git a/src/main/java/baritone/pathing/movement/movements/MovementAscend.java b/src/main/java/baritone/pathing/movement/movements/MovementAscend.java index 3d737580..9c33742e 100644 --- a/src/main/java/baritone/pathing/movement/movements/MovementAscend.java +++ b/src/main/java/baritone/pathing/movement/movements/MovementAscend.java @@ -21,7 +21,6 @@ import baritone.Baritone; import baritone.api.IBaritone; import baritone.api.pathing.movement.MovementStatus; import baritone.api.utils.BetterBlockPos; -import baritone.api.utils.RayTraceUtils; import baritone.api.utils.RotationUtils; import baritone.api.utils.input.Input; import baritone.pathing.movement.CalculationContext; @@ -31,7 +30,6 @@ import baritone.pathing.movement.MovementState; import baritone.utils.BlockStateInterface; import net.minecraft.block.BlockFalling; import net.minecraft.block.state.IBlockState; -import net.minecraft.client.Minecraft; import net.minecraft.init.Blocks; import net.minecraft.util.EnumFacing; import net.minecraft.util.math.BlockPos; @@ -181,9 +179,9 @@ public class MovementAscend extends Movement { double faceY = (dest.getY() + anAgainst.getY()) * 0.5D; double faceZ = (dest.getZ() + anAgainst.getZ() + 1.0D) * 0.5D; state.setTarget(new MovementState.MovementTarget(RotationUtils.calcRotationFromVec3d(ctx.playerHead(), new Vec3d(faceX, faceY, faceZ), ctx.playerRotations()), true)); - EnumFacing side = Minecraft.getMinecraft().objectMouseOver.sideHit; + EnumFacing side = ctx.objectMouseOver().sideHit; - RayTraceUtils.getSelectedBlock().ifPresent(selectedBlock -> { + ctx.getSelectedBlock().ifPresent(selectedBlock -> { if (Objects.equals(selectedBlock, anAgainst) && selectedBlock.offset(side).equals(positionToPlace)) { ticksWithoutPlacement++; state.setInput(Input.SNEAK, true); diff --git a/src/main/java/baritone/pathing/movement/movements/MovementParkour.java b/src/main/java/baritone/pathing/movement/movements/MovementParkour.java index ede456b6..44334569 100644 --- a/src/main/java/baritone/pathing/movement/movements/MovementParkour.java +++ b/src/main/java/baritone/pathing/movement/movements/MovementParkour.java @@ -34,7 +34,6 @@ import baritone.utils.Helper; import baritone.utils.pathing.MutableMoveResult; import net.minecraft.block.Block; import net.minecraft.block.state.IBlockState; -import net.minecraft.client.Minecraft; import net.minecraft.init.Blocks; import net.minecraft.util.EnumFacing; import net.minecraft.util.math.BlockPos; @@ -232,8 +231,8 @@ public class MovementParkour extends Movement { if (res != null && res.typeOfHit == RayTraceResult.Type.BLOCK && res.getBlockPos().equals(against1) && res.getBlockPos().offset(res.sideHit).equals(dest.down())) { state.setTarget(new MovementState.MovementTarget(place, true)); } - RayTraceUtils.getSelectedBlock().ifPresent(selectedBlock -> { - EnumFacing side = Minecraft.getMinecraft().objectMouseOver.sideHit; + ctx.getSelectedBlock().ifPresent(selectedBlock -> { + EnumFacing side = ctx.objectMouseOver().sideHit; if (Objects.equals(selectedBlock, against1) && selectedBlock.offset(side).equals(dest.down())) { state.setInput(Input.CLICK_RIGHT, true); } diff --git a/src/main/java/baritone/pathing/movement/movements/MovementTraverse.java b/src/main/java/baritone/pathing/movement/movements/MovementTraverse.java index d80043af..2dc0e0cf 100644 --- a/src/main/java/baritone/pathing/movement/movements/MovementTraverse.java +++ b/src/main/java/baritone/pathing/movement/movements/MovementTraverse.java @@ -20,7 +20,10 @@ package baritone.pathing.movement.movements; import baritone.Baritone; import baritone.api.IBaritone; import baritone.api.pathing.movement.MovementStatus; -import baritone.api.utils.*; +import baritone.api.utils.BetterBlockPos; +import baritone.api.utils.Rotation; +import baritone.api.utils.RotationUtils; +import baritone.api.utils.VecUtils; import baritone.api.utils.input.Input; import baritone.pathing.movement.CalculationContext; import baritone.pathing.movement.Movement; @@ -29,7 +32,6 @@ import baritone.pathing.movement.MovementState; import baritone.utils.BlockStateInterface; import net.minecraft.block.*; import net.minecraft.block.state.IBlockState; -import net.minecraft.client.Minecraft; import net.minecraft.init.Blocks; import net.minecraft.util.EnumFacing; import net.minecraft.util.math.BlockPos; @@ -265,8 +267,8 @@ public class MovementTraverse extends Movement { double faceZ = (dest.getZ() + against1.getZ() + 1.0D) * 0.5D; state.setTarget(new MovementState.MovementTarget(RotationUtils.calcRotationFromVec3d(ctx.playerHead(), new Vec3d(faceX, faceY, faceZ), ctx.playerRotations()), true)); - EnumFacing side = Minecraft.getMinecraft().objectMouseOver.sideHit; - if (Objects.equals(RayTraceUtils.getSelectedBlock().orElse(null), against1) && (ctx.player().isSneaking() || Baritone.settings().assumeSafeWalk.get()) && RayTraceUtils.getSelectedBlock().get().offset(side).equals(positionToPlace)) { + EnumFacing side = ctx.objectMouseOver().sideHit; + if (Objects.equals(ctx.getSelectedBlock().orElse(null), against1) && (ctx.player().isSneaking() || Baritone.settings().assumeSafeWalk.get()) && ctx.getSelectedBlock().get().offset(side).equals(positionToPlace)) { return state.setInput(Input.CLICK_RIGHT, true); } //System.out.println("Trying to look at " + against1 + ", actually looking at" + RayTraceUtils.getSelectedBlock()); @@ -300,7 +302,7 @@ public class MovementTraverse extends Movement { state.setTarget(new MovementState.MovementTarget(backToFace, true)); } state.setInput(Input.SNEAK, true); - if (Objects.equals(RayTraceUtils.getSelectedBlock().orElse(null), goalLook)) { + if (Objects.equals(ctx.getSelectedBlock().orElse(null), goalLook)) { return state.setInput(Input.CLICK_RIGHT, true); // wait to right click until we are able to place } // Out.log("Trying to look at " + goalLook + ", actually looking at" + Baritone.whatAreYouLookingAt()); diff --git a/src/main/java/baritone/utils/ExampleBaritoneControl.java b/src/main/java/baritone/utils/ExampleBaritoneControl.java index 18e2f740..9808de6d 100644 --- a/src/main/java/baritone/utils/ExampleBaritoneControl.java +++ b/src/main/java/baritone/utils/ExampleBaritoneControl.java @@ -23,7 +23,6 @@ import baritone.api.cache.IWaypoint; import baritone.api.event.events.ChatEvent; import baritone.api.pathing.goals.*; import baritone.api.pathing.movement.ActionCosts; -import baritone.api.utils.RayTraceUtils; import baritone.api.utils.SettingsUtil; import baritone.behavior.Behavior; import baritone.behavior.PathingBehavior; @@ -270,7 +269,7 @@ public class ExampleBaritoneControl extends Behavior implements Helper { String name = msg.substring(6).trim(); Optional toFollow = Optional.empty(); if (name.length() == 0) { - toFollow = RayTraceUtils.getSelectedEntity(); + toFollow = ctx.getSelectedEntity(); } else { for (EntityPlayer pl : ctx.world().playerEntities) { String theirName = pl.getName().trim().toLowerCase(); diff --git a/src/main/java/baritone/utils/PathRenderer.java b/src/main/java/baritone/utils/PathRenderer.java index da19e955..443cd035 100644 --- a/src/main/java/baritone/utils/PathRenderer.java +++ b/src/main/java/baritone/utils/PathRenderer.java @@ -31,7 +31,6 @@ import baritone.behavior.PathingBehavior; import baritone.pathing.calc.AbstractNodeCostSearch; import baritone.pathing.path.PathExecutor; import net.minecraft.block.state.IBlockState; -import net.minecraft.client.Minecraft; import net.minecraft.client.renderer.BufferBuilder; import net.minecraft.client.renderer.GlStateManager; import net.minecraft.client.renderer.Tessellator; @@ -224,9 +223,9 @@ public final class PathRenderer implements Helper { IBlockState state = BlockStateInterface.get(BaritoneAPI.getProvider().getPrimaryBaritone().getPlayerContext(), pos); AxisAlignedBB toDraw; if (state.getBlock().equals(Blocks.AIR)) { - toDraw = Blocks.DIRT.getDefaultState().getSelectedBoundingBox(Minecraft.getMinecraft().world, pos); + toDraw = Blocks.DIRT.getDefaultState().getSelectedBoundingBox(player.world, pos); } else { - toDraw = state.getSelectedBoundingBox(Minecraft.getMinecraft().world, pos); + toDraw = state.getSelectedBoundingBox(player.world, pos); } toDraw = toDraw.expand(expand, expand, expand).offset(-renderPosX, -renderPosY, -renderPosZ); BUFFER.begin(GL_LINE_STRIP, DefaultVertexFormats.POSITION); diff --git a/src/main/java/baritone/utils/player/LocalPlayerContext.java b/src/main/java/baritone/utils/player/LocalPlayerContext.java index a818c2d9..02c08c11 100644 --- a/src/main/java/baritone/utils/player/LocalPlayerContext.java +++ b/src/main/java/baritone/utils/player/LocalPlayerContext.java @@ -23,6 +23,7 @@ import baritone.api.utils.IPlayerContext; import net.minecraft.client.Minecraft; import net.minecraft.client.entity.EntityPlayerSP; import net.minecraft.client.multiplayer.PlayerControllerMP; +import net.minecraft.util.math.RayTraceResult; import net.minecraft.world.World; /** @@ -58,4 +59,9 @@ public final class LocalPlayerContext implements IPlayerContext { public IWorldData worldData() { return BaritoneAPI.getProvider().getPrimaryBaritone().getWorldProvider().getCurrentWorld(); } + + @Override + public RayTraceResult objectMouseOver() { + return mc.objectMouseOver; + } }