Merge pull request #216 from ludovic111/fix/berry-prick-sound-144

Play the player's hurt sound for every damage, by its damage effects (#144)
This commit is contained in:
Ludovic Marie
2026-10-15 00:56:39 +02:00
committed by GitHub
5 changed files with 63 additions and 7 deletions
+5
View File
@@ -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(
+4 -3
View File
@@ -2967,9 +2967,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),
+14
View File
@@ -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();
@@ -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);
}
+40 -1
View File
@@ -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}");
}
}
}