ghast-protocol 0.1.2

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.16.2/protocol.json, Minecraft 1.16.5). Do not edit; run `cargo run -p ghast-codegen-protocol`.
// minecraft-data is MIT licensed, Copyright (c) 2015 PrismarineJS; see ../NOTICE.

//! Serverbound 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::RawNbt;
use crate::packet::{Direction, PacketSet, RawPacket, State};
#[cfg(feature = "testgen")]
use crate::testgen::{Gen, Generate};
use crate::types::{BlockPos, ByteString, PositionLayout, Uuid};
use crate::v1_16_5::types;
use bytes::{BufMut, Bytes, BytesMut};

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

/// `ping_start` (serverbound status, id 0x00).
#[derive(Debug, Clone, PartialEq)]
pub struct PingStart {}
impl crate::packet::Packet for PingStart {
    const ID: i32 = id::PING_START;
    const NAME: &'static str = "ping_start";
    const STATE: State = State::Status;
    const DIRECTION: Direction = Direction::Serverbound;
}
impl Decode for PingStart {
    fn decode(r: &mut Reader<'_>) -> DecodeResult<Self> {
        Ok({ PingStart {} })
    }
}
impl Encode for PingStart {
    fn encode(&self, w: &mut BytesMut) -> EncodeResult {
        Ok(())
    }
}
#[cfg(feature = "testgen")]
impl Generate for PingStart {
    fn generate(g: &mut Gen) -> Self {
        { PingStart {} }
    }
}
impl From<PingStart> for ServerboundStatusPacket {
    fn from(p: PingStart) -> Self {
        Self::PingStart(p)
    }
}

/// `ping` (serverbound status, id 0x01).
#[derive(Debug, Clone, PartialEq)]
pub struct Ping {
    /// `time`
    pub time: i64,
}
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::Serverbound;
}
impl Decode for Ping {
    fn decode(r: &mut Reader<'_>) -> DecodeResult<Self> {
        Ok({
            let v15 = r.read_i64()?;
            Ping { time: v15 }
        })
    }
}
impl Encode for Ping {
    fn encode(&self, w: &mut BytesMut) -> EncodeResult {
        w.put_i64((*self).time);
        Ok(())
    }
}
#[cfg(feature = "testgen")]
impl Generate for Ping {
    fn generate(g: &mut Gen) -> Self {
        {
            let v16 = g.i64();
            Ping { time: v16 }
        }
    }
}
impl From<Ping> for ServerboundStatusPacket {
    fn from(p: Ping) -> Self {
        Self::Ping(p)
    }
}

/// Every Serverbound packet of the Status state.
#[derive(Debug, Clone, PartialEq)]
pub enum ServerboundStatusPacket {
    /// `ping_start`
    PingStart(PingStart),
    /// `ping`
    Ping(Ping),
}

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

impl ServerboundStatusPacket {
    /// All packet ids of this set, ascending.
    pub const IDS: &'static [i32] = &[id::PING_START, 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::PING_START => Some(Self::PingStart(<PingStart as Generate>::generate(g))),
            id::PING => Some(Self::Ping(<Ping as Generate>::generate(g))),
            _ => None,
        }
    }
}