ghast_inventory/window.rs
1//! Container window types: how the vanilla client builds the screen handler for an
2//! `open_window` packet, how many slots its container part has, and what kind each slot is.
3
4use crate::rules::Version;
5
6/// The vanilla screen handler (container class) the client created for a window.
7#[derive(Debug, Clone, Copy, PartialEq, Eq, Hash)]
8#[non_exhaustive]
9pub enum WindowKind {
10 /// Chest-like grid of `rows` × 9 slots (1.8 `minecraft:chest`, `minecraft:container` and
11 /// unknown types with slots; 1.16.5 `generic_9x1` … `generic_9x6`).
12 Chest {
13 /// Number of rows (1.8: `slotCount / 9`, may be 0).
14 rows: u8,
15 },
16 /// Dispenser / dropper (1.16.5 `generic_3x3`).
17 Generic3x3,
18 /// Hopper (5 slots).
19 Hopper,
20 /// 1.12 / 1.16.5 shulker box (27 slots that refuse shulker boxes).
21 ShulkerBox,
22 /// Crafting table: result slot 0, grid 1..=9.
23 Crafting,
24 /// Furnace (also 1.16.5 smoker and blast furnace): input 0, fuel 1, output 2.
25 Furnace,
26 /// Brewing stand (1.8: 4 slots; 1.12 / 1.16.5: 5 with the blaze powder slot).
27 BrewingStand,
28 /// Enchanting table (item, lapis).
29 Enchanting,
30 /// Anvil (2 inputs, result).
31 Anvil,
32 /// Beacon (payment slot).
33 Beacon,
34 /// Villager trading (2 inputs, result).
35 Merchant,
36 /// Horse / donkey / llama inventory with `slots` container slots (saddle, armor, chest).
37 Horse {
38 /// Container slots as sent by the server.
39 slots: u8,
40 },
41 /// 1.16.5 grindstone.
42 Grindstone,
43 /// 1.16.5 lectern (one book slot, no player inventory).
44 Lectern,
45 /// 1.16.5 loom.
46 Loom,
47 /// 1.16.5 smithing table.
48 Smithing,
49 /// 1.16.5 cartography table.
50 Cartography,
51 /// 1.16.5 stonecutter.
52 Stonecutter,
53 /// 26.1 crafter: 9 grid slots, the player inventory, then the result (screen slot 45).
54 Crafter,
55}
56
57/// What a slot of a screen handler is, as far as click prediction is concerned.
58#[derive(Debug, Clone, Copy, PartialEq, Eq, Hash)]
59#[non_exhaustive]
60pub enum SlotKind {
61 /// Plain slot: takes any item, up to 64.
62 Normal,
63 /// Window 0 armor slot (0 head … 3 feet): its armor type only, one item.
64 Armor(u8),
65 /// 1.12 / 1.16.5 offhand slot (window 0 slot 45): any item.
66 Offhand,
67 /// Crafting result: nothing can be put in; taking it consumes the grid.
68 CraftResult,
69 /// Output slot without side effects on the client (furnace output): nothing can be put in.
70 Output,
71 /// 1.12 / 1.16.5 shulker box slot: any item but shulker boxes.
72 Shulker,
73 /// Brewing stand bottle slot (0..=2): potions and glass bottles (1.12+: also splash and
74 /// lingering potions), one item.
75 Potion,
76 /// Brewing stand ingredient slot (3): the version's brewing ingredients.
77 BrewingIngredient,
78 /// 1.12+ brewing stand fuel slot (4): blaze powder.
79 BrewingFuel,
80 /// Enchanting table item slot (0): any item, one at a time.
81 EnchantItem,
82 /// Enchanting table lapis slot (1): lapis lazuli (1.8 / 1.12: dye damage 4).
83 Lapis,
84 /// Anvil output (2): computed by the client from the two inputs (repair, enchantment
85 /// combining, renaming), which ghast does not model: unknown after an input changed until
86 /// the server's `set_slot`; taking it (it needs the level cost) is refused.
87 AnvilOutput,
88 /// Beacon payment slot (0): emerald, diamond, gold and iron ingot (1.16.5: the
89 /// `beacon_payment_items` tag, also netherite ingot), one item.
90 BeaconPayment,
91 /// Villager trading output (2): the result of the offer the two payment slots buy, which
92 /// the client looks up itself; taking it uses the payments up.
93 TradeOutput,
94 /// Mount saddle slot (0): a saddle when the mount takes one
95 /// ([`MountRules`](crate::MountRules)), one at a time.
96 MountSaddle,
97 /// Mount armor slot (1): horse armor or carpet as the mount takes them
98 /// ([`MountRules`](crate::MountRules)).
99 MountArmor,
100 /// Furnace fuel slot (1): fuels and the bucket (one bucket at a time).
101 FurnaceFuel,
102 /// 1.16.5 workstation result (smithing table, stonecutter, grindstone, loom, cartography
103 /// table): filled by the client from the menu's other slots; taking it uses them up.
104 MenuResult,
105 /// 1.16.5 workstation input with the menu's own rules (grindstone: damageable, enchanted
106 /// or an enchanted book; loom: banner, dye, banner pattern).
107 MenuInput,
108 /// A slot whose rules need data this crate does not model (fuel, brewing ingredients,
109 /// enchanting, trading, anvil, beacon, horse equipment, …): clicks whose outcome depends on
110 /// it are refused as unpredictable.
111 Special,
112}
113
114impl WindowKind {
115 /// The window type of a 1.8 `open_window` (`inventoryType`, `slotCount`), as
116 /// `ClientPlayNetworkHandler.onOpenScreen` builds it. `None`: the vanilla client opens no
117 /// container for it (a slotless type it has no GUI for).
118 #[must_use]
119 pub fn from_v1_8(inventory_type: &str, slot_count: u8) -> Option<WindowKind> {
120 Some(match inventory_type {
121 // checked before "has slots": a generic container may have none
122 "minecraft:container" => WindowKind::Chest {
123 rows: slot_count / 9,
124 },
125 "minecraft:villager" => WindowKind::Merchant,
126 "EntityHorse" => WindowKind::Horse { slots: slot_count },
127 _ if slot_count == 0 => match inventory_type {
128 "minecraft:crafting_table" => WindowKind::Crafting,
129 "minecraft:enchanting_table" => WindowKind::Enchanting,
130 "minecraft:anvil" => WindowKind::Anvil,
131 _ => return None,
132 },
133 "minecraft:hopper" => WindowKind::Hopper,
134 "minecraft:furnace" => WindowKind::Furnace,
135 "minecraft:brewing_stand" => WindowKind::BrewingStand,
136 "minecraft:beacon" => WindowKind::Beacon,
137 "minecraft:dispenser" | "minecraft:dropper" => WindowKind::Generic3x3,
138 // minecraft:chest, minecraft:container and anything else with slots
139 _ => WindowKind::Chest {
140 rows: slot_count / 9,
141 },
142 })
143 }
144
145 /// The window type of a 1.12.2 `open_window` (`inventoryType`, `slotCount`): the 1.8 rules
146 /// plus `minecraft:shulker_box`.
147 #[must_use]
148 pub fn from_v1_12_2(inventory_type: &str, slot_count: u8) -> Option<WindowKind> {
149 if inventory_type == "minecraft:shulker_box" && slot_count > 0 {
150 return Some(WindowKind::ShulkerBox);
151 }
152 WindowKind::from_v1_8(inventory_type, slot_count)
153 }
154
155 /// The window type of a 1.16.5 `open_window` (menu type registry id).
156 #[must_use]
157 pub fn from_v1_16_5(menu_type: i32) -> Option<WindowKind> {
158 Some(match menu_type {
159 0..=5 => WindowKind::Chest {
160 rows: menu_type as u8 + 1,
161 },
162 6 => WindowKind::Generic3x3,
163 7 => WindowKind::Anvil,
164 8 => WindowKind::Beacon,
165 9 | 13 | 21 => WindowKind::Furnace, // blast_furnace, furnace, smoker
166 10 => WindowKind::BrewingStand,
167 11 => WindowKind::Crafting,
168 12 => WindowKind::Enchanting,
169 14 => WindowKind::Grindstone,
170 15 => WindowKind::Hopper,
171 16 => WindowKind::Lectern,
172 17 => WindowKind::Loom,
173 18 => WindowKind::Merchant,
174 19 => WindowKind::ShulkerBox,
175 20 => WindowKind::Smithing,
176 22 => WindowKind::Cartography,
177 23 => WindowKind::Stonecutter,
178 _ => return None,
179 })
180 }
181
182 /// Number of container slots (before the player's 27 + 9).
183 #[must_use]
184 pub fn container_len(self, version: Version) -> usize {
185 match self {
186 WindowKind::Chest { rows } => usize::from(rows) * 9,
187 WindowKind::Generic3x3 => 9,
188 WindowKind::Hopper => 5,
189 WindowKind::ShulkerBox => 27,
190 WindowKind::Crafting => 10,
191 WindowKind::Furnace => 3,
192 WindowKind::BrewingStand => match version {
193 Version::V1_8 => 4,
194 Version::V1_12
195 | Version::V1_16_5
196 | Version::V26_1
197 | Version::V26_2
198 | Version::V26_3 => 5,
199 },
200 WindowKind::Enchanting => 2,
201 WindowKind::Anvil => 3,
202 WindowKind::Beacon => 1,
203 WindowKind::Merchant => 3,
204 WindowKind::Horse { slots } => usize::from(slots),
205 WindowKind::Grindstone => 3,
206 WindowKind::Lectern => 1,
207 WindowKind::Loom => 4,
208 // 1.20+: template, base, addition, result
209 WindowKind::Smithing => match version {
210 Version::V26_1 | Version::V26_2 | Version::V26_3 => 4,
211 _ => 3,
212 },
213 WindowKind::Cartography => 3,
214 WindowKind::Stonecutter => 2,
215 WindowKind::Crafter => 10,
216 }
217 }
218
219 /// Whether the player's main inventory and hotbar follow the container slots.
220 #[must_use]
221 pub fn has_player_inventory(self) -> bool {
222 self != WindowKind::Lectern
223 }
224
225 /// Kind of container slot `i` (`i < container_len`).
226 #[must_use]
227 pub fn slot_kind(self, i: usize) -> SlotKind {
228 match self {
229 WindowKind::Chest { .. } | WindowKind::Generic3x3 | WindowKind::Hopper => {
230 SlotKind::Normal
231 }
232 WindowKind::ShulkerBox => SlotKind::Shulker,
233 WindowKind::Crafting => {
234 if i == 0 {
235 SlotKind::CraftResult
236 } else {
237 SlotKind::Normal
238 }
239 }
240 WindowKind::Furnace => match i {
241 0 => SlotKind::Normal,
242 1 => SlotKind::FurnaceFuel,
243 _ => SlotKind::Output,
244 },
245 WindowKind::Anvil => {
246 if i == 2 {
247 SlotKind::AnvilOutput
248 } else {
249 SlotKind::Normal
250 }
251 }
252 WindowKind::Enchanting => {
253 if i == 0 {
254 SlotKind::EnchantItem
255 } else {
256 SlotKind::Lapis
257 }
258 }
259 WindowKind::BrewingStand => match i {
260 0..=2 => SlotKind::Potion,
261 3 => SlotKind::BrewingIngredient,
262 _ => SlotKind::BrewingFuel,
263 },
264 WindowKind::Beacon => SlotKind::BeaconPayment,
265 WindowKind::Merchant => {
266 if i == 2 {
267 SlotKind::TradeOutput
268 } else {
269 SlotKind::Normal
270 }
271 }
272 WindowKind::Horse { .. } => match i {
273 0 => SlotKind::MountSaddle,
274 1 => SlotKind::MountArmor,
275 _ => SlotKind::Normal,
276 },
277 // 1.16.5 layouts
278 WindowKind::Smithing => {
279 if i == 2 {
280 SlotKind::MenuResult
281 } else {
282 SlotKind::Normal
283 }
284 }
285 WindowKind::Grindstone | WindowKind::Cartography => {
286 if i == 2 {
287 SlotKind::MenuResult
288 } else {
289 SlotKind::MenuInput
290 }
291 }
292 WindowKind::Loom => {
293 if i == 3 {
294 SlotKind::MenuResult
295 } else {
296 SlotKind::MenuInput
297 }
298 }
299 WindowKind::Stonecutter => {
300 if i == 1 {
301 SlotKind::MenuResult
302 } else {
303 SlotKind::Normal
304 }
305 }
306 WindowKind::Crafter => {
307 if i < 9 {
308 SlotKind::Normal
309 } else {
310 SlotKind::Output
311 }
312 }
313 _ => SlotKind::Special,
314 }
315 }
316}
317
318#[cfg(test)]
319mod tests {
320 use super::*;
321
322 #[test]
323 fn v1_8_types() {
324 assert_eq!(
325 WindowKind::from_v1_8("minecraft:chest", 54),
326 Some(WindowKind::Chest { rows: 6 })
327 );
328 assert_eq!(
329 WindowKind::from_v1_8("minecraft:container", 27),
330 Some(WindowKind::Chest { rows: 3 })
331 );
332 assert_eq!(
333 WindowKind::from_v1_8("plugin:whatever", 10),
334 Some(WindowKind::Chest { rows: 1 })
335 );
336 assert_eq!(
337 WindowKind::from_v1_8("minecraft:crafting_table", 0),
338 Some(WindowKind::Crafting)
339 );
340 assert_eq!(WindowKind::from_v1_8("minecraft:unknown", 0), None);
341 assert_eq!(
342 WindowKind::from_v1_8("minecraft:dropper", 9),
343 Some(WindowKind::Generic3x3)
344 );
345 assert_eq!(
346 WindowKind::from_v1_8("minecraft:brewing_stand", 4)
347 .unwrap()
348 .container_len(Version::V1_8),
349 4
350 );
351 }
352
353 #[test]
354 fn v1_16_types() {
355 assert_eq!(
356 WindowKind::from_v1_16_5(2),
357 Some(WindowKind::Chest { rows: 3 })
358 );
359 assert_eq!(WindowKind::from_v1_16_5(21), Some(WindowKind::Furnace));
360 assert_eq!(WindowKind::from_v1_16_5(24), None);
361 assert_eq!(WindowKind::from_v1_16_5(-1), None);
362 assert_eq!(WindowKind::BrewingStand.container_len(Version::V1_16_5), 5);
363 assert!(!WindowKind::Lectern.has_player_inventory());
364 }
365}