From bf7b3d140f8da83540a4f555a80e90b5efce44a9 Mon Sep 17 00:00:00 2001 From: Ludovic Marie <136255825+ludovic111@users.noreply.github.com> Date: Thu, 15 Oct 2026 00:22:08 +0200 Subject: [PATCH] Play the player's hurt sound for every damage, by its damage effects (#144) Vanilla plays the hurt sound on the client from broadcastDamageEvent (LivingEntity.handleDamageEvent -> Player.getHurtSound, the damage type's DamageEffects sound, pitch (r - r) * 0.2 + 1). Horizon only played entity.player.hurt by hand for falls and mob hits, so sweet berry bushes (poking -> hurt_sweet_berry_bush), fire and lava (burning -> hurt_on_fire), drowning, freezing, cactus and the rest were silent. The combat port's broadcast_damage now carries its damage source (HealthEvent::DamageBroadcast, same trace line as before) and the game plays hurt_sound(effects) for it; the two hand-written plays are gone. Fixes #144 Co-Authored-By: Claude Opus 5.5 --- crates/horizon-client/src/audio.rs | 5 +++ crates/horizon-client/src/game.rs | 7 ++-- crates/horizon-client/src/game/combat.rs | 14 +++++++ crates/horizon-client/src/game/creatures.rs | 3 -- crates/horizon-client/src/health.rs | 41 ++++++++++++++++++++- 5 files changed, 63 insertions(+), 7 deletions(-) diff --git a/crates/horizon-client/src/audio.rs b/crates/horizon-client/src/audio.rs index 6705235ae..54bd3bda8 100644 --- a/crates/horizon-client/src/audio.rs +++ b/crates/horizon-client/src/audio.rs @@ -617,6 +617,11 @@ impl Audio { let seed = self.random.next_long(); self.play_seeded(event, at, volume, pitch, seed); } + /// `LivingEntity.handleDamageEvent`'s pitch, `(r - r) * 0.2 + 1`, drawn on the + /// client (the entity's own random there, never the server's world random). + pub fn hurt_pitch(&mut self) -> f32 { + (self.random.next_float() - self.random.next_float()) * 0.2 + 1.0 + } /// `AbstractWidget.playButtonClickSound`: an unseeded, relative UI sound. pub fn play_button_click(&mut self) { self.play_instance( diff --git a/crates/horizon-client/src/game.rs b/crates/horizon-client/src/game.rs index 0d9c394e4..5da5b7654 100644 --- a/crates/horizon-client/src/game.rs +++ b/crates/horizon-client/src/game.rs @@ -2942,9 +2942,10 @@ impl Game<'_> { b.notify_written(level, ticks, position, level_flags::UPDATE_ALL) }); } - MotionEvent::Fall(horizon_client::landing::FallEvent::Damage { .. }) => { - self.audio - .play("minecraft:entity.player.hurt", Some(feet), 1.0, 1.0); + MotionEvent::Health(horizon_client::health::HealthEvent::DamageBroadcast { + source, + }) => { + self.player_hurt_sound(source, feet); } MotionEvent::Snowflake { position, velocity } => self.snowflake(position, velocity), MotionEvent::EnterBlock { state } => self.trigger_enter_block(state), diff --git a/crates/horizon-client/src/game/combat.rs b/crates/horizon-client/src/game/combat.rs index 73c6b7fc0..14a1133e0 100644 --- a/crates/horizon-client/src/game/combat.rs +++ b/crates/horizon-client/src/game/combat.rs @@ -54,6 +54,20 @@ impl Game<'_> { .push((damage_type.to_owned(), (mob, name))); } } + /// `LivingEntity.handleDamageEvent` for the player: the hurt sound of the + /// source's `DamageEffects` (`Player.getHurtSound`). + pub(super) fn player_hurt_sound( + &mut self, + source: horizon_world::damage_source::DamageTypeId, + at: [f64; 3], + ) { + let Some(d) = self.motion.health.damage.definition(source) else { + return; + }; + let sound = horizon_client::health::hurt_sound(&d.effects); + let pitch = self.audio.hurt_pitch(); + self.audio.play(sound, Some(at), 1.0, pitch); + } /// `CombatTracker.recordDamage` for the motion events of this tick. pub(super) fn record_combat(&mut self, events: &[MotionEvent]) { self.recheck_combat(); diff --git a/crates/horizon-client/src/game/creatures.rs b/crates/horizon-client/src/game/creatures.rs index 8c749b704..ee7f2c111 100644 --- a/crates/horizon-client/src/game/creatures.rs +++ b/crates/horizon-client/src/game/creatures.rs @@ -1352,9 +1352,6 @@ impl Game<'_> { (player.velocity.y / 2.0 + 0.4).min(0.4), player.velocity.z / 2.0 + dz / l * 0.4, ); - let at = [player.position.x, player.position.y, player.position.z]; - self.audio - .play("minecraft:entity.player.hurt", Some(at), 1.0, 1.0); self.hurt_armor(amount); self.thorns(from); } diff --git a/crates/horizon-client/src/health.rs b/crates/horizon-client/src/health.rs index 46b4f4542..29cba4610 100644 --- a/crates/horizon-client/src/health.rs +++ b/crates/horizon-client/src/health.rs @@ -76,6 +76,18 @@ impl HealthResources { .expect("validated landing source") } } +/// `DamageEffects.sound()` by its serialized id: what `Player.getHurtSound` +/// plays for a damage type's `effects` (unknown ids fall back to `hurt`, the codec default). +pub fn hurt_sound(effects: &str) -> &'static str { + match effects { + "drowning" => "minecraft:entity.player.hurt_drown", + "burning" => "minecraft:entity.player.hurt_on_fire", + "poking" => "minecraft:entity.player.hurt_sweet_berry_bush", + "freezing" => "minecraft:entity.player.hurt_freeze", + // HURT and THORNS (both SoundEvents.PLAYER_HURT). + _ => "minecraft:entity.player.hurt", + } +} #[derive(Clone, Copy, Debug)] pub enum HealthEvent { StarvationDamage(f32), @@ -97,6 +109,11 @@ pub enum HealthEvent { amount: f32, }, Phase(&'static str), + /// `ServerLevel.broadcastDamageEvent`: the client's + /// `LivingEntity.handleDamageEvent` plays the source's hurt sound. + DamageBroadcast { + source: DamageTypeId, + }, Criterion { original: f32, amount: f32, @@ -119,6 +136,7 @@ impl HealthEvent { format!("record-damage {:08x}", amount.to_bits()) } Self::Phase(name) => name.into(), + Self::DamageBroadcast { .. } => "broadcast-damage".into(), Self::Criterion { .. } => "hurt-criterion".into(), } } @@ -559,7 +577,10 @@ impl HurtWorld for Environment<'_> { Ok(()) } fn broadcast_damage(&mut self, _: &mut State) -> io::Result<()> { - self.phase("broadcast-damage") + self.event(HealthEvent::DamageBroadcast { + source: self.source.type_id(), + }); + Ok(()) } fn mark_hurt(&mut self, _: &mut State) -> io::Result<()> { *self.sync_velocity = true; @@ -655,3 +676,21 @@ impl ServerPlayerHurtWorld for Environment<'_> { Ok(true) } } + +#[cfg(test)] +mod tests { + #[test] + fn hurt_sounds_follow_damage_effects() { + // DamageEffects: HURT, THORNS, DROWNING, BURNING, POKING, FREEZING. + for (effects, sound) in [ + ("hurt", "minecraft:entity.player.hurt"), + ("thorns", "minecraft:entity.player.hurt"), + ("drowning", "minecraft:entity.player.hurt_drown"), + ("burning", "minecraft:entity.player.hurt_on_fire"), + ("poking", "minecraft:entity.player.hurt_sweet_berry_bush"), + ("freezing", "minecraft:entity.player.hurt_freeze"), + ] { + assert_eq!(super::hurt_sound(effects), sound, "{effects}"); + } + } +}