Skip to main content

steel_core/enchantment_helper/
item_in_use.rs

1use steel_registry::item_stack::ItemStack;
2
3use crate::entity::LivingEntity;
4use crate::inventory::equipment::EquipmentSlot;
5
6/// Access to the enchanted stack without holding equipment locks across entity effects.
7/// Retaliatory damage can inspect the owner's weapon while applying a victim's enchantments.
8pub(super) enum EnchantedItemInUse<'a> {
9    Stack(&'a mut ItemStack),
10    Equipped {
11        owner: &'a dyn LivingEntity,
12        slot: EquipmentSlot,
13    },
14}
15
16impl EnchantedItemInUse<'_> {
17    pub(super) fn with_item(&self, visitor: &mut dyn FnMut(&ItemStack)) {
18        match self {
19            Self::Stack(stack) => visitor(stack),
20            Self::Equipped { owner, slot } => owner.with_equipment_slot(*slot, visitor),
21        }
22    }
23
24    pub(super) fn hurt_and_break(&mut self, amount: i32, infinite_materials: bool) -> bool {
25        match self {
26            Self::Stack(stack) => stack.hurt_and_break(amount, infinite_materials),
27            Self::Equipped { owner, slot } => {
28                let mut broke = false;
29                owner.with_equipment_slot_mut(*slot, &mut |stack| {
30                    broke = stack.hurt_and_break(amount, infinite_materials);
31                });
32                broke
33            }
34        }
35    }
36}