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 <noreply@anthropic.com>
This commit is contained in:
Ludovic Marie
2026-10-24 11:13:27 +02:00
parent 7c83fbb6df
commit c33acfdd05
6 changed files with 63 additions and 18 deletions
+10 -2
View File
@@ -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;
}
+9 -1
View File
@@ -45,6 +45,14 @@ pub(super) struct Strike {
team: Option<horizon_client::scoreboard::Team>,
}
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();
}
+9
View File
@@ -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.
+4
View File
@@ -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,
+29 -10
View File
@@ -1477,16 +1477,7 @@ impl Hub {
attributes: Vec<play::AttributeSnapshot>,
) -> 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::*;
+2 -5
View File
@@ -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)",
}