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