ghast-inventory 0.5.2

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, 26.1, 26.2 and 26.3)
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
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
//! 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()),
        }
    }
}

/// `PacketByteBuf.writeString`: a VarInt byte length, then the UTF-8 bytes.
#[cfg(any(feature = "v1_8", feature = "v1_12"))]
fn mc_string(s: &str) -> bytes::Bytes {
    let mut out = Vec::with_capacity(s.len() + 5);
    let mut n = s.len() as u32;
    loop {
        let b = (n & 0x7f) as u8;
        n >>= 7;
        if n == 0 {
            out.push(b);
            break;
        }
        out.push(b | 0x80);
    }
    out.extend_from_slice(s.as_bytes());
    bytes::Bytes::from(out)
}

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),
                })
            }
            // the trading screen's arrows: `MC|TrSel` with the offer index as an int
            Outgoing::SelectTrade(i) => {
                sb8::ServerboundPlayPacket::CustomPayload(sb8::CustomPayload {
                    channel: "MC|TrSel".into(),
                    data: bytes::Bytes::copy_from_slice(&i.to_be_bytes()),
                })
            }
            // the anvil screen's name field: `MC|ItemName` with the name as a string
            Outgoing::RenameItem(n) => {
                sb8::ServerboundPlayPacket::CustomPayload(sb8::CustomPayload {
                    channel: "MC|ItemName".into(),
                    data: mc_string(n),
                })
            }
            Outgoing::ContainerClick(_)
            | Outgoing::Creative(_)
            | Outgoing::ButtonClick { .. }
            | Outgoing::Swing => {
                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::SelectTrade(i) => {
                sb12::ServerboundPlayPacket::CustomPayload(sb12::CustomPayload {
                    channel: "MC|TrSel".into(),
                    data: bytes::Bytes::copy_from_slice(&i.to_be_bytes()),
                })
            }
            // the anvil screen's name field: `MC|ItemName` with the name as a string
            Outgoing::RenameItem(n) => {
                sb12::ServerboundPlayPacket::CustomPayload(sb12::CustomPayload {
                    channel: "MC|ItemName".into(),
                    data: mc_string(n),
                })
            }
            Outgoing::OpenInventory
            | Outgoing::ContainerClick(_)
            | Outgoing::Creative(_)
            | Outgoing::ButtonClick { .. }
            | Outgoing::Swing => {
                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::SelectTrade(i) => {
                sb16::ServerboundPlayPacket::SelectTrade(sb16::SelectTrade { slot: *i })
            }
            // `clickButton`: the enchanting table's packet, for every menu button
            Outgoing::ButtonClick { window_id, button } => {
                sb16::ServerboundPlayPacket::EnchantItem(sb16::EnchantItem {
                    window_id: *window_id as i8,
                    enchantment: *button as i8,
                })
            }
            // the anvil screen's name field
            Outgoing::RenameItem(n) => sb16::ServerboundPlayPacket::NameItem(sb16::NameItem {
                name: n.as_str().into(),
            }),
            Outgoing::Swing => {
                sb16::ServerboundPlayPacket::ArmAnimation(sb16::ArmAnimation { hand: 0 })
            }
            Outgoing::OpenInventory | Outgoing::ContainerClick(_) | Outgoing::Creative(_) => {
                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()).map(crate::Stack::detached),
                );
            }
            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::CustomPayload(c) if c.channel == "MC|TrList" => self.on_trade_list_legacy(&c.data),
            // the anvil's level check
            P::Experience(x) => self.set_experience_level(x.level),
            P::Abilities(a) => self.set_creative(a.flags & 0x08 != 0),
            _ => {}
        }
        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()).map(crate::Stack::detached),
                );
            }
            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::CustomPayload(c) if c.channel == "MC|TrList" => self.on_trade_list_legacy(&c.data),
            // the anvil's level check
            P::Experience(x) => self.set_experience_level(x.level),
            P::Abilities(a) => self.set_creative(a.flags & 0x08 != 0),
            _ => {}
        }
        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) => {
                let swing = self.carried_drop_swing();
                self.on_open_window_v1_16_5(w.window_id, w.inventory_type, w.window_title.clone());
                if swing {
                    return Some(Outgoing::Swing);
                }
            }
            P::OpenHorseWindow(w) => {
                let swing = self.carried_drop_swing();
                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(),
                );
                if swing {
                    return Some(Outgoing::Swing);
                }
            }
            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()).map(crate::Stack::detached),
                );
            }
            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)),
            P::TradeList(t) => self.on_trade_list_v1_16_5(t),
            P::Tags(t) => self.on_tags_v1_16_5(t),
            P::Experience(x) => self.set_experience_level(x.level),
            // flag 0x08: instant build (creative)
            P::Abilities(a) => self.set_creative(a.flags & 0x08 != 0),
            _ => {}
        }
        None
    }
}

// ------------------------------------------------------------------ modern (26.x)

/// One instance per modern protocol: the generated packet types differ (registry-mapped
/// layouts are duplicated per protocol), the code does not. Raw window packets are applied
/// here, so the slot reader is a constant of each instance.
#[cfg(modern)]
macro_rules! modern_packets {
    ($m:ident, $proto:ident, $defs:ident, $mv:expr, $ty:path) => {
        modern_packets!($m, $proto, $defs, $mv, $ty, |hand| {
            Some(sb::ServerboundPlayPacket::ArmAnimation(sb::ArmAnimation {
                hand: sb::ArmAnimationHand(hand),
            }))
        });
    };
    ($m:ident, $proto:ident, $defs:ident, $mv:expr, $ty:path, $swing:expr) => {
        pub(crate) mod $m {
            use bytes::BytesMut;
            use ghast_protocol::codec::{Encode, Reader};
            use ghast_protocol::defs::$defs::types as t;
            use ghast_protocol::$proto::play::clientbound::{self as cb, ClientboundPlayPacket};
            use ghast_protocol::$proto::play::serverbound as sb;
            use ghast_protocol::{DecodeError, RawPacket};

            use crate::inventory::{ContainerClick, CreativePacket, Inventory, Outgoing};
            use crate::item26::{HashRegistries, Hashed, hashed};
            use crate::stack::Stack;
            use crate::tables::Mv;

            const MV: Mv = $mv;

            /// Clientbound play packet ids [`apply_raw`] handles.
            pub(crate) const PACKET_IDS: &[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,
                cb::id::DECLARE_RECIPES,
                cb::id::ABILITIES,
                cb::id::EXPERIENCE,
                cb::id::TAGS,
                cb::id::TRADE_LIST,
            ];

            #[inline]
            fn slot(r: &mut Reader<'_>) -> Result<Option<Stack>, DecodeError> {
                Ok(MV.read_stack(r)?.map(Stack::from_v775))
            }

            /// A generated slot as a stack (re-read lazily from its encoding).
            #[allow(dead_code)]
            fn from_generated(s: &t::Slot) -> Option<Stack> {
                let mut w = BytesMut::new();
                s.encode(&mut w).ok()?;
                let b = w.freeze();
                MV.read_stack(&mut Reader::new(&b))
                    .ok()?
                    .map(Stack::from_v775)
            }

            /// A hashed stack as the generated type.
            pub(crate) fn hashed_slot(h: Hashed) -> t::HashedSlot {
                t::HashedSlot {
                    item_id: h.item_id,
                    item_count: h.item_count,
                    components: h
                        .components
                        .into_iter()
                        .map(|(ty, hash)| t::HashedSlotComponents {
                            r#type: $ty(ty),
                            hash,
                        })
                        .collect(),
                    remove_components: h
                        .removed
                        .into_iter()
                        .map(|ty| t::HashedSlotRemoveComponents { r#type: $ty(ty) })
                        .collect(),
                }
            }

            fn hashed_opt(s: Option<&Stack>, reg: &HashRegistries) -> Option<t::HashedSlot> {
                hashed(MV, s, reg).0.map(hashed_slot)
            }

            /// The `container_click` with hashed stacks.
            pub(crate) fn click(c: &ContainerClick, reg: &HashRegistries) -> sb::WindowClick {
                sb::WindowClick {
                    window_id: c.window_id,
                    state_id: c.state_id,
                    slot: c.slot,
                    mouse_button: c.button as i8,
                    mode: i32::from(c.mode),
                    changed_slots: c
                        .changed
                        .iter()
                        .map(|(location, s)| sb::WindowClickChangedSlots {
                            location: *location,
                            item: hashed_opt(s.as_ref(), reg),
                        })
                        .collect(),
                    cursor_item: hashed_opt(c.carried.as_ref(), reg),
                }
            }

            /// `swing` (main hand) or `set_creative_mode_slot` (the stack in the untrusted
            /// layout, components in type id order); `None` for a swing in a version without
            /// a swing packet.
            pub(crate) fn creative(c: &CreativePacket) -> Option<sb::ServerboundPlayPacket> {
                let swing: fn(i32) -> Option<sb::ServerboundPlayPacket> = $swing;
                Some(match c {
                    CreativePacket::Swing => return swing(0),
                    CreativePacket::SetSlot { slot, item } => {
                        let item = match item.as_ref().filter(|s| s.count > 0 && s.id != 0) {
                            None => t::UntrustedSlot {
                                item_count: 0,
                                body: None,
                            },
                            Some(s) => {
                                let mut components: Vec<t::UntrustedSlotComponent> = Vec::new();
                                let mut removed: Vec<i32> = Vec::new();
                                if let Some(p) = s.components() {
                                    for (ty, b) in p.iter() {
                                        match components.iter_mut().find(|c| c.r#type.0 == ty) {
                                            Some(c) => c.data = b,
                                            None => components.push(t::UntrustedSlotComponent {
                                                r#type: $ty(ty),
                                                data: b,
                                            }),
                                        }
                                    }
                                    removed.extend(p.removed());
                                }
                                components.sort_by_key(|c| c.r#type.0);
                                removed.sort_unstable();
                                removed.dedup();
                                t::UntrustedSlot {
                                    item_count: s.count,
                                    body: Some(t::UntrustedSlotBodyDefault {
                                        item_id: s.id,
                                        added_component_count: components.len() as i32,
                                        removed_component_count: removed.len() as i32,
                                        components,
                                        remove_components: removed
                                            .into_iter()
                                            .map(|ty| t::UntrustedSlotBodyDefaultRemoveComponents {
                                                r#type: $ty(ty),
                                            })
                                            .collect(),
                                    }),
                                }
                            }
                        };
                        sb::ServerboundPlayPacket::SetCreativeSlot(sb::SetCreativeSlot {
                            slot: *slot,
                            item,
                        })
                    }
                })
            }

            /// The serverbound packet (`None` for the legacy-only variants and for
            /// [`Outgoing::Creative`], whose packets [`creative`] encodes one by one).
            pub(crate) fn outgoing(
                o: &Outgoing,
                reg: &HashRegistries,
            ) -> Option<sb::ServerboundPlayPacket> {
                Some(match o {
                    Outgoing::ContainerClick(c) => {
                        sb::ServerboundPlayPacket::WindowClick(click(c, reg))
                    }
                    Outgoing::CloseWindow(id) => {
                        sb::ServerboundPlayPacket::CloseWindow(sb::CloseWindow {
                            window_id: i32::from(*id),
                        })
                    }
                    Outgoing::RenameItem(n) => sb::ServerboundPlayPacket::NameItem(sb::NameItem {
                        name: n.as_str().into(),
                    }),
                    Outgoing::ButtonClick { window_id, button } => {
                        sb::ServerboundPlayPacket::EnchantItem(sb::EnchantItem {
                            window_id: *window_id,
                            enchantment: *button,
                        })
                    }
                    Outgoing::SelectTrade(i) => {
                        sb::ServerboundPlayPacket::SelectTrade(sb::SelectTrade { slot: *i })
                    }
                    _ => return None,
                })
            }

            /// A serverbound packet as raw id + body.
            pub(crate) fn raw(p: &sb::ServerboundPlayPacket) -> Option<RawPacket> {
                use ghast_protocol::PacketSet;
                let mut w = BytesMut::new();
                p.encode_body(&mut w).ok()?;
                Some(RawPacket::new(p.id(), w.freeze()))
            }

            /// Applies a typed clientbound play packet.
            #[allow(dead_code)]
            pub(crate) fn apply(inv: &mut Inventory, p: &ClientboundPlayPacket) {
                use ClientboundPlayPacket as P;
                match p {
                    P::Login(_) => inv.reset(),
                    // keep-inventory flags do not matter: a new player entity either way
                    P::Respawn(_) => inv.reset(),
                    P::OpenWindow(w) => open_window(inv, w),
                    P::CloseWindow(_) => inv.on_close_window(),
                    P::WindowItems(w) => inv.on_window_items_modern(
                        w.window_id,
                        w.state_id,
                        w.items.iter().map(from_generated),
                        from_generated(&w.carried_item),
                    ),
                    P::SetSlot(s) => inv.on_set_slot_modern(
                        s.window_id,
                        s.state_id,
                        s.slot,
                        from_generated(&s.item),
                    ),
                    P::SetCursorItem(c) => inv.on_set_cursor(from_generated(&c.contents)),
                    P::SetPlayerInventory(p) => {
                        inv.on_set_player_inventory(p.slot_id, from_generated(&p.contents))
                    }
                    P::HeldItemSlot(h) => inv.on_held_slot(i8::try_from(h.slot).unwrap_or(-1)),
                    P::CraftProgressBar(c) => {
                        inv.on_window_property(c.window_id, c.property, c.value)
                    }
                    P::DeclareRecipes(d) => declare_recipes(inv, d),
                    P::Abilities(a) => inv.set_creative_modern(a.flags & 0x08 != 0),
                    P::Experience(e) => inv.set_experience_level_modern(e.level),
                    _ => {}
                }
            }

            /// Parses the kept `update_recipes` bytes (once; a packet that does not decode
            /// leaves the item sets empty, like a client that got none).
            pub(crate) fn ensure_recipes(inv: &mut Inventory) {
                if let Some(body) = inv.take_recipes_raw_modern()
                    && let Ok(d) = RawPacket::new(cb::id::DECLARE_RECIPES, body)
                        .decode_as::<cb::DeclareRecipes>()
                {
                    declare_recipes(inv, &d);
                }
            }

            fn declare_recipes(inv: &mut Inventory, d: &cb::DeclareRecipes) {
                use ghast_protocol::types::holder::HolderSet;
                let mut inputs = Some(Vec::new());
                for r in &d.stone_cutter_recipes {
                    match (&r.input, &mut inputs) {
                        (HolderSet::Ids(ids), Some(v)) => v.extend_from_slice(ids),
                        _ => inputs = None,
                    }
                }
                inv.on_recipes_modern(
                    d.recipes.iter().map(|r| (&*r.name, r.items.as_slice())),
                    inputs,
                );
            }

            /// Applies a window packet from its raw body ([`PACKET_IDS`]).
            pub(crate) fn apply_raw(
                inv: &mut Inventory,
                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)?;
                        inv.on_window_items_modern(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)?.map(Stack::detached);
                        inv.on_set_slot_modern(window, state, s, item);
                    }
                    cb::id::SET_CURSOR_ITEM => {
                        let item = slot(&mut r)?.map(Stack::detached);
                        inv.on_set_cursor(item);
                    }
                    cb::id::SET_PLAYER_INVENTORY => {
                        let k = r.read_varint()?;
                        let item = slot(&mut r)?.map(Stack::detached);
                        inv.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>()?;
                        inv.reset();
                    }
                    cb::id::RESPAWN => {
                        raw.decode_as::<cb::Respawn>()?;
                        inv.reset();
                    }
                    cb::id::OPEN_WINDOW => open_window(inv, &raw.decode_as::<cb::OpenWindow>()?),
                    cb::id::CLOSE_WINDOW => {
                        raw.decode_as::<cb::CloseWindow>()?;
                        inv.on_close_window();
                    }
                    cb::id::HELD_ITEM_SLOT => {
                        let h = raw.decode_as::<cb::HeldItemSlot>()?;
                        inv.on_held_slot(i8::try_from(h.slot).unwrap_or(-1));
                    }
                    cb::id::CRAFT_PROGRESS_BAR => {
                        let c = raw.decode_as::<cb::CraftProgressBar>()?;
                        inv.on_window_property(c.window_id, c.property, c.value);
                    }
                    // large and sent at every join: kept as bytes, parsed when a menu first
                    // needs its item sets (`ensure_recipes`)
                    cb::id::DECLARE_RECIPES => {
                        inv.set_recipes_raw_modern(ghast_protocol::intern::bytes(&raw.body));
                    }
                    cb::id::TAGS => {
                        inv.on_update_tags_modern(bytes::Bytes::copy_from_slice(&raw.body))
                    }
                    cb::id::TRADE_LIST => inv.on_merchant_offers_modern(&raw.body),
                    cb::id::ABILITIES => {
                        let a = raw.decode_as::<cb::Abilities>()?;
                        inv.set_creative_modern(a.flags & 0x08 != 0);
                    }
                    cb::id::EXPERIENCE => {
                        let e = raw.decode_as::<cb::Experience>()?;
                        inv.set_experience_level_modern(e.level);
                    }
                    _ => {}
                }
                Ok(())
            }

            fn open_window(inv: &mut Inventory, w: &cb::OpenWindow) {
                inv.on_open_screen_modern(
                    w.window_id,
                    w.inventory_type,
                    ghast_protocol::chat::nbt_to_plain_text(&w.window_title).into(),
                );
            }
        }
    };
}

#[cfg(feature = "v26_1")]
modern_packets!(m775, v26_1, p775, Mv::V26_1, t::SlotComponentType);
#[cfg(feature = "v26_2")]
modern_packets!(m776, v26_2, p776, Mv::V26_2, t::SlotComponentType::new);
#[cfg(feature = "v26_3")]
// 26.3: an arm swing sends no packet (`LocalPlayer.swing` is client-side only)
modern_packets!(
    m777,
    v26_3,
    p777,
    Mv::V26_3,
    t::SlotComponentType::new,
    |_| None
);

#[cfg(modern)]
use crate::tables::Mv;

/// Clientbound modern play packet ids the inventory handles, per version (26.3 renumbered
/// the play packets).
#[cfg(modern)]
pub(crate) const fn packet_ids_modern(mv: Mv) -> &'static [i32] {
    match mv {
        #[cfg(feature = "v26_1")]
        Mv::V26_1 => m775::PACKET_IDS,
        #[cfg(feature = "v26_2")]
        Mv::V26_2 => m776::PACKET_IDS,
        #[cfg(feature = "v26_3")]
        Mv::V26_3 => m777::PACKET_IDS,
    }
}

/// Clientbound 26.1 play packet ids [`Inventory::apply_raw_v26_1`] handles.
#[cfg(feature = "v26_1")]
pub const PACKET_IDS_26_1: &[i32] = m775::PACKET_IDS;

#[cfg(feature = "v26_1")]
pub(crate) use m775::hashed_slot as hashed_775;

#[cfg(feature = "v26_1")]
mod v26_1_api {
    use ghast_protocol::v26_1::play::clientbound::ClientboundPlayPacket;
    use ghast_protocol::v26_1::play::serverbound as sb;
    use ghast_protocol::{DecodeError, RawPacket};

    use super::m775;
    use crate::inventory::{ContainerClick, CreativePacket, Inventory, Outgoing};
    use crate::v26_1::HashRegistries;

    impl ContainerClick {
        /// The 26.1 `container_click` with hashed stacks (see [`crate::v26_1::hashed_slot`]).
        #[must_use]
        pub fn to_v26_1(&self, reg: &HashRegistries) -> sb::WindowClick {
            m775::click(self, reg)
        }
    }

    impl CreativePacket {
        /// The 26.1 serverbound packet: `swing` (main hand) or `set_creative_mode_slot` (the
        /// stack in the untrusted layout, components in type id order).
        #[must_use]
        pub fn to_v26_1(&self) -> sb::ServerboundPlayPacket {
            m775::creative(self).unwrap_or_else(|| unreachable!("26.1 has a swing packet"))
        }
    }

    impl Outgoing {
        /// The 26.1 serverbound packet (`None` for the legacy-only variants, which a 26.1
        /// inventory never returns, and for [`Outgoing::Creative`], whose packets
        /// [`CreativePacket::to_v26_1`] encodes one by one).
        #[must_use]
        pub fn to_v26_1(&self, reg: &HashRegistries) -> Option<sb::ServerboundPlayPacket> {
            m775::outgoing(self, reg)
        }
    }

    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_modern())
        }

        /// 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_modern`] to
        /// avoid decoding components at all).
        pub fn apply_v26_1(&mut self, p: &ClientboundPlayPacket) {
            m775::apply(self, p);
        }

        /// The 26.1 name of [`Inventory::apply_raw_modern`] (a 26.1 inventory's packets).
        ///
        /// # Errors
        /// A malformed packet (nothing changed).
        pub fn apply_raw_v26_1(&mut self, raw: &RawPacket) -> Result<(), DecodeError> {
            m775::apply_raw(self, raw)
        }
    }
}

/// The typed 26.2 serverbound packets of the window intents (a client that writes 26.2 packets
/// itself; [`Inventory::encode_modern`] gives them as raw id + body).
#[cfg(feature = "v26_2")]
mod v26_2_api {
    use ghast_protocol::v26_2::play::serverbound as sb;

    use super::m776;
    use crate::inventory::{CreativePacket, Outgoing};
    use crate::modern::HashRegistries;

    impl CreativePacket {
        /// The 26.2 serverbound packet: `swing` (main hand) or `set_creative_mode_slot`.
        #[must_use]
        pub fn to_v26_2(&self) -> sb::ServerboundPlayPacket {
            m776::creative(self).unwrap_or_else(|| unreachable!("26.2 has a swing packet"))
        }
    }

    impl Outgoing {
        /// The 26.2 serverbound packet (`None` for the legacy-only variants and `Creative`).
        #[must_use]
        pub fn to_v26_2(&self, reg: &HashRegistries) -> Option<sb::ServerboundPlayPacket> {
            m776::outgoing(self, reg)
        }
    }
}

/// The typed 26.3 serverbound packets of the window intents (a client that writes 26.3 packets
/// itself; [`Inventory::encode_modern`] gives them as raw id + body).
#[cfg(feature = "v26_3")]
mod v26_3_api {
    use ghast_protocol::v26_3::play::serverbound as sb;

    use super::m777;
    use crate::inventory::{CreativePacket, Outgoing};
    use crate::modern::HashRegistries;

    impl CreativePacket {
        /// The 26.3 serverbound packet: `set_creative_mode_slot` (`None` for
        /// [`CreativePacket::Swing`]: 26.3 has no swing packet, and a 26.3 inventory never
        /// returns one).
        #[must_use]
        pub fn to_v26_3(&self) -> Option<sb::ServerboundPlayPacket> {
            m777::creative(self)
        }
    }

    impl Outgoing {
        /// The 26.3 serverbound packet (`None` for the legacy-only variants and `Creative`).
        #[must_use]
        pub fn to_v26_3(&self, reg: &HashRegistries) -> Option<sb::ServerboundPlayPacket> {
            m777::outgoing(self, reg)
        }
    }
}

#[cfg(modern)]
impl Inventory {
    /// Applies a modern window packet from its raw body (ids: [`crate::modern::packet_ids`]):
    /// slots are read with a structural skip and keep their components as bytes (nothing is
    /// decoded until an accessor asks). Dispatches on the inventory's version; does nothing
    /// for a legacy inventory.
    ///
    /// # Errors
    /// A malformed packet (nothing changed).
    pub fn apply_raw_modern(
        &mut self,
        raw: &ghast_protocol::RawPacket,
    ) -> Result<(), ghast_protocol::DecodeError> {
        match Mv::of(self.version()) {
            #[cfg(feature = "v26_1")]
            Some(Mv::V26_1) => m775::apply_raw(self, raw),
            #[cfg(feature = "v26_2")]
            Some(Mv::V26_2) => m776::apply_raw(self, raw),
            #[cfg(feature = "v26_3")]
            Some(Mv::V26_3) => m777::apply_raw(self, raw),
            None => Ok(()),
        }
    }

    /// The serverbound packet of an [`Outgoing`] as raw id + body, for the inventory's version
    /// (`None` for a legacy inventory, the legacy-only variants and [`Outgoing::Creative`],
    /// whose packets [`Inventory::encode_creative_modern`] encodes one by one).
    #[must_use]
    pub fn encode_modern(&self, out: &Outgoing) -> Option<ghast_protocol::RawPacket> {
        match Mv::of(self.version())? {
            #[cfg(feature = "v26_1")]
            Mv::V26_1 => m775::raw(&m775::outgoing(out, self.registries_modern())?),
            #[cfg(feature = "v26_2")]
            Mv::V26_2 => m776::raw(&m776::outgoing(out, self.registries_modern())?),
            #[cfg(feature = "v26_3")]
            Mv::V26_3 => m777::raw(&m777::outgoing(out, self.registries_modern())?),
        }
    }

    /// One packet of [`Outgoing::Creative`] as raw id + body for the inventory's version:
    /// `swing` (main hand) or `set_creative_mode_slot` (`None` for a legacy inventory).
    #[must_use]
    pub fn encode_creative_modern(
        &self,
        p: &crate::CreativePacket,
    ) -> Option<ghast_protocol::RawPacket> {
        match Mv::of(self.version())? {
            #[cfg(feature = "v26_1")]
            Mv::V26_1 => m775::raw(&m775::creative(p)?),
            #[cfg(feature = "v26_2")]
            Mv::V26_2 => m776::raw(&m776::creative(p)?),
            #[cfg(feature = "v26_3")]
            Mv::V26_3 => m777::raw(&m777::creative(p)?),
        }
    }

    /// Parses the kept `update_recipes` bytes (see the modern packet instances).
    pub(crate) fn ensure_recipes_modern(&mut self) {
        match Mv::of(self.version()) {
            #[cfg(feature = "v26_1")]
            Some(Mv::V26_1) => m775::ensure_recipes(self),
            #[cfg(feature = "v26_2")]
            Some(Mv::V26_2) => m776::ensure_recipes(self),
            #[cfg(feature = "v26_3")]
            Some(Mv::V26_3) => m777::ensure_recipes(self),
            None => {}
        }
    }
}