Skip to main content

azalea_inventory/
menu.rs

1// TODO: remove this here and in azalea-inventory-macros when rust makes
2// Default be implemented for all array sizes
3// https://github.com/rust-lang/rust/issues/61415
4
5use std::ops::{Deref, DerefMut, RangeInclusive};
6
7use azalea_inventory_macros::declare_menus;
8use azalea_registry::item::ItemStack;
9
10/// A fixed-size list of [`ItemStack`]s.
11#[derive(Clone, Debug)]
12pub struct SlotList<const N: usize>([ItemStack; N]);
13impl<const N: usize> Deref for SlotList<N> {
14    type Target = [ItemStack; N];
15    fn deref(&self) -> &Self::Target {
16        &self.0
17    }
18}
19impl<const N: usize> DerefMut for SlotList<N> {
20    fn deref_mut(&mut self) -> &mut Self::Target {
21        &mut self.0
22    }
23}
24impl<const N: usize> Default for SlotList<N> {
25    fn default() -> Self {
26        SlotList([(); N].map(|_| ItemStack::Empty))
27    }
28}
29impl<const N: usize> SlotList<N> {
30    pub fn new(items: [ItemStack; N]) -> Self {
31        SlotList(items)
32    }
33}
34
35impl Menu {
36    /// Get the [`Player`] from this [`Menu`].
37    ///
38    /// # Panics
39    ///
40    /// Will panic if the menu isn't `Menu::Player`.
41    pub fn as_player(&self) -> &Player {
42        self.try_as_player()
43            .expect("Called `Menu::as_player` on a menu that wasn't `Player`.")
44    }
45    /// Get the [`Player`] from this [`Menu`], or returns `None` if the menu
46    /// isn't a player menu.
47    pub fn try_as_player(&self) -> Option<&Player> {
48        if let Menu::Player(player) = &self {
49            Some(player)
50        } else {
51            None
52        }
53    }
54
55    /// Same as [`Menu::as_player`], but returns a mutable reference to the
56    /// [`Player`].
57    ///
58    /// # Panics
59    ///
60    /// Will panic if the menu isn't `Menu::Player`.
61    pub fn as_player_mut(&mut self) -> &mut Player {
62        self.try_as_player_mut()
63            .expect("Called `Menu::as_player_mut` on a menu that wasn't `Player`.")
64    }
65    /// Same as [`Menu::try_as_player`], but returns a mutable reference to the
66    /// [`Player`].
67    pub fn try_as_player_mut(&mut self) -> Option<&mut Player> {
68        if let Menu::Player(player) = self {
69            Some(player)
70        } else {
71            None
72        }
73    }
74}
75
76// the player inventory part is always the last 36 slots (except in the Player
77// menu), so we don't have to explicitly specify it
78
79// Client {
80//     ...
81//     pub menu: Menu,
82//     pub inventory: Arc<[Slot; 36]>
83// }
84
85// Generate a `struct Player`, `enum Menu`, and `impl Menu`.
86// a "player" field gets implicitly added with the player inventory
87
88declare_menus! {
89    Player {
90        craft_result: 1,
91        craft: 4,
92        armor: 4,
93        inventory: 36,
94        offhand: 1,
95    },
96    Generic9x1 {
97        contents: 9,
98    },
99    Generic9x2 {
100        contents: 18,
101    },
102    Generic9x3 {
103        contents: 27,
104    },
105    Generic9x4 {
106        contents: 36,
107    },
108    Generic9x5 {
109        contents: 45,
110    },
111    Generic9x6 {
112        contents: 54,
113    },
114    Generic3x3 {
115        contents: 9,
116    },
117    Crafter3x3 {
118        contents: 9,
119    },
120    Anvil {
121        first: 1,
122        second: 1,
123        result: 1,
124    },
125    Beacon {
126        payment: 1,
127    },
128    BlastFurnace {
129        ingredient: 1,
130        fuel: 1,
131        result: 1,
132    },
133    BrewingStand {
134        bottles: 3,
135        ingredient: 1,
136        fuel: 1,
137    },
138    Crafting {
139        result: 1,
140        grid: 9,
141    },
142    Enchantment {
143        item: 1,
144        lapis: 1,
145    },
146    Furnace {
147        ingredient: 1,
148        fuel: 1,
149        result: 1,
150    },
151    Grindstone {
152        input: 1,
153        additional: 1,
154        result: 1,
155    },
156    Hopper {
157        contents: 5,
158    },
159    Lectern {
160        book: 1,
161    },
162    Loom {
163        banner: 1,
164        dye: 1,
165        pattern: 1,
166        result: 1,
167    },
168    Merchant {
169        payments: 2,
170        result: 1,
171    },
172    ShulkerBox {
173        contents: 27,
174    },
175    Smithing {
176        template: 1,
177        base: 1,
178        additional: 1,
179        result: 1,
180    },
181    Smoker {
182        ingredient: 1,
183        fuel: 1,
184        result: 1,
185    },
186    CartographyTable {
187        map: 1,
188        additional: 1,
189        result: 1,
190    },
191    Stonecutter {
192        input: 1,
193        result: 1,
194    },
195}