ghast-protocol 0.4.3

Minecraft Java Edition network protocol for the ghast client library: generated per-version packets, framing, compression, encryption, NBT
Documentation
// @generated by ghast-codegen-protocol from PrismarineJS minecraft-data 3.119.0
// (data/pc/1.8/protocol.json, Minecraft 1.8.9). Do not edit; run `cargo run -p ghast-codegen-protocol`.
// minecraft-data is MIT licensed, Copyright (c) 2015 PrismarineJS; see the NOTICE file
// in crates/ghast-protocol/src/generated/.

//! Clientbound packets of the status state.

#![allow(
    unused_imports,
    unused_variables,
    unused_mut,
    unused_parens,
    unused_braces,
    clippy::all,
    clippy::pedantic,
    non_camel_case_types,
    non_snake_case
)]
use crate::codec::{Decode, DecodeResult, Encode, EncodeResult, McWrite, Reader, decode_exact};
use crate::error::{DecodeError, EncodeError};
use crate::nbt::{AnonNbt, RawNbt};
use crate::packet::{Direction, PacketSet, RawPacket, State};
#[cfg(feature = "testgen")]
use crate::testgen::{Gen, Generate};
use crate::types::{BlockPos, ByteString, Holder, HolderSet, LpVec3, PositionLayout, Uuid};
use bytes::{BufMut, Bytes, BytesMut};

/// Packet ids.
pub mod id {
    /// `server_info`
    pub const SERVER_INFO: i32 = 0x00;
    /// `ping`
    pub const PING: i32 = 0x01;
}

/// `server_info` (clientbound status, id 0x00).
pub type ServerInfo = crate::defs::p47::status::clientbound::ServerInfo<47>;
impl crate::packet::Packet for ServerInfo {
    const ID: i32 = id::SERVER_INFO;
    const NAME: &'static str = "server_info";
    const STATE: State = State::Status;
    const DIRECTION: Direction = Direction::Clientbound;
}
impl From<ServerInfo> for ClientboundStatusPacket {
    fn from(p: ServerInfo) -> Self {
        Self::ServerInfo(p)
    }
}

/// `ping` (clientbound status, id 0x01).
pub type Ping = crate::defs::p47::status::clientbound::Ping<47>;
impl crate::packet::Packet for Ping {
    const ID: i32 = id::PING;
    const NAME: &'static str = "ping";
    const STATE: State = State::Status;
    const DIRECTION: Direction = Direction::Clientbound;
}
impl From<Ping> for ClientboundStatusPacket {
    fn from(p: Ping) -> Self {
        Self::Ping(p)
    }
}

/// Every Clientbound packet of the Status state.
#[derive(Debug, Clone, PartialEq)]
pub enum ClientboundStatusPacket {
    /// `server_info`
    ServerInfo(ServerInfo),
    /// `ping`
    Ping(Ping),
}

impl PacketSet for ClientboundStatusPacket {
    const STATE: State = State::Status;
    const DIRECTION: Direction = Direction::Clientbound;
    fn decode_raw(raw: &RawPacket) -> DecodeResult<Self> {
        match raw.id {
            id::SERVER_INFO => decode_exact(&raw.body).map(Self::ServerInfo),
            id::PING => decode_exact(&raw.body).map(Self::Ping),
            other => Err(DecodeError::UnknownPacketId(other)),
        }
    }
    fn id(&self) -> i32 {
        match *self {
            Self::ServerInfo(_) => id::SERVER_INFO,
            Self::Ping(_) => id::PING,
        }
    }
    fn name(&self) -> &'static str {
        match *self {
            Self::ServerInfo(_) => "server_info",
            Self::Ping(_) => "ping",
        }
    }
    fn encode_body(&self, w: &mut BytesMut) -> EncodeResult {
        match *self {
            Self::ServerInfo(ref p) => p.encode(w),
            Self::Ping(ref p) => p.encode(w),
        }
    }
    fn name_of(id: i32) -> Option<&'static str> {
        match id {
            id::SERVER_INFO => Some("server_info"),
            id::PING => Some("ping"),
            _ => None,
        }
    }
}

impl ClientboundStatusPacket {
    /// All packet ids of this set, ascending.
    pub const IDS: &'static [i32] = &[id::SERVER_INFO, id::PING];
    /// Generates a random packet with the given id (test support).
    #[cfg(feature = "testgen")]
    pub fn generate_by_id(id: i32, g: &mut Gen) -> Option<Self> {
        match id {
            id::SERVER_INFO => Some(Self::ServerInfo(<ServerInfo as Generate>::generate(g))),
            id::PING => Some(Self::Ping(<Ping as Generate>::generate(g))),
            _ => None,
        }
    }
}