diff --git a/crates/horizon-client/src/effects.rs b/crates/horizon-client/src/effects.rs index 2e600333a..a258ccf3a 100644 --- a/crates/horizon-client/src/effects.rs +++ b/crates/horizon-client/src/effects.rs @@ -386,6 +386,12 @@ impl PlayerEffects { 4.0, "add_value", )), + "absorption" => Some(( + "minecraft:max_absorption", + "minecraft:effect.absorption", + 4.0, + "add_value", + )), "luck" => Some(("minecraft:luck", "minecraft:effect.luck", 1.0, "add_value")), "unluck" => Some(( "minecraft:luck", @@ -538,4 +544,29 @@ mod tests { assert!((fx.mining_speed_factor() - 1.4).abs() < 1e-6); assert_eq!(fx.modifiers().len(), 1); } + + #[test] + fn absorption_modifier_scales_with_amplifier() { + let mut fx = PlayerEffects::default(); + fx.add(EffectInstance::new("absorption", 2400, 0)); + assert_eq!( + fx.modifiers(), + vec![( + "minecraft:max_absorption", + "minecraft:effect.absorption", + 4.0, + "add_value", + )] + ); + fx.add(EffectInstance::new("absorption", 2400, 3)); + assert_eq!( + fx.modifiers(), + vec![( + "minecraft:max_absorption", + "minecraft:effect.absorption", + 16.0, + "add_value", + )] + ); + } } diff --git a/crates/horizon-client/src/game.rs b/crates/horizon-client/src/game.rs index 7c5846e2c..26709516f 100644 --- a/crates/horizon-client/src/game.rs +++ b/crates/horizon-client/src/game.rs @@ -2794,7 +2794,7 @@ impl Game<'_> { self.apply_effect_action(a); } } else { - self.potions.effects.add( + self.add_player_effect( horizon_client::effects::EffectInstance::new( def.id, eff.duration, @@ -2808,12 +2808,14 @@ impl Game<'_> { } ConsumeEffect::ClearAllEffects => { self.potions.effects.clear(); + self.apply_effect_status(); } ConsumeEffect::RemoveEffects(to_remove) => { for target in to_remove { let id = target.trim_start_matches("minecraft:"); self.potions.effects.list.retain(|e| e.id != id); } + self.apply_effect_status(); } ConsumeEffect::TeleportRandomly => {} ConsumeEffect::PlaySound(sound) => { diff --git a/crates/horizon-client/src/game/commands/effects.rs b/crates/horizon-client/src/game/commands/effects.rs index 3e04f7179..86726fed9 100644 --- a/crates/horizon-client/src/game/commands/effects.rs +++ b/crates/horizon-client/src/game/commands/effects.rs @@ -86,7 +86,7 @@ impl Game<'_> { Target::Player => { let mut instance = EffectInstance::new(effect.id, duration, amplifier); instance.visible = particles; - self.potions.effects.add(instance) + self.add_player_effect(instance) } Target::Mob(id) => match self.mobs.iter_mut().find(|m| m.id == id) { Some(m) => { @@ -134,11 +134,13 @@ impl Game<'_> { (Target::Player, None) => { let any = !self.potions.effects.list.is_empty(); self.potions.effects.clear(); + self.apply_effect_status(); any } (Target::Player, Some(e)) => { let before = self.potions.effects.list.len(); self.potions.effects.list.retain(|x| x.id != e.id); + self.apply_effect_status(); self.potions.effects.list.len() != before } (Target::Mob(id), e) => match self.mobs.iter_mut().find(|m| m.id == id) { diff --git a/crates/horizon-client/src/game/potions.rs b/crates/horizon-client/src/game/potions.rs index be5b24650..bc4f3b6bc 100644 --- a/crates/horizon-client/src/game/potions.rs +++ b/crates/horizon-client/src/game/potions.rs @@ -228,7 +228,7 @@ impl Game<'_> { } let (lasting, now) = PlayerEffects::potion_effects(stack.potion(), 1.0); for e in lasting { - self.potions.effects.add(e); + self.add_player_effect(e); } for a in now { self.apply_effect_action(a); @@ -329,9 +329,24 @@ impl Game<'_> { } self.apply_effect_status(); } + /// Add an active effect to the player and update attributes / absorption. + pub(super) fn add_player_effect(&mut self, effect: EffectInstance) -> bool { + let id = effect.id; + let amp = effect.amplifier; + let changed = self.potions.effects.add(effect); + self.apply_effect_status(); + if id == "absorption" + && let Some(p) = self.player.as_mut() + { + let target = 4.0 * (amp + 1) as f32; + let current = p.health.absorption(); + let _ = p.set_health(current.max(target), true); + } + changed + } /// The effects' attribute modifiers, armor protection and status flags /// on `self.player`. - fn apply_effect_status(&mut self) { + pub(super) fn apply_effect_status(&mut self) { let modifiers = self.potions.effects.modifiers(); if modifiers != self.potions.applied && let Some(p) = self.player.as_mut() @@ -344,6 +359,12 @@ impl Game<'_> { } self.potions.applied = modifiers; } + if let Some(p) = self.player.as_mut() + && self.potions.effects.amplifier("absorption").is_none() + && p.health.absorption() > 0.0 + { + let _ = p.set_health(0.0, true); + } // EnchantmentHelper.getDamageProtection per damage type, recomputed on armor change. let armor: Vec> = self.armor.to_vec(); if self.potions.protection_for.as_ref() != Some(&armor) { @@ -495,7 +516,7 @@ impl Game<'_> { if d > 20 && let Some(e) = brewing::effect(id) { - self.potions.effects.add(EffectInstance::new(e.id, d, *amp)); + self.add_player_effect(EffectInstance::new(e.id, d, *amp)); } } } diff --git a/crates/horizon-client/src/game/totem.rs b/crates/horizon-client/src/game/totem.rs index 805360c8d..47b67d9f8 100644 --- a/crates/horizon-client/src/game/totem.rs +++ b/crates/horizon-client/src/game/totem.rs @@ -53,9 +53,7 @@ impl Game<'_> { ("fire_resistance", 800, 0), ] { if let Some(e) = horizon_client::brewing::effect(id) { - self.potions - .effects - .add(EffectInstance::new(e.id, ticks, amp)); + self.add_player_effect(EffectInstance::new(e.id, ticks, amp)); } } self.audio