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
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
//! 26.1 (protocol 775) items: default components of every item (Mojang's `--reports` component
//! dump, see `examples/gen_items_26_1.rs`), readers for the components the click rules and the
//! item accessors use, the window types, and the hashed stacks a 26.1 client sends with every
//! click (vanilla `HashedStack` / `HashedPatchMap` over `HashOps.CRC32C_INSTANCE`).

use bytes::Bytes;
use ghast_protocol::codec::Reader;
use ghast_protocol::nbt::{AnonNbt, Tag};

use crate::stack::{ItemData, Stack};
use crate::window::WindowKind;

#[path = "v26_1_hash.rs"]
mod hash;
pub use hash::{HashRegistries, component_hash, hash_covers};

mod generated {
    use super::{Equip, ItemDefaults};
    include!("generated/items_26_1.rs");
}

pub use generated::{
    ATTRIBUTES, COMPONENT_NAMES, ITEM_COUNT, MENUS, POTIONS, SOUND_EVENTS, VILLAGER_TYPES,
    component,
};

/// The equipment slot of an item's `equippable` component.
#[derive(Debug, Clone, Copy, PartialEq, Eq, Hash)]
#[non_exhaustive]
pub enum Equip {
    /// Not equippable.
    None,
    /// Head.
    Head,
    /// Chest.
    Chest,
    /// Legs.
    Legs,
    /// Feet.
    Feet,
    /// Offhand (the shield).
    Offhand,
    /// Main hand.
    MainHand,
    /// Animal body armor.
    Body,
    /// Saddle.
    Saddle,
}

impl Equip {
    /// The player armor slot index (0 head … 3 feet) of a humanoid armor slot.
    #[must_use]
    pub fn armor_index(self) -> Option<u8> {
        match self {
            Equip::Head => Some(0),
            Equip::Chest => Some(1),
            Equip::Legs => Some(2),
            Equip::Feet => Some(3),
            _ => None,
        }
    }
}

/// The default components of one item that ghast reads (from Mojang's report).
#[derive(Debug, Clone, Copy, PartialEq, Eq)]
#[non_exhaustive]
pub struct ItemDefaults {
    /// Registry name without `minecraft:`.
    pub name: &'static str,
    /// `max_stack_size`.
    pub max_stack_size: u8,
    /// `max_damage` (0 = absent).
    pub max_damage: u16,
    /// Has a default `damage` component.
    pub damage: bool,
    /// Has a default `unbreakable` component.
    pub unbreakable: bool,
    /// `equippable.slot`.
    pub equip: Equip,
    /// `equippable.allowed_entities` is set (not wearable by players).
    pub equip_restricted: bool,
    /// Has `bundle_contents` (a bundle: clicks with it run the bundle's own rules).
    pub bundle: bool,
    /// `rarity` (0 common, 1 uncommon, 2 rare, 3 epic).
    pub rarity: u8,
}

/// Default components of item `id`.
#[must_use]
pub fn defaults(id: i32) -> Option<&'static ItemDefaults> {
    usize::try_from(id)
        .ok()
        .and_then(|i| generated::ITEMS.get(i))
}

/// Item registry id of a name (`"diamond_sword"` or `"minecraft:diamond_sword"`).
#[must_use]
pub fn item_id(name: &str) -> Option<i32> {
    let name = name.strip_prefix("minecraft:").unwrap_or(name);
    generated::ITEMS
        .iter()
        .position(|d| d.name == name)
        .map(|i| i as i32)
}

/// Component type id of a name (`"damage"` or `"minecraft:damage"`).
#[must_use]
pub fn component_id(name: &str) -> Option<i32> {
    let name = name.strip_prefix("minecraft:").unwrap_or(name);
    COMPONENT_NAMES
        .iter()
        .position(|n| *n == name)
        .map(|i| i as i32)
}

/// The window type of a 26.1 `open_screen` (`minecraft:menu` registry id). The crafter is
/// `None`: its result slot follows the player inventory, a layout ghast does not model.
#[must_use]
pub fn window_kind(menu: i32) -> Option<WindowKind> {
    let name = MENUS.get(usize::try_from(menu).ok()?)?;
    Some(match *name {
        "generic_9x1" => WindowKind::Chest { rows: 1 },
        "generic_9x2" => WindowKind::Chest { rows: 2 },
        "generic_9x3" => WindowKind::Chest { rows: 3 },
        "generic_9x4" => WindowKind::Chest { rows: 4 },
        "generic_9x5" => WindowKind::Chest { rows: 5 },
        "generic_9x6" => WindowKind::Chest { rows: 6 },
        "generic_3x3" => WindowKind::Generic3x3,
        "anvil" => WindowKind::Anvil,
        "beacon" => WindowKind::Beacon,
        "blast_furnace" | "furnace" | "smoker" => WindowKind::Furnace,
        "brewing_stand" => WindowKind::BrewingStand,
        "crafting" => WindowKind::Crafting,
        "enchantment" => WindowKind::Enchanting,
        "grindstone" => WindowKind::Grindstone,
        "hopper" => WindowKind::Hopper,
        "lectern" => WindowKind::Lectern,
        "loom" => WindowKind::Loom,
        "merchant" => WindowKind::Merchant,
        "shulker_box" => WindowKind::ShulkerBox,
        "smithing" => WindowKind::Smithing,
        "cartography_table" => WindowKind::Cartography,
        "stonecutter" => WindowKind::Stonecutter,
        _ => return None,
    })
}

// ---------------------------------------------------------------------- component values

#[cfg(test)]
fn patch(s: &Stack) -> Option<&ghast_protocol::types::slot::v775::ComponentPatch> {
    s.components()
}

/// The patch's verdict on component `ty`: `Some(Some(bytes))` set, `Some(None)` removed, `None`
/// not in the patch (the item's default applies).
pub(crate) fn patched(s: &Stack, ty: i32) -> Option<Option<Bytes>> {
    let ItemData::Components(p) = &s.data else {
        return None;
    };
    if let Some(b) = p.get(ty) {
        return Some(Some(b));
    }
    p.removes(ty).then_some(None)
}

fn varint(b: &Bytes) -> Option<i32> {
    Reader::new(b).read_varint().ok()
}

fn int_component(s: &Stack, ty: i32, default: Option<i32>) -> Option<i32> {
    match patched(s, ty) {
        Some(Some(b)) => varint(&b),
        Some(None) => None,
        None => default,
    }
}

fn has_component(s: &Stack, ty: i32, default: bool) -> bool {
    match patched(s, ty) {
        Some(v) => v.is_some(),
        None => default,
    }
}

/// `ItemStack.getMaxStackSize()`: the `max_stack_size` component (default 1 when absent).
pub(crate) fn max_stack_size(s: &Stack) -> i32 {
    let d = defaults(s.id).map(|d| i32::from(d.max_stack_size));
    int_component(s, component::MAX_STACK_SIZE, d).unwrap_or(1)
}

/// `max_damage` (`None` when absent).
pub(crate) fn max_damage(s: &Stack) -> Option<i32> {
    let d = defaults(s.id).and_then(|d| (d.max_damage > 0).then_some(i32::from(d.max_damage)));
    int_component(s, component::MAX_DAMAGE, d)
}

/// `ItemStack.isDamageableItem()`: `max_damage` and `damage` present, `unbreakable` absent.
pub(crate) fn is_damageable(s: &Stack) -> bool {
    let d = defaults(s.id);
    max_damage(s).is_some()
        && !unbreakable(s)
        && has_component(s, component::DAMAGE, d.is_some_and(|d| d.damage))
}

/// `ItemStack.getDamageValue()`: `damage` clamped to `0..=max_damage`.
pub(crate) fn damage_value(s: &Stack) -> i32 {
    let d = defaults(s.id).and_then(|d| d.damage.then_some(0));
    let v = int_component(s, component::DAMAGE, d).unwrap_or(0);
    v.clamp(0, max_damage(s).unwrap_or(0))
}

/// `unbreakable` present.
pub(crate) fn unbreakable(s: &Stack) -> bool {
    has_component(
        s,
        component::UNBREAKABLE,
        defaults(s.id).is_some_and(|d| d.unbreakable),
    )
}

/// The equipment slot of `equippable`: the item default, or the patched value's slot
/// (`Err` when a patch sets `allowed_entities`: whether a player may wear it is not modelled).
pub(crate) fn equip(s: &Stack) -> Result<Equip, ()> {
    match patched(s, component::EQUIPPABLE) {
        Some(Some(b)) => patched_equip(&b).ok_or(()),
        Some(None) => Ok(Equip::None),
        None => Ok(defaults(s.id).map_or(Equip::None, |d| {
            if d.equip_restricted {
                Equip::None
            } else {
                d.equip
            }
        })),
    }
}

/// The slot of a patched `equippable` (`Equippable.STREAM_CODEC`: slot, equip sound, asset id,
/// camera overlay, allowed entities, ...); `None` when `allowed_entities` is set or the bytes
/// do not read.
fn patched_equip(b: &Bytes) -> Option<Equip> {
    let mut r = Reader::new(b);
    let slot = r.read_varint().ok()?;
    // equip_sound: a registry id + 1, or 0 and an inline `SoundEvent` (id, optional range)
    if r.read_varint().ok()? == 0 {
        r.read_string().ok()?;
        if r.read_bool().ok()? {
            r.read_f32().ok()?;
        }
    }
    for _ in 0..2 {
        // asset_id, camera_overlay
        if r.read_bool().ok()? {
            r.read_string().ok()?;
        }
    }
    if r.read_bool().ok()? {
        return None;
    }
    Some(match slot {
        0 => Equip::MainHand,
        1 => Equip::Feet,
        2 => Equip::Legs,
        3 => Equip::Chest,
        4 => Equip::Head,
        5 => Equip::Offhand,
        6 => Equip::Body,
        7 => Equip::Saddle,
        _ => return None,
    })
}

/// Whether the stack has the `bundle_contents` component (a bundle: some clicks with it run
/// the bundle's own rules, `BundleItem.overrideStackedOnOther` / `overrideOtherStackedOnMe`).
pub(crate) fn overrides_clicks(s: &Stack) -> bool {
    has_component(
        s,
        component::BUNDLE_CONTENTS,
        defaults(s.id).is_some_and(|d| d.bundle),
    )
}

/// `(enchantment registry id, level)` of the `enchantments` (`stored = false`) or
/// `stored_enchantments` component, in wire order. `None` when the component is absent.
pub(crate) fn enchantments(s: &Stack, stored: bool) -> Option<Vec<(i32, i32)>> {
    let ty = if stored {
        component::STORED_ENCHANTMENTS
    } else {
        component::ENCHANTMENTS
    };
    let b = patched(s, ty)??;
    let mut r = Reader::new(&b);
    let n = r.read_varint().ok()?;
    let n = usize::try_from(n).ok()?;
    let mut v = Vec::with_capacity(n.min(64));
    for _ in 0..n {
        v.push((r.read_varint().ok()?, r.read_varint().ok()?));
    }
    Some(v)
}

/// A text component (`custom_name`, `item_name`) as NBT.
pub(crate) fn text(s: &Stack, ty: i32) -> Option<AnonNbt> {
    let b = patched(s, ty)??;
    AnonNbt::read(&mut Reader::new(&b)).ok()
}

/// The `lore` lines as NBT text components.
pub(crate) fn lore(s: &Stack) -> Vec<AnonNbt> {
    let Some(Some(b)) = patched(s, component::LORE) else {
        return Vec::new();
    };
    let mut r = Reader::new(&b);
    let Ok(n) = r.read_varint() else {
        return Vec::new();
    };
    (0..n.max(0))
        .map_while(|_| AnonNbt::read(&mut r).ok())
        .collect()
}

/// The `potion_contents` potion (registry name), if it names one.
pub(crate) fn potion(s: &Stack) -> Option<&'static str> {
    let b = patched(s, component::POTION_CONTENTS)??;
    let mut r = Reader::new(&b);
    if !r.read_bool().ok()? {
        return None;
    }
    let id = r.read_varint().ok()?;
    POTIONS.get(usize::try_from(id).ok()?).copied()
}

// ---------------------------------------------------------------------- hashed stacks

/// Hash put into a click for a component ghast cannot hash: certainly not the real one, so the
/// server answers with the slot's true contents (the click itself is applied either way).
pub const UNKNOWN_HASH: i32 = 0;

/// Guava `Hashing.crc32c()`.
struct Crc32c(u32);

const fn crc_table() -> [u32; 256] {
    let mut t = [0u32; 256];
    let mut i = 0;
    while i < 256 {
        let mut c = i as u32;
        let mut k = 0;
        while k < 8 {
            c = if c & 1 != 0 {
                (c >> 1) ^ 0x82F6_3B78
            } else {
                c >> 1
            };
            k += 1;
        }
        t[i] = c;
        i += 1;
    }
    t
}

static CRC_TABLE: [u32; 256] = crc_table();

impl Crc32c {
    fn new() -> Self {
        Crc32c(!0)
    }
    fn put(&mut self, b: &[u8]) -> &mut Self {
        let mut c = self.0;
        for &x in b {
            c = CRC_TABLE[((c ^ u32::from(x)) & 0xFF) as usize] ^ (c >> 8);
        }
        self.0 = c;
        self
    }
    fn byte(&mut self, b: u8) -> &mut Self {
        self.put(&[b])
    }
    fn finish(&self) -> u32 {
        !self.0
    }
}

fn hash_of(bytes: &[u8]) -> u32 {
    Crc32c::new().put(bytes).finish()
}

/// `HashOps` values (Guava hashers write little-endian).
mod ops {
    use super::{Crc32c, hash_of};

    pub fn int(v: i32) -> u32 {
        Crc32c::new().byte(8).put(&v.to_le_bytes()).finish()
    }
    pub fn byte(v: i8) -> u32 {
        Crc32c::new().byte(6).byte(v as u8).finish()
    }
    pub fn short(v: i16) -> u32 {
        Crc32c::new().byte(7).put(&v.to_le_bytes()).finish()
    }
    pub fn long(v: i64) -> u32 {
        Crc32c::new().byte(9).put(&v.to_le_bytes()).finish()
    }
    pub fn float(v: f32) -> u32 {
        Crc32c::new()
            .byte(10)
            .put(&v.to_bits().to_le_bytes())
            .finish()
    }
    pub fn double(v: f64) -> u32 {
        Crc32c::new()
            .byte(11)
            .put(&v.to_bits().to_le_bytes())
            .finish()
    }
    pub fn string(s: &str) -> u32 {
        let units: Vec<u16> = s.encode_utf16().collect();
        let mut h = Crc32c::new();
        h.byte(12).put(&(units.len() as i32).to_le_bytes());
        for u in units {
            h.put(&u.to_le_bytes());
        }
        h.finish()
    }
    pub fn boolean(b: bool) -> u32 {
        hash_of(&[13, u8::from(b)])
    }
    pub fn empty_map() -> u32 {
        hash_of(&[2, 3])
    }
    /// Entries sorted by key hash, then value hash (unsigned), as `HashOps.hashMap` does.
    pub fn map(mut entries: Vec<(u32, u32)>) -> u32 {
        entries.sort_unstable();
        let mut h = Crc32c::new();
        h.byte(2);
        for (k, v) in entries {
            h.put(&k.to_le_bytes()).put(&v.to_le_bytes());
        }
        h.byte(3).finish()
    }
    pub fn list(items: impl IntoIterator<Item = u32>) -> u32 {
        let mut h = Crc32c::new();
        h.byte(4);
        for v in items {
            h.put(&v.to_le_bytes());
        }
        h.byte(5).finish()
    }
    pub fn byte_list(b: &[u8]) -> u32 {
        Crc32c::new().byte(14).put(b).byte(15).finish()
    }
    pub fn int_list(v: &[i32]) -> u32 {
        let mut h = Crc32c::new();
        h.byte(16);
        for x in v {
            h.put(&x.to_le_bytes());
        }
        h.byte(17).finish()
    }
    pub fn long_list(v: &[i64]) -> u32 {
        let mut h = Crc32c::new();
        h.byte(18);
        for x in v {
            h.put(&x.to_le_bytes());
        }
        h.byte(19).finish()
    }
}

/// NBT converted into `HashOps` (vanilla `NbtOps.convertTo`); `text = true` reads the byte
/// fields a text component's codec writes as booleans (`bold`, `italic`, …) as booleans, which
/// is what encoding the component itself to `HashOps` produces.
fn nbt_hash(t: &Tag, text: bool) -> u32 {
    const BOOL_KEYS: &[&str] = &[
        "bold",
        "italic",
        "underlined",
        "strikethrough",
        "obfuscated",
        "interpret",
    ];
    match t {
        Tag::Byte(v) => ops::byte(*v),
        Tag::Short(v) => ops::short(*v),
        Tag::Int(v) => ops::int(*v),
        Tag::Long(v) => ops::long(*v),
        Tag::Float(v) => ops::float(*v),
        Tag::Double(v) => ops::double(*v),
        Tag::String(s) => ops::string(s),
        Tag::ByteArray(b) => ops::byte_list(b),
        Tag::IntArray(v) => ops::int_list(v),
        Tag::LongArray(v) => ops::long_list(v),
        Tag::List(l) => ops::list(l.items.iter().map(|e| match e {
            // ListTag unwraps the `{"": value}` wrappers of mixed lists when it is read.
            Tag::Compound(c) if c.0.len() == 1 && c.0[0].0.is_empty() => nbt_hash(&c.0[0].1, text),
            e => nbt_hash(e, text),
        })),
        Tag::Compound(c) => {
            let mut seen: Vec<&str> = Vec::new();
            let mut entries = Vec::with_capacity(c.0.len());
            // Java maps keep the last duplicate.
            for (k, v) in c.0.iter().rev() {
                if seen.contains(&k.as_str()) {
                    continue;
                }
                seen.push(k);
                let vh = match v {
                    Tag::Byte(b)
                        if text && BOOL_KEYS.contains(&k.as_str()) && (*b == 0 || *b == 1) =>
                    {
                        ops::boolean(*b != 0)
                    }
                    v => nbt_hash(v, text),
                };
                entries.push((ops::string(k), vh));
            }
            ops::map(entries)
        }
    }
}

/// `TypedEntityData.codec`: the compound without its `id`, then `id` = the type's name.
fn typed_nbt_hash(n: &AnonNbt, type_name: &str) -> Option<u32> {
    let Tag::Compound(mut c) = n.parse().ok()?? else {
        return None;
    };
    c.0.retain(|(k, _)| k != "id");
    c.0.push(("id".into(), Tag::String(type_name.into())));
    Some(nbt_hash(&Tag::Compound(c), false))
}

fn anon_hash(n: &AnonNbt, text: bool) -> Option<u32> {
    if n.is_end() {
        return None;
    }
    Some(nbt_hash(&n.parse().ok()??, text))
}

/// One slot as a 26.1 click sends it (vanilla `HashedStack.create`), with
/// [`UNKNOWN_HASH`] for components ghast cannot hash. The second value is `false` when such a
/// component was present.
///
/// Every component kind a client can receive is hashed (checked against the real client's
/// hashes of 4000 stacks in `tests/golden/26.1/hashed_stacks.jsonl.gz`). What still falls back
/// to [`UNKNOWN_HASH`]: a registry name the server did not send in configuration, an inline
/// holder value without a modelled direct form (trim materials/patterns, banner patterns,
/// instruments, jukebox songs, painting variants; inline sound events are modelled), and an
/// adventure-mode block predicate with an `nbt` test or partial component predicates.
#[must_use]
pub fn hashed_slot(
    s: Option<&Stack>,
    reg: &HashRegistries,
) -> (Option<ghast_protocol::defs::p775::types::HashedSlot>, bool) {
    use ghast_protocol::defs::p775::types::{
        HashedSlot, HashedSlotComponents, HashedSlotRemoveComponents, SlotComponentType,
    };
    let Some(s) = s.filter(|s| s.count > 0 && s.id != 0) else {
        return (None, true);
    };
    let mut exact = true;
    let mut components = Vec::new();
    let mut removed = Vec::new();
    if let ItemData::Components(p) = &s.data {
        // DataComponentPatch is a map: the last value of a type wins.
        let mut seen: Vec<i32> = Vec::new();
        let all: Vec<(i32, Bytes)> = p.iter().collect();
        for (t, b) in all.iter().rev() {
            if seen.contains(t) {
                continue;
            }
            seen.push(*t);
            let h = component_hash(*t, b, reg).unwrap_or_else(|| {
                exact = false;
                UNKNOWN_HASH
            });
            components.push(HashedSlotComponents {
                r#type: SlotComponentType(*t),
                hash: h,
            });
        }
        for t in p.removed() {
            if !removed
                .iter()
                .any(|r: &HashedSlotRemoveComponents| r.r#type.0 == t)
            {
                removed.push(HashedSlotRemoveComponents {
                    r#type: SlotComponentType(t),
                });
            }
        }
    }
    (
        Some(HashedSlot {
            item_id: s.id,
            item_count: s.count,
            components,
            remove_components: removed,
        }),
        exact,
    )
}

#[cfg(test)]
mod tests {
    use super::*;
    use ghast_protocol::types::slot::v775::ComponentPatch;

    /// Known answers computed with the 26.1 jar's `HashOps.CRC32C_INSTANCE` (Guava crc32c):
    /// `createInt(5)`, `createString("minecraft:sharpness")`, `emptyMap()`,
    /// `createBoolean(true)`, `createMap({"a": 1})`, `createList([int 1, int 2])`.
    #[test]
    fn hash_ops_known_answers() {
        // crc32c("123456789") = 0xE3069283 (the standard check value).
        assert_eq!(hash_of(b"123456789"), 0xE306_9283);
        for (got, want) in KNOWN.iter() {
            assert_eq!(got(), *want);
        }
    }

    type Known = (fn() -> u32, u32);
    const KNOWN: &[Known] = &[
        (|| ops::int(5), 645_064_431),
        (
            || ops::string("minecraft:sharpness"),
            -1_560_913_390i32 as u32,
        ),
        (ops::empty_map, -982_207_288i32 as u32),
        (|| ops::boolean(true), -1_019_818_302i32 as u32),
        (
            || ops::map(vec![(ops::string("a"), ops::int(1))]),
            -108_872_706i32 as u32,
        ),
        (
            || ops::list([ops::int(1), ops::int(2)]),
            -1_373_960_668i32 as u32,
        ),
        (|| ops::byte(-3), -722_991_802i32 as u32),
        (|| ops::double(1.5), -1_387_852_210i32 as u32),
        (
            || {
                ops::map(vec![
                    (ops::string("minecraft:sharpness"), ops::int(5)),
                    (ops::string("minecraft:unbreaking"), ops::int(3)),
                ])
            },
            1_231_404_551,
        ),
    ];

    fn unhex(s: &str) -> Bytes {
        Bytes::from(
            (0..s.len())
                .step_by(2)
                .map(|i| u8::from_str_radix(&s[i..i + 2], 16).unwrap())
                .collect::<Vec<u8>>(),
        )
    }

    /// Text components: the jar's `ComponentSerialization.CODEC` encoded once to `HashOps`
    /// (the hash) and once to `NbtOps` (the bytes a server sends); ghast hashes the NBT.
    #[test]
    fn text_hashes_match_the_codec() {
        let cases: &[(&str, i32, &str)] = &[
            ("plain", 815_466_656, "08000548656c6c6f"),
            (
                "bold",
                -608_560_159,
                "0a080005636f6c6f720004676f6c6408000474657874000453686f70010004626f6c640100",
            ),
            (
                "notitalic",
                185_662_785,
                "0a080005636f6c6f720004677261790800047465787400044c696e650100066974616c69630000",
            ),
            (
                "mixed",
                -198_755_598,
                "0a09000565787472610a0000000208000000016200080005636f6c6f72000372656408000474657874000163000800047465787400016100",
            ),
            (
                "translate",
                1_855_830_573,
                "0a0800097472616e736c617465001c6974656d2e6d696e6563726166742e6469616d6f6e645f73776f726400",
            ),
            ("unicode", -45_199_928, "08000ac39cc3a9eda0bdedb880"),
        ];
        for (label, hash, hex) in cases {
            let nbt = AnonNbt::from_bytes(unhex(hex)).unwrap();
            assert_eq!(
                anon_hash(&nbt, true).map(|h| h as i32),
                Some(*hash),
                "{label}"
            );
            // the component value on the wire is the NBT itself
            let v = nbt.as_bytes().clone();
            let reg = HashRegistries::default();
            assert_eq!(
                component_hash(component::CUSTOM_NAME, &v, &reg),
                Some(*hash),
                "{label}"
            );
        }
    }

    #[test]
    fn defaults_table() {
        assert_eq!(ITEM_COUNT, 1506);
        let sword = item_id("diamond_sword").unwrap();
        let st = Stack::new(sword, 1, 0);
        assert_eq!(max_stack_size(&st), 1);
        assert_eq!(max_damage(&st), Some(1561));
        assert!(is_damageable(&st));
        assert_eq!(
            equip(&Stack::new(item_id("elytra").unwrap(), 1, 0)),
            Ok(Equip::Chest)
        );
        assert_eq!(
            equip(&Stack::new(item_id("shield").unwrap(), 1, 0)),
            Ok(Equip::Offhand)
        );
        assert_eq!(
            equip(&Stack::new(item_id("diamond_horse_armor").unwrap(), 1, 0)),
            Ok(Equip::None)
        );
        assert!(overrides_clicks(&Stack::new(
            item_id("red_bundle").unwrap(),
            1,
            0
        )));
        assert_eq!(
            max_stack_size(&Stack::new(item_id("ender_pearl").unwrap(), 1, 0)),
            16
        );
        assert_eq!(component_id("damage"), Some(component::DAMAGE));
        assert_eq!(window_kind(2), Some(WindowKind::Chest { rows: 3 }));
        assert_eq!(window_kind(7), None); // crafter
        assert_eq!(window_kind(20), Some(WindowKind::ShulkerBox));
    }

    #[test]
    fn patched_components() {
        let sword = item_id("diamond_sword").unwrap();
        let mut st = Stack::new(sword, 1, 0);
        // damage 37, max_stack_size 4 (wire: varints), unbreakable removed? no: added
        let p = ComponentPatch::from_parts(
            &[
                (component::DAMAGE, &[37]),
                (component::MAX_STACK_SIZE, &[4]),
            ],
            &[component::MAX_DAMAGE],
        );
        st.data = ItemData::Components(Box::new(p));
        assert_eq!(patch(&st).unwrap().added_len(), 2);
        assert_eq!(max_stack_size(&st), 4);
        assert_eq!(max_damage(&st), None);
        assert!(!is_damageable(&st));
        assert_eq!(damage_value(&st), 0); // clamped to max_damage 0
        let (h, exact) = hashed_slot(Some(&st), &HashRegistries::default());
        let h = h.unwrap();
        assert!(exact);
        assert_eq!(h.components.len(), 2);
        assert_eq!(h.remove_components.len(), 1);
        assert_eq!(
            h.components
                .iter()
                .find(|c| c.r#type.0 == component::DAMAGE)
                .unwrap()
                .hash,
            ops::int(37) as i32
        );
    }
}