Skip to main content

steel_core/inventory/container/
simple.rs

1use steel_registry::item_stack::ItemStack;
2use steel_utils::{DowncastType, DowncastTypeKey};
3
4use crate::inventory::container::Container;
5
6/// A Simple Container
7pub struct SimpleContainer {
8    items: Vec<ItemStack>,
9    times_changed: u32,
10}
11
12// SAFETY: This key is owned by Steel and uniquely identifies `SimpleContainer`.
13unsafe impl DowncastType for SimpleContainer {
14    const TYPE_KEY: DowncastTypeKey = DowncastTypeKey::new("steel:container/simple");
15}
16
17impl SimpleContainer {
18    /// Creates a new Simple Container
19    #[must_use]
20    pub fn new(size: usize) -> Self {
21        Self {
22            items: vec![ItemStack::empty(); size],
23            times_changed: 0,
24        }
25    }
26
27    /// Creates a Simple Container with already initialized items
28    #[must_use]
29    pub const fn from_items(items: Vec<ItemStack>) -> Self {
30        Self {
31            items,
32            times_changed: 0,
33        }
34    }
35
36    /// Number of `set_changed` calls so far; compare snapshots to detect a change.
37    #[must_use]
38    pub const fn times_changed(&self) -> u32 {
39        self.times_changed
40    }
41}
42
43impl Container for SimpleContainer {
44    fn items(&self) -> &[ItemStack] {
45        &self.items
46    }
47
48    fn items_mut(&mut self) -> &mut [ItemStack] {
49        &mut self.items
50    }
51
52    fn set_changed(&mut self) {
53        self.times_changed = self.times_changed.wrapping_add(1);
54    }
55}