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 non_empty_items(&self) -> Vec<(EquipmentSlot, ItemStack)> {
28 EquipmentSlot::ALL
29 .into_iter()
30 .filter_map(|slot| {
31 let item = self.get_ref(slot);
32 (!item.is_empty()).then(|| (slot, item.clone()))
33 })
34 .collect()
35 }
36}
37
38pub struct OwnedEntityEquipment {
40 slots: [ItemStack; 8],
41}
42
43impl Default for OwnedEntityEquipment {
44 fn default() -> Self {
45 Self::new()
46 }
47}
48
49impl OwnedEntityEquipment {
50 #[must_use]
52 pub fn new() -> Self {
53 Self {
54 slots: [
55 ItemStack::empty(),
56 ItemStack::empty(),
57 ItemStack::empty(),
58 ItemStack::empty(),
59 ItemStack::empty(),
60 ItemStack::empty(),
61 ItemStack::empty(),
62 ItemStack::empty(),
63 ],
64 }
65 }
66}
67
68impl EntityEquipment for OwnedEntityEquipment {
69 fn get_ref(&self, slot: EquipmentSlot) -> &ItemStack {
70 &self.slots[slot.index()]
71 }
72
73 fn get_mut(&mut self, slot: EquipmentSlot) -> &mut ItemStack {
74 &mut self.slots[slot.index()]
75 }
76
77 fn set(&mut self, slot: EquipmentSlot, stack: ItemStack) -> ItemStack {
78 mem::replace(&mut self.slots[slot.index()], stack)
79 }
80
81 fn take(&mut self, slot: EquipmentSlot) -> ItemStack {
82 mem::take(&mut self.slots[slot.index()])
83 }
84
85 fn clear(&mut self) {
86 for slot in EquipmentSlot::ALL {
87 self.slots[slot.index()] = ItemStack::empty();
88 }
89 }
90}