Skip to main content

steel_core/inventory/
equipment.rs

1//! Entity equipment access and owned storage.
2
3use std::mem;
4
5use steel_registry::item_stack::ItemStack;
6
7pub use steel_registry::equipment::{EquipmentSlot, EquipmentSlotType};
8
9/// Equipment access shared by player inventories and owned entity storage.
10pub trait EntityEquipment: Send {
11    /// Gets a reference to the item in a slot.
12    fn get_ref(&self, slot: EquipmentSlot) -> &ItemStack;
13
14    /// Gets a mutable reference to the item in a slot.
15    fn get_mut(&mut self, slot: EquipmentSlot) -> &mut ItemStack;
16
17    /// Sets the item in a slot, returning the old item.
18    fn set(&mut self, slot: EquipmentSlot, stack: ItemStack) -> ItemStack;
19
20    /// Takes the item from a slot, leaving an empty stack in its place.
21    fn take(&mut self, slot: EquipmentSlot) -> ItemStack;
22
23    /// Clears all equipment slots.
24    fn clear(&mut self);
25
26    /// Clones the item in every equipment slot, indexed by [`EquipmentSlot::index`].
27    fn snapshot(&self) -> [ItemStack; EquipmentSlot::ALL.len()] {
28        EquipmentSlot::ALL.map(|slot| self.get_ref(slot).clone())
29    }
30
31    /// Returns non-empty equipment slots for initial spawn synchronization.
32    fn non_empty_items(&self) -> Vec<(EquipmentSlot, ItemStack)> {
33        EquipmentSlot::ALL
34            .into_iter()
35            .filter_map(|slot| {
36                let item = self.get_ref(slot);
37                (!item.is_empty()).then(|| (slot, item.clone()))
38            })
39            .collect()
40    }
41}
42
43/// Owned equipment storage used by non-player living entities.
44pub struct OwnedEntityEquipment {
45    slots: [ItemStack; 8],
46}
47
48impl Default for OwnedEntityEquipment {
49    fn default() -> Self {
50        Self::new()
51    }
52}
53
54impl OwnedEntityEquipment {
55    /// Creates a new empty equipment storage.
56    #[must_use]
57    pub fn new() -> Self {
58        Self {
59            slots: [
60                ItemStack::empty(),
61                ItemStack::empty(),
62                ItemStack::empty(),
63                ItemStack::empty(),
64                ItemStack::empty(),
65                ItemStack::empty(),
66                ItemStack::empty(),
67                ItemStack::empty(),
68            ],
69        }
70    }
71}
72
73impl EntityEquipment for OwnedEntityEquipment {
74    fn get_ref(&self, slot: EquipmentSlot) -> &ItemStack {
75        &self.slots[slot.index()]
76    }
77
78    fn get_mut(&mut self, slot: EquipmentSlot) -> &mut ItemStack {
79        &mut self.slots[slot.index()]
80    }
81
82    fn set(&mut self, slot: EquipmentSlot, stack: ItemStack) -> ItemStack {
83        mem::replace(&mut self.slots[slot.index()], stack)
84    }
85
86    fn take(&mut self, slot: EquipmentSlot) -> ItemStack {
87        mem::take(&mut self.slots[slot.index()])
88    }
89
90    fn clear(&mut self) {
91        for slot in EquipmentSlot::ALL {
92            self.slots[slot.index()] = ItemStack::empty();
93        }
94    }
95}