aboutsummaryrefslogtreecommitdiff
path: root/azalea-client/src/plugins/inventory/equipment_effects.rs
blob: d5897f48cbde49127d4cd94eccde8176b0942e17 (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
//! Support for enchantments and items with attribute modifiers.

use std::collections::HashMap;

use azalea_core::{data_registry::ResolvableDataRegistry, registry_holder::value::AttributeEffect};
use azalea_entity::{Attributes, inventory::Inventory};
use azalea_inventory::{
    ItemStack,
    components::{self, AttributeModifier, EquipmentSlot},
};
use azalea_registry::identifier::Identifier;
use bevy_ecs::{
    component::Component,
    entity::Entity,
    event::EntityEvent,
    observer::On,
    query::With,
    system::{Commands, Query},
};
use tracing::{debug, error, warn};

use crate::local_player::WorldHolder;

/// A component that contains the equipment slots that we had last tick.
///
/// This is used by [`collect_equipment_changes`] for applying enchantments.
#[derive(Component, Debug, Default)]
pub struct LastEquipmentItems {
    pub map: HashMap<EquipmentSlot, ItemStack>,
}

pub fn collect_equipment_changes(
    mut commands: Commands,
    mut query: Query<(Entity, &Inventory, Option<&LastEquipmentItems>), With<Attributes>>,
) {
    for (entity, inventory, last_equipment_items) in &mut query {
        let last_equipment_items = if let Some(e) = last_equipment_items {
            e
        } else {
            commands
                .entity(entity)
                .insert(LastEquipmentItems::default());
            continue;
        };

        let mut changes = HashMap::new();

        for equipment_slot in EquipmentSlot::values() {
            let current_item = inventory
                .get_equipment(equipment_slot)
                .unwrap_or(&ItemStack::Empty);
            let last_item = last_equipment_items
                .map
                .get(&equipment_slot)
                .unwrap_or(&ItemStack::Empty);

            if current_item == last_item {
                // item hasn't changed, nothing to do
                continue;
            }

            changes.insert(
                equipment_slot,
                EquipmentChange {
                    old: last_item.clone(),
                    new: current_item.clone(),
                },
            );
        }

        if changes.is_empty() {
            continue;
        }
        commands.trigger(EquipmentChangesEvent {
            entity,
            map: changes,
        });
    }
}

#[derive(Debug, EntityEvent)]
pub struct EquipmentChangesEvent {
    pub entity: Entity,
    pub map: HashMap<EquipmentSlot, EquipmentChange>,
}
#[derive(Debug)]
pub struct EquipmentChange {
    pub old: ItemStack,
    pub new: ItemStack,
}

pub fn handle_equipment_changes(
    equipment_changes: On<EquipmentChangesEvent>,
    mut query: Query<(&WorldHolder, &mut LastEquipmentItems, &mut Attributes)>,
) {
    let Ok((world_holder, mut last_equipment_items, mut attributes)) =
        query.get_mut(equipment_changes.entity)
    else {
        error!(
            "got EquipmentChangesEvent with unknown entity {}",
            equipment_changes.entity
        );
        return;
    };

    if !equipment_changes.map.is_empty() {
        debug!("equipment changes: {:?}", equipment_changes.map);
    }

    for (&slot, change) in &equipment_changes.map {
        if change.old.is_present() {
            // stopLocationBasedEffects

            for (attribute, modifier) in
                collect_attribute_modifiers_from_item(slot, &change.old, world_holder)
            {
                if let Some(attribute) = attributes.get_mut(attribute) {
                    attribute.remove(&modifier.id);
                }
            }

            last_equipment_items.map.remove(&slot);
        }

        if change.new.is_present() {
            // see ItemStack.forEachModifier in vanilla

            for (attribute, modifier) in
                collect_attribute_modifiers_from_item(slot, &change.new, world_holder)
            {
                if let Some(attribute) = attributes.get_mut(attribute) {
                    attribute.remove(&modifier.id);
                    attribute.insert(modifier);
                }
            }

            // runLocationChangedEffects

            last_equipment_items.map.insert(slot, change.new.clone());
        }
    }
}

fn collect_attribute_modifiers_from_item(
    slot: EquipmentSlot,
    item: &ItemStack,
    world_holder: &WorldHolder,
) -> Vec<(azalea_registry::builtin::Attribute, AttributeModifier)> {
    let mut modifiers = Vec::new();

    // handle the attribute_modifiers component first
    let attribute_modifiers = item
        .get_component::<components::AttributeModifiers>()
        .unwrap_or_default();
    for modifier in &attribute_modifiers.modifiers {
        modifiers.push((modifier.kind, modifier.modifier.clone()));
    }

    // now handle enchants
    let enchants = item
        .get_component::<components::Enchantments>()
        .unwrap_or_default();
    if !enchants.levels.is_empty() {
        let registry_holder = &world_holder.shared.read().registries;
        for (enchant, &level) in &enchants.levels {
            let Some((_enchant_id, enchant_definition)) = enchant.resolve(registry_holder) else {
                warn!(
                    "Got equipment with an enchantment that wasn't in the registry, so it couldn't be resolved to an ID"
                );
                continue;
            };

            let effects = enchant_definition.get::<AttributeEffect>();
            for effect in effects.unwrap_or_default() {
                // TODO: check if the effect definition allows the slot

                let modifier = AttributeModifier {
                    id: Identifier::new(format!("{}/{slot}", effect.id)),
                    amount: effect.amount.calculate(level) as f64,
                    operation: effect.operation,
                };

                modifiers.push((effect.attribute, modifier));
            }
        }
    }

    modifiers
}