Skip to main content

Encode

Trait Encode 

Source
pub trait Encode {
    // Required method
    fn encode(&self, buf: &mut Vec<u8>) -> Result<()>;
}
Expand description

Serialize a value into a byte buffer.

Required Methods§

Source

fn encode(&self, buf: &mut Vec<u8>) -> Result<()>

Implementations on Foreign Types§

Source§

impl Encode for bool

Encodes a boolean as a single byte in the Minecraft protocol.

The Minecraft protocol represents booleans as a single unsigned byte: 0x00 for false, 0x01 for true. This is used in many packets for flags like on-ground state, sneaking, sprinting, etc.

Source§

fn encode(&self, buf: &mut Vec<u8>) -> Result<()>

Writes 0x01 if true, 0x00 if false. Always writes exactly one byte.

Source§

impl Encode for f32

Encodes as a fixed-size big-endian value.

The Minecraft protocol uses big-endian (network byte order) for all fixed-size numeric types. The value is written as exactly 4 bytes.

Source§

fn encode(&self, buf: &mut Vec<u8>) -> Result<()>

Writes the value as big-endian bytes. Always writes exactly 4 bytes.

Source§

impl Encode for f64

Encodes as a fixed-size big-endian value.

The Minecraft protocol uses big-endian (network byte order) for all fixed-size numeric types. The value is written as exactly 8 bytes.

Source§

fn encode(&self, buf: &mut Vec<u8>) -> Result<()>

Writes the value as big-endian bytes. Always writes exactly 8 bytes.

Source§

impl Encode for i8

Encodes as a fixed-size big-endian value.

The Minecraft protocol uses big-endian (network byte order) for all fixed-size numeric types. The value is written as exactly 1 bytes.

Source§

fn encode(&self, buf: &mut Vec<u8>) -> Result<()>

Writes the value as big-endian bytes. Always writes exactly 1 bytes.

Source§

impl Encode for i16

Encodes as a fixed-size big-endian value.

The Minecraft protocol uses big-endian (network byte order) for all fixed-size numeric types. The value is written as exactly 2 bytes.

Source§

fn encode(&self, buf: &mut Vec<u8>) -> Result<()>

Writes the value as big-endian bytes. Always writes exactly 2 bytes.

Source§

impl Encode for i32

Encodes as a fixed-size big-endian value.

The Minecraft protocol uses big-endian (network byte order) for all fixed-size numeric types. The value is written as exactly 4 bytes.

Source§

fn encode(&self, buf: &mut Vec<u8>) -> Result<()>

Writes the value as big-endian bytes. Always writes exactly 4 bytes.

Source§

impl Encode for i64

Encodes as a fixed-size big-endian value.

The Minecraft protocol uses big-endian (network byte order) for all fixed-size numeric types. The value is written as exactly 8 bytes.

Source§

fn encode(&self, buf: &mut Vec<u8>) -> Result<()>

Writes the value as big-endian bytes. Always writes exactly 8 bytes.

Source§

impl Encode for u8

Encodes as a fixed-size big-endian value.

The Minecraft protocol uses big-endian (network byte order) for all fixed-size numeric types. The value is written as exactly 1 bytes.

Source§

fn encode(&self, buf: &mut Vec<u8>) -> Result<()>

Writes the value as big-endian bytes. Always writes exactly 1 bytes.

Source§

impl Encode for u16

Encodes as a fixed-size big-endian value.

The Minecraft protocol uses big-endian (network byte order) for all fixed-size numeric types. The value is written as exactly 2 bytes.

Source§

fn encode(&self, buf: &mut Vec<u8>) -> Result<()>

Writes the value as big-endian bytes. Always writes exactly 2 bytes.

Source§

impl Encode for u32

Encodes as a fixed-size big-endian value.

The Minecraft protocol uses big-endian (network byte order) for all fixed-size numeric types. The value is written as exactly 4 bytes.

Source§

fn encode(&self, buf: &mut Vec<u8>) -> Result<()>

Writes the value as big-endian bytes. Always writes exactly 4 bytes.

Source§

impl Encode for u64

Encodes as a fixed-size big-endian value.

The Minecraft protocol uses big-endian (network byte order) for all fixed-size numeric types. The value is written as exactly 8 bytes.

Source§

fn encode(&self, buf: &mut Vec<u8>) -> Result<()>

Writes the value as big-endian bytes. Always writes exactly 8 bytes.

Source§

impl Encode for String

Encodes a Rust String as a Minecraft protocol string.

Minecraft protocol strings are UTF-8 byte sequences prefixed by a VarInt indicating the byte length (not character count). They are used for player names, chat messages, identifiers, server addresses, and many other text fields. The maximum allowed length is 32767 bytes.

Source§

fn encode(&self, buf: &mut Vec<u8>) -> Result<()>

Writes a VarInt length prefix followed by the UTF-8 bytes.

Fails with Error::StringTooLong if the string exceeds 32767 bytes.

Source§

impl Encode for Vec<u8>

Encodes a byte vector as a Minecraft protocol byte array.

Minecraft byte arrays are raw byte sequences prefixed by a VarInt indicating the length. They are used for plugin channel data, chunk sections, encryption payloads, and other binary blobs in the protocol. Unlike strings, byte arrays have no encoding or length constraints beyond what the packet format imposes.

Source§

fn encode(&self, buf: &mut Vec<u8>) -> Result<()>

Writes a VarInt length prefix followed by the raw bytes.

Source§

impl<T: Encode + ?Sized> Encode for Box<T>

Source§

fn encode(&self, buf: &mut Vec<u8>) -> Result<()>

Implementors§

Source§

impl Encode for NbtTag

Encodes a single NbtTag as a network NBT root.

Only NbtTag::Compound is valid as a network NBT root. This delegates to the NbtCompound encoder for compound tags. Other tag types are wrapped in a single-entry compound for compatibility, though in practice the protocol always uses compound roots.

Source§

impl Encode for NbtCompound

Encodes an NbtCompound as a network NBT root compound.

Since Minecraft 1.20.3, network NBT uses a simplified root format: a compound tag type byte (0x0A) followed directly by the compound payload (no root tag name). This differs from the traditional NBT format which includes a root tag name after the type byte.

This encoder produces the network NBT format used in modern protocol packets (chat components, item stacks, registry data).

Source§

impl Encode for Angle

Encodes an Angle as a single unsigned byte.

The angle is written directly as one byte with no transformation. This is the simplest encoding in the Minecraft protocol.

Source§

impl Encode for BitSet

Encodes the BitSet as a VarInt-prefixed array of big-endian i64 values.

The VarInt indicates the number of longs in the array, followed by each long encoded as 8 big-endian bytes. An empty BitSet encodes as a single VarInt(0) byte.

Source§

impl Encode for Identifier

Encodes an Identifier as a VarInt-prefixed UTF-8 string in namespace:path format.

The full namespace:path string is written using the standard Minecraft string encoding (VarInt length prefix + UTF-8 bytes). This is the same wire format used for all string fields in the protocol.

Source§

impl Encode for OpaqueBytes

Source§

impl Encode for Position

Encodes a Position as a packed 64-bit big-endian integer.

The three coordinates are packed into a single i64 using the bit layout described on the type. The packed value is then written as 8 big-endian bytes, matching the Minecraft protocol wire format.

Source§

impl Encode for Slot

Encodes a Slot in the Minecraft protocol format.

Empty slots encode as a single VarInt(0). Non-empty slots encode the item count, item ID, then component data. If no components are present, explicit zero counts are written (VarInt(0) + VarInt(0)).

Source§

impl Encode for TextComponent

Encodes a TextComponent as network NBT (compound tag).

Converts the component tree into an NbtCompound, then encodes it using the network NBT format (1.20.3+). The resulting bytes can be used directly in protocol packets for chat, disconnect, title, etc.

Source§

impl Encode for Uuid

Encodes a UUID as two consecutive big-endian u64 values (16 bytes total).

The most significant 64 bits are written first, followed by the least significant 64 bits. This matches the Minecraft protocol wire format for all UUID fields.

Source§

impl Encode for VarInt

Encodes the VarInt as 1-5 bytes using MSB continuation bit encoding.

Each byte carries 7 bits of the value (least significant first). The MSB is set to 1 if more bytes follow, 0 on the final byte. Negative values are encoded as their unsigned two’s complement representation and always produce 5 bytes.

Source§

impl Encode for VarLong

Encodes the VarLong as 1-10 bytes using MSB continuation bit encoding.

Same algorithm as VarInt but operating on 64-bit values. Negative values are encoded as their unsigned two’s complement representation and always produce 10 bytes.

Source§

impl Encode for Vec2f

Encodes a Vec2f as two consecutive big-endian f32 values.

Source§

impl Encode for Vec3f64

Encodes a Vec3f64 as three consecutive big-endian f64 values.

Source§

impl Encode for Vec3f

Encodes a Vec3f as three consecutive big-endian f32 values.

Source§

impl Encode for Vec3i16

Encodes a Vec3i16 as three consecutive big-endian i16 values.