//! Living entity status storage and add/replace/remove lifecycle ordering. use crate::{ java_hash_map::{JavaHashMap, JavaKey}, status_effect::EffectInstance, }; pub struct ActiveEffects { /// LivingEntity schedules effect synchronization from construction. pub dirty: bool, /// Native activeEffects is exposed by getActiveEffectsMap. Direct mutations /// bypass lifecycle notifications, but retain the map's iterator semantics. pub entries: JavaHashMap>, } impl Default for ActiveEffects { fn default() -> Self { Self { entries: JavaHashMap::default(), dirty: true, } } } pub trait EffectHooks { type Error; type Source: Clone; type Level: Copy; fn can_be_affected( &mut self, storage: &mut ActiveEffects, incoming: EffectInstance, ) -> Result; fn on_added( &mut self, storage: &mut ActiveEffects, incoming: EffectInstance, source: Option, ) -> Result<(), Self::Error>; fn on_updated( &mut self, storage: &mut ActiveEffects, existing: EffectInstance, reapply: bool, source: Option, ) -> Result<(), Self::Error>; /// Concrete MobEffect.onEffectAdded dispatch, after the actor callback. fn effect_added( &mut self, storage: &mut ActiveEffects, effect: H, amplifier: i32, ) -> Result<(), Self::Error>; fn effect_started( &mut self, storage: &mut ActiveEffects, effect: H, amplifier: i32, ) -> Result<(), Self::Error>; fn level(&mut self, storage: &mut ActiveEffects) -> Result; fn is_client( &mut self, storage: &mut ActiveEffects, level: Self::Level, ) -> Result; fn on_removed( &mut self, storage: &mut ActiveEffects, effects: Vec>, ) -> Result<(), Self::Error>; fn get_effect( &mut self, storage: &mut ActiveEffects, effect: H, ) -> Result>, Self::Error> { Ok(storage.get(&effect)) } /// Entity.isRemoved is a final field-based query. fn actor_removed(&self) -> bool; } impl ActiveEffects { pub fn get(&self, effect: &H) -> Option> { self.entries.get(effect).cloned() } pub fn has(&self, effect: &H) -> bool { self.entries.get(effect).is_some() } pub fn add>( &mut self, incoming: EffectInstance, source: Option, world: &mut W, ) -> Result { if !world.can_be_affected(self, incoming.clone())? { return Ok(false); } let key = incoming.read().effect.clone(); let existing = self.get(&key); let mut changed = false; if let Some(existing) = existing { if existing.update(&incoming) { world.on_updated(self, existing, true, source)?; changed = true; } } else { self.entries.put(key, incoming.clone()); world.on_added(self, incoming.clone(), source)?; changed = true; let data = incoming.read(); let effect = data.effect.clone(); let amplifier = data.details.amplifier; drop(data); world.effect_added(self, effect, amplifier)?; } let data = incoming.read(); let effect = data.effect.clone(); let amplifier = data.details.amplifier; drop(data); world.effect_started(self, effect, amplifier)?; Ok(changed) } pub fn force_add>( &mut self, incoming: EffectInstance, source: Option, world: &mut W, ) -> Result<(), W::Error> { if !world.can_be_affected(self, incoming.clone())? { return Ok(()); } let key = incoming.read().effect.clone(); let previous = self.entries.put(key, incoming.clone()); if let Some(previous) = previous { incoming.copy_blend_from(&previous); world.on_updated(self, incoming, true, source)?; } else { world.on_added(self, incoming, source)?; } Ok(()) } pub fn remove_no_update(&mut self, effect: &H) -> Option> { self.entries.remove(effect) } pub fn remove>( &mut self, effect: &H, world: &mut W, ) -> Result { if let Some(instance) = self.remove_no_update(effect) { world.on_removed(self, vec![instance])?; Ok(true) } else { Ok(false) } } pub fn remove_all>(&mut self, world: &mut W) -> Result { let level = world.level(self)?; if world.is_client(self, level)? || self.entries.is_empty() { return Ok(false); } let previous = self.entries.copied(); self.entries.clear(); let values = previous.entries().map(|(_, v)| v.clone()).collect(); world.on_removed(self, values)?; Ok(true) } pub fn blend_factor>( &mut self, effect: H, partial: f32, world: &mut W, ) -> Result { let effect = world.get_effect(self, effect)?; Ok(effect.map_or(0.0, |e| e.blend_factor(partial, world.actor_removed()))) } }