Merge pull request #1648 from ludovic111/fix/remote-fall-hurt

This commit is contained in:
Ludovic Marie
2026-10-25 02:38:44 +01:00
committed by GitHub
5 changed files with 105 additions and 4 deletions
+2
View File
@@ -472,6 +472,8 @@ impl Game<'_> {
"sprinting": self.keys.contains(&KeyCode::ControlLeft),
"health": p.health.health(),
"absorption": p.health.absorption(),
// The hurt animation's ticks left (the camera's hurt tilt).
"hurt_time": p.health.hurt_animation_time,
"air": p.air,
"food": p.health.food.food,
"saturation": p.health.food.saturation,
+24 -4
View File
@@ -143,6 +143,9 @@ pub(super) struct Remote {
/// Item entities waiting for their item data.
pending_items: HashMap<i32, [f64; 3]>,
health: (f32, i32, f32),
/// `LocalPlayer.flashOnSetHealth`: set by the first health the server
/// sends; from then on a drop starts the hurt animation.
flash_on_set_health: bool,
game_mode: i32,
pub chat: Vec<(String, Instant)>,
/// Lines received since joining, for the control socket's events.
@@ -411,6 +414,7 @@ impl Game<'_> {
head_lerps: HashMap::new(),
pending_items: HashMap::new(),
health: (20.0, 20, 5.0),
flash_on_set_health: false,
game_mode: 0,
chat: Vec::new(),
chat_received: 0,
@@ -2128,14 +2132,24 @@ impl Game<'_> {
// ClientPacketListener.handleDamageEvent: LivingEntity.handleDamageEvent
// jolts the walk animation and restarts the hurt flash. Its hurt
// sound comes as a sound from Horizon's host, which plays it itself.
// This player's own hurt tilt and flash come from the server too
// (sent to the player itself): its own moves never hurt it.
C::DamageEvent(p) => {
if let Some(m) = self.mobs.iter_mut().find(|m| m.id == p.id as u32) {
if self.remote.as_ref().is_some_and(|r| r.entity_id == p.id) {
if let Some(pl) = self.player.as_mut() {
pl.health.animate_hurt();
}
} else if let Some(m) = self.mobs.iter_mut().find(|m| m.id == p.id as u32) {
m.limb_speed = 1.5;
m.hurt_time = 10;
}
}
C::HurtAnimation(p) => {
if let Some(m) = self.mobs.iter_mut().find(|m| m.id == p.id as u32) {
if self.remote.as_ref().is_some_and(|r| r.entity_id == p.id) {
if let Some(pl) = self.player.as_mut() {
pl.health.animate_hurt();
}
} else if let Some(m) = self.mobs.iter_mut().find(|m| m.id == p.id as u32) {
m.hurt_time = 10;
}
}
@@ -2201,9 +2215,14 @@ impl Game<'_> {
self.remote_world_options_difficulty(self.ui.difficulty, p.locked);
}
C::GameRuleValues(p) => self.remote_world_options_rules(p.entries),
// ClientPacketListener.handleSetHealth: LocalPlayer.hurtTo.
C::SetHealth(p) => {
if let Some(r) = self.remote.as_mut() {
let flash = self.remote.as_mut().is_some_and(|r| {
r.health = (p.health, p.food, p.saturation);
std::mem::replace(&mut r.flash_on_set_health, true)
});
if flash && let Some(pl) = self.player.as_mut() {
pl.health.flash_health_change(p.health);
}
self.apply_remote_health();
if p.health <= 0.0 && self.screen != Screen::Dead && self.player.is_some() {
@@ -3179,6 +3198,7 @@ impl Game<'_> {
);
player.velocity = Vec3::ZERO;
} else if !dead && !riding {
player.client_side = true;
player.velocity = player.velocity.add_vector(pushed);
MotionWorld {
entities: &entity_collisions,
@@ -3222,7 +3242,7 @@ impl Game<'_> {
_ => {}
}
}
// The server owns health and hunger: undo local food/fall changes.
// The server owns health and hunger.
self.apply_remote_health();
self.tick_avatar(before, false);
// The player's own model (third person, the inventory's preview).
+19
View File
@@ -253,6 +253,22 @@ impl PlayerHealth {
self.hurt_animation_time -= 1;
}
}
/// `LivingEntity.animateHurt` / `handleDamageEvent` on a client: the hurt
/// tilt and red flash restart.
pub fn animate_hurt(&mut self) {
self.hurt.hurt_duration = 10;
self.hurt.hurt_time = 10;
self.hurt_animation_time = 10;
}
/// `LocalPlayer.hurtTo` with `flashOnSetHealth`: a drop below the current
/// health starts the hurt animation.
pub fn flash_health_change(&mut self, health: f32) {
let damage = self.health() - health;
if damage > 0.0 {
self.hurt.last_hurt = damage;
self.animate_hurt();
}
}
}
impl PlayerMotion {
pub fn hurt(
@@ -274,6 +290,9 @@ impl PlayerMotion {
amount: f32,
blocking: Option<Blocking>,
) -> Result<BlockOutcome> {
if self.client_side {
return Ok(BlockOutcome::default());
}
let source = resources
.damage
.source::<()>(source, None, None, None)
+3
View File
@@ -55,6 +55,9 @@ impl PlayerMotion {
Ok(())
}
pub fn food_tick(&mut self, resources: &HealthResources, rules: HealthRules) -> Result<()> {
if self.client_side {
return Ok(());
}
let h = &mut self.health;
let mut world = TickWorld {
resources,
+57
View File
@@ -300,6 +300,10 @@ pub struct PlayerMotion {
/// it travels on the server: `isLocalInstanceAuthoritative` is false, so
/// its moves check no fall damage.
pub server_copy: bool,
/// A client's copy of its own player (`Level.isClientSide`): only the
/// server hurts it (`Entity.hurt` does nothing on a client) and ticks its
/// food (`ServerPlayer.tick`). Its hurt animation comes from the server.
pub client_side: bool,
/// `Entity.applyEffectsFromBlocks` runs after travel (always in the game;
/// differential fixtures turn it off where their reference does).
pub block_effects: bool,
@@ -465,6 +469,7 @@ impl PlayerMotion {
freeze_immune: false,
old_position: position,
server_copy: false,
client_side: false,
block_effects: true,
powder_snow_entered: Vec::new(),
hurt_log: Vec::new(),
@@ -2860,6 +2865,58 @@ mod tests {
}
}
}
/// A client's own fall (multiplayer, a LAN guest, single player on its
/// server thread) plays its landing but never hurts it: only the server
/// does (`Entity.hurt` on a client), so a server that cancels fall damage
/// leaves the screen still.
#[test]
fn a_clients_own_fall_does_not_hurt_it() {
let root = Path::new(env!("CARGO_MANIFEST_DIR"))
.join("../../.local/horizon-client")
.join(horizon_core::VANILLA_VERSION);
if !root.join("movement-properties.nbt").exists() {
return;
}
let assets = Assets::load(&root).unwrap();
let render = assets.reload(&root).unwrap();
let binding = crate::collisions::load(&root, assets.registry).unwrap();
let resources = MotionResources::load(&root, &assets).unwrap();
for client_side in [false, true] {
let mut scene = World::test_scene(&assets, &render, 2).unwrap();
let mut player = PlayerMotion::new(&resources, Vec3::new(20.5, 20.0, 20.5)).unwrap();
player.client_side = client_side;
player.fall_distance = 30.0;
let mut w = MotionWorld {
entities: &[],
player: &mut player,
scene: &mut scene,
assets: &assets,
collision_binding: &binding,
resources: &resources,
};
w.move_self(Vec3::new(0.0, -30.0, 0.0)).unwrap();
assert!(w.player.ground.on_ground);
assert_eq!(w.player.fall_distance, 0.0);
// LivingEntity.causeFallDamage's sounds play on both sides.
assert!(w.player.events.iter().any(|e| matches!(
e,
MotionEvent::Fall(crate::landing::FallEvent::Sound {
sound: crate::landing::SceneSound::BigFall,
..
})
)));
let hurt = w.player.health.health() < 20.0;
assert_eq!(hurt, !client_side);
assert_eq!(w.player.health.hurt_animation_time > 0, !client_side);
// The server's health then starts the client's hurt animation.
if client_side {
w.player.health.flash_health_change(20.0);
assert_eq!(w.player.health.hurt_animation_time, 0);
w.player.health.flash_health_change(12.0);
assert_eq!(w.player.health.hurt_animation_time, 10);
}
}
}
#[test]
fn a_riptide_spin_lies_flat_and_ends_on_the_first_entity_it_touches() {
let root = Path::new(env!("CARGO_MANIFEST_DIR"))