azalea_inventory/
lib.rs

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