steel_core/inventory/
equipment.rs1use std::mem;
4
5use steel_registry::item_stack::ItemStack;
6
7pub use steel_registry::equipment::{EquipmentSlot, EquipmentSlotType};
8
9pub trait EntityEquipment: Send {
11 fn get_ref(&self, slot: EquipmentSlot) -> &ItemStack;
13
14 fn get_mut(&mut self, slot: EquipmentSlot) -> &mut ItemStack;
16
17 fn set(&mut self, slot: EquipmentSlot, stack: ItemStack) -> ItemStack;
19
20 fn take(&mut self, slot: EquipmentSlot) -> ItemStack;
22
23 fn clear(&mut self);
25
26 fn snapshot(&self) -> [ItemStack; EquipmentSlot::ALL.len()] {
28 EquipmentSlot::ALL.map(|slot| self.get_ref(slot).clone())
29 }
30
31 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
43pub 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 #[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}