Skip to main content

ghast_protocol/generated/v1_8/login/
clientbound.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//! Clientbound packets of the login 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    /// `disconnect`
31    pub const DISCONNECT: i32 = 0x00;
32    /// `encryption_begin`
33    pub const ENCRYPTION_BEGIN: i32 = 0x01;
34    /// `success`
35    pub const SUCCESS: i32 = 0x02;
36    /// `compress`
37    pub const COMPRESS: i32 = 0x03;
38}
39
40/// `disconnect` (clientbound login, id 0x00).
41#[derive(Debug, Clone, PartialEq)]
42pub struct Disconnect {
43    /// `reason`
44    pub reason: ByteString,
45}
46impl crate::packet::Packet for Disconnect {
47    const ID: i32 = id::DISCONNECT;
48    const NAME: &'static str = "disconnect";
49    const STATE: State = State::Login;
50    const DIRECTION: Direction = Direction::Clientbound;
51}
52impl Decode for Disconnect {
53    fn decode(r: &mut Reader<'_>) -> DecodeResult<Self> {
54        Ok({
55            let v17 = r.read_string()?;
56            Disconnect { reason: v17 }
57        })
58    }
59}
60impl Encode for Disconnect {
61    fn encode(&self, w: &mut BytesMut) -> EncodeResult {
62        w.put_string(&(*self).reason)?;
63        Ok(())
64    }
65}
66#[cfg(feature = "testgen")]
67impl Generate for Disconnect {
68    fn generate(g: &mut Gen) -> Self {
69        {
70            let v18 = g.string();
71            Disconnect { reason: v18 }
72        }
73    }
74}
75impl From<Disconnect> for ClientboundLoginPacket {
76    fn from(p: Disconnect) -> Self {
77        Self::Disconnect(p)
78    }
79}
80
81/// `encryption_begin` (clientbound login, id 0x01).
82#[derive(Debug, Clone, PartialEq)]
83pub struct EncryptionBegin {
84    /// `serverId`
85    pub server_id: ByteString,
86    /// `publicKey`
87    pub public_key: Bytes,
88    /// `verifyToken`
89    pub verify_token: Bytes,
90}
91impl crate::packet::Packet for EncryptionBegin {
92    const ID: i32 = id::ENCRYPTION_BEGIN;
93    const NAME: &'static str = "encryption_begin";
94    const STATE: State = State::Login;
95    const DIRECTION: Direction = Direction::Clientbound;
96}
97impl Decode for EncryptionBegin {
98    fn decode(r: &mut Reader<'_>) -> DecodeResult<Self> {
99        Ok({
100            let v19 = r.read_string()?;
101            let v20 = r.read_byte_array()?;
102            let v21 = r.read_byte_array()?;
103            EncryptionBegin {
104                server_id: v19,
105                public_key: v20,
106                verify_token: v21,
107            }
108        })
109    }
110}
111impl Encode for EncryptionBegin {
112    fn encode(&self, w: &mut BytesMut) -> EncodeResult {
113        w.put_string(&(*self).server_id)?;
114        w.put_byte_array(&(*self).public_key)?;
115        w.put_byte_array(&(*self).verify_token)?;
116        Ok(())
117    }
118}
119#[cfg(feature = "testgen")]
120impl Generate for EncryptionBegin {
121    fn generate(g: &mut Gen) -> Self {
122        {
123            let v22 = g.string();
124            let v23 = g.bytes();
125            let v24 = g.bytes();
126            EncryptionBegin {
127                server_id: v22,
128                public_key: v23,
129                verify_token: v24,
130            }
131        }
132    }
133}
134impl From<EncryptionBegin> for ClientboundLoginPacket {
135    fn from(p: EncryptionBegin) -> Self {
136        Self::EncryptionBegin(p)
137    }
138}
139
140/// `success` (clientbound login, id 0x02).
141#[derive(Debug, Clone, PartialEq)]
142pub struct Success {
143    /// `uuid`
144    pub uuid: ByteString,
145    /// `username`
146    pub username: ByteString,
147}
148impl crate::packet::Packet for Success {
149    const ID: i32 = id::SUCCESS;
150    const NAME: &'static str = "success";
151    const STATE: State = State::Login;
152    const DIRECTION: Direction = Direction::Clientbound;
153}
154impl Decode for Success {
155    fn decode(r: &mut Reader<'_>) -> DecodeResult<Self> {
156        Ok({
157            let v25 = r.read_string()?;
158            let v26 = r.read_string()?;
159            Success {
160                uuid: v25,
161                username: v26,
162            }
163        })
164    }
165}
166impl Encode for Success {
167    fn encode(&self, w: &mut BytesMut) -> EncodeResult {
168        w.put_string(&(*self).uuid)?;
169        w.put_string(&(*self).username)?;
170        Ok(())
171    }
172}
173#[cfg(feature = "testgen")]
174impl Generate for Success {
175    fn generate(g: &mut Gen) -> Self {
176        {
177            let v27 = g.string();
178            let v28 = g.string();
179            Success {
180                uuid: v27,
181                username: v28,
182            }
183        }
184    }
185}
186impl From<Success> for ClientboundLoginPacket {
187    fn from(p: Success) -> Self {
188        Self::Success(p)
189    }
190}
191
192/// `compress` (clientbound login, id 0x03).
193#[derive(Debug, Clone, PartialEq)]
194pub struct Compress {
195    /// `threshold`
196    pub threshold: i32,
197}
198impl crate::packet::Packet for Compress {
199    const ID: i32 = id::COMPRESS;
200    const NAME: &'static str = "compress";
201    const STATE: State = State::Login;
202    const DIRECTION: Direction = Direction::Clientbound;
203}
204impl Decode for Compress {
205    fn decode(r: &mut Reader<'_>) -> DecodeResult<Self> {
206        Ok({
207            let v29 = r.read_varint()?;
208            Compress { threshold: v29 }
209        })
210    }
211}
212impl Encode for Compress {
213    fn encode(&self, w: &mut BytesMut) -> EncodeResult {
214        w.put_varint((*self).threshold);
215        Ok(())
216    }
217}
218#[cfg(feature = "testgen")]
219impl Generate for Compress {
220    fn generate(g: &mut Gen) -> Self {
221        {
222            let v30 = (g.int_in(-2147483648, 2147483647, &[]) as i32);
223            Compress { threshold: v30 }
224        }
225    }
226}
227impl From<Compress> for ClientboundLoginPacket {
228    fn from(p: Compress) -> Self {
229        Self::Compress(p)
230    }
231}
232
233/// Every Clientbound packet of the Login state.
234#[derive(Debug, Clone, PartialEq)]
235pub enum ClientboundLoginPacket {
236    /// `disconnect`
237    Disconnect(Disconnect),
238    /// `encryption_begin`
239    EncryptionBegin(EncryptionBegin),
240    /// `success`
241    Success(Success),
242    /// `compress`
243    Compress(Compress),
244}
245
246impl PacketSet for ClientboundLoginPacket {
247    const STATE: State = State::Login;
248    const DIRECTION: Direction = Direction::Clientbound;
249    fn decode_raw(raw: &RawPacket) -> DecodeResult<Self> {
250        match raw.id {
251            id::DISCONNECT => decode_exact(&raw.body).map(Self::Disconnect),
252            id::ENCRYPTION_BEGIN => decode_exact(&raw.body).map(Self::EncryptionBegin),
253            id::SUCCESS => decode_exact(&raw.body).map(Self::Success),
254            id::COMPRESS => decode_exact(&raw.body).map(Self::Compress),
255            other => Err(DecodeError::UnknownPacketId(other)),
256        }
257    }
258    fn id(&self) -> i32 {
259        match *self {
260            Self::Disconnect(_) => id::DISCONNECT,
261            Self::EncryptionBegin(_) => id::ENCRYPTION_BEGIN,
262            Self::Success(_) => id::SUCCESS,
263            Self::Compress(_) => id::COMPRESS,
264        }
265    }
266    fn name(&self) -> &'static str {
267        match *self {
268            Self::Disconnect(_) => "disconnect",
269            Self::EncryptionBegin(_) => "encryption_begin",
270            Self::Success(_) => "success",
271            Self::Compress(_) => "compress",
272        }
273    }
274    fn encode_body(&self, w: &mut BytesMut) -> EncodeResult {
275        match *self {
276            Self::Disconnect(ref p) => p.encode(w),
277            Self::EncryptionBegin(ref p) => p.encode(w),
278            Self::Success(ref p) => p.encode(w),
279            Self::Compress(ref p) => p.encode(w),
280        }
281    }
282    fn name_of(id: i32) -> Option<&'static str> {
283        match id {
284            id::DISCONNECT => Some("disconnect"),
285            id::ENCRYPTION_BEGIN => Some("encryption_begin"),
286            id::SUCCESS => Some("success"),
287            id::COMPRESS => Some("compress"),
288            _ => None,
289        }
290    }
291}
292
293impl ClientboundLoginPacket {
294    /// All packet ids of this set, ascending.
295    pub const IDS: &'static [i32] = &[
296        id::DISCONNECT,
297        id::ENCRYPTION_BEGIN,
298        id::SUCCESS,
299        id::COMPRESS,
300    ];
301    /// Generates a random packet with the given id (test support).
302    #[cfg(feature = "testgen")]
303    pub fn generate_by_id(id: i32, g: &mut Gen) -> Option<Self> {
304        match id {
305            id::DISCONNECT => Some(Self::Disconnect(<Disconnect as Generate>::generate(g))),
306            id::ENCRYPTION_BEGIN => Some(Self::EncryptionBegin(
307                <EncryptionBegin as Generate>::generate(g),
308            )),
309            id::SUCCESS => Some(Self::Success(<Success as Generate>::generate(g))),
310            id::COMPRESS => Some(Self::Compress(<Compress as Generate>::generate(g))),
311            _ => None,
312        }
313    }
314}