From 364ae87ef861dd12570a0db6081ad1d4f5d4a399 Mon Sep 17 00:00:00 2001 From: Brady Date: Sun, 11 Jun 2023 12:36:53 -0500 Subject: [PATCH 01/12] Add `blockFreeLook` setting --- src/api/java/baritone/api/Settings.java | 5 +++++ src/api/java/baritone/api/utils/Rotation.java | 8 +++++-- .../baritone/api/utils/RotationUtils.java | 6 +++-- .../java/baritone/behavior/LookBehavior.java | 22 ++++++++++++++----- .../pathing/movement/MovementHelper.java | 4 ++-- .../movement/movements/MovementDescend.java | 5 ++--- .../movement/movements/MovementPillar.java | 4 ++-- .../utils/player/PrimaryPlayerContext.java | 16 ++++++++++---- 8 files changed, 50 insertions(+), 20 deletions(-) diff --git a/src/api/java/baritone/api/Settings.java b/src/api/java/baritone/api/Settings.java index b7a4e41a4..b49fce01b 100644 --- a/src/api/java/baritone/api/Settings.java +++ b/src/api/java/baritone/api/Settings.java @@ -723,6 +723,11 @@ public final class Settings { */ public final Setting freeLook = new Setting<>(true); + /** + * Break and place blocks without having to force the client-sided rotations + */ + public final Setting blockFreeLook = new Setting<>(true); + /** * Will cause some minor behavioral differences to ensure that Baritone works on anticheats. *

diff --git a/src/api/java/baritone/api/utils/Rotation.java b/src/api/java/baritone/api/utils/Rotation.java index 54f63ebfa..8dab287de 100644 --- a/src/api/java/baritone/api/utils/Rotation.java +++ b/src/api/java/baritone/api/utils/Rotation.java @@ -26,12 +26,12 @@ public class Rotation { /** * The yaw angle of this Rotation */ - private float yaw; + private final float yaw; /** * The pitch angle of this Rotation */ - private float pitch; + private final float pitch; public Rotation(float yaw, float pitch) { this.yaw = yaw; @@ -110,6 +110,10 @@ public class Rotation { ); } + public Rotation withPitch(float pitch) { + return new Rotation(this.yaw, pitch); + } + /** * Is really close to * diff --git a/src/api/java/baritone/api/utils/RotationUtils.java b/src/api/java/baritone/api/utils/RotationUtils.java index 39e68fd4f..7b30f4abb 100644 --- a/src/api/java/baritone/api/utils/RotationUtils.java +++ b/src/api/java/baritone/api/utils/RotationUtils.java @@ -162,7 +162,8 @@ public final class RotationUtils { public static Optional reachable(EntityPlayerSP entity, BlockPos pos, double blockReachDistance, boolean wouldSneak) { IBaritone baritone = BaritoneAPI.getProvider().getBaritoneForPlayer(entity); - if (baritone.getPlayerContext().isLookingAt(pos)) { + IPlayerContext ctx = baritone.getPlayerContext(); + if (ctx.isLookingAt(pos)) { /* * why add 0.0001? * to indicate that we actually have a desired pitch @@ -173,7 +174,7 @@ public final class RotationUtils { * * or if you're a normal person literally all this does it ensure that we don't nudge the pitch to a normal level */ - Rotation hypothetical = new Rotation(entity.rotationYaw, entity.rotationPitch + 0.0001F); + Rotation hypothetical = ctx.playerRotations().add(new Rotation(0, 0.0001F)); if (wouldSneak) { // the concern here is: what if we're looking at it now, but as soon as we start sneaking we no longer are RayTraceResult result = RayTraceUtils.rayTraceTowards(entity, hypothetical, blockReachDistance, true); @@ -217,6 +218,7 @@ public final class RotationUtils { */ public static Optional reachableOffset(Entity entity, BlockPos pos, Vec3d offsetPos, double blockReachDistance, boolean wouldSneak) { Vec3d eyes = wouldSneak ? RayTraceUtils.inferSneakingEyePosition(entity) : entity.getPositionEyes(1.0F); + // TODO: pr/feature/blockFreeLook - Use playerRotations() here? Rotation rotation = calcRotationFromVec3d(eyes, offsetPos, new Rotation(entity.rotationYaw, entity.rotationPitch)); RayTraceResult result = RayTraceUtils.rayTraceTowards(entity, rotation, blockReachDistance, wouldSneak); //System.out.println(result); diff --git a/src/main/java/baritone/behavior/LookBehavior.java b/src/main/java/baritone/behavior/LookBehavior.java index 32e5c22f5..7db3da562 100644 --- a/src/main/java/baritone/behavior/LookBehavior.java +++ b/src/main/java/baritone/behavior/LookBehavior.java @@ -22,6 +22,7 @@ import baritone.api.Settings; import baritone.api.behavior.ILookBehavior; import baritone.api.event.events.PlayerUpdateEvent; import baritone.api.event.events.RotationMoveEvent; +import baritone.api.event.events.type.EventState; import baritone.api.utils.Rotation; public final class LookBehavior extends Behavior implements ILookBehavior { @@ -34,17 +35,19 @@ public final class LookBehavior extends Behavior implements ILookBehavior { */ private Rotation target; + private Rotation serverAngles; + /** * Whether or not rotations are currently being forced */ private boolean force; /** - * The last player yaw angle. Used when free looking + * The last player angles. Used when free looking * * @see Settings#freeLook */ - private float lastYaw; + private Rotation prevAngles; public LookBehavior(Baritone baritone) { super(baritone); @@ -53,11 +56,14 @@ public final class LookBehavior extends Behavior implements ILookBehavior { @Override public void updateTarget(Rotation target, boolean force) { this.target = target; - this.force = force || !Baritone.settings().freeLook.value; + this.force = !Baritone.settings().blockFreeLook.value && (force || !Baritone.settings().freeLook.value); } @Override public void onPlayerUpdate(PlayerUpdateEvent event) { + if (event.getState() == EventState.POST) { + this.serverAngles = new Rotation(ctx.player().rotationYaw, ctx.player().rotationPitch); + } if (this.target == null) { return; } @@ -80,14 +86,16 @@ public final class LookBehavior extends Behavior implements ILookBehavior { this.target = null; } if (silent) { - this.lastYaw = ctx.player().rotationYaw; + this.prevAngles = new Rotation(ctx.player().rotationYaw, ctx.player().rotationPitch); ctx.player().rotationYaw = this.target.getYaw(); + ctx.player().rotationPitch = this.target.getPitch(); } break; } case POST: { if (silent) { - ctx.player().rotationYaw = this.lastYaw; + ctx.player().rotationYaw = this.prevAngles.getYaw(); + ctx.player().rotationPitch = this.prevAngles.getPitch(); this.target = null; } break; @@ -103,6 +111,10 @@ public final class LookBehavior extends Behavior implements ILookBehavior { } } + public Rotation getEffectiveAngles() { + return this.serverAngles; + } + @Override public void onPlayerRotationMove(RotationMoveEvent event) { if (this.target != null) { diff --git a/src/main/java/baritone/pathing/movement/MovementHelper.java b/src/main/java/baritone/pathing/movement/MovementHelper.java index 575815ea2..b14b2d69c 100644 --- a/src/main/java/baritone/pathing/movement/MovementHelper.java +++ b/src/main/java/baritone/pathing/movement/MovementHelper.java @@ -599,9 +599,9 @@ public interface MovementHelper extends ActionCosts, Helper { static void moveTowards(IPlayerContext ctx, MovementState state, BlockPos pos) { state.setTarget(new MovementTarget( - new Rotation(RotationUtils.calcRotationFromVec3d(ctx.playerHead(), + RotationUtils.calcRotationFromVec3d(ctx.playerHead(), VecUtils.getBlockPosCenter(pos), - ctx.playerRotations()).getYaw(), ctx.player().rotationPitch), + ctx.playerRotations()).withPitch(ctx.playerRotations().getPitch()), false )).setInput(Input.MOVE_FORWARD, true); } diff --git a/src/main/java/baritone/pathing/movement/movements/MovementDescend.java b/src/main/java/baritone/pathing/movement/movements/MovementDescend.java index d36843cdd..2d8180356 100644 --- a/src/main/java/baritone/pathing/movement/movements/MovementDescend.java +++ b/src/main/java/baritone/pathing/movement/movements/MovementDescend.java @@ -234,11 +234,10 @@ public class MovementDescend extends Movement { if (safeMode()) { double destX = (src.getX() + 0.5) * 0.17 + (dest.getX() + 0.5) * 0.83; double destZ = (src.getZ() + 0.5) * 0.17 + (dest.getZ() + 0.5) * 0.83; - EntityPlayerSP player = ctx.player(); state.setTarget(new MovementState.MovementTarget( - new Rotation(RotationUtils.calcRotationFromVec3d(ctx.playerHead(), + RotationUtils.calcRotationFromVec3d(ctx.playerHead(), new Vec3d(destX, dest.getY(), destZ), - new Rotation(player.rotationYaw, player.rotationPitch)).getYaw(), player.rotationPitch), + ctx.playerRotations()).withPitch(ctx.playerRotations().getPitch()), false )).setInput(Input.MOVE_FORWARD, true); return state; diff --git a/src/main/java/baritone/pathing/movement/movements/MovementPillar.java b/src/main/java/baritone/pathing/movement/movements/MovementPillar.java index cfa9d9260..88f1e26ff 100644 --- a/src/main/java/baritone/pathing/movement/movements/MovementPillar.java +++ b/src/main/java/baritone/pathing/movement/movements/MovementPillar.java @@ -190,9 +190,9 @@ public class MovementPillar extends Movement { boolean vine = fromDown.getBlock() == Blocks.VINE; Rotation rotation = RotationUtils.calcRotationFromVec3d(ctx.playerHead(), VecUtils.getBlockPosCenter(positionToPlace), - new Rotation(ctx.player().rotationYaw, ctx.player().rotationPitch)); + ctx.playerRotations()); if (!ladder) { - state.setTarget(new MovementState.MovementTarget(new Rotation(ctx.player().rotationYaw, rotation.getPitch()), true)); + state.setTarget(new MovementState.MovementTarget(ctx.playerRotations().withPitch(rotation.getPitch()), true)); } boolean blockIsThere = MovementHelper.canWalkOn(ctx, src) || ladder; diff --git a/src/main/java/baritone/utils/player/PrimaryPlayerContext.java b/src/main/java/baritone/utils/player/PrimaryPlayerContext.java index 3cb498acd..cc35c156c 100644 --- a/src/main/java/baritone/utils/player/PrimaryPlayerContext.java +++ b/src/main/java/baritone/utils/player/PrimaryPlayerContext.java @@ -17,12 +17,11 @@ package baritone.utils.player; +import baritone.Baritone; import baritone.api.BaritoneAPI; import baritone.api.cache.IWorldData; -import baritone.api.utils.Helper; -import baritone.api.utils.IPlayerContext; -import baritone.api.utils.IPlayerController; -import baritone.api.utils.RayTraceUtils; +import baritone.api.utils.*; +import baritone.behavior.LookBehavior; import net.minecraft.client.entity.EntityPlayerSP; import net.minecraft.util.math.RayTraceResult; import net.minecraft.world.World; @@ -57,6 +56,15 @@ public enum PrimaryPlayerContext implements IPlayerContext, Helper { return BaritoneAPI.getProvider().getPrimaryBaritone().getWorldProvider().getCurrentWorld(); } + @Override + public Rotation playerRotations() { + final Rotation lbTarget = ((LookBehavior) BaritoneAPI.getProvider().getPrimaryBaritone().getLookBehavior()).getEffectiveAngles(); + if (lbTarget == null || !Baritone.settings().blockFreeLook.value) { + return IPlayerContext.super.playerRotations(); + } + return lbTarget; + } + @Override public RayTraceResult objectMouseOver() { return RayTraceUtils.rayTraceTowards(player(), playerRotations(), playerController().getBlockReachDistance()); From fbe28e397e880e022c43af81c59dc8f088cd42b6 Mon Sep 17 00:00:00 2001 From: Brady Date: Sun, 11 Jun 2023 12:38:13 -0500 Subject: [PATCH 02/12] Make setting disabled by default --- src/api/java/baritone/api/Settings.java | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/api/java/baritone/api/Settings.java b/src/api/java/baritone/api/Settings.java index b49fce01b..bf3cb1bdd 100644 --- a/src/api/java/baritone/api/Settings.java +++ b/src/api/java/baritone/api/Settings.java @@ -726,7 +726,7 @@ public final class Settings { /** * Break and place blocks without having to force the client-sided rotations */ - public final Setting blockFreeLook = new Setting<>(true); + public final Setting blockFreeLook = new Setting<>(false); /** * Will cause some minor behavioral differences to ensure that Baritone works on anticheats. From 77ca36c7947541faef6e1519823c4b06c913bc88 Mon Sep 17 00:00:00 2001 From: Brady Date: Sun, 11 Jun 2023 15:49:04 -0500 Subject: [PATCH 03/12] Use `playerRotations` in `reachableOffset` --- src/api/java/baritone/api/utils/RotationUtils.java | 8 ++++---- 1 file changed, 4 insertions(+), 4 deletions(-) diff --git a/src/api/java/baritone/api/utils/RotationUtils.java b/src/api/java/baritone/api/utils/RotationUtils.java index 7b30f4abb..0d5a3ad1f 100644 --- a/src/api/java/baritone/api/utils/RotationUtils.java +++ b/src/api/java/baritone/api/utils/RotationUtils.java @@ -216,10 +216,10 @@ public final class RotationUtils { * @param blockReachDistance The block reach distance of the entity * @return The optional rotation */ - public static Optional reachableOffset(Entity entity, BlockPos pos, Vec3d offsetPos, double blockReachDistance, boolean wouldSneak) { + public static Optional reachableOffset(EntityPlayerSP entity, BlockPos pos, Vec3d offsetPos, double blockReachDistance, boolean wouldSneak) { + IPlayerContext ctx = BaritoneAPI.getProvider().getBaritoneForPlayer(entity).getPlayerContext(); Vec3d eyes = wouldSneak ? RayTraceUtils.inferSneakingEyePosition(entity) : entity.getPositionEyes(1.0F); - // TODO: pr/feature/blockFreeLook - Use playerRotations() here? - Rotation rotation = calcRotationFromVec3d(eyes, offsetPos, new Rotation(entity.rotationYaw, entity.rotationPitch)); + Rotation rotation = calcRotationFromVec3d(eyes, offsetPos, ctx.playerRotations()); RayTraceResult result = RayTraceUtils.rayTraceTowards(entity, rotation, blockReachDistance, wouldSneak); //System.out.println(result); if (result != null && result.typeOfHit == RayTraceResult.Type.BLOCK) { @@ -242,7 +242,7 @@ public final class RotationUtils { * @param blockReachDistance The block reach distance of the entity * @return The optional rotation */ - public static Optional reachableCenter(Entity entity, BlockPos pos, double blockReachDistance, boolean wouldSneak) { + public static Optional reachableCenter(EntityPlayerSP entity, BlockPos pos, double blockReachDistance, boolean wouldSneak) { return reachableOffset(entity, pos, VecUtils.calculateBlockCenter(entity.world, pos), blockReachDistance, wouldSneak); } } From 4317dca024ec0d68b3d402b82777a453f7c121b1 Mon Sep 17 00:00:00 2001 From: Brady Date: Sun, 11 Jun 2023 18:35:34 -0500 Subject: [PATCH 04/12] Determine target mode (client/server) in `updateTarget` --- src/api/java/baritone/api/Settings.java | 3 +- .../baritone/api/behavior/ILookBehavior.java | 13 +- .../java/baritone/behavior/LookBehavior.java | 125 +++++++++++------- 3 files changed, 87 insertions(+), 54 deletions(-) diff --git a/src/api/java/baritone/api/Settings.java b/src/api/java/baritone/api/Settings.java index bf3cb1bdd..bcd3eaa63 100644 --- a/src/api/java/baritone/api/Settings.java +++ b/src/api/java/baritone/api/Settings.java @@ -724,7 +724,8 @@ public final class Settings { public final Setting freeLook = new Setting<>(true); /** - * Break and place blocks without having to force the client-sided rotations + * Break and place blocks without having to force the client-sided rotations. Having this setting enabled implies + * {@link #freeLook}. */ public final Setting blockFreeLook = new Setting<>(false); diff --git a/src/api/java/baritone/api/behavior/ILookBehavior.java b/src/api/java/baritone/api/behavior/ILookBehavior.java index 058a5dd88..218e8b59a 100644 --- a/src/api/java/baritone/api/behavior/ILookBehavior.java +++ b/src/api/java/baritone/api/behavior/ILookBehavior.java @@ -26,14 +26,11 @@ import baritone.api.utils.Rotation; public interface ILookBehavior extends IBehavior { /** - * Updates the current {@link ILookBehavior} target to target - * the specified rotations on the next tick. If force is {@code true}, - * then freeLook will be overriden and angles will be set regardless. - * If any sort of block interaction is required, force should be {@code true}, - * otherwise, it should be {@code false}; + * Updates the current {@link ILookBehavior} target to target the specified rotations on the next tick. If any sort + * of block interaction is required, {@code blockInteract} should be {@code true}. * - * @param rotation The target rotations - * @param force Whether or not to "force" the rotations + * @param rotation The target rotations + * @param blockInteract Whether the target rotations are needed for a block interaction */ - void updateTarget(Rotation rotation, boolean force); + void updateTarget(Rotation rotation, boolean blockInteract); } diff --git a/src/main/java/baritone/behavior/LookBehavior.java b/src/main/java/baritone/behavior/LookBehavior.java index 7db3da562..93e0af3bf 100644 --- a/src/main/java/baritone/behavior/LookBehavior.java +++ b/src/main/java/baritone/behavior/LookBehavior.java @@ -28,19 +28,11 @@ import baritone.api.utils.Rotation; public final class LookBehavior extends Behavior implements ILookBehavior { /** - * Target's values are as follows: - *

- * getFirst() -> yaw - * getSecond() -> pitch + * The current look target, may be {@code null}. */ - private Rotation target; + private Target target; - private Rotation serverAngles; - - /** - * Whether or not rotations are currently being forced - */ - private boolean force; + private Rotation serverRotation; /** * The last player angles. Used when free looking @@ -54,50 +46,60 @@ public final class LookBehavior extends Behavior implements ILookBehavior { } @Override - public void updateTarget(Rotation target, boolean force) { - this.target = target; - this.force = !Baritone.settings().blockFreeLook.value && (force || !Baritone.settings().freeLook.value); + public void updateTarget(Rotation rotation, boolean blockInteract) { + this.target = new Target(rotation, blockInteract); } @Override public void onPlayerUpdate(PlayerUpdateEvent event) { + // Capture the player rotation before it's reset to determine the rotation the server knows + // Alternatively, this could be done with a packet event... Maybe that's a better idea. if (event.getState() == EventState.POST) { - this.serverAngles = new Rotation(ctx.player().rotationYaw, ctx.player().rotationPitch); + this.serverRotation = new Rotation(ctx.player().rotationYaw, ctx.player().rotationPitch); } + + // There's nothing left to be done if there isn't a set target if (this.target == null) { return; } - // Whether or not we're going to silently set our angles - boolean silent = Baritone.settings().antiCheatCompatibility.value && !this.force; - switch (event.getState()) { case PRE: { - if (this.force) { - ctx.player().rotationYaw = this.target.getYaw(); - float oldPitch = ctx.player().rotationPitch; - float desiredPitch = this.target.getPitch(); - ctx.player().rotationPitch = desiredPitch; - ctx.player().rotationYaw += (Math.random() - 0.5) * Baritone.settings().randomLooking.value; - ctx.player().rotationPitch += (Math.random() - 0.5) * Baritone.settings().randomLooking.value; - if (desiredPitch == oldPitch && !Baritone.settings().freeLook.value) { - nudgeToLevel(); + switch (this.target.mode) { + case CLIENT: { + ctx.player().rotationYaw = this.target.rotation.getYaw(); + float oldPitch = ctx.player().rotationPitch; + float desiredPitch = this.target.rotation.getPitch(); + ctx.player().rotationPitch = desiredPitch; + ctx.player().rotationYaw += (Math.random() - 0.5) * Baritone.settings().randomLooking.value; + ctx.player().rotationPitch += (Math.random() - 0.5) * Baritone.settings().randomLooking.value; + if (desiredPitch == oldPitch && !Baritone.settings().freeLook.value) { + nudgeToLevel(); + } + // The target can be invalidated now since it won't be needed for RotationMoveEvent + this.target = null; + break; } - this.target = null; - } - if (silent) { - this.prevAngles = new Rotation(ctx.player().rotationYaw, ctx.player().rotationPitch); - ctx.player().rotationYaw = this.target.getYaw(); - ctx.player().rotationPitch = this.target.getPitch(); + case SERVER: { + // Copy the player's actual angles + this.prevAngles = new Rotation(ctx.player().rotationYaw, ctx.player().rotationPitch); + ctx.player().rotationYaw = this.target.rotation.getYaw(); + ctx.player().rotationPitch = this.target.rotation.getPitch(); + break; + } + default: + break; } break; } case POST: { - if (silent) { + // Reset the player's rotations back to their original values + if (this.target.mode == Target.Mode.SERVER) { ctx.player().rotationYaw = this.prevAngles.getYaw(); ctx.player().rotationPitch = this.prevAngles.getPitch(); - this.target = null; } + // The target is done being used for this game tick, so it can be invalidated + this.target = null; break; } default: @@ -107,25 +109,18 @@ public final class LookBehavior extends Behavior implements ILookBehavior { public void pig() { if (this.target != null) { - ctx.player().rotationYaw = this.target.getYaw(); + ctx.player().rotationYaw = this.target.rotation.getYaw(); } } public Rotation getEffectiveAngles() { - return this.serverAngles; + return this.serverRotation; } @Override public void onPlayerRotationMove(RotationMoveEvent event) { if (this.target != null) { - - event.setYaw(this.target.getYaw()); - - // If we have antiCheatCompatibility on, we're going to use the target value later in onPlayerUpdate() - // Also the type has to be MOTION_UPDATE because that is called after JUMP - if (!Baritone.settings().antiCheatCompatibility.value && event.getType() == RotationMoveEvent.Type.MOTION_UPDATE && !this.force) { - this.target = null; - } + event.setYaw(this.target.rotation.getYaw()); } } @@ -139,4 +134,44 @@ public final class LookBehavior extends Behavior implements ILookBehavior { ctx.player().rotationPitch--; } } + + private static class Target { + + public final Rotation rotation; + public final Mode mode; + + public Target(Rotation rotation, boolean blockInteract) { + this.rotation = rotation; + this.mode = Mode.resolve(blockInteract); + } + + enum Mode { + /** + * Angles will be set client-side and are visual to the player + */ + CLIENT, + + /** + * Angles will be set server-side and are silent to the player + */ + SERVER, + + /** + * Angles will remain unaffected on both the client and server + */ + NONE; + + static Mode resolve(boolean blockInteract) { + final Settings settings = Baritone.settings(); + final boolean antiCheat = settings.antiCheatCompatibility.value; + final boolean blockFreeLook = settings.blockFreeLook.value; + final boolean freeLook = settings.freeLook.value; + + if (!freeLook && !blockFreeLook) return CLIENT; + if (!blockFreeLook && blockInteract) return CLIENT; + if (antiCheat || blockInteract) return SERVER; + return NONE; + } + } + } } From a7d15d1e32a1438bce8223a3483a76dcd5d14444 Mon Sep 17 00:00:00 2001 From: Brady Date: Sun, 11 Jun 2023 18:43:48 -0500 Subject: [PATCH 05/12] Consistent naming and comments for clarification --- .../java/baritone/behavior/LookBehavior.java | 30 ++++++++++++------- .../utils/player/PrimaryPlayerContext.java | 2 +- 2 files changed, 21 insertions(+), 11 deletions(-) diff --git a/src/main/java/baritone/behavior/LookBehavior.java b/src/main/java/baritone/behavior/LookBehavior.java index 93e0af3bf..c38cee6f9 100644 --- a/src/main/java/baritone/behavior/LookBehavior.java +++ b/src/main/java/baritone/behavior/LookBehavior.java @@ -23,6 +23,7 @@ import baritone.api.behavior.ILookBehavior; import baritone.api.event.events.PlayerUpdateEvent; import baritone.api.event.events.RotationMoveEvent; import baritone.api.event.events.type.EventState; +import baritone.api.utils.IPlayerContext; import baritone.api.utils.Rotation; public final class LookBehavior extends Behavior implements ILookBehavior { @@ -32,14 +33,17 @@ public final class LookBehavior extends Behavior implements ILookBehavior { */ private Target target; + /** + * The rotation known to the server. Returned by {@link #getEffectiveRotation()} for use in {@link IPlayerContext}. + */ private Rotation serverRotation; /** - * The last player angles. Used when free looking + * The last player rotation. Used when free looking * * @see Settings#freeLook */ - private Rotation prevAngles; + private Rotation prevRotation; public LookBehavior(Baritone baritone) { super(baritone); @@ -81,8 +85,8 @@ public final class LookBehavior extends Behavior implements ILookBehavior { break; } case SERVER: { - // Copy the player's actual angles - this.prevAngles = new Rotation(ctx.player().rotationYaw, ctx.player().rotationPitch); + // Copy the player's actual rotation + this.prevRotation = new Rotation(ctx.player().rotationYaw, ctx.player().rotationPitch); ctx.player().rotationYaw = this.target.rotation.getYaw(); ctx.player().rotationPitch = this.target.rotation.getPitch(); break; @@ -95,8 +99,8 @@ public final class LookBehavior extends Behavior implements ILookBehavior { case POST: { // Reset the player's rotations back to their original values if (this.target.mode == Target.Mode.SERVER) { - ctx.player().rotationYaw = this.prevAngles.getYaw(); - ctx.player().rotationPitch = this.prevAngles.getPitch(); + ctx.player().rotationYaw = this.prevRotation.getYaw(); + ctx.player().rotationPitch = this.prevRotation.getPitch(); } // The target is done being used for this game tick, so it can be invalidated this.target = null; @@ -113,7 +117,7 @@ public final class LookBehavior extends Behavior implements ILookBehavior { } } - public Rotation getEffectiveAngles() { + public Rotation getEffectiveRotation() { return this.serverRotation; } @@ -147,17 +151,17 @@ public final class LookBehavior extends Behavior implements ILookBehavior { enum Mode { /** - * Angles will be set client-side and are visual to the player + * Rotation will be set client-side and is visual to the player */ CLIENT, /** - * Angles will be set server-side and are silent to the player + * Rotation will be set server-side and is silent to the player */ SERVER, /** - * Angles will remain unaffected on both the client and server + * Rotation will remain unaffected on both the client and server */ NONE; @@ -169,7 +173,13 @@ public final class LookBehavior extends Behavior implements ILookBehavior { if (!freeLook && !blockFreeLook) return CLIENT; if (!blockFreeLook && blockInteract) return CLIENT; + + // Regardless of if antiCheatCompatibility is enabled, if a blockInteract is requested then the player + // rotation needs to be set somehow, otherwise Baritone will halt since objectMouseOver() will just be + // whatever the player is mousing over visually. Let's just settle for setting it silently. if (antiCheat || blockInteract) return SERVER; + + // Pathing regularly without antiCheatCompatibility, don't set the player rotation return NONE; } } diff --git a/src/main/java/baritone/utils/player/PrimaryPlayerContext.java b/src/main/java/baritone/utils/player/PrimaryPlayerContext.java index cc35c156c..e4484f0be 100644 --- a/src/main/java/baritone/utils/player/PrimaryPlayerContext.java +++ b/src/main/java/baritone/utils/player/PrimaryPlayerContext.java @@ -58,7 +58,7 @@ public enum PrimaryPlayerContext implements IPlayerContext, Helper { @Override public Rotation playerRotations() { - final Rotation lbTarget = ((LookBehavior) BaritoneAPI.getProvider().getPrimaryBaritone().getLookBehavior()).getEffectiveAngles(); + final Rotation lbTarget = ((LookBehavior) BaritoneAPI.getProvider().getPrimaryBaritone().getLookBehavior()).getEffectiveRotation(); if (lbTarget == null || !Baritone.settings().blockFreeLook.value) { return IPlayerContext.super.playerRotations(); } From bb36ebfc0c7cb39ac24852fef1b28c25c995975d Mon Sep 17 00:00:00 2001 From: Brady Date: Sun, 11 Jun 2023 20:09:37 -0500 Subject: [PATCH 06/12] Use `IPlayerContext` for all `RotationUtils` methods Deprecates all old overloads --- .../baritone/api/utils/RotationUtils.java | 87 +++++++++++++------ .../baritone/pathing/movement/Movement.java | 2 +- .../movement/movements/MovementPillar.java | 2 +- .../java/baritone/process/BuilderProcess.java | 2 +- .../java/baritone/process/FarmProcess.java | 4 +- .../baritone/process/GetToBlockProcess.java | 2 +- .../java/baritone/process/MineProcess.java | 2 +- 7 files changed, 66 insertions(+), 35 deletions(-) diff --git a/src/api/java/baritone/api/utils/RotationUtils.java b/src/api/java/baritone/api/utils/RotationUtils.java index 0d5a3ad1f..1991ab878 100644 --- a/src/api/java/baritone/api/utils/RotationUtils.java +++ b/src/api/java/baritone/api/utils/RotationUtils.java @@ -134,14 +134,14 @@ public final class RotationUtils { * @param ctx Context for the viewing entity * @param pos The target block position * @return The optional rotation - * @see #reachable(EntityPlayerSP, BlockPos, double) + * @see #reachable(IPlayerContext, BlockPos, double) */ public static Optional reachable(IPlayerContext ctx, BlockPos pos) { - return reachable(ctx.player(), pos, ctx.playerController().getBlockReachDistance()); + return reachable(ctx, pos, false); } public static Optional reachable(IPlayerContext ctx, BlockPos pos, boolean wouldSneak) { - return reachable(ctx.player(), pos, ctx.playerController().getBlockReachDistance(), wouldSneak); + return reachable(ctx, pos, ctx.playerController().getBlockReachDistance(), wouldSneak); } /** @@ -151,18 +151,16 @@ public final class RotationUtils { * side that is reachable. The return type will be {@link Optional#empty()} if the entity is * unable to reach any of the sides of the block. * - * @param entity The viewing entity + * @param ctx Context for the viewing entity * @param pos The target block position * @param blockReachDistance The block reach distance of the entity * @return The optional rotation */ - public static Optional reachable(EntityPlayerSP entity, BlockPos pos, double blockReachDistance) { - return reachable(entity, pos, blockReachDistance, false); + public static Optional reachable(IPlayerContext ctx, BlockPos pos, double blockReachDistance) { + return reachable(ctx, pos, blockReachDistance, false); } - public static Optional reachable(EntityPlayerSP entity, BlockPos pos, double blockReachDistance, boolean wouldSneak) { - IBaritone baritone = BaritoneAPI.getProvider().getBaritoneForPlayer(entity); - IPlayerContext ctx = baritone.getPlayerContext(); + public static Optional reachable(IPlayerContext ctx, BlockPos pos, double blockReachDistance, boolean wouldSneak) { if (ctx.isLookingAt(pos)) { /* * why add 0.0001? @@ -177,7 +175,7 @@ public final class RotationUtils { Rotation hypothetical = ctx.playerRotations().add(new Rotation(0, 0.0001F)); if (wouldSneak) { // the concern here is: what if we're looking at it now, but as soon as we start sneaking we no longer are - RayTraceResult result = RayTraceUtils.rayTraceTowards(entity, hypothetical, blockReachDistance, true); + RayTraceResult result = RayTraceUtils.rayTraceTowards(ctx.player(), hypothetical, blockReachDistance, true); if (result != null && result.typeOfHit == RayTraceResult.Type.BLOCK && result.getBlockPos().equals(pos)) { return Optional.of(hypothetical); // yes, if we sneaked we would still be looking at the block } @@ -185,19 +183,19 @@ public final class RotationUtils { return Optional.of(hypothetical); } } - Optional possibleRotation = reachableCenter(entity, pos, blockReachDistance, wouldSneak); + Optional possibleRotation = reachableCenter(ctx, pos, blockReachDistance, wouldSneak); //System.out.println("center: " + possibleRotation); if (possibleRotation.isPresent()) { return possibleRotation; } - IBlockState state = entity.world.getBlockState(pos); - AxisAlignedBB aabb = state.getBoundingBox(entity.world, pos); + IBlockState state = ctx.world().getBlockState(pos); + AxisAlignedBB aabb = state.getBoundingBox(ctx.world(), pos); for (Vec3d sideOffset : BLOCK_SIDE_MULTIPLIERS) { double xDiff = aabb.minX * sideOffset.x + aabb.maxX * (1 - sideOffset.x); double yDiff = aabb.minY * sideOffset.y + aabb.maxY * (1 - sideOffset.y); double zDiff = aabb.minZ * sideOffset.z + aabb.maxZ * (1 - sideOffset.z); - possibleRotation = reachableOffset(entity, pos, new Vec3d(pos).add(xDiff, yDiff, zDiff), blockReachDistance, wouldSneak); + possibleRotation = reachableOffset(ctx, pos, new Vec3d(pos).add(xDiff, yDiff, zDiff), blockReachDistance, wouldSneak); if (possibleRotation.isPresent()) { return possibleRotation; } @@ -210,16 +208,57 @@ public final class RotationUtils { * the given offsetted position. The return type will be {@link Optional#empty()} if * the entity is unable to reach the block with the offset applied. * - * @param entity The viewing entity + * @param ctx Context for the viewing entity * @param pos The target block position * @param offsetPos The position of the block with the offset applied. * @param blockReachDistance The block reach distance of the entity * @return The optional rotation */ - public static Optional reachableOffset(EntityPlayerSP entity, BlockPos pos, Vec3d offsetPos, double blockReachDistance, boolean wouldSneak) { - IPlayerContext ctx = BaritoneAPI.getProvider().getBaritoneForPlayer(entity).getPlayerContext(); - Vec3d eyes = wouldSneak ? RayTraceUtils.inferSneakingEyePosition(entity) : entity.getPositionEyes(1.0F); + public static Optional reachableOffset(IPlayerContext ctx, BlockPos pos, Vec3d offsetPos, double blockReachDistance, boolean wouldSneak) { + Vec3d eyes = wouldSneak ? RayTraceUtils.inferSneakingEyePosition(ctx.player()) : ctx.player().getPositionEyes(1.0F); Rotation rotation = calcRotationFromVec3d(eyes, offsetPos, ctx.playerRotations()); + RayTraceResult result = RayTraceUtils.rayTraceTowards(ctx.player(), rotation, blockReachDistance, wouldSneak); + //System.out.println(result); + if (result != null && result.typeOfHit == RayTraceResult.Type.BLOCK) { + if (result.getBlockPos().equals(pos)) { + return Optional.of(rotation); + } + if (ctx.world().getBlockState(pos).getBlock() instanceof BlockFire && result.getBlockPos().equals(pos.down())) { + return Optional.of(rotation); + } + } + return Optional.empty(); + } + + /** + * Determines if the specified entity is able to reach the specified block where it is + * looking at the direct center of it's hitbox. + * + * @param ctx Context for the viewing entity + * @param pos The target block position + * @param blockReachDistance The block reach distance of the entity + * @return The optional rotation + */ + public static Optional reachableCenter(IPlayerContext ctx, BlockPos pos, double blockReachDistance, boolean wouldSneak) { + return reachableOffset(ctx, pos, VecUtils.calculateBlockCenter(ctx.world(), pos), blockReachDistance, wouldSneak); + } + + @Deprecated + public static Optional reachable(EntityPlayerSP entity, BlockPos pos, double blockReachDistance) { + return reachable(entity, pos, blockReachDistance, false); + } + + @Deprecated + public static Optional reachable(EntityPlayerSP entity, BlockPos pos, double blockReachDistance, boolean wouldSneak) { + IBaritone baritone = BaritoneAPI.getProvider().getBaritoneForPlayer(entity); + IPlayerContext ctx = baritone.getPlayerContext(); + return reachable(ctx, pos, blockReachDistance, wouldSneak); + } + + @Deprecated + public static Optional reachableOffset(Entity entity, BlockPos pos, Vec3d offsetPos, double blockReachDistance, boolean wouldSneak) { + Vec3d eyes = wouldSneak ? RayTraceUtils.inferSneakingEyePosition(entity) : entity.getPositionEyes(1.0F); + Rotation rotation = calcRotationFromVec3d(eyes, offsetPos, new Rotation(entity.rotationYaw, entity.rotationPitch)); RayTraceResult result = RayTraceUtils.rayTraceTowards(entity, rotation, blockReachDistance, wouldSneak); //System.out.println(result); if (result != null && result.typeOfHit == RayTraceResult.Type.BLOCK) { @@ -233,16 +272,8 @@ public final class RotationUtils { return Optional.empty(); } - /** - * Determines if the specified entity is able to reach the specified block where it is - * looking at the direct center of it's hitbox. - * - * @param entity The viewing entity - * @param pos The target block position - * @param blockReachDistance The block reach distance of the entity - * @return The optional rotation - */ - public static Optional reachableCenter(EntityPlayerSP entity, BlockPos pos, double blockReachDistance, boolean wouldSneak) { + @Deprecated + public static Optional reachableCenter(Entity entity, BlockPos pos, double blockReachDistance, boolean wouldSneak) { return reachableOffset(entity, pos, VecUtils.calculateBlockCenter(entity.world, pos), blockReachDistance, wouldSneak); } } diff --git a/src/main/java/baritone/pathing/movement/Movement.java b/src/main/java/baritone/pathing/movement/Movement.java index c46b24dea..5a17d26c5 100644 --- a/src/main/java/baritone/pathing/movement/Movement.java +++ b/src/main/java/baritone/pathing/movement/Movement.java @@ -164,7 +164,7 @@ public abstract class Movement implements IMovement, MovementHelper { if (!MovementHelper.canWalkThrough(ctx, blockPos) && !(BlockStateInterface.getBlock(ctx, blockPos) instanceof BlockLiquid)) { // can't break liquid, so don't try somethingInTheWay = true; MovementHelper.switchToBestToolFor(ctx, BlockStateInterface.get(ctx, blockPos)); - Optional reachable = RotationUtils.reachable(ctx.player(), blockPos, ctx.playerController().getBlockReachDistance()); + Optional reachable = RotationUtils.reachable(ctx, blockPos, ctx.playerController().getBlockReachDistance()); if (reachable.isPresent()) { Rotation rotTowardsBlock = reachable.get(); state.setTarget(new MovementState.MovementTarget(rotTowardsBlock, true)); diff --git a/src/main/java/baritone/pathing/movement/movements/MovementPillar.java b/src/main/java/baritone/pathing/movement/movements/MovementPillar.java index 88f1e26ff..0198b28fe 100644 --- a/src/main/java/baritone/pathing/movement/movements/MovementPillar.java +++ b/src/main/java/baritone/pathing/movement/movements/MovementPillar.java @@ -251,7 +251,7 @@ public class MovementPillar extends Movement { Block fr = frState.getBlock(); // TODO: Evaluate usage of getMaterial().isReplaceable() if (!(fr instanceof BlockAir || frState.getMaterial().isReplaceable())) { - RotationUtils.reachable(ctx.player(), src, ctx.playerController().getBlockReachDistance()) + RotationUtils.reachable(ctx, src, ctx.playerController().getBlockReachDistance()) .map(rot -> new MovementState.MovementTarget(rot, true)) .ifPresent(state::setTarget); state.setInput(Input.JUMP, false); // breaking is like 5x slower when you're jumping diff --git a/src/main/java/baritone/process/BuilderProcess.java b/src/main/java/baritone/process/BuilderProcess.java index c7868a4a0..6f8582457 100644 --- a/src/main/java/baritone/process/BuilderProcess.java +++ b/src/main/java/baritone/process/BuilderProcess.java @@ -283,7 +283,7 @@ public final class BuilderProcess extends BaritoneProcessHelper implements IBuil IBlockState curr = bcc.bsi.get0(x, y, z); if (curr.getBlock() != Blocks.AIR && !(curr.getBlock() instanceof BlockLiquid) && !valid(curr, desired, false)) { BetterBlockPos pos = new BetterBlockPos(x, y, z); - Optional rot = RotationUtils.reachable(ctx.player(), pos, ctx.playerController().getBlockReachDistance()); + Optional rot = RotationUtils.reachable(ctx, pos, ctx.playerController().getBlockReachDistance()); if (rot.isPresent()) { return Optional.of(new Tuple<>(pos, rot.get())); } diff --git a/src/main/java/baritone/process/FarmProcess.java b/src/main/java/baritone/process/FarmProcess.java index a9188299c..1536bdb61 100644 --- a/src/main/java/baritone/process/FarmProcess.java +++ b/src/main/java/baritone/process/FarmProcess.java @@ -268,7 +268,7 @@ public final class FarmProcess extends BaritoneProcessHelper implements IFarmPro both.addAll(openSoulsand); for (BlockPos pos : both) { boolean soulsand = openSoulsand.contains(pos); - Optional rot = RotationUtils.reachableOffset(ctx.player(), pos, new Vec3d(pos.getX() + 0.5, pos.getY() + 1, pos.getZ() + 0.5), ctx.playerController().getBlockReachDistance(), false); + Optional rot = RotationUtils.reachableOffset(ctx, pos, new Vec3d(pos.getX() + 0.5, pos.getY() + 1, pos.getZ() + 0.5), ctx.playerController().getBlockReachDistance(), false); if (rot.isPresent() && isSafeToCancel && baritone.getInventoryBehavior().throwaway(true, soulsand ? this::isNetherWart : this::isPlantable)) { RayTraceResult result = RayTraceUtils.rayTraceTowards(ctx.player(), rot.get(), ctx.playerController().getBlockReachDistance()); if (result.typeOfHit == RayTraceResult.Type.BLOCK && result.sideHit == EnumFacing.UP) { @@ -286,7 +286,7 @@ public final class FarmProcess extends BaritoneProcessHelper implements IFarmPro continue; } Vec3d faceCenter = new Vec3d(pos).add(0.5, 0.5, 0.5).add(new Vec3d(dir.getDirectionVec()).scale(0.5)); - Optional rot = RotationUtils.reachableOffset(ctx.player(), pos, faceCenter, ctx.playerController().getBlockReachDistance(), false); + Optional rot = RotationUtils.reachableOffset(ctx, pos, faceCenter, ctx.playerController().getBlockReachDistance(), false); if (rot.isPresent() && isSafeToCancel && baritone.getInventoryBehavior().throwaway(true, this::isCocoa)) { RayTraceResult result = RayTraceUtils.rayTraceTowards(ctx.player(), rot.get(), ctx.playerController().getBlockReachDistance()); if (result.typeOfHit == RayTraceResult.Type.BLOCK && result.sideHit == dir) { diff --git a/src/main/java/baritone/process/GetToBlockProcess.java b/src/main/java/baritone/process/GetToBlockProcess.java index 16fc3dda5..7f8376b84 100644 --- a/src/main/java/baritone/process/GetToBlockProcess.java +++ b/src/main/java/baritone/process/GetToBlockProcess.java @@ -210,7 +210,7 @@ public final class GetToBlockProcess extends BaritoneProcessHelper implements IG private boolean rightClick() { for (BlockPos pos : knownLocations) { - Optional reachable = RotationUtils.reachable(ctx.player(), pos, ctx.playerController().getBlockReachDistance()); + Optional reachable = RotationUtils.reachable(ctx, pos, ctx.playerController().getBlockReachDistance()); if (reachable.isPresent()) { baritone.getLookBehavior().updateTarget(reachable.get(), true); if (knownLocations.contains(ctx.getSelectedBlock().orElse(null))) { diff --git a/src/main/java/baritone/process/MineProcess.java b/src/main/java/baritone/process/MineProcess.java index 6880dd86c..e7d7b3365 100644 --- a/src/main/java/baritone/process/MineProcess.java +++ b/src/main/java/baritone/process/MineProcess.java @@ -397,7 +397,7 @@ public final class MineProcess extends BaritoneProcessHelper implements IMinePro // is an x-ray and it'll get caught if (filter.has(bsi.get0(x, y, z))) { BlockPos pos = new BlockPos(x, y, z); - if ((Baritone.settings().legitMineIncludeDiagonals.value && knownOreLocations.stream().anyMatch(ore -> ore.distanceSq(pos) <= 2 /* sq means this is pytha dist <= sqrt(2) */)) || RotationUtils.reachable(ctx.player(), pos, fakedBlockReachDistance).isPresent()) { + if ((Baritone.settings().legitMineIncludeDiagonals.value && knownOreLocations.stream().anyMatch(ore -> ore.distanceSq(pos) <= 2 /* sq means this is pytha dist <= sqrt(2) */)) || RotationUtils.reachable(ctx, pos, fakedBlockReachDistance).isPresent()) { knownOreLocations.add(pos); } } From 1d5ee079b4aea9925041eae05a923da513b1ba5e Mon Sep 17 00:00:00 2001 From: Brady Date: Mon, 12 Jun 2023 12:11:50 -0500 Subject: [PATCH 07/12] Read `serverRotation` from packet and invalidate on world load --- .../java/baritone/behavior/LookBehavior.java | 30 +++++++++++++------ 1 file changed, 21 insertions(+), 9 deletions(-) diff --git a/src/main/java/baritone/behavior/LookBehavior.java b/src/main/java/baritone/behavior/LookBehavior.java index c38cee6f9..d1e5d244f 100644 --- a/src/main/java/baritone/behavior/LookBehavior.java +++ b/src/main/java/baritone/behavior/LookBehavior.java @@ -20,11 +20,13 @@ package baritone.behavior; import baritone.Baritone; import baritone.api.Settings; import baritone.api.behavior.ILookBehavior; +import baritone.api.event.events.PacketEvent; import baritone.api.event.events.PlayerUpdateEvent; import baritone.api.event.events.RotationMoveEvent; -import baritone.api.event.events.type.EventState; +import baritone.api.event.events.WorldEvent; import baritone.api.utils.IPlayerContext; import baritone.api.utils.Rotation; +import net.minecraft.network.play.client.CPacketPlayer; public final class LookBehavior extends Behavior implements ILookBehavior { @@ -56,17 +58,9 @@ public final class LookBehavior extends Behavior implements ILookBehavior { @Override public void onPlayerUpdate(PlayerUpdateEvent event) { - // Capture the player rotation before it's reset to determine the rotation the server knows - // Alternatively, this could be done with a packet event... Maybe that's a better idea. - if (event.getState() == EventState.POST) { - this.serverRotation = new Rotation(ctx.player().rotationYaw, ctx.player().rotationPitch); - } - - // There's nothing left to be done if there isn't a set target if (this.target == null) { return; } - switch (event.getState()) { case PRE: { switch (this.target.mode) { @@ -111,6 +105,24 @@ public final class LookBehavior extends Behavior implements ILookBehavior { } } + @Override + public void onSendPacket(PacketEvent event) { + if (!(event.getPacket() instanceof CPacketPlayer)) { + return; + } + + final CPacketPlayer packet = (CPacketPlayer) event.getPacket(); + if (packet instanceof CPacketPlayer.Rotation || packet instanceof CPacketPlayer.PositionRotation) { + this.serverRotation = new Rotation(packet.getYaw(0.0f), packet.getPitch(0.0f)); + } + } + + @Override + public void onWorldEvent(WorldEvent event) { + this.serverRotation = null; + this.target = null; + } + public void pig() { if (this.target != null) { ctx.player().rotationYaw = this.target.rotation.getYaw(); From c8a0ae9e102e9f85efba642f8a52744d7dc2d1d1 Mon Sep 17 00:00:00 2001 From: Brady Date: Mon, 12 Jun 2023 13:44:08 -0500 Subject: [PATCH 08/12] Do `nudgeToLevel` and `randomLooking` when using freeLook --- .../java/baritone/behavior/LookBehavior.java | 46 ++++++++----------- 1 file changed, 20 insertions(+), 26 deletions(-) diff --git a/src/main/java/baritone/behavior/LookBehavior.java b/src/main/java/baritone/behavior/LookBehavior.java index d1e5d244f..0dc56a3e2 100644 --- a/src/main/java/baritone/behavior/LookBehavior.java +++ b/src/main/java/baritone/behavior/LookBehavior.java @@ -58,43 +58,37 @@ public final class LookBehavior extends Behavior implements ILookBehavior { @Override public void onPlayerUpdate(PlayerUpdateEvent event) { - if (this.target == null) { + if (this.target == null || this.target.mode == Target.Mode.NONE) { return; } switch (event.getState()) { case PRE: { - switch (this.target.mode) { - case CLIENT: { - ctx.player().rotationYaw = this.target.rotation.getYaw(); - float oldPitch = ctx.player().rotationPitch; - float desiredPitch = this.target.rotation.getPitch(); - ctx.player().rotationPitch = desiredPitch; - ctx.player().rotationYaw += (Math.random() - 0.5) * Baritone.settings().randomLooking.value; - ctx.player().rotationPitch += (Math.random() - 0.5) * Baritone.settings().randomLooking.value; - if (desiredPitch == oldPitch && !Baritone.settings().freeLook.value) { - nudgeToLevel(); - } - // The target can be invalidated now since it won't be needed for RotationMoveEvent - this.target = null; - break; - } - case SERVER: { - // Copy the player's actual rotation - this.prevRotation = new Rotation(ctx.player().rotationYaw, ctx.player().rotationPitch); - ctx.player().rotationYaw = this.target.rotation.getYaw(); - ctx.player().rotationPitch = this.target.rotation.getPitch(); - break; - } - default: - break; + if (this.target.mode == Target.Mode.SERVER) { + this.prevRotation = new Rotation(ctx.player().rotationYaw, ctx.player().rotationPitch); + } + + ctx.player().rotationYaw = this.target.rotation.getYaw(); + float oldPitch = ctx.playerRotations().getPitch(); + float desiredPitch = this.target.rotation.getPitch(); + ctx.player().rotationPitch = desiredPitch; + ctx.player().rotationYaw += (Math.random() - 0.5) * Baritone.settings().randomLooking.value; + ctx.player().rotationPitch += (Math.random() - 0.5) * Baritone.settings().randomLooking.value; + if (desiredPitch == oldPitch) { + nudgeToLevel(); + } + + if (this.target.mode == Target.Mode.CLIENT) { + // The target can be invalidated now since it won't be needed for RotationMoveEvent + this.target = null; } break; } case POST: { // Reset the player's rotations back to their original values - if (this.target.mode == Target.Mode.SERVER) { + if (this.prevRotation != null) { ctx.player().rotationYaw = this.prevRotation.getYaw(); ctx.player().rotationPitch = this.prevRotation.getPitch(); + this.prevRotation = null; } // The target is done being used for this game tick, so it can be invalidated this.target = null; From ed34ae73c002d5b23e0e6efc36283dfbe4c78fb7 Mon Sep 17 00:00:00 2001 From: Brady Date: Mon, 12 Jun 2023 13:46:10 -0500 Subject: [PATCH 09/12] Fix `Mode.NONE` target invalidation --- src/main/java/baritone/behavior/LookBehavior.java | 5 ++++- 1 file changed, 4 insertions(+), 1 deletion(-) diff --git a/src/main/java/baritone/behavior/LookBehavior.java b/src/main/java/baritone/behavior/LookBehavior.java index 0dc56a3e2..1e109482c 100644 --- a/src/main/java/baritone/behavior/LookBehavior.java +++ b/src/main/java/baritone/behavior/LookBehavior.java @@ -58,11 +58,14 @@ public final class LookBehavior extends Behavior implements ILookBehavior { @Override public void onPlayerUpdate(PlayerUpdateEvent event) { - if (this.target == null || this.target.mode == Target.Mode.NONE) { + if (this.target == null) { return; } switch (event.getState()) { case PRE: { + if (this.target.mode == Target.Mode.NONE) { + return; + } if (this.target.mode == Target.Mode.SERVER) { this.prevRotation = new Rotation(ctx.player().rotationYaw, ctx.player().rotationPitch); } From 13fc58933d6cf1db497d5303d290f65588b6c652 Mon Sep 17 00:00:00 2001 From: Brady Date: Mon, 12 Jun 2023 19:03:03 -0500 Subject: [PATCH 10/12] Don't bother returning `serverRotations` if no free look --- src/main/java/baritone/behavior/LookBehavior.java | 12 +++++++++--- .../baritone/utils/player/PrimaryPlayerContext.java | 7 ++----- 2 files changed, 11 insertions(+), 8 deletions(-) diff --git a/src/main/java/baritone/behavior/LookBehavior.java b/src/main/java/baritone/behavior/LookBehavior.java index 1e109482c..702c3738d 100644 --- a/src/main/java/baritone/behavior/LookBehavior.java +++ b/src/main/java/baritone/behavior/LookBehavior.java @@ -28,6 +28,8 @@ import baritone.api.utils.IPlayerContext; import baritone.api.utils.Rotation; import net.minecraft.network.play.client.CPacketPlayer; +import java.util.Optional; + public final class LookBehavior extends Behavior implements ILookBehavior { /** @@ -70,9 +72,9 @@ public final class LookBehavior extends Behavior implements ILookBehavior { this.prevRotation = new Rotation(ctx.player().rotationYaw, ctx.player().rotationPitch); } - ctx.player().rotationYaw = this.target.rotation.getYaw(); float oldPitch = ctx.playerRotations().getPitch(); float desiredPitch = this.target.rotation.getPitch(); + ctx.player().rotationYaw = this.target.rotation.getYaw(); ctx.player().rotationPitch = desiredPitch; ctx.player().rotationYaw += (Math.random() - 0.5) * Baritone.settings().randomLooking.value; ctx.player().rotationPitch += (Math.random() - 0.5) * Baritone.settings().randomLooking.value; @@ -126,8 +128,12 @@ public final class LookBehavior extends Behavior implements ILookBehavior { } } - public Rotation getEffectiveRotation() { - return this.serverRotation; + public Optional getEffectiveRotation() { + if (Baritone.settings().freeLook.value || Baritone.settings().blockFreeLook.value) { + return Optional.of(this.serverRotation); + } + // If neither of the freeLook settings are on, just defer to the player's actual rotations + return Optional.empty(); } @Override diff --git a/src/main/java/baritone/utils/player/PrimaryPlayerContext.java b/src/main/java/baritone/utils/player/PrimaryPlayerContext.java index e4484f0be..20d82f2bc 100644 --- a/src/main/java/baritone/utils/player/PrimaryPlayerContext.java +++ b/src/main/java/baritone/utils/player/PrimaryPlayerContext.java @@ -58,11 +58,8 @@ public enum PrimaryPlayerContext implements IPlayerContext, Helper { @Override public Rotation playerRotations() { - final Rotation lbTarget = ((LookBehavior) BaritoneAPI.getProvider().getPrimaryBaritone().getLookBehavior()).getEffectiveRotation(); - if (lbTarget == null || !Baritone.settings().blockFreeLook.value) { - return IPlayerContext.super.playerRotations(); - } - return lbTarget; + return ((LookBehavior) BaritoneAPI.getProvider().getPrimaryBaritone().getLookBehavior()).getEffectiveRotation() + .orElseGet(IPlayerContext.super::playerRotations); } @Override From 4885d49d20debf7bc4f8328c11e08549e57c5abe Mon Sep 17 00:00:00 2001 From: Brady Date: Mon, 12 Jun 2023 20:00:03 -0500 Subject: [PATCH 11/12] Reorganize code --- .../java/baritone/behavior/LookBehavior.java | 32 ++++++++++++------- .../utils/player/PrimaryPlayerContext.java | 1 - 2 files changed, 20 insertions(+), 13 deletions(-) diff --git a/src/main/java/baritone/behavior/LookBehavior.java b/src/main/java/baritone/behavior/LookBehavior.java index 702c3738d..db0c255f7 100644 --- a/src/main/java/baritone/behavior/LookBehavior.java +++ b/src/main/java/baritone/behavior/LookBehavior.java @@ -72,16 +72,23 @@ public final class LookBehavior extends Behavior implements ILookBehavior { this.prevRotation = new Rotation(ctx.player().rotationYaw, ctx.player().rotationPitch); } - float oldPitch = ctx.playerRotations().getPitch(); + final float oldPitch = ctx.playerRotations().getPitch(); + + float desiredYaw = this.target.rotation.getYaw(); float desiredPitch = this.target.rotation.getPitch(); - ctx.player().rotationYaw = this.target.rotation.getYaw(); - ctx.player().rotationPitch = desiredPitch; - ctx.player().rotationYaw += (Math.random() - 0.5) * Baritone.settings().randomLooking.value; - ctx.player().rotationPitch += (Math.random() - 0.5) * Baritone.settings().randomLooking.value; + + // In other words, the target doesn't care about the pitch, so it used playerRotations().getPitch() + // and it's safe to adjust it to a normal level if (desiredPitch == oldPitch) { - nudgeToLevel(); + desiredPitch = nudgeToLevel(desiredPitch); } + desiredYaw += (Math.random() - 0.5) * Baritone.settings().randomLooking.value; + desiredPitch += (Math.random() - 0.5) * Baritone.settings().randomLooking.value; + + ctx.player().rotationYaw = desiredYaw; + ctx.player().rotationPitch = desiredPitch; + if (this.target.mode == Target.Mode.CLIENT) { // The target can be invalidated now since it won't be needed for RotationMoveEvent this.target = null; @@ -130,7 +137,7 @@ public final class LookBehavior extends Behavior implements ILookBehavior { public Optional getEffectiveRotation() { if (Baritone.settings().freeLook.value || Baritone.settings().blockFreeLook.value) { - return Optional.of(this.serverRotation); + return Optional.ofNullable(this.serverRotation); } // If neither of the freeLook settings are on, just defer to the player's actual rotations return Optional.empty(); @@ -146,12 +153,13 @@ public final class LookBehavior extends Behavior implements ILookBehavior { /** * Nudges the player's pitch to a regular level. (Between {@code -20} and {@code 10}, increments are by {@code 1}) */ - private void nudgeToLevel() { - if (ctx.player().rotationPitch < -20) { - ctx.player().rotationPitch++; - } else if (ctx.player().rotationPitch > 10) { - ctx.player().rotationPitch--; + private float nudgeToLevel(float pitch) { + if (pitch < -20) { + return pitch + 1; + } else if (pitch > 10) { + return pitch - 1; } + return pitch; } private static class Target { diff --git a/src/main/java/baritone/utils/player/PrimaryPlayerContext.java b/src/main/java/baritone/utils/player/PrimaryPlayerContext.java index 20d82f2bc..02db73a5c 100644 --- a/src/main/java/baritone/utils/player/PrimaryPlayerContext.java +++ b/src/main/java/baritone/utils/player/PrimaryPlayerContext.java @@ -17,7 +17,6 @@ package baritone.utils.player; -import baritone.Baritone; import baritone.api.BaritoneAPI; import baritone.api.cache.IWorldData; import baritone.api.utils.*; From c8b8deb3d62d5a786054fd1939293cb71673258c Mon Sep 17 00:00:00 2001 From: Brady Date: Mon, 12 Jun 2023 21:04:30 -0500 Subject: [PATCH 12/12] Ensure angle delta is producible with mouse movement --- .../java/baritone/behavior/LookBehavior.java | 24 ++++++++++++++++--- 1 file changed, 21 insertions(+), 3 deletions(-) diff --git a/src/main/java/baritone/behavior/LookBehavior.java b/src/main/java/baritone/behavior/LookBehavior.java index db0c255f7..99e496e6f 100644 --- a/src/main/java/baritone/behavior/LookBehavior.java +++ b/src/main/java/baritone/behavior/LookBehavior.java @@ -24,6 +24,7 @@ import baritone.api.event.events.PacketEvent; import baritone.api.event.events.PlayerUpdateEvent; import baritone.api.event.events.RotationMoveEvent; import baritone.api.event.events.WorldEvent; +import baritone.api.utils.Helper; import baritone.api.utils.IPlayerContext; import baritone.api.utils.Rotation; import net.minecraft.network.play.client.CPacketPlayer; @@ -72,6 +73,7 @@ public final class LookBehavior extends Behavior implements ILookBehavior { this.prevRotation = new Rotation(ctx.player().rotationYaw, ctx.player().rotationPitch); } + final float oldYaw = ctx.playerRotations().getYaw(); final float oldPitch = ctx.playerRotations().getPitch(); float desiredYaw = this.target.rotation.getYaw(); @@ -86,8 +88,8 @@ public final class LookBehavior extends Behavior implements ILookBehavior { desiredYaw += (Math.random() - 0.5) * Baritone.settings().randomLooking.value; desiredPitch += (Math.random() - 0.5) * Baritone.settings().randomLooking.value; - ctx.player().rotationYaw = desiredYaw; - ctx.player().rotationPitch = desiredPitch; + ctx.player().rotationYaw = calculateMouseMove(oldYaw, desiredYaw); + ctx.player().rotationPitch = calculateMouseMove(oldPitch, desiredPitch); if (this.target.mode == Target.Mode.CLIENT) { // The target can be invalidated now since it won't be needed for RotationMoveEvent @@ -153,7 +155,7 @@ public final class LookBehavior extends Behavior implements ILookBehavior { /** * Nudges the player's pitch to a regular level. (Between {@code -20} and {@code 10}, increments are by {@code 1}) */ - private float nudgeToLevel(float pitch) { + private static float nudgeToLevel(float pitch) { if (pitch < -20) { return pitch + 1; } else if (pitch > 10) { @@ -162,6 +164,22 @@ public final class LookBehavior extends Behavior implements ILookBehavior { return pitch; } + private static float calculateMouseMove(float current, float target) { + final float delta = target - current; + final int deltaPx = angleToMouse(delta); + return current + mouseToAngle(deltaPx); + } + + private static int angleToMouse(float angleDelta) { + final float minAngleChange = mouseToAngle(1); + return Math.round(angleDelta / minAngleChange); + } + + private static float mouseToAngle(int mouseDelta) { + final float f = Helper.mc.gameSettings.mouseSensitivity * 0.6f + 0.2f; + return mouseDelta * f * f * f * 8.0f * 0.15f; + } + private static class Target { public final Rotation rotation;