azalea_inventory/
lib.rs

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