Expand description
Inventory and container windows for the ghast Minecraft Java Edition client library (1.8.9, 1.12.2, 1.16.5, 26.1, 26.2 and 26.3).
Inventory holds what the vanilla client holds — the player inventory, the cursor stack
and the open window — fed by the server’s window packets, and turns click intents into
window_click packets with vanilla’s client-side prediction: the new slot contents, the
“clicked item” field (what vanilla’s onSlotClick returns; the server rejects the click when
its own result differs) and the per-window action number.
- One click in flight. A click stays pending until the server’s confirm (
transaction) with its action number arrives. A rejected click makes the server resend the window; clicks wait for that resync.Inventory::on_transactionalso produces vanilla’s answer to rejected confirms — exactly once per confirm (route every confirm through it). - Exact or refused. Clicks are simulated on a scratch copy; when the outcome depends on
data this crate does not model (recipes, furnace fuel, brewing, trading, …) the click is
refused with
ClickError::Unpredictableinstead of guessing, so the client state never silently diverges from the server’s (after an accepted click the server sends no corrections — it assumes the client predicted right). - Lazy NBT. Stacks keep their NBT as raw bytes (
ghast_protocol::nbt::RawNbt);window_itemsis applied without parsing any NBT. Stack comparisons compare the bytes and only parse when they differ (vanilla compares trees). - 26.1 / 26.2 / 26.3 have no confirms: the server applies every click and answers only the slots
whose hash (vanilla
HashedStack) differs from its own result. A modern click is never refused: one ghast cannot predict is sent unpredicted (ContainerClick::predicted) and the server’s corrections bring the result. Data components stay raw too (ItemData::Components);window_itemsis applied with a structural skip (Inventory::apply_raw_modern). Item facts come from Mojang’s reports (modern; one implementation over per-version tables). - Item accessors (
Stack::item_name,Stack::max_stack_size,Stack::damage_value,Stack::custom_name,Stack::enchantments, …) answer from NBT or components, decoding only what they read. - Pure. No I/O, no clock, no async: packets in, packets out. The client writes the
returned packets immediately, in call order, before the tick’s movement packet, and calls
Inventory::tickonce per tick.
use ghast_inventory::{Click, Inventory, Outgoing, Stack, Version};
let mut inv = Inventory::new(Version::V1_8).unwrap();
// The server sends window 0 (45 slots): 64 stone in hotbar slot 0 (window slot 36).
let mut items = vec![None; 45];
items[36] = Some(Stack::new(1, 64, 0));
inv.on_window_items(0, items);
inv.tick();
assert_eq!(inv.open_inventory(), Ok(Some(Outgoing::OpenInventory))); // 1.8: C16
inv.tick();
let Ok(Outgoing::Click(c)) = inv.click(Click::Pickup { slot: 36, right: true }) else { panic!() };
assert_eq!((c.action, c.item.as_ref().unwrap().count()), (1, 64)); // carries the slot before the click
assert_eq!(inv.cursor().unwrap().count(), 32);
assert_eq!(inv.window0_slot(36).unwrap().count(), 32);
assert!(inv.pending());
assert_eq!(inv.on_transaction(0, 1, true), None); // accepted: nothing to answer
assert!(!inv.pending());Modules§
- modern
- Modern (26.x) items for any modern
Version: the item registry and default components (Mojang’s reports), the component readers, the window types and the component hashes a modern click carries. - v26_1
- 26.1 (protocol 775) items: default components of every item (Mojang’s
--reportscomponent dump, seeexamples/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 (vanillaHashedStack/HashedPatchMapoverHashOps.CRC32C_INSTANCE).
Structs§
- Click
Packet - A window click as sent (
window_click; 1.8ClickWindowC2SPacket). - Click
Result - A finished click.
- Container
Click - A 26.1 click as sent: what vanilla
MultiPlayerGameMode.handleContainerInputwrites. - Enchantment
- One enchantment of a stack.
- Events
- Changes since the last
Inventory::drain_events. - Inventory
- The client’s inventory: player inventory, cursor, the open window, click prediction and
the confirm protocol. Pure and deterministic: packets in, packets out, no I/O or clock
(
tickcounts time). - Item
Attack - What an item held in the main hand gives the player’s attack attributes, as a server applies
it (stack without an
AttributeModifierstag; seeghast_inventoryfor stacks). - Mount
Rules - What a mount’s saddle and armor slots accept, read off the entity the screen belongs to
(vanilla’s
HorseScreenHandlerasks the entity). Give it withInventory::set_mount_rules; without it only stacks the rules cannot concern (no saddle, horse armor or carpet) are predicted in those slots. - Recipe
Book - The crafting recipes the server declared (1.16.5
declare_recipes), in packet order. - Stack
- A non-empty item stack as the client holds it.
- Trade
Offer - One villager offer as the client holds it (1.8 / 1.12
TradeOfferfromMC|TrList, 1.16.5 fromtrade_list). - Version
NotEnabled crate::Inventory::newfor aVersionwhose crate feature (v1_8,v1_12,v1_16_5,v26_1,v26_2,v26_3) is off in this build.- Window
- An open container window.
Enums§
- Click
- A click intent: the vanilla GUI gestures a bot needs. Slot numbers are the open screen’s
(window 0 when only the player inventory is open);
OUTSIDE(-999) is a click outside the window. - Click
Error - Why a click (or open / close) was refused. Nothing was sent and nothing changed.
- Click
Outcome - What became of a click.
- Creative
Packet - One packet the 26.1 creative inventory screen sends (
Outgoing::Creative): the server trusts these stacks, so they are exactly what the real client sends. - Creative
Tab - The page the creative inventory screen shows (1.8.9 – 1.16.5).
- Drag
Kind - How a drag spreads the carried stack (
Click::QuickCraft): vanilla’s quick-craft type. - Drag
Stage - The packet of a drag (
Click::QuickCraft): vanilla’s quick-craft header. - Enchantment
Id - An enchantment id in its version’s form.
- Item
Data - The item data a stack carries, in its version’s form.
- Item
Kind - A broad item kind, version-independent (
Stack::kind): what combat and tool logic asks (#[non_exhaustive]: kinds may be added). - Item
Text - A text field of an item (custom name, lore line) in its version’s form.
- Mount
Armor - The item a mount wears in its armor slot.
- Outgoing
- A packet the inventory wants sent. Write it right away, in call order (before the tick’s movement packet when it comes from a behaviour action).
- Slot
Kind - What a slot of a screen handler is, as far as click prediction is concerned.
- Version
- Protocol version of the connection; selects the vanilla rules being reproduced.
- Window
Kind - The vanilla screen handler (container class) the client created for a window.
Constants§
- DEFAULT_
CLICK_ SETTLE_ TICKS - 26.1: ticks without a correcting container packet after which a click counts as accepted
(see
Inventory::set_click_settle_ticks). - DEFAULT_
CLICK_ TIMEOUT_ TICKS - Ticks a click may wait for its confirm before it is given up (
ClickOutcome::TimedOut). - OUTSIDE
- Slot number of a click outside the window.
- PACKET_
IDS_ 26_ 1 - Clientbound 26.1 play packet ids
Inventory::apply_raw_v26_1handles.
Functions§
- from_
legacy_ slot - Converts a 1.8 wire slot.
- from_
modern_ slot - Converts a 1.16.5 wire slot; stacks that vanilla treats as empty (air, count <= 0) are
None. - nbt_eq
- Vanilla tag equality on raw NBT: equal bytes are equal; otherwise both trees are parsed and compared (compounds as maps, root names ignored). Unparsable NBT only equals itself.
- stacks_
equal Option<&Stack>exact equality (the server’sItemStack.equalsAll).- to_
legacy_ slot - The 1.8 wire slot of an optional stack.
- to_
modern_ slot - The 1.16.5 wire slot of an optional stack.