basalt-mc-protocol 0.2.1

Minecraft packet definitions and version-aware packet registry
Documentation
//! Status 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 = 0x01)]
pub struct ServerboundStatusPing {
    pub time: i64,
}

#[derive(Debug, Clone, Default, PartialEq)]
#[packet(id = 0x00)]
pub struct ServerboundStatusPingStart;

// -- Clientbound packets --

#[derive(Debug, Clone, Default, PartialEq)]
#[packet(id = 0x01)]
pub struct ClientboundStatusPing {
    pub time: i64,
}

#[derive(Debug, Clone, Default, PartialEq)]
#[packet(id = 0x00)]
pub struct ClientboundStatusServerInfo {
    pub response: String,
}

/// serverbound packets in the Status state.
#[derive(Debug, Clone, PartialEq)]
pub enum ServerboundStatusPacket {
    Ping(ServerboundStatusPing),
    PingStart(ServerboundStatusPingStart),
}

impl ServerboundStatusPacket {
    /// Decodes a packet from its ID and payload.
    pub fn decode_by_id(id: i32, buf: &mut &[u8]) -> Result<Self> {
        match id {
            ServerboundStatusPing::PACKET_ID => Ok(Self::Ping(ServerboundStatusPing::decode(buf)?)),
            ServerboundStatusPingStart::PACKET_ID => {
                Ok(Self::PingStart(ServerboundStatusPingStart::decode(buf)?))
            }
            _ => Err(Error::UnknownPacket {
                id,
                state: "status",
            }),
        }
    }
}

/// clientbound packets in the Status state.
#[derive(Debug, Clone, PartialEq)]
pub enum ClientboundStatusPacket {
    Ping(ClientboundStatusPing),
    ServerInfo(ClientboundStatusServerInfo),
}

impl ClientboundStatusPacket {
    /// Decodes a packet from its ID and payload.
    pub fn decode_by_id(id: i32, buf: &mut &[u8]) -> Result<Self> {
        match id {
            ClientboundStatusPing::PACKET_ID => Ok(Self::Ping(ClientboundStatusPing::decode(buf)?)),
            ClientboundStatusServerInfo::PACKET_ID => {
                Ok(Self::ServerInfo(ClientboundStatusServerInfo::decode(buf)?))
            }
            _ => Err(Error::UnknownPacket {
                id,
                state: "status",
            }),
        }
    }
}