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.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 ../NOTICE.

//! 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::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_8::types;
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;
}

/// `disconnect` (clientbound login, id 0x00).
#[derive(Debug, Clone, PartialEq)]
pub struct Disconnect {
    /// `reason`
    pub reason: ByteString,
}
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 Decode for Disconnect {
    fn decode(r: &mut Reader<'_>) -> DecodeResult<Self> {
        Ok({
            let v17 = r.read_string()?;
            Disconnect { reason: v17 }
        })
    }
}
impl Encode for Disconnect {
    fn encode(&self, w: &mut BytesMut) -> EncodeResult {
        w.put_string(&(*self).reason)?;
        Ok(())
    }
}
#[cfg(feature = "testgen")]
impl Generate for Disconnect {
    fn generate(g: &mut Gen) -> Self {
        {
            let v18 = g.string();
            Disconnect { reason: v18 }
        }
    }
}
impl From<Disconnect> for ClientboundLoginPacket {
    fn from(p: Disconnect) -> Self {
        Self::Disconnect(p)
    }
}

/// `encryption_begin` (clientbound login, id 0x01).
#[derive(Debug, Clone, PartialEq)]
pub struct EncryptionBegin {
    /// `serverId`
    pub server_id: ByteString,
    /// `publicKey`
    pub public_key: Bytes,
    /// `verifyToken`
    pub verify_token: Bytes,
}
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 Decode for EncryptionBegin {
    fn decode(r: &mut Reader<'_>) -> DecodeResult<Self> {
        Ok({
            let v19 = r.read_string()?;
            let v20 = r.read_byte_array()?;
            let v21 = r.read_byte_array()?;
            EncryptionBegin {
                server_id: v19,
                public_key: v20,
                verify_token: v21,
            }
        })
    }
}
impl Encode for EncryptionBegin {
    fn encode(&self, w: &mut BytesMut) -> EncodeResult {
        w.put_string(&(*self).server_id)?;
        w.put_byte_array(&(*self).public_key)?;
        w.put_byte_array(&(*self).verify_token)?;
        Ok(())
    }
}
#[cfg(feature = "testgen")]
impl Generate for EncryptionBegin {
    fn generate(g: &mut Gen) -> Self {
        {
            let v22 = g.string();
            let v23 = g.bytes();
            let v24 = g.bytes();
            EncryptionBegin {
                server_id: v22,
                public_key: v23,
                verify_token: v24,
            }
        }
    }
}
impl From<EncryptionBegin> for ClientboundLoginPacket {
    fn from(p: EncryptionBegin) -> Self {
        Self::EncryptionBegin(p)
    }
}

/// `success` (clientbound login, id 0x02).
#[derive(Debug, Clone, PartialEq)]
pub struct Success {
    /// `uuid`
    pub uuid: ByteString,
    /// `username`
    pub username: ByteString,
}
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 Decode for Success {
    fn decode(r: &mut Reader<'_>) -> DecodeResult<Self> {
        Ok({
            let v25 = r.read_string()?;
            let v26 = r.read_string()?;
            Success {
                uuid: v25,
                username: v26,
            }
        })
    }
}
impl Encode for Success {
    fn encode(&self, w: &mut BytesMut) -> EncodeResult {
        w.put_string(&(*self).uuid)?;
        w.put_string(&(*self).username)?;
        Ok(())
    }
}
#[cfg(feature = "testgen")]
impl Generate for Success {
    fn generate(g: &mut Gen) -> Self {
        {
            let v27 = g.string();
            let v28 = g.string();
            Success {
                uuid: v27,
                username: v28,
            }
        }
    }
}
impl From<Success> for ClientboundLoginPacket {
    fn from(p: Success) -> Self {
        Self::Success(p)
    }
}

/// `compress` (clientbound login, id 0x03).
#[derive(Debug, Clone, PartialEq)]
pub struct Compress {
    /// `threshold`
    pub threshold: i32,
}
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 Decode for Compress {
    fn decode(r: &mut Reader<'_>) -> DecodeResult<Self> {
        Ok({
            let v29 = r.read_varint()?;
            Compress { threshold: v29 }
        })
    }
}
impl Encode for Compress {
    fn encode(&self, w: &mut BytesMut) -> EncodeResult {
        w.put_varint((*self).threshold);
        Ok(())
    }
}
#[cfg(feature = "testgen")]
impl Generate for Compress {
    fn generate(g: &mut Gen) -> Self {
        {
            let v30 = (g.int_in(-2147483648, 2147483647, &[]) as i32);
            Compress { threshold: v30 }
        }
    }
}
impl From<Compress> for ClientboundLoginPacket {
    fn from(p: Compress) -> Self {
        Self::Compress(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),
}

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),
            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,
        }
    }
    fn name(&self) -> &'static str {
        match *self {
            Self::Disconnect(_) => "disconnect",
            Self::EncryptionBegin(_) => "encryption_begin",
            Self::Success(_) => "success",
            Self::Compress(_) => "compress",
        }
    }
    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),
        }
    }
    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"),
            _ => None,
        }
    }
}

impl ClientboundLoginPacket {
    /// All packet ids of this set, ascending.
    pub const IDS: &'static [i32] = &[
        id::DISCONNECT,
        id::ENCRYPTION_BEGIN,
        id::SUCCESS,
        id::COMPRESS,
    ];
    /// 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))),
            _ => None,
        }
    }
}