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 929c720d8..26709516f 100644 --- a/crates/horizon-client/src/game.rs +++ b/crates/horizon-client/src/game.rs @@ -2764,7 +2764,7 @@ impl Game<'_> { || !self .player .as_ref() - .is_some_and(|p| p.health.food.food < 20 || self.creative) + .is_some_and(|p| info.can_always_eat || p.health.food.food < 20 || self.creative) { return false; } @@ -2773,6 +2773,56 @@ impl Game<'_> { } true } + fn apply_consume_effect( + &mut self, + effect: &horizon_client::items::ConsumeEffect, + eye: [f64; 3], + ) { + use horizon_client::items::ConsumeEffect; + match effect { + ConsumeEffect::ApplyEffects { + effects, + probability, + } => { + if *probability >= 1.0 || self.world.random.next_float() <= *probability { + for eff in effects { + if let Some(def) = horizon_client::brewing::effect(&eff.id) { + if def.instant { + if let Some(a) = + horizon_client::effects::instant(def.id, eff.amplifier, 1.0) + { + self.apply_effect_action(a); + } + } else { + self.add_player_effect( + horizon_client::effects::EffectInstance::new( + def.id, + eff.duration, + eff.amplifier, + ), + ); + } + } + } + } + } + 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) => { + self.audio.play(sound, Some(eye), 1.0, 1.0); + } + } + } fn consume_held(&mut self) { if self.creative { return; @@ -2875,19 +2925,22 @@ impl Game<'_> { self.eat_particles(&stack, 16); // Consumable.onConsume. self.post_entity_event("minecraft:eat", horizon_client::mobs::Who::Player); - if let Some((nutrition, saturation)) = info.food - && let Some(p) = self.player.as_mut() - { - // FoodData.eat(nutrition, saturation) - let food = &mut p.health.food; - food.food = (food.food + nutrition as i32).clamp(0, 20); - food.saturation = (food.saturation + saturation).clamp(0.0, food.food as f32); + if let Some((nutrition, saturation)) = info.food { + if let Some(p) = self.player.as_mut() { + // FoodData.eat(nutrition, saturation) + let food = &mut p.health.food; + food.food = (food.food + nutrition as i32).clamp(0, 20); + food.saturation = (food.saturation + saturation).clamp(0.0, food.food as f32); + } self.audio.play( "minecraft:entity.player.burp", Some(eye), 0.5, self.world.random.next_float() * 0.1 + 0.9, ); + for effect in &info.consume_effects { + self.apply_consume_effect(effect, eye); + } self.trigger_item("minecraft:consume_item", &stack); self.consume_held(); } 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 ffa63b7c9..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); @@ -249,7 +249,7 @@ impl Game<'_> { } true } - fn apply_effect_action(&mut self, action: EffectAction) { + pub(super) fn apply_effect_action(&mut self, action: EffectAction) { let creative = self.creative; let rules = self.world.health_rules; let Some(p) = self.player.as_mut() else { @@ -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 diff --git a/crates/horizon-client/src/items.rs b/crates/horizon-client/src/items.rs index 54a505ab9..2782d254f 100644 --- a/crates/horizon-client/src/items.rs +++ b/crates/horizon-client/src/items.rs @@ -39,6 +39,25 @@ pub struct ToolRule { pub speed: Option, pub correct: Option, } +#[derive(Clone, Debug, PartialEq)] +pub struct ConsumeEffectSpec { + pub id: String, + pub duration: i32, + pub amplifier: i32, +} + +#[derive(Clone, Debug, PartialEq)] +pub enum ConsumeEffect { + ApplyEffects { + effects: Vec, + probability: f32, + }, + ClearAllEffects, + RemoveEffects(Vec), + TeleportRandomly, + PlaySound(String), +} + #[derive(Clone, Debug)] pub struct ItemInfo { pub fire_resistant: bool, @@ -49,7 +68,9 @@ pub struct ItemInfo { pub tool: Vec, pub tool_default_speed: f32, pub food: Option<(u32, f32)>, + pub can_always_eat: bool, pub consumable: bool, + pub consume_effects: Vec, pub model: String, /// Main-hand `attack_damage` / `attack_speed` modifier amounts. pub attack_damage: f32, @@ -421,6 +442,79 @@ impl Items { f["saturation"].as_f64().unwrap_or(0.0) as f32, ) }); + let can_always_eat = c + .get("minecraft:food") + .and_then(|f| f.get("can_always_eat")) + .and_then(|v| v.as_bool()) + .unwrap_or(false); + let mut consume_effects = Vec::new(); + if let Some(effects) = c + .get("minecraft:consumable") + .and_then(|cons| cons.get("on_consume_effects")) + .and_then(|e| e.as_array()) + { + for effect in effects { + match effect.get("type").and_then(|t| t.as_str()) { + Some("minecraft:apply_effects") => { + let probability = effect + .get("probability") + .and_then(|p| p.as_f64()) + .map_or(1.0, |p| p as f32); + let mut specs = Vec::new(); + if let Some(list) = effect.get("effects").and_then(|l| l.as_array()) { + for e in list { + if let Some(id) = e.get("id").and_then(|i| i.as_str()) { + let duration = + e.get("duration").and_then(|d| d.as_i64()).unwrap_or(0) + as i32; + let amplifier = e + .get("amplifier") + .and_then(|a| a.as_i64()) + .unwrap_or(0) + as i32; + specs.push(ConsumeEffectSpec { + id: id.to_string(), + duration, + amplifier, + }); + } + } + } + consume_effects.push(ConsumeEffect::ApplyEffects { + effects: specs, + probability, + }); + } + Some("minecraft:clear_all_effects") => { + consume_effects.push(ConsumeEffect::ClearAllEffects); + } + Some("minecraft:remove_effects") => { + let mut targets = Vec::new(); + if let Some(eff) = effect.get("effects") { + if let Some(s) = eff.as_str() { + targets.push(s.to_string()); + } else if let Some(arr) = eff.as_array() { + for item in arr { + if let Some(s) = item.as_str() { + targets.push(s.to_string()); + } + } + } + } + consume_effects.push(ConsumeEffect::RemoveEffects(targets)); + } + Some("minecraft:teleport_randomly") => { + consume_effects.push(ConsumeEffect::TeleportRandomly); + } + Some("minecraft:play_sound") => { + if let Some(sound) = effect.get("sound").and_then(|s| s.as_str()) { + consume_effects.push(ConsumeEffect::PlaySound(sound.to_string())); + } + } + _ => {} + } + } + } items.insert( id.clone(), ItemInfo { @@ -435,7 +529,9 @@ impl Items { .as_f64() .unwrap_or(1.0) as f32, food, + can_always_eat, consumable: c.get("minecraft:consumable").is_some(), + consume_effects, model: c["minecraft:item_model"] .as_str() .unwrap_or(name) @@ -1496,6 +1592,34 @@ mod tests { .is_empty() ); } + + #[test] + fn always_edible_and_consume_effects_loaded() { + let root = std::path::PathBuf::from(env!("CARGO_MANIFEST_DIR")) + .join("../../.local/horizon-client/26.3"); + if !root.join("items.json").exists() { + return; + } + let assets = crate::assets::Assets::load(&root).unwrap(); + let facts = crate::blocks::BlockFacts::load(&root, assets.registry).unwrap(); + let items = Items::load(&root, assets.registry, &facts).unwrap(); + + let apple = items.get("minecraft:golden_apple").expect("golden apple"); + assert!(apple.can_always_eat); + assert!(apple.food.is_some()); + assert!(!apple.consume_effects.is_empty()); + + let enchanted = items + .get("minecraft:enchanted_golden_apple") + .expect("enchanted golden apple"); + assert!(enchanted.can_always_eat); + assert!(enchanted.food.is_some()); + assert!(!enchanted.consume_effects.is_empty()); + + let beef = items.get("minecraft:cooked_beef").expect("cooked beef"); + assert!(!beef.can_always_eat); + assert!(beef.food.is_some()); + } } pub fn take_held_drop(slot: &mut Option, all: bool) -> Option {