Skip to main content

Crate ghast_inventory

Crate ghast_inventory 

Source
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_transaction also 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::Unpredictable instead 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_items is 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_items is 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::tick once 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 --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).

Structs§

ClickPacket
A window click as sent (window_click; 1.8 ClickWindowC2SPacket).
ClickResult
A finished click.
ContainerClick
A 26.1 click as sent: what vanilla MultiPlayerGameMode.handleContainerInput writes.
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 (tick counts time).
ItemAttack
What an item held in the main hand gives the player’s attack attributes, as a server applies it (stack without an AttributeModifiers tag; see ghast_inventory for stacks).
MountRules
What a mount’s saddle and armor slots accept, read off the entity the screen belongs to (vanilla’s HorseScreenHandler asks the entity). Give it with Inventory::set_mount_rules; without it only stacks the rules cannot concern (no saddle, horse armor or carpet) are predicted in those slots.
RecipeBook
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.
TradeOffer
One villager offer as the client holds it (1.8 / 1.12 TradeOffer from MC|TrList, 1.16.5 from trade_list).
VersionNotEnabled
crate::Inventory::new for a Version whose 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.
ClickError
Why a click (or open / close) was refused. Nothing was sent and nothing changed.
ClickOutcome
What became of a click.
CreativePacket
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.
CreativeTab
The page the creative inventory screen shows (1.8.9 – 1.16.5).
DragKind
How a drag spreads the carried stack (Click::QuickCraft): vanilla’s quick-craft type.
DragStage
The packet of a drag (Click::QuickCraft): vanilla’s quick-craft header.
EnchantmentId
An enchantment id in its version’s form.
ItemData
The item data a stack carries, in its version’s form.
ItemKind
A broad item kind, version-independent (Stack::kind): what combat and tool logic asks (#[non_exhaustive]: kinds may be added).
ItemText
A text field of an item (custom name, lore line) in its version’s form.
MountArmor
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).
SlotKind
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.
WindowKind
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_1 handles.

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’s ItemStack.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.