Repository: cabaletta/baritone (language: java)

Issue:
1.20.1 Use pose system for calculating the height of crouching instead of hardcoded values
instead of using hardcoded crouching values, just use the built in method for geting a entitys eye height with a specific pose. Should close #4484 from personal testing

closes #4484 


Discussion / hints:
Removing a public method from the API package. I'd say an `@Deprecated` is more appropriate.

Otherwise a simple and sane looking change.
IPlayerContext does have access to the player entity, so you could just put the pose thing in the old function
> IPlayerContext does have access to the player entity, so you could just put the pose thing in the old function

the problem is its static so I'm not sure how to even reference the IPlayerContext directly here since it doesn't have a instance reference. and if it was changed to non static it would still be a breaking change
There is the possibility of `BaritoneAPI.getProvider().getAllBaritones().stream().map(Baritone::getPlayerContext).filter(c -> c.player() == entity).findFirst().get().getEyeHeight(true)`, but that's clearly worse and (re?)introducing `BaritoneProvider::getBaritoneForPlayer` wouldn't help much.
Also why assume the entity is a player? I'd say either the method has to take a `LocalPlayer` argument or assuming the entity is a player is plain wrong. Not sure whether we should assume there is a Baritone instance for the passed player.

EDIT: I've been tricked by an interface default implementation. `IBaritoneProvider::getBaritoneForPlayer` does exist, but not in the file I looked at. So it would be `BaritoneAPI.getProvider().getBaritoneForPlayer((LocalPlayer) entity).getPlayerContext().getEyeHeight(true)`, with potential for CCE and NPE for no reason.
> There is the possibility of `BaritoneAPI.getProvider().getAllBaritones().stream().map(Baritone::getPlayerContext).filter(c -> c.player() == player).findFirst().get().getEyeHeight(true)`, but that's clearly worse and (re?)introducing `BaritoneProvider::getBaritoneForPlayer` wouldn't help much. Also why assume the entity is a player? I'd say either the method has to take a `LocalPlayer` argument or assuming the entity is a player is plain wrong. Not sure whether we should assume there is a Baritone instance for the passed player.

yeah that long method call is just painful to read as is and feels very breakable lol. I think deprecating it is a better option here, and anyone using it should just refer to the entity itself in the same way the codebase does
can this be done to the 1.19 branch?

Fix patch:
diff --git a/src/api/java/baritone/api/utils/IPlayerContext.java b/src/api/java/baritone/api/utils/IPlayerContext.java
index a97475a5b..de6d7cd58 100644
--- a/src/api/java/baritone/api/utils/IPlayerContext.java
+++ b/src/api/java/baritone/api/utils/IPlayerContext.java
@@ -99,6 +99,14 @@ default Rotation playerRotations() {
         return new Rotation(player().getYRot(), player().getXRot());
     }
 
+    /**
+     * Returns the player's eye height, taking into account whether or not the player is sneaking.
+     *
+     * @param ifSneaking Whether or not the player is sneaking
+     * @return The player's eye height
+     * @deprecated Use entity.getEyeHeight(Pose.CROUCHING) instead
+     */
+    @Deprecated
     static double eyeHeight(boolean ifSneaking) {
         return ifSneaking ? 1.27 : 1.62;
     }
diff --git a/src/api/java/baritone/api/utils/RayTraceUtils.java b/src/api/java/baritone/api/utils/RayTraceUtils.java
index e99566863..ed544302d 100644
--- a/src/api/java/baritone/api/utils/RayTraceUtils.java
+++ b/src/api/java/baritone/api/utils/RayTraceUtils.java
@@ -18,6 +18,7 @@
 package baritone.api.utils;
 
 import net.minecraft.world.entity.Entity;
+import net.minecraft.world.entity.Pose;
 import net.minecraft.world.level.ClipContext;
 import net.minecraft.world.phys.HitResult;
 import net.minecraft.world.phys.Vec3;
@@ -62,6 +63,6 @@ public static HitResult rayTraceTowards(Entity entity, Rotation rotation, double
     }
 
     public static Vec3 inferSneakingEyePosition(Entity entity) {
-        return new Vec3(entity.getX(), entity.getY() + IPlayerContext.eyeHeight(true), entity.getZ());
+        return new Vec3(entity.getX(), entity.getY() + entity.getEyeHeight(Pose.CROUCHING), entity.getZ());
     }
 }


Test patch: