Skip to main content

steel_core/inventory/container/
result.rs

1use core::slice;
2use std::mem;
3
4use steel_registry::item_stack::ItemStack;
5use steel_utils::{DowncastType, DowncastTypeKey};
6
7use crate::inventory::container::Container;
8
9/// A simple container for holding a single crafting result.
10pub struct ResultContainer {
11    result: ItemStack,
12}
13
14// SAFETY: This key is owned by Steel and uniquely identifies `ResultContainer`.
15unsafe impl DowncastType for ResultContainer {
16    const TYPE_KEY: DowncastTypeKey = DowncastTypeKey::new("steel:container/result");
17}
18
19impl ResultContainer {
20    /// Creates a new empty result container.
21    #[must_use]
22    pub fn new() -> Self {
23        Self {
24            result: ItemStack::empty(),
25        }
26    }
27}
28
29impl Default for ResultContainer {
30    fn default() -> Self {
31        Self::new()
32    }
33}
34
35impl Container for ResultContainer {
36    fn get_container_size(&self) -> usize {
37        1
38    }
39
40    fn get_item(&self, _slot: usize) -> &ItemStack {
41        &self.result
42    }
43
44    fn get_item_mut(&mut self, _slot: usize) -> &mut ItemStack {
45        &mut self.result
46    }
47
48    fn set_item(&mut self, _slot: usize, stack: ItemStack) {
49        self.result = stack;
50    }
51
52    /// Removes items from the result container.
53    ///
54    /// Unlike normal containers, this **always takes the entire stack**
55    /// regardless of the `count` parameter. This matches Java's
56    /// `ResultContainer.removeItem()` behavior which uses `takeItem()`.
57    ///
58    /// This ensures that right-clicking on a crafting result takes the
59    /// full crafted item, not half of it.
60    fn remove_item(&mut self, _slot: usize, _count: i32) -> ItemStack {
61        mem::take(&mut self.result)
62    }
63
64    fn set_changed(&mut self) {
65        // Result container doesn't track dirty state.
66    }
67
68    fn items(&self) -> &[ItemStack] {
69        slice::from_ref(&self.result)
70    }
71
72    fn items_mut(&mut self) -> &mut [ItemStack] {
73        slice::from_mut(&mut self.result)
74    }
75}