diff --git a/crates/horizon-client/src/health.rs b/crates/horizon-client/src/health.rs index a9a4864ce..db0677d5f 100644 --- a/crates/horizon-client/src/health.rs +++ b/crates/horizon-client/src/health.rs @@ -583,6 +583,10 @@ impl HurtWorld for Environment<'_> { self.event(HealthEvent::DamageBroadcast { source: self.source.type_id(), }); + // The player hears its own hurt sound from handleDamageEvent; a + // blocked hit sends the block sound (onBlocked) instead. + let sound = hurt_sound(&self.source.definition().effects); + self.events.push(MotionEvent::HurtSound(sound)); Ok(()) } fn mark_hurt(&mut self, _: &mut State) -> io::Result<()> { @@ -619,11 +623,9 @@ impl HurtWorld for Environment<'_> { *self.dead = true; self.phase("death-request") } + /// `Player.playSound` leaves the player itself out: others hear this one. fn hurt_sound(&mut self, _: &mut State) -> io::Result<()> { - self.phase("hurt-sound")?; - let sound = hurt_sound(&self.source.definition().effects); - self.events.push(MotionEvent::HurtSound(sound)); - Ok(()) + self.phase("hurt-sound") } fn game_time(&mut self, _: &mut State) -> io::Result { Ok(self.game_time) diff --git a/crates/horizon-client/src/motion.rs b/crates/horizon-client/src/motion.rs index 602590e15..b8697eeb2 100644 --- a/crates/horizon-client/src/motion.rs +++ b/crates/horizon-client/src/motion.rs @@ -113,6 +113,7 @@ pub enum MotionEvent { enabled: bool, }, Health(crate::health::HealthEvent), + /// `LivingEntity.handleDamageEvent`: the hurt sound the player hears. HurtSound(&'static str), Fall(crate::landing::FallEvent), Recorded { @@ -2109,6 +2110,47 @@ mod tests { .count(), 1 ); + // A shield that takes the whole arrow: onBlocked's sound, no damage + // event, so the player hears no hurt sound. + use horizon_world::blocking_rules::{BlockingRules, DamageReduction, ItemDamageFunction}; + let mut player = PlayerMotion::new(&resources, Vec3::new(20.5, 4.0, 20.5)).unwrap(); + let source = resources.health.damage.id("minecraft:arrow").unwrap(); + let shield = crate::health::Blocking { + rules: BlockingRules { + delay_seconds: 0.25, + disable_scale: 1.0, + reductions: vec![DamageReduction { + horizontal_angle: 90.0, + types: None, + base: 0.0, + factor: 1.0, + }], + item_damage: ItemDamageFunction { + threshold: 3.0, + base: 1.0, + factor: 1.0, + }, + }, + bypassed: false, + pierce: false, + angle: 0.0, + }; + let outcome = player + .hurt_blocking( + &resources.health, + Default::default(), + source, + 4.0, + Some(shield), + ) + .unwrap(); + assert!(!outcome.hurt && outcome.sound && outcome.blocked == 4.0); + assert!( + !player + .events + .iter() + .any(|e| matches!(e, MotionEvent::HurtSound(_))) + ); } /// Footsteps on the prepared vanilla resources, when they are present: