ghast-protocol 0.4.0

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/26.1/protocol.json, Minecraft 26.1.2). 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 login 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 {
    /// `disconnect`
    pub const DISCONNECT: i32 = 0x00;
    /// `encryption_begin`
    pub const ENCRYPTION_BEGIN: i32 = 0x01;
    /// `success`
    pub const SUCCESS: i32 = 0x02;
    /// `compress`
    pub const COMPRESS: i32 = 0x03;
    /// `login_plugin_request`
    pub const LOGIN_PLUGIN_REQUEST: i32 = 0x04;
    /// `cookie_request`
    pub const COOKIE_REQUEST: i32 = 0x05;
}

pub use crate::defs::p775::login::clientbound::SuccessProperties;

/// `disconnect` (clientbound login, id 0x00).
pub type Disconnect = crate::defs::p47::login::clientbound::Disconnect<775>;
impl crate::packet::Packet for Disconnect {
    const ID: i32 = id::DISCONNECT;
    const NAME: &'static str = "disconnect";
    const STATE: State = State::Login;
    const DIRECTION: Direction = Direction::Clientbound;
}
impl From<Disconnect> for ClientboundLoginPacket {
    fn from(p: Disconnect) -> Self {
        Self::Disconnect(p)
    }
}

/// `encryption_begin` (clientbound login, id 0x01).
pub type EncryptionBegin = crate::defs::p775::login::clientbound::EncryptionBegin<775>;
impl crate::packet::Packet for EncryptionBegin {
    const ID: i32 = id::ENCRYPTION_BEGIN;
    const NAME: &'static str = "encryption_begin";
    const STATE: State = State::Login;
    const DIRECTION: Direction = Direction::Clientbound;
}
impl From<EncryptionBegin> for ClientboundLoginPacket {
    fn from(p: EncryptionBegin) -> Self {
        Self::EncryptionBegin(p)
    }
}

/// `success` (clientbound login, id 0x02).
pub type Success = crate::defs::p775::login::clientbound::Success<775>;
impl crate::packet::Packet for Success {
    const ID: i32 = id::SUCCESS;
    const NAME: &'static str = "success";
    const STATE: State = State::Login;
    const DIRECTION: Direction = Direction::Clientbound;
}
impl From<Success> for ClientboundLoginPacket {
    fn from(p: Success) -> Self {
        Self::Success(p)
    }
}

/// `compress` (clientbound login, id 0x03).
pub type Compress = crate::defs::p47::login::clientbound::Compress<775>;
impl crate::packet::Packet for Compress {
    const ID: i32 = id::COMPRESS;
    const NAME: &'static str = "compress";
    const STATE: State = State::Login;
    const DIRECTION: Direction = Direction::Clientbound;
}
impl From<Compress> for ClientboundLoginPacket {
    fn from(p: Compress) -> Self {
        Self::Compress(p)
    }
}

/// `login_plugin_request` (clientbound login, id 0x04).
pub type LoginPluginRequest = crate::defs::p754::login::clientbound::LoginPluginRequest<775>;
impl crate::packet::Packet for LoginPluginRequest {
    const ID: i32 = id::LOGIN_PLUGIN_REQUEST;
    const NAME: &'static str = "login_plugin_request";
    const STATE: State = State::Login;
    const DIRECTION: Direction = Direction::Clientbound;
}
impl From<LoginPluginRequest> for ClientboundLoginPacket {
    fn from(p: LoginPluginRequest) -> Self {
        Self::LoginPluginRequest(p)
    }
}

/// `cookie_request` (clientbound login, id 0x05).
pub type CookieRequest = crate::defs::p775::login::clientbound::CookieRequest<775>;
impl crate::packet::Packet for CookieRequest {
    const ID: i32 = id::COOKIE_REQUEST;
    const NAME: &'static str = "cookie_request";
    const STATE: State = State::Login;
    const DIRECTION: Direction = Direction::Clientbound;
}
impl From<CookieRequest> for ClientboundLoginPacket {
    fn from(p: CookieRequest) -> Self {
        Self::CookieRequest(p)
    }
}

/// Every Clientbound packet of the Login state.
#[derive(Debug, Clone, PartialEq)]
pub enum ClientboundLoginPacket {
    /// `disconnect`
    Disconnect(Disconnect),
    /// `encryption_begin`
    EncryptionBegin(EncryptionBegin),
    /// `success`
    Success(Success),
    /// `compress`
    Compress(Compress),
    /// `login_plugin_request`
    LoginPluginRequest(LoginPluginRequest),
    /// `cookie_request`
    CookieRequest(CookieRequest),
}

impl PacketSet for ClientboundLoginPacket {
    const STATE: State = State::Login;
    const DIRECTION: Direction = Direction::Clientbound;
    fn decode_raw(raw: &RawPacket) -> DecodeResult<Self> {
        match raw.id {
            id::DISCONNECT => decode_exact(&raw.body).map(Self::Disconnect),
            id::ENCRYPTION_BEGIN => decode_exact(&raw.body).map(Self::EncryptionBegin),
            id::SUCCESS => decode_exact(&raw.body).map(Self::Success),
            id::COMPRESS => decode_exact(&raw.body).map(Self::Compress),
            id::LOGIN_PLUGIN_REQUEST => decode_exact(&raw.body).map(Self::LoginPluginRequest),
            id::COOKIE_REQUEST => decode_exact(&raw.body).map(Self::CookieRequest),
            other => Err(DecodeError::UnknownPacketId(other)),
        }
    }
    fn id(&self) -> i32 {
        match *self {
            Self::Disconnect(_) => id::DISCONNECT,
            Self::EncryptionBegin(_) => id::ENCRYPTION_BEGIN,
            Self::Success(_) => id::SUCCESS,
            Self::Compress(_) => id::COMPRESS,
            Self::LoginPluginRequest(_) => id::LOGIN_PLUGIN_REQUEST,
            Self::CookieRequest(_) => id::COOKIE_REQUEST,
        }
    }
    fn name(&self) -> &'static str {
        match *self {
            Self::Disconnect(_) => "disconnect",
            Self::EncryptionBegin(_) => "encryption_begin",
            Self::Success(_) => "success",
            Self::Compress(_) => "compress",
            Self::LoginPluginRequest(_) => "login_plugin_request",
            Self::CookieRequest(_) => "cookie_request",
        }
    }
    fn encode_body(&self, w: &mut BytesMut) -> EncodeResult {
        match *self {
            Self::Disconnect(ref p) => p.encode(w),
            Self::EncryptionBegin(ref p) => p.encode(w),
            Self::Success(ref p) => p.encode(w),
            Self::Compress(ref p) => p.encode(w),
            Self::LoginPluginRequest(ref p) => p.encode(w),
            Self::CookieRequest(ref p) => p.encode(w),
        }
    }
    fn name_of(id: i32) -> Option<&'static str> {
        match id {
            id::DISCONNECT => Some("disconnect"),
            id::ENCRYPTION_BEGIN => Some("encryption_begin"),
            id::SUCCESS => Some("success"),
            id::COMPRESS => Some("compress"),
            id::LOGIN_PLUGIN_REQUEST => Some("login_plugin_request"),
            id::COOKIE_REQUEST => Some("cookie_request"),
            _ => None,
        }
    }
}

impl ClientboundLoginPacket {
    /// All packet ids of this set, ascending.
    pub const IDS: &'static [i32] = &[
        id::DISCONNECT,
        id::ENCRYPTION_BEGIN,
        id::SUCCESS,
        id::COMPRESS,
        id::LOGIN_PLUGIN_REQUEST,
        id::COOKIE_REQUEST,
    ];
    /// 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::DISCONNECT => Some(Self::Disconnect(<Disconnect as Generate>::generate(g))),
            id::ENCRYPTION_BEGIN => Some(Self::EncryptionBegin(
                <EncryptionBegin as Generate>::generate(g),
            )),
            id::SUCCESS => Some(Self::Success(<Success as Generate>::generate(g))),
            id::COMPRESS => Some(Self::Compress(<Compress as Generate>::generate(g))),
            id::LOGIN_PLUGIN_REQUEST => Some(Self::LoginPluginRequest(
                <LoginPluginRequest as Generate>::generate(g),
            )),
            id::COOKIE_REQUEST => Some(Self::CookieRequest(<CookieRequest as Generate>::generate(
                g,
            ))),
            _ => None,
        }
    }
}