From 9779eb7271e29c08424a6853db5a6bc598af7e8a Mon Sep 17 00:00:00 2001 From: lazy-importer Date: Sun, 18 Oct 2026 00:16:44 +0200 Subject: [PATCH] fix(game): allow always-edible food when full and apply consume effects (#750) --- crates/horizon-client/src/game.rs | 67 ++++++++++-- crates/horizon-client/src/game/potions.rs | 2 +- crates/horizon-client/src/items.rs | 124 ++++++++++++++++++++++ 3 files changed, 184 insertions(+), 9 deletions(-) diff --git a/crates/horizon-client/src/game.rs b/crates/horizon-client/src/game.rs index 929c720d8..7c5846e2c 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,54 @@ 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.potions.effects.add( + horizon_client::effects::EffectInstance::new( + def.id, + eff.duration, + eff.amplifier, + ), + ); + } + } + } + } + } + ConsumeEffect::ClearAllEffects => { + self.potions.effects.clear(); + } + 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); + } + } + 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 +2923,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/potions.rs b/crates/horizon-client/src/game/potions.rs index ffa63b7c9..be5b24650 100644 --- a/crates/horizon-client/src/game/potions.rs +++ b/crates/horizon-client/src/game/potions.rs @@ -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 { 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 {