basalt-mc-protocol 0.2.1

Minecraft packet definitions and version-aware packet registry
Documentation
//! Handshake state packet definitions.
//!
//! Auto-generated by `cargo xt codegen` from minecraft-data.
//! Do not edit manually — changes will be overwritten.

use basalt_derive::packet;
use basalt_types::Decode as _;

use crate::error::{Error, Result};

// -- Serverbound packets --

#[derive(Debug, Clone, Default, PartialEq)]
#[packet(id = 0xfe)]
pub struct ServerboundHandshakeLegacyServerListPing {
    pub payload: u8,
}

#[derive(Debug, Clone, Default, PartialEq)]
#[packet(id = 0x00)]
pub struct ServerboundHandshakeSetProtocol {
    #[field(varint)]
    pub protocol_version: i32,
    pub server_host: String,
    pub server_port: u16,
    #[field(varint)]
    pub next_state: i32,
}

/// serverbound packets in the Handshake state.
#[derive(Debug, Clone, PartialEq)]
pub enum ServerboundHandshakePacket {
    LegacyServerListPing(ServerboundHandshakeLegacyServerListPing),
    SetProtocol(ServerboundHandshakeSetProtocol),
}

impl ServerboundHandshakePacket {
    /// Decodes a packet from its ID and payload.
    pub fn decode_by_id(id: i32, buf: &mut &[u8]) -> Result<Self> {
        match id {
            ServerboundHandshakeLegacyServerListPing::PACKET_ID => Ok(Self::LegacyServerListPing(
                ServerboundHandshakeLegacyServerListPing::decode(buf)?,
            )),
            ServerboundHandshakeSetProtocol::PACKET_ID => Ok(Self::SetProtocol(
                ServerboundHandshakeSetProtocol::decode(buf)?,
            )),
            _ => Err(Error::UnknownPacket {
                id,
                state: "handshake",
            }),
        }
    }
}