From f4539717fc6d91acc8ddc04a597447152bd77276 Mon Sep 17 00:00:00 2001 From: Ludovic Marie <136255825+ludovic111@users.noreply.github.com> Date: Fri, 9 Oct 2026 01:46:19 +0200 Subject: [PATCH] Add the totem of undying The combat kernel's death-protection hook fires for a totem in either hand unless the damage bypasses invulnerability; the game then uses up the totem (main hand first), sets health to 1, clears effects, applies regeneration II, absorption II and fire resistance, and plays the totem sound and 30-tick particle emitter. Co-Authored-By: Claude Opus 5.5 --- crates/horizon-client/src/game.rs | 4 ++ crates/horizon-client/src/game/totem.rs | 71 +++++++++++++++++++++++++ crates/horizon-client/src/health.rs | 33 ++++++++++-- crates/horizon-client/src/hunger.rs | 2 + crates/horizon-client/src/particles.rs | 15 +++++- 5 files changed, 120 insertions(+), 5 deletions(-) create mode 100644 crates/horizon-client/src/game/totem.rs diff --git a/crates/horizon-client/src/game.rs b/crates/horizon-client/src/game.rs index e0d512c1f..f19b1f751 100644 --- a/crates/horizon-client/src/game.rs +++ b/crates/horizon-client/src/game.rs @@ -31,6 +31,7 @@ mod station_control; mod stations; mod titles; mod tnt; +mod totem; mod trading; use horizon_client::{ Result, @@ -1923,6 +1924,8 @@ impl Game<'_> { if self.use_delay > 0 { self.use_delay -= 1; } + self.apply_totem(); + self.update_totem_ready(); if self.riding.is_some() && self.player.is_some() { let playing = self.screen == Screen::Playing; let held = |k| playing && self.keys.contains(&k); @@ -2043,6 +2046,7 @@ impl Game<'_> { _ => {} } } + self.apply_totem(); let Some(player) = self.player.as_mut() else { return Ok(()); }; diff --git a/crates/horizon-client/src/game/totem.rs b/crates/horizon-client/src/game/totem.rs new file mode 100644 index 000000000..c3e8fbf42 --- /dev/null +++ b/crates/horizon-client/src/game/totem.rs @@ -0,0 +1,71 @@ +//! The totem of undying (`LivingEntity.checkTotemDeathProtection` with the +//! totem's `death_protection` component, entity event 35 on the client). +use super::Game; +use horizon_client::{ + effects::EffectInstance, + particles::{ParticleOptions, ParticleType}, +}; + +const TOTEM: &str = "minecraft:totem_of_undying"; + +impl Game<'_> { + /// Whether either hand holds a totem, for the next hurt. + pub(super) fn update_totem_ready(&mut self) { + let ready = self.held().is_some_and(|s| &*s.item == TOTEM) + || self.offhand.as_ref().is_some_and(|s| &*s.item == TOTEM); + if let Some(p) = self.player.as_mut() { + p.health.totem_ready = ready; + } + } + /// A totem fired: use it up (main hand first), set health to 1, clear and + /// apply its effects, then the sound and particles. + pub(super) fn apply_totem(&mut self) { + let Some(p) = self.player.as_mut() else { + return; + }; + if !p.health.totem_used { + return; + } + p.health.totem_used = false; + let _ = p.set_health(1.0, false); + let at = [p.position.x, p.position.y, p.position.z]; + let height = f64::from(p.dimensions.height); + let slot = if self.held().is_some_and(|s| &*s.item == TOTEM) { + Some(self.selected) + } else { + None + }; + let cell = match slot { + Some(i) => &mut self.inventory[i], + None => &mut self.offhand, + }; + if let Some(s) = cell.as_mut() { + s.count -= 1; + if s.count == 0 { + *cell = None; + } + } + // DeathProtection.TOTEM_EFFECTS: clear, then regeneration II, absorption II, fire resistance. + self.potions.effects.clear(); + for (id, ticks, amp) in [ + ("regeneration", 900, 1), + ("absorption", 100, 1), + ("fire_resistance", 800, 0), + ] { + if let Some(e) = horizon_client::brewing::effect(id) { + self.potions + .effects + .add(EffectInstance::new(e.id, ticks, amp)); + } + } + self.audio + .play("minecraft:item.totem.use", Some(at), 1.0, 1.0); + self.particles.tracking_emitter_for( + ParticleOptions::simple(ParticleType::TotemOfUndying), + at, + 0.6, + height, + 30, + ); + } +} diff --git a/crates/horizon-client/src/health.rs b/crates/horizon-client/src/health.rs index ec926aa60..77151edde 100644 --- a/crates/horizon-client/src/health.rs +++ b/crates/horizon-client/src/health.rs @@ -136,6 +136,10 @@ pub struct PlayerHealth { pub sync_velocity: bool, pub game_time: i64, pub tick_count: i32, + /// A death-protection item (totem) is in either hand. + pub totem_ready: bool, + /// The totem saved the player this tick; the game applies its effects. + pub totem_used: bool, } impl PlayerHealth { pub fn new(resources: &HealthResources, attributes: &mut Attributes) -> Result { @@ -181,6 +185,8 @@ impl PlayerHealth { sync_velocity: false, game_time: 0, tick_count: 0, + totem_ready: false, + totem_used: false, }) } pub fn health(&self) -> f32 { @@ -233,6 +239,7 @@ impl PlayerMotion { .damage .source::<()>(source, None, None, None) .ok_or("unknown resident damage source")?; + let totem_ready = self.health.totem_ready; let h = &mut self.health; let mut environment = Environment { rules, @@ -245,6 +252,8 @@ impl PlayerMotion { blocking, blocked: 0.0, block_sound: false, + totem: totem_ready, + totem_fired: false, }; let hurt = ServerPlayerCombat::new( &mut self.attributes, @@ -254,11 +263,16 @@ impl PlayerMotion { &mut environment, )? .hurt(&mut h.hurt, amount)?; - Ok(BlockOutcome { + let outcome = BlockOutcome { hurt, blocked: environment.blocked, sound: environment.block_sound, - }) + }; + if environment.totem_fired { + self.health.totem_used = true; + self.health.totem_ready = false; + } + Ok(outcome) } pub fn set_health(&mut self, value: f32, absorption: bool) -> Result<()> { let h = &mut self.health; @@ -291,6 +305,9 @@ pub(crate) struct Environment<'a> { pub(crate) blocking: Option, pub(crate) blocked: f32, pub(crate) block_sound: bool, + /// LivingEntity.checkTotemDeathProtection: a totem is held / was used. + pub(crate) totem: bool, + pub(crate) totem_fired: bool, } /// `LivingEntity.applyItemBlocking` inputs for a raised `BlocksAttacks` item: /// reductions whose damage-type sets are already matched against the source. @@ -540,8 +557,16 @@ impl HurtWorld for Environment<'_> { Ok(()) } // no entity or position on these sources fn death_protection(&mut self, _: &mut State) -> io::Result { - Ok(false) - } // empty hands + if !self.totem + || self + .source + .immunity_tag(ImmunityTag::BypassesInvulnerability) + { + return Ok(false); + } + self.totem_fired = true; + Ok(true) + } fn death_sound(&mut self, _: &mut State) -> io::Result<()> { Ok(()) } diff --git a/crates/horizon-client/src/hunger.rs b/crates/horizon-client/src/hunger.rs index 0a46c3f34..cc46d9f28 100644 --- a/crates/horizon-client/src/hunger.rs +++ b/crates/horizon-client/src/hunger.rs @@ -138,6 +138,8 @@ impl FoodWorld for TickWorld<'_> { blocking: None, blocked: 0.0, block_sound: false, + totem: false, + totem_fired: false, }; ServerPlayerCombat::new(self.attributes, self.data, food, self.ids, &mut environment)? .hurt(self.hurt, amount)?; diff --git a/crates/horizon-client/src/particles.rs b/crates/horizon-client/src/particles.rs index d44df31be..8f0425ada 100644 --- a/crates/horizon-client/src/particles.rs +++ b/crates/horizon-client/src/particles.rs @@ -729,9 +729,22 @@ impl ParticleEngine { feet: [f64; 3], width: f64, height: f64, + ) { + self.tracking_emitter_for(options, feet, width, height, 3); + } + /// `createTrackingEmitter(entity, type, lifetime)`. + pub fn tracking_emitter_for( + &mut self, + options: ParticleOptions, + feet: [f64; 3], + width: f64, + height: f64, + life: u32, ) { self.emit(&options, feet, width, height); - self.emitters.push((options, feet, width, height, 2)); + if life > 1 { + self.emitters.push((options, feet, width, height, life - 1)); + } } fn emit(&mut self, options: &ParticleOptions, feet: [f64; 3], width: f64, height: f64) { for _ in 0..16 {