basalt_mc_protocol/packets/
login.rs1use basalt_derive::{Decode, Encode, EncodedSize, packet};
7use basalt_types::Decode as _;
8use basalt_types::Uuid;
9
10use crate::error::{Error, Result};
11
12#[derive(Debug, Clone, Default, PartialEq)]
15#[packet(id = 0x01)]
16pub struct ServerboundLoginEncryptionBegin {
17 #[field(length = "varint")]
18 pub shared_secret: Vec<u8>,
19 #[field(length = "varint")]
20 pub verify_token: Vec<u8>,
21}
22
23#[derive(Debug, Clone, Default, PartialEq)]
24#[packet(id = 0x03)]
25pub struct ServerboundLoginLoginAcknowledged;
26
27#[derive(Debug, Clone, Default, PartialEq)]
28#[packet(id = 0x02)]
29pub struct ServerboundLoginLoginPluginResponse {
30 #[field(varint)]
31 pub message_id: i32,
32 #[field(optional)]
33 pub data: Option<Vec<u8>>,
34}
35
36#[derive(Debug, Clone, Default, PartialEq)]
37#[packet(id = 0x00)]
38pub struct ServerboundLoginLoginStart {
39 pub username: String,
40 pub player_uuid: Uuid,
41}
42
43#[derive(Debug, Clone, Default, PartialEq)]
46#[packet(id = 0x03)]
47pub struct ClientboundLoginCompress {
48 #[field(varint)]
49 pub threshold: i32,
50}
51
52#[derive(Debug, Clone, Default, PartialEq)]
53#[packet(id = 0x00)]
54pub struct ClientboundLoginDisconnect {
55 pub reason: String,
56}
57
58#[derive(Debug, Clone, Default, PartialEq)]
59#[packet(id = 0x01)]
60pub struct ClientboundLoginEncryptionBegin {
61 pub server_id: String,
62 #[field(length = "varint")]
63 pub public_key: Vec<u8>,
64 #[field(length = "varint")]
65 pub verify_token: Vec<u8>,
66 pub should_authenticate: bool,
67}
68
69#[derive(Debug, Clone, Default, PartialEq)]
70#[packet(id = 0x04)]
71pub struct ClientboundLoginLoginPluginRequest {
72 #[field(varint)]
73 pub message_id: i32,
74 pub channel: String,
75 #[field(rest)]
76 pub data: Vec<u8>,
77}
78
79#[derive(Debug, Clone, Default, PartialEq, Encode, Decode, EncodedSize)]
81pub struct ClientboundLoginSuccessProperties {
82 pub name: String,
83 pub value: String,
84 #[field(optional)]
85 pub signature: Option<String>,
86}
87
88#[derive(Debug, Clone, Default, PartialEq)]
89#[packet(id = 0x02)]
90pub struct ClientboundLoginSuccess {
91 pub uuid: Uuid,
92 pub username: String,
93 #[field(length = "varint")]
94 pub properties: Vec<ClientboundLoginSuccessProperties>,
95}
96
97#[derive(Debug, Clone, PartialEq)]
99pub enum ServerboundLoginPacket {
100 EncryptionBegin(ServerboundLoginEncryptionBegin),
101 LoginAcknowledged(ServerboundLoginLoginAcknowledged),
102 LoginPluginResponse(ServerboundLoginLoginPluginResponse),
103 LoginStart(ServerboundLoginLoginStart),
104}
105
106impl ServerboundLoginPacket {
107 pub fn decode_by_id(id: i32, buf: &mut &[u8]) -> Result<Self> {
109 match id {
110 ServerboundLoginEncryptionBegin::PACKET_ID => Ok(Self::EncryptionBegin(
111 ServerboundLoginEncryptionBegin::decode(buf)?,
112 )),
113 ServerboundLoginLoginAcknowledged::PACKET_ID => Ok(Self::LoginAcknowledged(
114 ServerboundLoginLoginAcknowledged::decode(buf)?,
115 )),
116 ServerboundLoginLoginPluginResponse::PACKET_ID => Ok(Self::LoginPluginResponse(
117 ServerboundLoginLoginPluginResponse::decode(buf)?,
118 )),
119 ServerboundLoginLoginStart::PACKET_ID => {
120 Ok(Self::LoginStart(ServerboundLoginLoginStart::decode(buf)?))
121 }
122 _ => Err(Error::UnknownPacket { id, state: "login" }),
123 }
124 }
125}
126
127#[derive(Debug, Clone, PartialEq)]
129pub enum ClientboundLoginPacket {
130 Compress(ClientboundLoginCompress),
131 Disconnect(ClientboundLoginDisconnect),
132 EncryptionBegin(ClientboundLoginEncryptionBegin),
133 LoginPluginRequest(ClientboundLoginLoginPluginRequest),
134 Success(ClientboundLoginSuccess),
135}
136
137impl ClientboundLoginPacket {
138 pub fn decode_by_id(id: i32, buf: &mut &[u8]) -> Result<Self> {
140 match id {
141 ClientboundLoginCompress::PACKET_ID => {
142 Ok(Self::Compress(ClientboundLoginCompress::decode(buf)?))
143 }
144 ClientboundLoginDisconnect::PACKET_ID => {
145 Ok(Self::Disconnect(ClientboundLoginDisconnect::decode(buf)?))
146 }
147 ClientboundLoginEncryptionBegin::PACKET_ID => Ok(Self::EncryptionBegin(
148 ClientboundLoginEncryptionBegin::decode(buf)?,
149 )),
150 ClientboundLoginLoginPluginRequest::PACKET_ID => Ok(Self::LoginPluginRequest(
151 ClientboundLoginLoginPluginRequest::decode(buf)?,
152 )),
153 ClientboundLoginSuccess::PACKET_ID => {
154 Ok(Self::Success(ClientboundLoginSuccess::decode(buf)?))
155 }
156 _ => Err(Error::UnknownPacket { id, state: "login" }),
157 }
158 }
159}