Skip to main content

ghast_protocol/generated/v1_8/handshaking/
serverbound.rs

1// @generated by ghast-codegen-protocol from PrismarineJS minecraft-data 3.119.0
2// (data/pc/1.8/protocol.json, Minecraft 1.8.9). Do not edit; run `cargo run -p ghast-codegen-protocol`.
3// minecraft-data is MIT licensed, Copyright (c) 2015 PrismarineJS; see ../NOTICE.
4
5//! Serverbound packets of the handshaking state.
6
7#![allow(
8    unused_imports,
9    unused_variables,
10    unused_mut,
11    unused_parens,
12    unused_braces,
13    clippy::all,
14    clippy::pedantic,
15    non_camel_case_types,
16    non_snake_case
17)]
18use crate::codec::{Decode, DecodeResult, Encode, EncodeResult, McWrite, Reader, decode_exact};
19use crate::error::{DecodeError, EncodeError};
20use crate::nbt::RawNbt;
21use crate::packet::{Direction, PacketSet, RawPacket, State};
22#[cfg(feature = "testgen")]
23use crate::testgen::{Gen, Generate};
24use crate::types::{BlockPos, ByteString, PositionLayout, Uuid};
25use crate::v1_8::types;
26use bytes::{BufMut, Bytes, BytesMut};
27
28/// Packet ids.
29pub mod id {
30    /// `set_protocol`
31    pub const SET_PROTOCOL: i32 = 0x00;
32    /// `legacy_server_list_ping`
33    pub const LEGACY_SERVER_LIST_PING: i32 = 0xfe;
34}
35
36/// `set_protocol` (serverbound handshaking, id 0x00).
37#[derive(Debug, Clone, PartialEq)]
38pub struct SetProtocol {
39    /// `protocolVersion`
40    pub protocol_version: i32,
41    /// `serverHost`
42    pub server_host: ByteString,
43    /// `serverPort`
44    pub server_port: u16,
45    /// `nextState`
46    pub next_state: i32,
47}
48impl crate::packet::Packet for SetProtocol {
49    const ID: i32 = id::SET_PROTOCOL;
50    const NAME: &'static str = "set_protocol";
51    const STATE: State = State::Handshaking;
52    const DIRECTION: Direction = Direction::Serverbound;
53}
54impl Decode for SetProtocol {
55    fn decode(r: &mut Reader<'_>) -> DecodeResult<Self> {
56        Ok({
57            let v1 = r.read_varint()?;
58            let v2 = r.read_string()?;
59            let v3 = r.read_u16()?;
60            let v4 = r.read_varint()?;
61            SetProtocol {
62                protocol_version: v1,
63                server_host: v2,
64                server_port: v3,
65                next_state: v4,
66            }
67        })
68    }
69}
70impl Encode for SetProtocol {
71    fn encode(&self, w: &mut BytesMut) -> EncodeResult {
72        w.put_varint((*self).protocol_version);
73        w.put_string(&(*self).server_host)?;
74        w.put_u16((*self).server_port);
75        w.put_varint((*self).next_state);
76        Ok(())
77    }
78}
79#[cfg(feature = "testgen")]
80impl Generate for SetProtocol {
81    fn generate(g: &mut Gen) -> Self {
82        {
83            let v5 = (g.int_in(-2147483648, 2147483647, &[]) as i32);
84            let v6 = g.string();
85            let v7 = (g.int_in(0, 65535, &[]) as u16);
86            let v8 = (g.int_in(-2147483648, 2147483647, &[]) as i32);
87            SetProtocol {
88                protocol_version: v5,
89                server_host: v6,
90                server_port: v7,
91                next_state: v8,
92            }
93        }
94    }
95}
96impl From<SetProtocol> for ServerboundHandshakingPacket {
97    fn from(p: SetProtocol) -> Self {
98        Self::SetProtocol(p)
99    }
100}
101
102/// `legacy_server_list_ping` (serverbound handshaking, id 0xfe).
103#[derive(Debug, Clone, PartialEq)]
104pub struct LegacyServerListPing {
105    /// `payload`
106    pub payload: u8,
107}
108impl crate::packet::Packet for LegacyServerListPing {
109    const ID: i32 = id::LEGACY_SERVER_LIST_PING;
110    const NAME: &'static str = "legacy_server_list_ping";
111    const STATE: State = State::Handshaking;
112    const DIRECTION: Direction = Direction::Serverbound;
113}
114impl Decode for LegacyServerListPing {
115    fn decode(r: &mut Reader<'_>) -> DecodeResult<Self> {
116        Ok({
117            let v9 = r.read_u8()?;
118            LegacyServerListPing { payload: v9 }
119        })
120    }
121}
122impl Encode for LegacyServerListPing {
123    fn encode(&self, w: &mut BytesMut) -> EncodeResult {
124        w.put_u8((*self).payload);
125        Ok(())
126    }
127}
128#[cfg(feature = "testgen")]
129impl Generate for LegacyServerListPing {
130    fn generate(g: &mut Gen) -> Self {
131        {
132            let v10 = (g.int_in(0, 255, &[]) as u8);
133            LegacyServerListPing { payload: v10 }
134        }
135    }
136}
137impl From<LegacyServerListPing> for ServerboundHandshakingPacket {
138    fn from(p: LegacyServerListPing) -> Self {
139        Self::LegacyServerListPing(p)
140    }
141}
142
143/// Every Serverbound packet of the Handshaking state.
144#[derive(Debug, Clone, PartialEq)]
145pub enum ServerboundHandshakingPacket {
146    /// `set_protocol`
147    SetProtocol(SetProtocol),
148    /// `legacy_server_list_ping`
149    LegacyServerListPing(LegacyServerListPing),
150}
151
152impl PacketSet for ServerboundHandshakingPacket {
153    const STATE: State = State::Handshaking;
154    const DIRECTION: Direction = Direction::Serverbound;
155    fn decode_raw(raw: &RawPacket) -> DecodeResult<Self> {
156        match raw.id {
157            id::SET_PROTOCOL => decode_exact(&raw.body).map(Self::SetProtocol),
158            id::LEGACY_SERVER_LIST_PING => decode_exact(&raw.body).map(Self::LegacyServerListPing),
159            other => Err(DecodeError::UnknownPacketId(other)),
160        }
161    }
162    fn id(&self) -> i32 {
163        match *self {
164            Self::SetProtocol(_) => id::SET_PROTOCOL,
165            Self::LegacyServerListPing(_) => id::LEGACY_SERVER_LIST_PING,
166        }
167    }
168    fn name(&self) -> &'static str {
169        match *self {
170            Self::SetProtocol(_) => "set_protocol",
171            Self::LegacyServerListPing(_) => "legacy_server_list_ping",
172        }
173    }
174    fn encode_body(&self, w: &mut BytesMut) -> EncodeResult {
175        match *self {
176            Self::SetProtocol(ref p) => p.encode(w),
177            Self::LegacyServerListPing(ref p) => p.encode(w),
178        }
179    }
180    fn name_of(id: i32) -> Option<&'static str> {
181        match id {
182            id::SET_PROTOCOL => Some("set_protocol"),
183            id::LEGACY_SERVER_LIST_PING => Some("legacy_server_list_ping"),
184            _ => None,
185        }
186    }
187}
188
189impl ServerboundHandshakingPacket {
190    /// All packet ids of this set, ascending.
191    pub const IDS: &'static [i32] = &[id::SET_PROTOCOL, id::LEGACY_SERVER_LIST_PING];
192    /// Generates a random packet with the given id (test support).
193    #[cfg(feature = "testgen")]
194    pub fn generate_by_id(id: i32, g: &mut Gen) -> Option<Self> {
195        match id {
196            id::SET_PROTOCOL => Some(Self::SetProtocol(<SetProtocol as Generate>::generate(g))),
197            id::LEGACY_SERVER_LIST_PING => Some(Self::LegacyServerListPing(
198                <LegacyServerListPing as Generate>::generate(g),
199            )),
200            _ => None,
201        }
202    }
203}