Skip to main content

Crate ghast_protocol

Crate ghast_protocol 

Source
Expand description

Minecraft Java Edition network protocol for the ghast client library.

Layers, bottom to top:

  • codec: bounds-checked Reader over bytes::Bytes (zero-copy byte arrays and strings), McWrite for encoding, VarInt/VarLong.
  • types, nbt, chat: hand-written shared types — BlockPos with the per-version bit layout, slots, lazily parsed NBT (nbt::RawNbt), chat JSON.
  • Generated packet modules per protocol version (v1_8 = protocol 47, v1_12_2 = 340, v1_16_5 = 754, v26_1 = 775, v26_2 = 776), each with typed packets, one enum per state and direction, and id constants. The definitions themselves live once in defs and are shared by every version whose code for them is identical. A Version marker type ties a version’s packet sets together so a version-agnostic layer can be written generically; versions is the table of known protocol numbers.
  • frame: length framing, zlib compression with partial inflation, the AES-128-CFB8 hook (crypto), and the per-id PacketFilter that drops unwanted packets before they are decoded.
  • io (feature tokio): async framed reader/writer.

Decoding never panics on untrusted input: every failure is a DecodeError or FrameError, and allocations are bounded by the input size.

use ghast_protocol::{FrameDecoder, FrameEncoder, PacketSet, RawPacket};
use ghast_protocol::v1_8::play::serverbound::{KeepAlive, ServerboundPlayPacket};

let mut enc = FrameEncoder::new();
enc.write_packet(&KeepAlive { keep_alive_id: 42 }).unwrap();
let mut dec = FrameDecoder::new();
dec.push(&enc.take());
let raw: RawPacket = dec.next_packet().unwrap().unwrap();
let p = ServerboundPlayPacket::decode_raw(&raw).unwrap();
assert_eq!(p, ServerboundPlayPacket::KeepAlive(KeepAlive { keep_alive_id: 42 }));

Re-exports§

pub use codec::Decode;
pub use codec::Encode;
pub use codec::McWrite;
pub use codec::Reader;
pub use error::DecodeError;
pub use error::EncodeError;
pub use error::FrameError;
pub use frame::FrameDecoder;
pub use frame::FrameEncoder;
pub use frame::FrameLimits;
pub use io::FramedReader;
pub use io::FramedWriter;
pub use packet::Direction;
pub use packet::NoPackets;
pub use packet::Packet;
pub use packet::PacketFilter;
pub use packet::PacketSet;
pub use packet::RawPacket;
pub use packet::State;
pub use packet::Version;
pub use types::BlockPos;
pub use versions::ProtocolVersion;

Modules§

chat
Chat components.
codec
Low-level reading and writing of protocol primitives.
crypto
AES-128-CFB8 stream cipher used after the login encryption handshake.
defs
Packet and type definitions, each exactly once.
error
Error types.
frame
Framing: length prefixes, zlib compression and the encryption hook.
intern
Process-wide sharing of immutable data that every connection to the same server receives identically (registry names, tags, recipes, the command tree). Many clients in one process then hold one copy instead of one each. Values are matched by content when they are stored (once per join, under a lock); reads go through the Arc / Bytes taken then and never touch the pool. An entry lives as long as something holds it.
io
Async framed reader and writer over tokio’s AsyncRead / AsyncWrite (feature tokio).
nbt
Named Binary Tag (NBT) as used on the network.
packet
Packet traits, raw packets and the per-id interest filter.
testgen
Deterministic random value generation for tests (feature testgen).
types
Hand-written protocol types shared by the generated packet code.
v1_8
Protocol 47 (Minecraft 1.8 – 1.8.9), generated from minecraft-data.
v1_12_2
Protocol 340 (Minecraft 1.12.2), generated from minecraft-data.
v1_16_5
Protocol 754 (Minecraft 1.16.4 – 1.16.5), generated from minecraft-data.
v26_1
Protocol 775 (Minecraft 26.1 – 26.1.2), generated from minecraft-data.
v26_2
Protocol 776 (Minecraft 26.2), generated from minecraft-data.
versions
The protocol version table: one row per protocol number this crate knows.