aboutsummaryrefslogtreecommitdiff
path: root/azalea-core/src/registry_holder/entity_effect.rs
blob: 00efde21b9ad8b0f378217d369727c1fb841322e (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
use std::{collections::HashMap, str::FromStr};

use azalea_registry::{
    Holder,
    builtin::{
        EnchantmentEntityEffectKind as EntityEffectKind, GameEvent, ParticleKind, SoundEvent,
    },
    data::DamageKindKey,
    identifier::Identifier,
};
use simdnbt::{
    Deserialize, DeserializeError,
    borrow::{NbtCompound, NbtTag},
};
use tracing::error;

use crate::{
    position::{Vec3, Vec3i},
    registry_holder::{
        block_predicate::BlockPredicate, block_state_provider::BlockStateProvider,
        float_provider::FloatProvider, get_in_compound, value::LevelBasedValue,
    },
    sound::CustomSound,
};

#[derive(Clone, Debug)]
pub enum EntityEffect {
    AllOf(AllOf),
    ApplyMobEffect(ApplyMobEffect),
    ChangeItemDamage(ChangeItemDamage),
    DamageEntity(DamageEntity),
    Explode(Explode),
    Ignite(Ignite),
    ApplyImpulse(ApplyEntityImpulse),
    ApplyExhaustion(ApplyExhaustion),
    PlaySound(PlaySound),
    ReplaceBlock(ReplaceBlock),
    ReplaceDisk(ReplaceDisk),
    RunFunction(RunFunction),
    SetBlockProperties(SetBlockProperties),
    SpawnParticles(SpawnParticles),
    SummonEntity(SummonEntity),
}

impl Deserialize for EntityEffect {
    fn from_compound(nbt: NbtCompound) -> Result<Self, DeserializeError> {
        let kind = get_in_compound(&nbt, "type")?;
        let res = match kind {
            EntityEffectKind::AllOf => Deserialize::from_compound(nbt).map(Self::AllOf),
            EntityEffectKind::ApplyMobEffect => {
                Deserialize::from_compound(nbt).map(Self::ApplyMobEffect)
            }
            EntityEffectKind::ChangeItemDamage => {
                Deserialize::from_compound(nbt).map(Self::ChangeItemDamage)
            }
            EntityEffectKind::DamageEntity => {
                Deserialize::from_compound(nbt).map(Self::DamageEntity)
            }
            EntityEffectKind::Explode => Deserialize::from_compound(nbt).map(Self::Explode),
            EntityEffectKind::Ignite => Deserialize::from_compound(nbt).map(Self::Ignite),
            EntityEffectKind::ApplyImpulse => {
                Deserialize::from_compound(nbt).map(Self::ApplyImpulse)
            }
            EntityEffectKind::ApplyExhaustion => {
                Deserialize::from_compound(nbt).map(Self::ApplyExhaustion)
            }
            EntityEffectKind::PlaySound => Deserialize::from_compound(nbt).map(Self::PlaySound),
            EntityEffectKind::ReplaceBlock => {
                Deserialize::from_compound(nbt).map(Self::ReplaceBlock)
            }
            EntityEffectKind::ReplaceDisk => Deserialize::from_compound(nbt).map(Self::ReplaceDisk),
            EntityEffectKind::RunFunction => Deserialize::from_compound(nbt).map(Self::RunFunction),
            EntityEffectKind::SetBlockProperties => {
                Deserialize::from_compound(nbt).map(Self::SetBlockProperties)
            }
            EntityEffectKind::SpawnParticles => {
                Deserialize::from_compound(nbt).map(Self::SpawnParticles)
            }
            EntityEffectKind::SummonEntity => {
                Deserialize::from_compound(nbt).map(Self::SummonEntity)
            }
        };
        if res.is_err() {
            error!("Error deserializing EntityEffect {kind}: {nbt:?}");
        }
        res
    }
}

#[derive(Clone, Debug, simdnbt::Deserialize)]
pub struct AllOf {
    pub effects: Vec<EntityEffect>,
}

#[derive(Clone, Debug, simdnbt::Deserialize)]
pub struct ApplyMobEffect {
    /// IDs of mob effects.
    pub to_apply: HomogeneousList,
    pub min_duration: LevelBasedValue,
    pub max_duration: LevelBasedValue,
    pub min_amplifier: LevelBasedValue,
    pub max_amplifier: LevelBasedValue,
}

// TODO: in vanilla this is just a HolderSetCodec using a RegistryFixedCodec,
// azalea registries should probably be refactored first tho
#[derive(Clone, Debug, Default)]
pub struct HomogeneousList {
    pub ids: Vec<Identifier>,
}
impl simdnbt::FromNbtTag for HomogeneousList {
    fn from_nbt_tag(tag: NbtTag) -> Option<Self> {
        if let Some(string) = tag.string() {
            return Some(Self {
                ids: vec![Identifier::new(string.to_str())],
            });
        }
        if let Some(list) = tag.list()
            && let Some(strings) = list.strings()
        {
            return Some(Self {
                ids: strings
                    .iter()
                    .map(|&s| Identifier::new(s.to_str()))
                    .collect(),
            });
        }
        None
    }
}

#[derive(Clone, Debug, simdnbt::Deserialize)]
pub struct ChangeItemDamage {
    pub amount: LevelBasedValue,
}

#[derive(Clone, Debug, simdnbt::Deserialize)]
pub struct DamageEntity {
    pub min_damage: LevelBasedValue,
    pub max_damage: LevelBasedValue,
    #[simdnbt(rename = "damage_type")]
    pub damage_kind: DamageKindKey,
}

#[derive(Clone, Debug, simdnbt::Deserialize)]
pub struct Explode {
    pub attribute_to_user: Option<bool>,
    #[simdnbt(rename = "damage_type")]
    pub damage_kind: Option<DamageKindKey>,
    pub knockback_multiplier: Option<LevelBasedValue>,
    pub immune_blocks: Option<HomogeneousList>,
    pub offset: Option<Vec3>,
}

#[derive(Clone, Debug, simdnbt::Deserialize)]
pub struct Ignite {
    pub duration: LevelBasedValue,
}

#[derive(Clone, Debug, simdnbt::Deserialize)]
pub struct ApplyEntityImpulse {
    pub direction: Vec3,
    pub coordinate_scale: Vec3,
    pub magnitude: LevelBasedValue,
}

#[derive(Clone, Debug, simdnbt::Deserialize)]
pub struct ApplyExhaustion {
    pub amount: LevelBasedValue,
}

#[derive(Clone, Debug)]
pub struct PlaySound {
    // #[simdnbt(compact)]
    pub sound: Vec<Holder<SoundEvent, CustomSound>>,
    pub volume: FloatProvider,
    pub pitch: FloatProvider,
}

impl Deserialize for PlaySound {
    fn from_compound(nbt: NbtCompound) -> Result<Self, DeserializeError> {
        let sound = if let Some(list) = nbt.list("sound") {
            // TODO: this will probably break in the future because it's only handling lists
            // of strings. you should refactor simdnbt to have an owned NbtTag enum that
            // contains the borrow types so this works for more than just
            // strings.
            list.strings()
                .ok_or(DeserializeError::MissingField)?
                .iter()
                .map(|s| {
                    SoundEvent::from_str(&s.to_str())
                        .map(Holder::Reference)
                        .ok()
                })
                .collect::<Option<_>>()
                .ok_or(DeserializeError::MissingField)?
        } else {
            vec![get_in_compound(&nbt, "sound")?]
        };

        let volume = get_in_compound(&nbt, "volume")?;
        let pitch = get_in_compound(&nbt, "pitch")?;

        Ok(Self {
            sound,
            volume,
            pitch,
        })
    }
}

#[derive(Clone, Debug, simdnbt::Deserialize)]
pub struct ReplaceBlock {
    pub offset: Option<Vec3i>,
    pub predicate: Option<BlockPredicate>,
    pub block_state: BlockStateProvider,
    pub trigger_game_event: Option<GameEvent>,
}

#[derive(Clone, Debug, simdnbt::Deserialize)]
pub struct ReplaceDisk {
    pub radius: LevelBasedValue,
    pub height: LevelBasedValue,
    pub offset: Option<Vec3i>,
    pub predicate: Option<BlockPredicate>,
    pub block_state: BlockStateProvider,
    pub trigger_game_event: Option<GameEvent>,
}

#[derive(Clone, Debug, simdnbt::Deserialize)]
pub struct RunFunction {
    pub function: Identifier,
}

#[derive(Clone, Debug, simdnbt::Deserialize)]
pub struct SetBlockProperties {
    pub properties: HashMap<String, String>,
    pub offset: Option<Vec3i>,
    pub trigger_game_event: Option<GameEvent>,
}

#[derive(Clone, Debug, simdnbt::Deserialize)]
pub struct SpawnParticles {
    pub particle: ParticleKindCodec,
    pub horizontal_position: SpawnParticlesPosition,
    pub vertical_position: SpawnParticlesPosition,
    pub horizontal_velocity: SpawnParticlesVelocity,
    pub vertical_velocity: SpawnParticlesVelocity,
    pub speed: Option<FloatProvider>,
}

#[derive(Clone, Debug, simdnbt::Deserialize)]
pub struct ParticleKindCodec {
    #[simdnbt(rename = "type")]
    pub kind: ParticleKind,
}

#[derive(Clone, Debug, simdnbt::Deserialize)]
pub struct SpawnParticlesPosition {
    #[simdnbt(rename = "type")]
    pub kind: Identifier,
    pub offset: Option<f32>,
    pub scale: Option<f32>,
}

#[derive(Clone, Debug, simdnbt::Deserialize)]
pub struct SpawnParticlesVelocity {
    pub movement_scale: Option<f32>,
    pub base: Option<FloatProvider>,
}

#[derive(Clone, Debug, simdnbt::Deserialize)]
pub struct SummonEntity {
    pub entity: HomogeneousList,
    pub join_team: Option<bool>,
}