deepslate_protocol/packet/mod.rs
1//! Minecraft packet definitions.
2//!
3//! Only packets needed for the proxy's connection lifecycle are fully parsed.
4//! All other packets are treated as opaque [`RawPacket`] values and forwarded
5//! without inspection.
6
7pub mod handshake;
8pub mod login;
9pub mod play;
10pub mod status;
11
12use bytes::{Buf, BufMut, Bytes};
13
14use crate::types::ProtocolError;
15
16/// A packet that the proxy does not need to inspect.
17/// Contains the raw packet ID and undecoded payload bytes.
18#[derive(Debug, Clone)]
19pub struct RawPacket {
20 /// The Minecraft packet ID.
21 pub id: i32,
22 /// The raw payload bytes (everything after the packet ID).
23 pub data: Bytes,
24}
25
26/// Trait implemented by all typed packets.
27pub trait Packet: Sized {
28 /// The packet ID for this packet type.
29 const PACKET_ID: i32;
30
31 /// Decode this packet from the payload buffer (after the packet ID has been read).
32 ///
33 /// # Errors
34 ///
35 /// Returns `ProtocolError` if the data is malformed.
36 fn decode(buf: &mut impl Buf) -> Result<Self, ProtocolError>;
37
38 /// Encode this packet's fields into the buffer (without the packet ID).
39 fn encode(&self, buf: &mut impl BufMut);
40}