diff options
| author | mat <git@matdoes.dev> | 2025-08-12 10:47:33 +1000 |
|---|---|---|
| committer | mat <git@matdoes.dev> | 2025-08-12 07:47:38 +0700 |
| commit | 29c32c9ba89d60b5b0e415eaab58b947080655c5 (patch) | |
| tree | ce7a6291aa83409f86037f264c7053450ce6dd1d /azalea-entity/src | |
| parent | 7d9dee7a4dcf7e2b41bee37d2aebaa6ef48a5ba2 (diff) | |
| download | azalea-drasl-29c32c9ba89d60b5b0e415eaab58b947080655c5.tar.xz | |
fix rotations flagging anticheats
Diffstat (limited to 'azalea-entity/src')
| -rw-r--r-- | azalea-entity/src/lib.rs | 56 | ||||
| -rw-r--r-- | azalea-entity/src/plugin/mod.rs | 4 |
2 files changed, 53 insertions, 7 deletions
diff --git a/azalea-entity/src/lib.rs b/azalea-entity/src/lib.rs index 984cce27..8758c258 100644 --- a/azalea-entity/src/lib.rs +++ b/azalea-entity/src/lib.rs @@ -217,13 +217,16 @@ impl From<&LastSentPosition> for BlockPos { #[derive(Debug, Component, Copy, Clone, Deref, DerefMut, Default, PartialEq, Eq)] pub struct Jumping(pub bool); -/// A component that contains the direction an entity is looking. +/// A component that contains the direction an entity is looking, in degrees. +/// +/// To avoid flagging anticheats, consider using [`Self::update`] when updating +/// the values of this struct. #[derive(Debug, Component, Copy, Clone, Default, PartialEq, AzBuf)] pub struct LookDirection { - /// Left and right. AKA yaw. - pub y_rot: f32, - /// Up and down. AKA pitch. - pub x_rot: f32, + /// Left and right. AKA yaw. In degrees. + y_rot: f32, + /// Up and down. AKA pitch. In degrees. + x_rot: f32, } impl LookDirection { @@ -232,6 +235,49 @@ impl LookDirection { pub fn new(y_rot: f32, x_rot: f32) -> Self { apply_clamp_look_direction(Self { y_rot, x_rot }) } + /// Returns yaw (left and right) in degrees. + /// + /// Minecraft allows this to go outside of ±360°, so it won't necessarily be + /// in any range. + pub fn y_rot(&self) -> f32 { + self.y_rot + } + /// Returns pitch (up and down) in degrees. + /// + /// Clamped to ±90°. + pub fn x_rot(&self) -> f32 { + self.x_rot + } + + /// Update this look direction to the new value, while handling relative + /// rotations correctly and with the default Minecraft sensitivity to avoid + /// triggering anticheats. + pub fn update(&mut self, new: LookDirection) { + self.update_with_sensitivity(new, 1.); + } + + /// Update this look direction to the new value, using the given + /// sensitivity value. + /// + /// Consider using [`Self::set`] instead, which uses 1.0 as the sensitivity + /// (equivalent to 100% sensitivity in Minecraft). + pub fn update_with_sensitivity(&mut self, new: LookDirection, sensitivity: f32) { + let mut delta_y_rot = new.y_rot.rem_euclid(360.) - self.y_rot.rem_euclid(360.); + let delta_x_rot = new.x_rot - self.x_rot; + + if delta_y_rot > 180. { + delta_y_rot -= 360.; + } else if delta_y_rot < -180. { + delta_y_rot += 360.; + } + + let sensitivity = sensitivity * 0.15; + let delta_y_rot = (delta_y_rot / sensitivity).round() * sensitivity; + let delta_x_rot = (delta_x_rot / sensitivity).round() * sensitivity; + + self.y_rot += delta_y_rot; + self.x_rot += delta_x_rot; + } } impl From<LookDirection> for (f32, f32) { diff --git a/azalea-entity/src/plugin/mod.rs b/azalea-entity/src/plugin/mod.rs index 88e87de1..924a6f53 100644 --- a/azalea-entity/src/plugin/mod.rs +++ b/azalea-entity/src/plugin/mod.rs @@ -191,8 +191,8 @@ pub fn clamp_look_direction(mut query: Query<&mut LookDirection>) { } } pub fn apply_clamp_look_direction(mut look_direction: LookDirection) -> LookDirection { - look_direction.y_rot = look_direction.y_rot.rem_euclid(360.0); - look_direction.x_rot = look_direction.x_rot.clamp(-90.0, 90.0) % 360.0; + look_direction.x_rot = look_direction.x_rot.clamp(-90., 90.); + look_direction } |
