From a68ee8009f43ef692ecaac095218a1650db60585 Mon Sep 17 00:00:00 2001 From: Kacpherek Date: Mon, 26 Oct 2026 23:33:46 +0100 Subject: [PATCH 1/2] Save and load a mob's active effects, keep a player's hidden particles A mob's effects were never written to its NBT and never read back, so they were gone after a reload. LivingEntity.addAdditionalSaveData and readAdditionalSaveData store them as active_effects. A player's effect saved show_particles as always on and read it back from show_icon, so an effect given with hideParticles came back with particles. --- crates/horizon-client/src/brewing.rs | 25 +++++++++++++- crates/horizon-client/src/effects.rs | 5 +-- crates/horizon-client/src/entity_nbt.rs | 34 +++++++++++++++++++ .../src/game/commands/effects.rs | 21 +----------- crates/horizon-client/src/save.rs | 10 +++--- 5 files changed, 67 insertions(+), 28 deletions(-) diff --git a/crates/horizon-client/src/brewing.rs b/crates/horizon-client/src/brewing.rs index f29bfc08b..cdd988a7a 100644 --- a/crates/horizon-client/src/brewing.rs +++ b/crates/horizon-client/src/brewing.rs @@ -7,7 +7,12 @@ use crate::{ items::{ItemStack, Items}, }; use serde_json::Value; -use std::{collections::HashSet, fs, path::Path, sync::Arc}; +use std::{ + collections::{HashMap, HashSet}, + fs, + path::Path, + sync::{Arc, OnceLock}, +}; #[derive(Clone, Copy, Debug, PartialEq, Eq)] pub enum Category { @@ -94,6 +99,24 @@ pub fn effect(id: &str) -> Option<&'static EffectDef> { let id = id.trim_start_matches("minecraft:"); EFFECTS.iter().find(|e| e.id == id) } +/// Namespaced effect ids for mob effect storage. +pub fn namespaced(id: &'static str) -> &'static str { + static IDS: OnceLock> = OnceLock::new(); + IDS.get_or_init(|| { + EFFECTS + .iter() + .map(|e| { + ( + e.id, + &*Box::leak(format!("minecraft:{}", e.id).into_boxed_str()), + ) + }) + .collect() + }) + .get(id) + .copied() + .unwrap_or(id) +} /// A `Potion`: id, name suffix (`item.minecraft.potion.effect.`) and /// effects as (effect, duration ticks, amplifier). #[derive(Clone, Copy, Debug)] diff --git a/crates/horizon-client/src/effects.rs b/crates/horizon-client/src/effects.rs index 17dce93ab..16a7a273a 100644 --- a/crates/horizon-client/src/effects.rs +++ b/crates/horizon-client/src/effects.rs @@ -179,13 +179,14 @@ impl PlayerEffects { self.list.clear(); } /// The effects of a saved player (`Player.active_effects`: id, - /// duration, amplifier, ambient, visible). + /// duration, amplifier, ambient, particles visible). pub fn from_saved(saved: &[(String, i32, i32, bool, bool)]) -> Self { let mut effects = Self::default(); - for (id, duration, amplifier, ambient, _) in saved { + for (id, duration, amplifier, ambient, particles) in saved { if let Some(def) = brewing::effect(id) { let mut e = EffectInstance::new(def.id, *duration, *amplifier); e.ambient = *ambient; + e.visible = *particles; effects.add(e); } } diff --git a/crates/horizon-client/src/entity_nbt.rs b/crates/horizon-client/src/entity_nbt.rs index 161ca12e9..22824805a 100644 --- a/crates/horizon-client/src/entity_nbt.rs +++ b/crates/horizon-client/src/entity_nbt.rs @@ -188,6 +188,26 @@ pub fn mob_to_nbt(m: &Mob, uuid: [i32; 4], raw: Option<&Tag>) -> Tag { map.remove(&horizon_world::nbt::JavaString::from(key)); } } + // LivingEntity.addAdditionalSaveData: `active_effects` only when there are + // any (MobEffectInstance.CODEC; ambient and show_particles are at their + // defaults for a mob's effects). + if m.extra.effects.is_empty() { + map.remove(&horizon_world::nbt::JavaString::from("active_effects")); + } else { + let effects = m + .extra + .effects + .iter() + .map(|&(effect, duration, amplifier)| { + Tag::compound([ + ("id", string(crate::brewing::namespaced(effect))), + ("amplifier", Tag::Byte(amplifier.clamp(0, 255) as u8 as i8)), + ("duration", Tag::Int(duration)), + ("show_icon", Tag::Byte(1)), + ]) + }); + map.insert("active_effects".into(), Tag::List(effects.collect())); + } if m.ticks_frozen > 0 { map.insert("TicksFrozen".into(), Tag::Int(m.ticks_frozen)); } else { @@ -712,6 +732,20 @@ pub fn mob_from_nbt(tag: &Tag, types: &HashMap>, id: u32) - m.no_gravity = flag(tag, "NoGravity"); m.glowing = flag(tag, "Glowing"); m.ticks_frozen = number(tag.get("TicksFrozen")).unwrap_or(0) as i32; + // LivingEntity.readAdditionalSaveData: the saved `active_effects`, the last + // entry of an effect winning. + if let Some(Tag::List(effects)) = tag.get("active_effects") { + for e in effects { + let Some(def) = text(e.get("id")).and_then(|id| crate::brewing::effect(&id)) else { + continue; + }; + let effect = crate::brewing::namespaced(def.id); + let duration = number(e.get("duration")).unwrap_or(0) as i32; + let amplifier = number(e.get("amplifier")).unwrap_or(0) as i32; + m.extra.effects.retain(|x| x.0 != effect); + m.extra.effects.push((effect, duration, amplifier)); + } + } m.silent = flag(tag, "Silent"); m.invulnerable_flag = flag(tag, "Invulnerable"); if m.species() == crate::mobs::Species::Bat { diff --git a/crates/horizon-client/src/game/commands/effects.rs b/crates/horizon-client/src/game/commands/effects.rs index 65d6592f7..85ccc398f 100644 --- a/crates/horizon-client/src/game/commands/effects.rs +++ b/crates/horizon-client/src/game/commands/effects.rs @@ -1,11 +1,10 @@ //! `/effect` (EffectCommands) and `/enchant` (EnchantCommand). use super::{Arg, Context, Game, R, Target, err, texts}; use horizon_client::{ - brewing, + brewing::{self, namespaced}, effects::EffectInstance, text::{self, Style, Text}, }; -use std::{collections::HashMap, sync::OnceLock}; /// `CommandResponseTracker` over targets: elements with a non-zero result. #[derive(Default)] @@ -21,24 +20,6 @@ impl Tracker { } } } -/// Namespaced effect ids for mob effect storage. -fn namespaced(id: &'static str) -> &'static str { - static IDS: OnceLock> = OnceLock::new(); - IDS.get_or_init(|| { - brewing::EFFECTS - .iter() - .map(|e| { - ( - e.id, - &*Box::leak(format!("minecraft:{}", e.id).into_boxed_str()), - ) - }) - .collect() - }) - .get(id) - .copied() - .unwrap_or(id) -} fn effect_name(id: &str) -> Text { Text::translate(&format!("effect.minecraft.{id}"), &[]) } diff --git a/crates/horizon-client/src/save.rs b/crates/horizon-client/src/save.rs index 5431adcd6..f965cf406 100644 --- a/crates/horizon-client/src/save.rs +++ b/crates/horizon-client/src/save.rs @@ -2763,7 +2763,7 @@ pub struct Level { pub xp_seed: i32, /// ServerPlayer.seenCredits: the end poem has been shown. pub seen_credits: bool, - /// Player.active_effects: (id, duration, amplifier, ambient, show_icon). + /// Player.active_effects: (id, duration, amplifier, ambient, show_particles). pub active_effects: Vec<(String, i32, i32, bool, bool)>, /// Player.EnderItems: the ender chest inventory (27 slots). pub ender_items: Vec>, @@ -3043,7 +3043,7 @@ impl Level { get("duration").unwrap_or(0) as i32, get("amplifier").unwrap_or(0) as i32, get("ambient").unwrap_or(0) != 0, - get("show_icon").is_none_or(|v| v != 0), + get("show_particles").is_none_or(|v| v != 0), )) }) .collect(), @@ -3145,14 +3145,14 @@ impl Level { Tag::List( self.active_effects .iter() - .map(|(id, duration, amplifier, ambient, icon)| { + .map(|(id, duration, amplifier, ambient, particles)| { Tag::compound([ ("id", string(id)), ("duration", Tag::Int(*duration)), ("amplifier", Tag::Byte(*amplifier as i8)), ("ambient", Tag::Byte(i8::from(*ambient))), - ("show_particles", Tag::Byte(1)), - ("show_icon", Tag::Byte(i8::from(*icon))), + ("show_particles", Tag::Byte(i8::from(*particles))), + ("show_icon", Tag::Byte(1)), ]) }) .collect(), From 6c09ec2cc89febd655ff7eb414e8c1faf8c93df8 Mon Sep 17 00:00:00 2001 From: Ludovic Marie <136255825+ludovic111@users.noreply.github.com> Date: Tue, 27 Oct 2026 06:30:27 +0100 Subject: [PATCH 2/2] Save a player effect's icon as shown only when its particles are MobEffectInstance's show_icon defaults to show_particles: an effect given with hidden particles hides its icon too, and keeps doing so after a reload. Co-Authored-By: Claude Opus 5.5 --- crates/horizon-client/src/save.rs | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/crates/horizon-client/src/save.rs b/crates/horizon-client/src/save.rs index f965cf406..f3149c578 100644 --- a/crates/horizon-client/src/save.rs +++ b/crates/horizon-client/src/save.rs @@ -3152,7 +3152,7 @@ impl Level { ("amplifier", Tag::Byte(*amplifier as i8)), ("ambient", Tag::Byte(i8::from(*ambient))), ("show_particles", Tag::Byte(i8::from(*particles))), - ("show_icon", Tag::Byte(1)), + ("show_icon", Tag::Byte(i8::from(*particles))), ]) }) .collect(),