ghast_inventory/lib.rs
1//! Inventory and container windows for the ghast Minecraft Java Edition client library
2//! (1.8.9, 1.12.2, 1.16.5, 26.1, 26.2 and 26.3).
3//!
4//! [`Inventory`] holds what the vanilla client holds — the player inventory, the cursor stack
5//! and the open window — fed by the server's window packets, and turns click intents into
6//! `window_click` packets with **vanilla's client-side prediction**: the new slot contents, the
7//! "clicked item" field (what vanilla's `onSlotClick` returns; the server rejects the click when
8//! its own result differs) and the per-window action number.
9//!
10//! - **One click in flight.** A click stays pending until the server's confirm (`transaction`)
11//! with its action number arrives. A rejected click makes the server resend the window;
12//! clicks wait for that resync. [`Inventory::on_transaction`] also produces vanilla's
13//! answer to rejected confirms — exactly once per confirm (route every confirm through it).
14//! - **Exact or refused.** Clicks are simulated on a scratch copy; when the outcome depends on
15//! data this crate does not model (recipes, furnace fuel, brewing, trading, …) the click is
16//! refused with [`ClickError::Unpredictable`] instead of guessing, so the client state never
17//! silently diverges from the server's (after an accepted click the server sends no
18//! corrections — it assumes the client predicted right).
19//! - **Lazy NBT.** Stacks keep their NBT as raw bytes ([`ghast_protocol::nbt::RawNbt`]);
20//! `window_items` is applied without parsing any NBT. Stack comparisons compare the bytes
21//! and only parse when they differ (vanilla compares trees).
22//! - **26.1 / 26.2 / 26.3** have no confirms: the server applies every click and answers only the slots
23//! whose hash (vanilla `HashedStack`) differs from its own result. A modern click is never refused:
24//! one ghast cannot predict is sent unpredicted ([`ContainerClick::predicted`]) and the
25//! server's corrections bring the result. Data components stay raw too
26//! ([`ItemData::Components`]); `window_items` is applied with a structural skip
27//! ([`Inventory::apply_raw_modern`]). Item facts come from Mojang's reports ([`modern`]; one
28//! implementation over per-version tables).
29//! - **Item accessors** ([`Stack::item_name`], [`Stack::max_stack_size`],
30//! [`Stack::damage_value`], [`Stack::custom_name`], [`Stack::enchantments`], …) answer from NBT
31//! or components, decoding only what they read.
32//! - **Pure.** No I/O, no clock, no async: packets in, packets out. The client writes the
33//! returned packets immediately, in call order, before the tick's movement packet, and calls
34//! [`Inventory::tick`] once per tick.
35//!
36//! ```
37//! use ghast_inventory::{Click, Inventory, Outgoing, Stack, Version};
38//!
39//! let mut inv = Inventory::new(Version::V1_8).unwrap();
40//! // The server sends window 0 (45 slots): 64 stone in hotbar slot 0 (window slot 36).
41//! let mut items = vec![None; 45];
42//! items[36] = Some(Stack::new(1, 64, 0));
43//! inv.on_window_items(0, items);
44//!
45//! inv.tick();
46//! assert_eq!(inv.open_inventory(), Ok(Some(Outgoing::OpenInventory))); // 1.8: C16
47//! inv.tick();
48//! let Ok(Outgoing::Click(c)) = inv.click(Click::Pickup { slot: 36, right: true }) else { panic!() };
49//! assert_eq!((c.action, c.item.as_ref().unwrap().count()), (1, 64)); // carries the slot before the click
50//! assert_eq!(inv.cursor().unwrap().count(), 32);
51//! assert_eq!(inv.window0_slot(36).unwrap().count(), 32);
52//! assert!(inv.pending());
53//! assert_eq!(inv.on_transaction(0, 1, true), None); // accepted: nothing to answer
54//! assert!(!inv.pending());
55//! ```
56
57#![forbid(unsafe_code)]
58#![warn(missing_docs, unnameable_types)]
59
60mod anvil12;
61mod anvil16;
62#[cfg(any(feature = "v1_8", feature = "v1_12", feature = "v1_16_5"))]
63mod attack;
64mod crafting;
65#[cfg(modern)]
66mod data26;
67#[cfg(modern)]
68mod ench_value;
69mod furnace;
70mod inventory;
71mod item;
72#[cfg(modern)]
73mod item26;
74mod menus;
75mod merchant;
76#[cfg(modern)]
77pub mod modern;
78mod mount;
79mod nbt16;
80mod packets;
81mod rules;
82mod sim;
83#[cfg(modern)]
84mod sim26;
85mod stack;
86mod station;
87#[cfg(modern)]
88mod tables;
89mod tags16;
90#[cfg(feature = "v26_1")]
91pub mod v26_1;
92mod window;
93
94#[cfg(any(feature = "v1_8", feature = "v1_12", feature = "v1_16_5"))]
95pub use attack::ItemAttack;
96pub use crafting::RecipeBook;
97pub use inventory::{
98 Click, ClickError, ClickOutcome, ClickPacket, ClickResult, ContainerClick, CreativePacket,
99 CreativeTab, DEFAULT_CLICK_SETTLE_TICKS, DEFAULT_CLICK_TIMEOUT_TICKS, DragKind, DragStage,
100 Events, Inventory, Outgoing, Window,
101};
102pub use item::{Enchantment, EnchantmentId, ItemKind, ItemText};
103pub use merchant::TradeOffer;
104pub use mount::{MountArmor, MountRules};
105#[cfg(feature = "v26_1")]
106pub use packets::PACKET_IDS_26_1;
107pub use rules::{Version, VersionNotEnabled};
108pub use sim::OUTSIDE;
109pub use stack::{
110 ItemData, Stack, from_legacy_slot, from_modern_slot, nbt_eq, stacks_equal, to_legacy_slot,
111 to_modern_slot,
112};
113pub use window::{SlotKind, WindowKind};