Skip to main content

steel_core/entity/mob_effect/
mod.rs

1//! Mob-effect behaviors: one small module per vanilla `MobEffect` subtype
2//! under `net/minecraft/world/effect`.
3
4mod absorption;
5mod bad_omen;
6mod heal_or_harm;
7mod hunger;
8mod infested;
9mod oozing;
10mod poison;
11mod raid_omen;
12mod regeneration;
13mod saturation;
14mod weaving;
15mod wind_charged;
16mod wither;
17
18#[cfg(test)]
19pub(crate) use absorption::ABSORPTION_PER_LEVEL;
20pub use absorption::AbsorptionBehavior;
21pub use bad_omen::BadOmenBehavior;
22pub use heal_or_harm::HealOrHarmBehavior;
23pub use hunger::HungerBehavior;
24pub use infested::InfestedBehavior;
25pub use oozing::OozingBehavior;
26pub use poison::PoisonBehavior;
27pub use raid_omen::RaidOmenBehavior;
28pub use regeneration::RegenerationBehavior;
29pub use saturation::SaturationBehavior;
30pub use weaving::WeavingBehavior;
31pub use wind_charged::WindChargedBehavior;
32pub use wither::WitherBehavior;
33
34use crate::entity::LivingEntity;
35use crate::entity::SharedEntity;
36use crate::world::World;
37
38/// One vanilla `MobEffect` subtype's runtime behavior. Default methods match
39/// vanilla's own `MobEffect` base-class defaults, so an effect with no
40/// registered behavior (most of them) behaves exactly like a bare vanilla
41/// `MobEffect` instance.
42pub trait MobEffectBehavior: Send + Sync {
43    /// Returns the instantaneous-only half of this behavior, if any.
44    fn as_instantaneous(&self) -> Option<&dyn InstantaneousMobEffect> {
45        None
46    }
47
48    /// Determines whether to apply the effect tick this tick.
49    fn should_apply_effect_tick_this_tick(&self, tick_count: i32, _amplifier: i32) -> bool {
50        self.as_instantaneous().is_some() && tick_count >= 1
51    }
52
53    /// Applies one tick of the effect. Returns whether the effect remains
54    /// active.
55    fn apply_effect_tick(&self, _world: &World, _user: &dyn LivingEntity, _amplifier: i32) -> bool {
56        true
57    }
58
59    /// Called when the effect starts.
60    fn on_effect_started(&self, _user: &dyn LivingEntity, _amplifier: i32) {}
61}
62
63/// The instantaneous-only half of a [`MobEffectBehavior`] that also extends
64/// vanilla `InstantaneousMobEffect`.
65pub trait InstantaneousMobEffect: MobEffectBehavior {
66    /// Applies the instantaneous effect immediately, e.g. from drinking a
67    /// potion or being hit by a splash/lingering one. `direct_entity` and
68    /// `causing_entity` attribute any resulting damage, and `scale` applies
69    /// splash/lingering falloff; the default ignores those extras and just
70    /// delegates to [`apply_effect_tick`](MobEffectBehavior::apply_effect_tick).
71    fn apply_instantaneous(
72        &self,
73        world: &World,
74        user: &dyn LivingEntity,
75        amplifier: i32,
76        direct_entity: Option<&SharedEntity>,
77        causing_entity: Option<&SharedEntity>,
78        scale: f64,
79    ) {
80        let _ = (direct_entity, causing_entity, scale);
81        self.apply_effect_tick(world, user, amplifier);
82    }
83}