ghast-inventory 0.4.0

Inventory and container windows for the ghast Minecraft Java Edition client library: vanilla client-side click prediction, the window confirm protocol, hashed 26.1 clicks and lazy NBT / data-component item stacks (1.8.9, 1.12.2, 1.16.5 and 26.1)
Documentation
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
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
//! Adapters between the inventory and the generated packets of `ghast-protocol`.

#[cfg(feature = "v1_8")]
use ghast_protocol::v1_8::play::{clientbound as cb8, serverbound as sb8};
#[cfg(feature = "v1_12")]
use ghast_protocol::v1_12_2::play::{clientbound as cb12, serverbound as sb12};
#[cfg(feature = "v1_16_5")]
use ghast_protocol::v1_16_5::play::{clientbound as cb16, serverbound as sb16};

#[allow(unused_imports)]
use crate::inventory::{ClickPacket, Inventory, Outgoing};
#[allow(unused_imports)]
use crate::stack::{from_legacy_slot, from_modern_slot, to_legacy_slot, to_modern_slot};
#[allow(unused_imports)]
use crate::window::WindowKind;

impl ClickPacket {
    /// The 1.8 `window_click`.
    #[cfg(feature = "v1_8")]
    #[must_use]
    pub fn to_v1_8(&self) -> sb8::WindowClick {
        sb8::WindowClick {
            window_id: self.window_id,
            slot: self.slot,
            mouse_button: self.button as i8,
            action: self.action,
            mode: self.mode as i8,
            item: to_legacy_slot(self.item.as_ref()),
        }
    }

    /// The 1.12.2 `window_click`.
    #[cfg(feature = "v1_12")]
    #[must_use]
    pub fn to_v1_12_2(&self) -> sb12::WindowClick {
        sb12::WindowClick {
            window_id: self.window_id,
            slot: self.slot,
            mouse_button: self.button as i8,
            action: self.action,
            mode: self.mode as i8,
            item: to_legacy_slot(self.item.as_ref()),
        }
    }

    /// The 1.16.5 `window_click`.
    #[cfg(feature = "v1_16_5")]
    #[must_use]
    pub fn to_v1_16_5(&self) -> sb16::WindowClick {
        sb16::WindowClick {
            window_id: self.window_id,
            slot: self.slot,
            mouse_button: self.button as i8,
            action: self.action,
            mode: self.mode as i8,
            item: to_modern_slot(self.item.as_ref()),
        }
    }
}

impl Outgoing {
    /// The 1.8 serverbound packet (`None` for [`Outgoing::ContainerClick`], which only a 26.1
    /// inventory produces).
    #[cfg(feature = "v1_8")]
    #[must_use]
    pub fn to_v1_8(&self) -> Option<sb8::ServerboundPlayPacket> {
        Some(match self {
            Outgoing::Click(c) => sb8::ServerboundPlayPacket::WindowClick(c.to_v1_8()),
            Outgoing::Confirm { window_id, action } => {
                sb8::ServerboundPlayPacket::Transaction(sb8::Transaction {
                    window_id: *window_id,
                    action: *action,
                    accepted: true,
                })
            }
            Outgoing::CloseWindow(id) => {
                sb8::ServerboundPlayPacket::CloseWindow(sb8::CloseWindow { window_id: *id })
            }
            Outgoing::OpenInventory => {
                sb8::ServerboundPlayPacket::ClientCommand(sb8::ClientCommand {
                    payload: sb8::ClientCommandPayload(2),
                })
            }
            Outgoing::ContainerClick(_) => return None,
        })
    }

    /// The 1.12.2 serverbound packet (`None` for [`Outgoing::OpenInventory`], which 1.12 does
    /// not have; [`Inventory::open_inventory`] never returns it on 1.12).
    #[cfg(feature = "v1_12")]
    #[must_use]
    pub fn to_v1_12_2(&self) -> Option<sb12::ServerboundPlayPacket> {
        Some(match self {
            Outgoing::Click(c) => sb12::ServerboundPlayPacket::WindowClick(c.to_v1_12_2()),
            Outgoing::Confirm { window_id, action } => {
                sb12::ServerboundPlayPacket::Transaction(sb12::Transaction {
                    window_id: *window_id,
                    action: *action,
                    accepted: true,
                })
            }
            Outgoing::CloseWindow(id) => {
                sb12::ServerboundPlayPacket::CloseWindow(sb12::CloseWindow { window_id: *id })
            }
            Outgoing::OpenInventory | Outgoing::ContainerClick(_) => return None,
        })
    }

    /// The 1.16.5 serverbound packet (`None` for [`Outgoing::OpenInventory`], which 1.16.5
    /// does not have; [`Inventory::open_inventory`] never returns it on 1.16.5).
    #[cfg(feature = "v1_16_5")]
    #[must_use]
    pub fn to_v1_16_5(&self) -> Option<sb16::ServerboundPlayPacket> {
        Some(match self {
            Outgoing::Click(c) => sb16::ServerboundPlayPacket::WindowClick(c.to_v1_16_5()),
            Outgoing::Confirm { window_id, action } => {
                sb16::ServerboundPlayPacket::Transaction(sb16::Transaction {
                    window_id: *window_id,
                    action: *action,
                    accepted: true,
                })
            }
            Outgoing::CloseWindow(id) => {
                sb16::ServerboundPlayPacket::CloseWindow(sb16::CloseWindow { window_id: *id })
            }
            Outgoing::OpenInventory | Outgoing::ContainerClick(_) => return None,
        })
    }
}

impl Inventory {
    /// Applies a 1.8 clientbound play packet: `login` / `respawn` (new player entity),
    /// `open_window`, `close_window`, `window_items`, `set_slot`, `transaction`,
    /// `held_item_slot`, `craft_progress_bar`; everything else is ignored. Returns the confirm
    /// answer to send now, if any (see [`Inventory::on_transaction`]).
    #[cfg(feature = "v1_8")]
    pub fn apply_v1_8(&mut self, p: &cb8::ClientboundPlayPacket) -> Option<Outgoing> {
        use cb8::ClientboundPlayPacket as P;
        match p {
            P::Login(_) | P::Respawn(_) => self.reset(),
            P::OpenWindow(w) => self.on_open_window(
                i32::from(w.window_id),
                WindowKind::from_v1_8(&w.inventory_type, w.slot_count),
                w.window_title.clone(),
            ),
            P::CloseWindow(_) => self.on_close_window(),
            P::WindowItems(w) => self.on_window_items_legacy(i32::from(w.window_id), &w.items),
            P::SetSlot(s) => {
                self.on_set_slot(
                    i32::from(s.window_id),
                    s.slot,
                    from_legacy_slot(s.item.clone()),
                );
            }
            P::Transaction(t) => return self.on_transaction(t.window_id, t.action, t.accepted),
            P::HeldItemSlot(h) => self.on_held_slot(h.slot),
            P::CraftProgressBar(c) => {
                self.on_window_property(i32::from(c.window_id), c.property, c.value);
            }
            _ => {}
        }
        None
    }

    /// Applies a 1.12.2 clientbound play packet, like [`apply_v1_8`](Inventory::apply_v1_8)
    /// (`open_window` types as [`WindowKind::from_v1_12_2`] maps them).
    #[cfg(feature = "v1_12")]
    pub fn apply_v1_12_2(&mut self, p: &cb12::ClientboundPlayPacket) -> Option<Outgoing> {
        use cb12::ClientboundPlayPacket as P;
        match p {
            P::Login(_) | P::Respawn(_) => self.reset(),
            P::OpenWindow(w) => self.on_open_window(
                i32::from(w.window_id),
                WindowKind::from_v1_12_2(&w.inventory_type, w.slot_count),
                w.window_title.clone(),
            ),
            P::CloseWindow(_) => self.on_close_window(),
            P::WindowItems(w) => self.on_window_items_legacy(i32::from(w.window_id), &w.items),
            P::SetSlot(s) => {
                self.on_set_slot(
                    i32::from(s.window_id),
                    s.slot,
                    from_legacy_slot(s.item.clone()),
                );
            }
            P::Transaction(t) => return self.on_transaction(t.window_id, t.action, t.accepted),
            P::HeldItemSlot(h) => self.on_held_slot(h.slot),
            P::CraftProgressBar(c) => {
                self.on_window_property(i32::from(c.window_id), c.property, c.value);
            }
            _ => {}
        }
        None
    }

    /// Applies a 1.16.5 clientbound play packet, like [`apply_v1_8`](Inventory::apply_v1_8)
    /// plus `open_horse_window` and `declare_recipes`.
    #[cfg(feature = "v1_16_5")]
    pub fn apply_v1_16_5(&mut self, p: &cb16::ClientboundPlayPacket) -> Option<Outgoing> {
        use cb16::ClientboundPlayPacket as P;
        match p {
            P::Login(_) | P::Respawn(_) => self.reset(),
            P::OpenWindow(w) => self.on_open_window(
                w.window_id,
                WindowKind::from_v1_16_5(w.inventory_type),
                w.window_title.clone(),
            ),
            P::OpenHorseWindow(w) => self.on_open_window(
                i32::from(w.window_id),
                // the slot count is a VarInt; vanilla horse inventories have at most 17
                u8::try_from(w.nb_slots)
                    .ok()
                    .map(|slots| WindowKind::Horse { slots }),
                bytestring::ByteString::new(),
            ),
            P::CloseWindow(_) => self.on_close_window(),
            P::WindowItems(w) => self.on_window_items(
                i32::from(w.window_id),
                w.items.iter().cloned().map(from_modern_slot),
            ),
            P::SetSlot(s) => {
                self.on_set_slot(
                    i32::from(s.window_id),
                    s.slot,
                    from_modern_slot(s.item.clone()),
                );
            }
            P::Transaction(t) => return self.on_transaction(t.window_id, t.action, t.accepted),
            P::HeldItemSlot(h) => self.on_held_slot(h.slot),
            P::CraftProgressBar(c) => {
                self.on_window_property(i32::from(c.window_id), c.property, c.value);
            }
            P::DeclareRecipes(d) => self.on_declare_recipes(crate::RecipeBook::from_v1_16_5(d)),
            _ => {}
        }
        None
    }
}

#[cfg(feature = "v26_1")]
mod v26_1 {
    use bytes::BytesMut;
    use ghast_protocol::codec::{Encode, Reader};
    use ghast_protocol::types::slot::v775::ItemStack;
    use ghast_protocol::v26_1::play::clientbound::{self as cb, ClientboundPlayPacket};
    use ghast_protocol::v26_1::play::serverbound as sb;
    use ghast_protocol::{DecodeError, RawPacket};

    use crate::inventory::{ContainerClick, Inventory, Outgoing};
    use crate::stack::Stack;
    use crate::v26_1::{HashRegistries, hashed_slot};

    /// Clientbound 26.1 play packet ids [`Inventory::apply_raw_v26_1`] handles.
    pub const PACKET_IDS_26_1: &[i32] = &[
        cb::id::LOGIN,
        cb::id::RESPAWN,
        cb::id::OPEN_WINDOW,
        cb::id::CLOSE_WINDOW,
        cb::id::WINDOW_ITEMS,
        cb::id::SET_SLOT,
        cb::id::SET_CURSOR_ITEM,
        cb::id::SET_PLAYER_INVENTORY,
        cb::id::HELD_ITEM_SLOT,
        cb::id::CRAFT_PROGRESS_BAR,
    ];

    fn slot(r: &mut Reader<'_>) -> Result<Option<Stack>, DecodeError> {
        Ok(ItemStack::read(r)?.map(Stack::from_v775))
    }

    /// A generated 26.1 slot as a stack (re-read lazily from its encoding).
    fn from_generated(s: &ghast_protocol::defs::p775::types::Slot) -> Option<Stack> {
        let mut w = BytesMut::new();
        s.encode(&mut w).ok()?;
        let b = w.freeze();
        ItemStack::read(&mut Reader::new(&b))
            .ok()?
            .map(Stack::from_v775)
    }

    impl ContainerClick {
        /// The 26.1 `container_click` with hashed stacks (see [`hashed_slot`]).
        #[must_use]
        pub fn to_v26_1(&self, reg: &HashRegistries) -> sb::WindowClick {
            sb::WindowClick {
                window_id: self.window_id,
                state_id: self.state_id,
                slot: self.slot,
                mouse_button: self.button as i8,
                mode: i32::from(self.mode),
                changed_slots: self
                    .changed
                    .iter()
                    .map(|(location, s)| sb::WindowClickChangedSlots {
                        location: *location,
                        item: hashed_slot(s.as_ref(), reg).0,
                    })
                    .collect(),
                cursor_item: hashed_slot(self.carried.as_ref(), reg).0,
            }
        }
    }

    impl Outgoing {
        /// The 26.1 serverbound packet (`None` for the legacy-only variants, which a 26.1
        /// inventory never returns).
        #[must_use]
        pub fn to_v26_1(&self, reg: &HashRegistries) -> Option<sb::ServerboundPlayPacket> {
            Some(match self {
                Outgoing::ContainerClick(c) => {
                    sb::ServerboundPlayPacket::WindowClick(c.to_v26_1(reg))
                }
                Outgoing::CloseWindow(id) => {
                    sb::ServerboundPlayPacket::CloseWindow(sb::CloseWindow {
                        window_id: i32::from(*id),
                    })
                }
                _ => return None,
            })
        }
    }

    impl Inventory {
        /// [`Outgoing::to_v26_1`] with this inventory's registries.
        #[must_use]
        pub fn encode_v26_1(&self, out: &Outgoing) -> Option<sb::ServerboundPlayPacket> {
            out.to_v26_1(self.registries_26_1())
        }

        /// Applies a 26.1 clientbound play packet: `login` / `respawn`, `open_screen`,
        /// `container_close`, `container_set_content`, `container_set_slot`,
        /// `set_cursor_item`, `set_player_inventory`, `set_held_slot`, `container_set_data`.
        /// Slots are converted through their encoding (use [`Inventory::apply_raw_v26_1`] to
        /// avoid decoding components at all).
        pub fn apply_v26_1(&mut self, p: &ClientboundPlayPacket) {
            use ClientboundPlayPacket as P;
            match p {
                P::Login(_) => self.reset(),
                P::Respawn(r) => {
                    // keep-inventory flags do not matter: a new player entity either way
                    let _ = r;
                    self.reset();
                }
                P::OpenWindow(w) => self.open_window_26_1(w),
                P::CloseWindow(_) => self.on_close_window(),
                P::WindowItems(w) => self.on_window_items_26_1(
                    w.window_id,
                    w.state_id,
                    w.items.iter().map(from_generated),
                    from_generated(&w.carried_item),
                ),
                P::SetSlot(s) => {
                    self.on_set_slot_26_1(s.window_id, s.state_id, s.slot, from_generated(&s.item))
                }
                P::SetCursorItem(c) => self.on_set_cursor(from_generated(&c.contents)),
                P::SetPlayerInventory(p) => {
                    self.on_set_player_inventory(p.slot_id, from_generated(&p.contents))
                }
                P::HeldItemSlot(h) => self.on_held_slot(i8::try_from(h.slot).unwrap_or(-1)),
                P::CraftProgressBar(c) => self.on_window_property(c.window_id, c.property, c.value),
                _ => {}
            }
        }

        /// Applies a 26.1 window packet from its raw body ([`PACKET_IDS_26_1`]): slots are read
        /// with a structural skip and keep their components as bytes (nothing is decoded until an
        /// accessor asks). Other ids go through [`Inventory::apply_v26_1`].
        ///
        /// # Errors
        /// A malformed packet (nothing changed).
        pub fn apply_raw_v26_1(&mut self, raw: &RawPacket) -> Result<(), DecodeError> {
            let mut r = Reader::new(&raw.body);
            match raw.id {
                cb::id::WINDOW_ITEMS => {
                    let window = r.read_varint()?;
                    let state = r.read_varint()?;
                    let n = r.read_varint()?;
                    let n = r.check_count(i64::from(n), 1)?;
                    let mut items = Vec::with_capacity(n.min(256));
                    for _ in 0..n {
                        items.push(slot(&mut r)?);
                    }
                    let carried = slot(&mut r)?;
                    self.on_window_items_26_1(window, state, items, carried);
                }
                cb::id::SET_SLOT => {
                    let window = r.read_varint()?;
                    let state = r.read_varint()?;
                    let s = r.read_i16()?;
                    let item = slot(&mut r)?;
                    self.on_set_slot_26_1(window, state, s, item);
                }
                cb::id::SET_CURSOR_ITEM => {
                    let item = slot(&mut r)?;
                    self.on_set_cursor(item);
                }
                cb::id::SET_PLAYER_INVENTORY => {
                    let k = r.read_varint()?;
                    let item = slot(&mut r)?;
                    self.on_set_player_inventory(k, item);
                }
                // decoded as their own type: no whole-protocol decoder is linked
                cb::id::LOGIN => {
                    raw.decode_as::<cb::Login>()?;
                    self.reset();
                }
                cb::id::RESPAWN => {
                    raw.decode_as::<cb::Respawn>()?;
                    self.reset();
                }
                cb::id::OPEN_WINDOW => self.open_window_26_1(&raw.decode_as::<cb::OpenWindow>()?),
                cb::id::CLOSE_WINDOW => {
                    raw.decode_as::<cb::CloseWindow>()?;
                    self.on_close_window();
                }
                cb::id::HELD_ITEM_SLOT => {
                    let h = raw.decode_as::<cb::HeldItemSlot>()?;
                    self.on_held_slot(i8::try_from(h.slot).unwrap_or(-1));
                }
                cb::id::CRAFT_PROGRESS_BAR => {
                    let c = raw.decode_as::<cb::CraftProgressBar>()?;
                    self.on_window_property(c.window_id, c.property, c.value);
                }
                _ => {}
            }
            Ok(())
        }

        fn open_window_26_1(&mut self, w: &cb::OpenWindow) {
            self.on_open_window(
                w.window_id,
                crate::v26_1::window_kind(w.inventory_type),
                ghast_protocol::chat::nbt_to_plain_text(&w.window_title).into(),
            );
        }
    }
}

#[cfg(feature = "v26_1")]
pub use v26_1::PACKET_IDS_26_1;