From c33acfdd058069f7eb256604445cafe2e07dca68 Mon Sep 17 00:00:00 2001 From: Ludovic Marie <136255825+ludovic111@users.noreply.github.com> Date: Sat, 24 Oct 2026 11:13:27 +0200 Subject: [PATCH] Stop a guest's sprint when its hit knocks back Player.causeExtraKnockback stops the attacker's sprint on the server, and the shared flag reaches the attacker's own client (ServerEntity sends a player its own dirty entity data). A guest's sprint was only stopped on the server copy for the length of the hit and never sent, so a guest (and, on the server thread, the window) kept sprinting: every hit while sprinting had the sprint knockback, where vanilla's needs the sprint to start again. - leave_guest keeps the sprint stop a guest's hit made; a guest hitting a player stops its sprint too. - The hub sends each guest its own shared flags (crouching, sprinting, gliding) when they change. - A remote client takes the sprinting bit of its own shared flags (the flag only, as vanilla's assignValues: the speed modifier and the wasSprinting it compares stay, so it sends START_SPRINTING again only after it has stopped). Co-Authored-By: Claude Opus 5.5 --- crates/horizon-client/src/game/lan_actions.rs | 12 +++++- crates/horizon-client/src/game/lan_pvp.rs | 10 ++++- crates/horizon-client/src/game/remote.rs | 9 +++++ crates/horizon-client/src/lan/guest.rs | 4 ++ crates/horizon-client/src/lan/hub.rs | 39 ++++++++++++++----- tools/test_server_knockback_parity.py | 7 +--- 6 files changed, 63 insertions(+), 18 deletions(-) diff --git a/crates/horizon-client/src/game/lan_actions.rs b/crates/horizon-client/src/game/lan_actions.rs index d42ffc511..500c22097 100644 --- a/crates/horizon-client/src/game/lan_actions.rs +++ b/crates/horizon-client/src/game/lan_actions.rs @@ -595,6 +595,11 @@ impl Game<'_> { // A teleport (commands, a pearl) moves the guest; a pearl's is // sent even to where they stand (ServerPlayer.teleport). let at = [m.position.x, m.position.y, m.position.z]; + // A hit with extra knockback stopped the guest's sprint + // (Player.causeExtraKnockback's setSprinting(false)). + if !m.sprinting { + g.sprinting = false; + } m.sprinting = false; let teleported = std::mem::take(&mut m.teleported); let moved: f64 = (0..3).map(|k| (at[k] - g.position[k]).powi(2)).sum(); @@ -1322,8 +1327,11 @@ impl Game<'_> { return; } if let Some(victim) = Self::pvp_victim(host, conn, id) { - if let Some(Some(strike)) = self.as_guest(host, conn, |g| g.pvp_strike()) { - self.pvp_land(host, victim, &strike); + if let Some(Some(strike)) = self.as_guest(host, conn, |g| g.pvp_strike()) + && self.pvp_land(host, victim, &strike) + && strike.stops_sprint() + { + self.as_guest(host, conn, |g| g.stop_sprint_after_extra_knockback()); } return; } diff --git a/crates/horizon-client/src/game/lan_pvp.rs b/crates/horizon-client/src/game/lan_pvp.rs index 74c88895b..467c4537c 100644 --- a/crates/horizon-client/src/game/lan_pvp.rs +++ b/crates/horizon-client/src/game/lan_pvp.rs @@ -45,6 +45,14 @@ pub(super) struct Strike { team: Option, } +impl Strike { + /// Whether it has extra knockback, which stops the attacker's sprint + /// once it lands (Player.causeExtraKnockback). + pub(super) fn stops_sprint(&self) -> bool { + self.knockback.0 > 0.0 + } +} + impl Game<'_> { /// The guest on the host's crosshair and how far, within reach. pub(super) fn guest_hit(&self) -> Option<(u32, f64)> { @@ -74,7 +82,7 @@ impl Game<'_> { }; if let Some(strike) = self.pvp_strike() && self.pvp_land(&mut host, Victim::Guest(conn), &strike) - && strike.knockback.0 > 0.0 + && strike.stops_sprint() { self.stop_sprint_after_extra_knockback(); } diff --git a/crates/horizon-client/src/game/remote.rs b/crates/horizon-client/src/game/remote.rs index d831d92f8..df0da4bac 100644 --- a/crates/horizon-client/src/game/remote.rs +++ b/crates/horizon-client/src/game/remote.rs @@ -1990,6 +1990,15 @@ impl Game<'_> { } self.sleeping = *bed; } + // Entity.DATA_SHARED_FLAGS_ID's sprinting bit: the + // server stops the sprint after a hit's extra + // knockback (the flag only, as assignValues sets + // it: the speed modifier and wasSprinting stay). + (0, Value::Byte(flags)) => { + if let Some(pl) = self.player.as_mut() { + pl.sprinting = flags & 0x08 != 0; + } + } // Entity.DATA_AIR_SUPPLY_ID and // Player.DATA_PLAYER_ABSORPTION_ID: the bubbles // and golden hearts the server's player has. diff --git a/crates/horizon-client/src/lan/guest.rs b/crates/horizon-client/src/lan/guest.rs index 7d1b10eaa..256f3a5cf 100644 --- a/crates/horizon-client/src/lan/guest.rs +++ b/crates/horizon-client/src/lan/guest.rs @@ -206,6 +206,9 @@ pub struct Guest { /// (Entity.DATA_AIR_SUPPLY_ID, Player.DATA_PLAYER_ABSORPTION_ID); their /// defaults until either changes. pub sent_air: (i32, f32), + /// The shared flags (Entity.DATA_SHARED_FLAGS_ID) the player's client was + /// last sent about itself. + pub sent_shared: i8, /// Where the player last died (Player.lastDeathLocation), sent with the respawn. pub last_death: Option<[i32; 3]>, /// The last action sequence to acknowledge at the next tick (-1: none). @@ -336,6 +339,7 @@ impl Guest { sent_health: None, sent_xp: None, sent_air: (300, 0.0), + sent_shared: 0, last_death: None, ack_up_to: std::cell::Cell::new(-1), sent_selected: 0, diff --git a/crates/horizon-client/src/lan/hub.rs b/crates/horizon-client/src/lan/hub.rs index 406f2f872..4f93b6ff9 100644 --- a/crates/horizon-client/src/lan/hub.rs +++ b/crates/horizon-client/src/lan/hub.rs @@ -1477,16 +1477,7 @@ impl Hub { attributes: Vec, ) -> EntityState { let (sneaking, sprinting, gliding, sleeping) = flags; - let mut shared = 0i8; - if sneaking { - shared |= 0x02; - } - if sprinting { - shared |= 0x08; - } - if gliding { - shared |= -128; - } + let shared = shared_flags(sneaking, sprinting, gliding); let pose = if sleeping { 2 } else if gliding { @@ -1764,6 +1755,18 @@ impl Hub { })); } } + // ServerEntity.sendDirtyEntityData: the shared flags go to the player + // too, so the sprint a hit stops (Player.causeExtraKnockback) stops + // on its client. + let g = &mut self.guests[i]; + let shared = shared_flags(g.sneaking, g.sprinting, g.fall_flying); + if shared != g.sent_shared { + g.sent_shared = shared; + g.send(&C::SetEntityData(play::SetEntityData { + id: g.entity_id, + values: vec![(0, serializer::BYTE, Value::Byte(shared))], + })); + } // Sent when the health or the food level changes, or the saturation // reaches or leaves zero; a dead player's health of 0 too. let g = &mut self.guests[i]; @@ -2480,6 +2483,7 @@ impl Hub { g.sent_health = None; g.sent_xp = None; g.sent_air = (300, 0.0); + g.sent_shared = 0; let (cx, cz) = g.chunk(); g.send(&C::SetChunkCacheCenter(play::SetChunkCacheCenter { x: cx, @@ -2519,6 +2523,21 @@ fn ticking_state(rate: f32, frozen: bool) -> C { raw(ids::TICKING_STATE, w.data) } +/// Entity.DATA_SHARED_FLAGS_ID of a player: crouching, sprinting, gliding. +fn shared_flags(sneaking: bool, sprinting: bool, gliding: bool) -> i8 { + let mut shared = 0i8; + if sneaking { + shared |= 0x02; + } + if sprinting { + shared |= 0x08; + } + if gliding { + shared |= -128; + } + shared +} + #[cfg(test)] mod tests { use super::*; diff --git a/tools/test_server_knockback_parity.py b/tools/test_server_knockback_parity.py index 547e803a2..c6c634c0f 100644 --- a/tools/test_server_knockback_parity.py +++ b/tools/test_server_knockback_parity.py @@ -56,15 +56,12 @@ PROPERTIES = { 'pause-when-empty-seconds': '-1', } # Cases Horizon still gets wrong, and why. -SPRINT = "the server never sends a guest its own sprint flag, so a sprint hit doesn't stop the attacker's sprint" YAW = "the extra knockback's direction comes from f32 sin/cos, not Mth's sine table (z is -2e-8 a block, not 0)" KNOWN = { - 'cow-sprint': f'{SPRINT}; {YAW}', + 'cow-sprint': YAW, 'cow-knockback-2': YAW, - 'zombie-sprint': f'{SPRINT}; {YAW}', + 'zombie-sprint': YAW, 'zombie-knockback-2': YAW, - 'pvp-sprint': SPRINT, - 'pvp-sprint-twice': SPRINT, 'pvp-mace': "a smash on a player runs none of MaceItem.hurtEnemy (the attacker's fall goes on)", }