ghast-protocol 0.1.1

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 handshaking 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 {
    /// `set_protocol`
    pub const SET_PROTOCOL: i32 = 0x00;
    /// `legacy_server_list_ping`
    pub const LEGACY_SERVER_LIST_PING: i32 = 0xfe;
}

/// `set_protocol` (serverbound handshaking, id 0x00).
#[derive(Debug, Clone, PartialEq)]
pub struct SetProtocol {
    /// `protocolVersion`
    pub protocol_version: i32,
    /// `serverHost`
    pub server_host: ByteString,
    /// `serverPort`
    pub server_port: u16,
    /// `nextState`
    pub next_state: i32,
}
impl crate::packet::Packet for SetProtocol {
    const ID: i32 = id::SET_PROTOCOL;
    const NAME: &'static str = "set_protocol";
    const STATE: State = State::Handshaking;
    const DIRECTION: Direction = Direction::Serverbound;
}
impl Decode for SetProtocol {
    fn decode(r: &mut Reader<'_>) -> DecodeResult<Self> {
        Ok({
            let v1 = r.read_varint()?;
            let v2 = r.read_string()?;
            let v3 = r.read_u16()?;
            let v4 = r.read_varint()?;
            SetProtocol {
                protocol_version: v1,
                server_host: v2,
                server_port: v3,
                next_state: v4,
            }
        })
    }
}
impl Encode for SetProtocol {
    fn encode(&self, w: &mut BytesMut) -> EncodeResult {
        w.put_varint((*self).protocol_version);
        w.put_string(&(*self).server_host)?;
        w.put_u16((*self).server_port);
        w.put_varint((*self).next_state);
        Ok(())
    }
}
#[cfg(feature = "testgen")]
impl Generate for SetProtocol {
    fn generate(g: &mut Gen) -> Self {
        {
            let v5 = (g.int_in(-2147483648, 2147483647, &[]) as i32);
            let v6 = g.string();
            let v7 = (g.int_in(0, 65535, &[]) as u16);
            let v8 = (g.int_in(-2147483648, 2147483647, &[]) as i32);
            SetProtocol {
                protocol_version: v5,
                server_host: v6,
                server_port: v7,
                next_state: v8,
            }
        }
    }
}
impl From<SetProtocol> for ServerboundHandshakingPacket {
    fn from(p: SetProtocol) -> Self {
        Self::SetProtocol(p)
    }
}

/// `legacy_server_list_ping` (serverbound handshaking, id 0xfe).
#[derive(Debug, Clone, PartialEq)]
pub struct LegacyServerListPing {
    /// `payload`
    pub payload: u8,
}
impl crate::packet::Packet for LegacyServerListPing {
    const ID: i32 = id::LEGACY_SERVER_LIST_PING;
    const NAME: &'static str = "legacy_server_list_ping";
    const STATE: State = State::Handshaking;
    const DIRECTION: Direction = Direction::Serverbound;
}
impl Decode for LegacyServerListPing {
    fn decode(r: &mut Reader<'_>) -> DecodeResult<Self> {
        Ok({
            let v9 = r.read_u8()?;
            LegacyServerListPing { payload: v9 }
        })
    }
}
impl Encode for LegacyServerListPing {
    fn encode(&self, w: &mut BytesMut) -> EncodeResult {
        w.put_u8((*self).payload);
        Ok(())
    }
}
#[cfg(feature = "testgen")]
impl Generate for LegacyServerListPing {
    fn generate(g: &mut Gen) -> Self {
        {
            let v10 = (g.int_in(0, 255, &[]) as u8);
            LegacyServerListPing { payload: v10 }
        }
    }
}
impl From<LegacyServerListPing> for ServerboundHandshakingPacket {
    fn from(p: LegacyServerListPing) -> Self {
        Self::LegacyServerListPing(p)
    }
}

/// Every Serverbound packet of the Handshaking state.
#[derive(Debug, Clone, PartialEq)]
pub enum ServerboundHandshakingPacket {
    /// `set_protocol`
    SetProtocol(SetProtocol),
    /// `legacy_server_list_ping`
    LegacyServerListPing(LegacyServerListPing),
}

impl PacketSet for ServerboundHandshakingPacket {
    const STATE: State = State::Handshaking;
    const DIRECTION: Direction = Direction::Serverbound;
    fn decode_raw(raw: &RawPacket) -> DecodeResult<Self> {
        match raw.id {
            id::SET_PROTOCOL => decode_exact(&raw.body).map(Self::SetProtocol),
            id::LEGACY_SERVER_LIST_PING => decode_exact(&raw.body).map(Self::LegacyServerListPing),
            other => Err(DecodeError::UnknownPacketId(other)),
        }
    }
    fn id(&self) -> i32 {
        match *self {
            Self::SetProtocol(_) => id::SET_PROTOCOL,
            Self::LegacyServerListPing(_) => id::LEGACY_SERVER_LIST_PING,
        }
    }
    fn name(&self) -> &'static str {
        match *self {
            Self::SetProtocol(_) => "set_protocol",
            Self::LegacyServerListPing(_) => "legacy_server_list_ping",
        }
    }
    fn encode_body(&self, w: &mut BytesMut) -> EncodeResult {
        match *self {
            Self::SetProtocol(ref p) => p.encode(w),
            Self::LegacyServerListPing(ref p) => p.encode(w),
        }
    }
    fn name_of(id: i32) -> Option<&'static str> {
        match id {
            id::SET_PROTOCOL => Some("set_protocol"),
            id::LEGACY_SERVER_LIST_PING => Some("legacy_server_list_ping"),
            _ => None,
        }
    }
}

impl ServerboundHandshakingPacket {
    /// All packet ids of this set, ascending.
    pub const IDS: &'static [i32] = &[id::SET_PROTOCOL, id::LEGACY_SERVER_LIST_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::SET_PROTOCOL => Some(Self::SetProtocol(<SetProtocol as Generate>::generate(g))),
            id::LEGACY_SERVER_LIST_PING => Some(Self::LegacyServerListPing(
                <LegacyServerListPing as Generate>::generate(g),
            )),
            _ => None,
        }
    }
}