ghast-protocol 0.2.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 ../NOTICE.

//! Serverbound 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 {
    /// `login_start`
    pub const LOGIN_START: i32 = 0x00;
    /// `encryption_begin`
    pub const ENCRYPTION_BEGIN: i32 = 0x01;
}

/// `login_start` (serverbound login, id 0x00).
#[derive(Debug, Clone, PartialEq)]
pub struct LoginStart {
    /// `username`
    pub username: ByteString,
}
impl crate::packet::Packet for LoginStart {
    const ID: i32 = id::LOGIN_START;
    const NAME: &'static str = "login_start";
    const STATE: State = State::Login;
    const DIRECTION: Direction = Direction::Serverbound;
}
impl Decode for LoginStart {
    fn decode(r: &mut Reader<'_>) -> DecodeResult<Self> {
        Ok({
            let v31 = r.read_string()?;
            LoginStart { username: v31 }
        })
    }
}
impl Encode for LoginStart {
    fn encode(&self, w: &mut BytesMut) -> EncodeResult {
        w.put_string(&(*self).username)?;
        Ok(())
    }
}
#[cfg(feature = "testgen")]
impl Generate for LoginStart {
    fn generate(g: &mut Gen) -> Self {
        {
            let v32 = g.string();
            LoginStart { username: v32 }
        }
    }
}
impl From<LoginStart> for ServerboundLoginPacket {
    fn from(p: LoginStart) -> Self {
        Self::LoginStart(p)
    }
}

/// `encryption_begin` (serverbound login, id 0x01).
#[derive(Debug, Clone, PartialEq)]
pub struct EncryptionBegin {
    /// `sharedSecret`
    pub shared_secret: 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::Serverbound;
}
impl Decode for EncryptionBegin {
    fn decode(r: &mut Reader<'_>) -> DecodeResult<Self> {
        Ok({
            let v33 = r.read_byte_array()?;
            let v34 = r.read_byte_array()?;
            EncryptionBegin {
                shared_secret: v33,
                verify_token: v34,
            }
        })
    }
}
impl Encode for EncryptionBegin {
    fn encode(&self, w: &mut BytesMut) -> EncodeResult {
        w.put_byte_array(&(*self).shared_secret)?;
        w.put_byte_array(&(*self).verify_token)?;
        Ok(())
    }
}
#[cfg(feature = "testgen")]
impl Generate for EncryptionBegin {
    fn generate(g: &mut Gen) -> Self {
        {
            let v35 = g.bytes();
            let v36 = g.bytes();
            EncryptionBegin {
                shared_secret: v35,
                verify_token: v36,
            }
        }
    }
}
impl From<EncryptionBegin> for ServerboundLoginPacket {
    fn from(p: EncryptionBegin) -> Self {
        Self::EncryptionBegin(p)
    }
}

/// Every Serverbound packet of the Login state.
#[derive(Debug, Clone, PartialEq)]
pub enum ServerboundLoginPacket {
    /// `login_start`
    LoginStart(LoginStart),
    /// `encryption_begin`
    EncryptionBegin(EncryptionBegin),
}

impl PacketSet for ServerboundLoginPacket {
    const STATE: State = State::Login;
    const DIRECTION: Direction = Direction::Serverbound;
    fn decode_raw(raw: &RawPacket) -> DecodeResult<Self> {
        match raw.id {
            id::LOGIN_START => decode_exact(&raw.body).map(Self::LoginStart),
            id::ENCRYPTION_BEGIN => decode_exact(&raw.body).map(Self::EncryptionBegin),
            other => Err(DecodeError::UnknownPacketId(other)),
        }
    }
    fn id(&self) -> i32 {
        match *self {
            Self::LoginStart(_) => id::LOGIN_START,
            Self::EncryptionBegin(_) => id::ENCRYPTION_BEGIN,
        }
    }
    fn name(&self) -> &'static str {
        match *self {
            Self::LoginStart(_) => "login_start",
            Self::EncryptionBegin(_) => "encryption_begin",
        }
    }
    fn encode_body(&self, w: &mut BytesMut) -> EncodeResult {
        match *self {
            Self::LoginStart(ref p) => p.encode(w),
            Self::EncryptionBegin(ref p) => p.encode(w),
        }
    }
    fn name_of(id: i32) -> Option<&'static str> {
        match id {
            id::LOGIN_START => Some("login_start"),
            id::ENCRYPTION_BEGIN => Some("encryption_begin"),
            _ => None,
        }
    }
}

impl ServerboundLoginPacket {
    /// All packet ids of this set, ascending.
    pub const IDS: &'static [i32] = &[id::LOGIN_START, id::ENCRYPTION_BEGIN];
    /// 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::LOGIN_START => Some(Self::LoginStart(<LoginStart as Generate>::generate(g))),
            id::ENCRYPTION_BEGIN => Some(Self::EncryptionBegin(
                <EncryptionBegin as Generate>::generate(g),
            )),
            _ => None,
        }
    }
}