Compare commits
5 Commits
196592ae78
...
bcf1038148
Author | SHA1 | Date |
---|---|---|
![]() |
bcf1038148 | |
![]() |
5ed241d868 | |
![]() |
84218292e7 | |
![]() |
5d40ddd58e | |
![]() |
11496b308d |
108
src/login.rs
108
src/login.rs
|
@ -10,6 +10,7 @@ pub mod clientbound {
|
|||
LoginSuccess(LoginSuccess),
|
||||
SetCompression(SetCompression),
|
||||
PluginRequest(PluginRequest),
|
||||
CookieRequest(CookieRequest),
|
||||
}
|
||||
|
||||
impl Login {
|
||||
|
@ -31,6 +32,9 @@ pub mod clientbound {
|
|||
} else if packet_id == PluginRequest::packet_id() {
|
||||
return Ok(Self::PluginRequest(
|
||||
PluginRequest::get(&mut data)?))
|
||||
} else if packet_id == CookieRequest::packet_id() {
|
||||
return Ok(Self::CookieRequest(
|
||||
CookieRequest::get(&mut data)?))
|
||||
} else {
|
||||
return Err(Box::new(PacketError::InvalidPacketId))
|
||||
}
|
||||
|
@ -97,6 +101,7 @@ pub mod clientbound {
|
|||
pub uuid: u128,
|
||||
pub username: String,
|
||||
pub properties: Vec<LoginSuccessProperty>,
|
||||
pub strict_error_handling: bool,
|
||||
}
|
||||
|
||||
impl Packet for LoginSuccess {
|
||||
|
@ -108,6 +113,7 @@ pub mod clientbound {
|
|||
uuid: mc_types::get_uuid(&mut data),
|
||||
username: mc_types::get_string(&mut data)?,
|
||||
properties: LoginSuccessProperty::get_array(&mut data)?,
|
||||
strict_error_handling: mc_types::get_bool(&mut data),
|
||||
})
|
||||
}
|
||||
|
||||
|
@ -118,6 +124,8 @@ pub mod clientbound {
|
|||
data.append(&mut mc_types::convert_string(&self.username));
|
||||
data.append(&mut LoginSuccessProperty::convert_array(
|
||||
&mut self.properties.clone()));
|
||||
data.append(&mut mc_types::convert_bool(
|
||||
self.strict_error_handling));
|
||||
|
||||
data
|
||||
}
|
||||
|
@ -228,6 +236,30 @@ pub mod clientbound {
|
|||
|
||||
}
|
||||
|
||||
pub struct CookieRequest {
|
||||
pub key: String,
|
||||
}
|
||||
|
||||
impl Packet for CookieRequest {
|
||||
|
||||
fn packet_id() -> i32 {5}
|
||||
|
||||
fn get(mut data: &mut Vec<u8>) -> Result<Self> {
|
||||
Ok(Self {
|
||||
key: mc_types::get_string(&mut data)?,
|
||||
})
|
||||
}
|
||||
|
||||
fn convert(&self) -> Vec<u8> {
|
||||
let mut data: Vec<u8> = vec![];
|
||||
data.append(&mut mc_types::convert_var_int(Self::packet_id()));
|
||||
data.append(&mut mc_types::convert_string(&self.key));
|
||||
|
||||
data
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
pub mod serverbound {
|
||||
|
@ -238,6 +270,8 @@ pub mod serverbound {
|
|||
LoginStart(LoginStart),
|
||||
EncryptionResponse(EncryptionResponse),
|
||||
PluginResponse(PluginResponse),
|
||||
Acknowledged(Acknowledged),
|
||||
CookieResponse(CookieResponse),
|
||||
}
|
||||
|
||||
impl Login {
|
||||
|
@ -254,6 +288,12 @@ pub mod serverbound {
|
|||
} else if packet_id == PluginResponse::packet_id() {
|
||||
return Ok(Self::PluginResponse(
|
||||
PluginResponse::get(&mut data)?))
|
||||
} else if packet_id == Acknowledged::packet_id() {
|
||||
return Ok(Self::Acknowledged(
|
||||
Acknowledged::get(&mut data)?))
|
||||
} else if packet_id == CookieResponse::packet_id() {
|
||||
return Ok(Self::CookieResponse(
|
||||
CookieResponse::get(&mut data)?))
|
||||
} else {
|
||||
return Err(Box::new(PacketError::InvalidPacketId))
|
||||
}
|
||||
|
@ -262,7 +302,7 @@ pub mod serverbound {
|
|||
|
||||
pub struct LoginStart {
|
||||
pub name: String,
|
||||
pub player_uuid: Option<u128>,
|
||||
pub player_uuid: u128,
|
||||
}
|
||||
|
||||
impl Packet for LoginStart {
|
||||
|
@ -271,11 +311,7 @@ pub mod serverbound {
|
|||
|
||||
fn get(mut data: &mut Vec<u8>) -> Result<Self> {
|
||||
let name = mc_types::get_string(&mut data)?;
|
||||
let has_uuid = mc_types::get_bool(&mut data);
|
||||
let mut player_uuid: Option<u128> = None;
|
||||
if has_uuid {
|
||||
player_uuid = Some(mc_types::get_uuid(&mut data));
|
||||
}
|
||||
let player_uuid: u128 = mc_types::get_uuid(&mut data);
|
||||
Ok(Self {
|
||||
name,
|
||||
player_uuid,
|
||||
|
@ -286,13 +322,7 @@ pub mod serverbound {
|
|||
let mut data: Vec<u8> = vec![];
|
||||
data.append(&mut mc_types::convert_var_int(Self::packet_id()));
|
||||
data.append(&mut mc_types::convert_string(&self.name));
|
||||
match self.player_uuid {
|
||||
Some(value) => {
|
||||
data.append(&mut mc_types::convert_bool(true));
|
||||
data.append(&mut mc_types::convert_uuid(value));
|
||||
},
|
||||
None => data.append(&mut mc_types::convert_bool(false))
|
||||
}
|
||||
data.append(&mut mc_types::convert_uuid(self.player_uuid));
|
||||
|
||||
data
|
||||
}
|
||||
|
@ -358,4 +388,56 @@ pub mod serverbound {
|
|||
|
||||
}
|
||||
|
||||
pub struct Acknowledged {}
|
||||
|
||||
impl Packet for Acknowledged {
|
||||
|
||||
fn packet_id() -> i32 {3}
|
||||
|
||||
fn get(_data: &mut Vec<u8>) -> Result<Self> {
|
||||
Ok(Self {})
|
||||
}
|
||||
|
||||
fn convert(&self) -> Vec<u8> {
|
||||
let mut data: Vec<u8> = vec![];
|
||||
data.append(&mut mc_types::convert_var_int(Self::packet_id()));
|
||||
|
||||
data
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
pub struct CookieResponse {
|
||||
pub key: String,
|
||||
pub has_payload: bool,
|
||||
pub payload_load: i32,
|
||||
pub payload: Vec<u8>,
|
||||
}
|
||||
|
||||
impl Packet for CookieResponse {
|
||||
|
||||
fn packet_id() -> i32 {4}
|
||||
|
||||
fn get(mut data: &mut Vec<u8>) -> Result<Self> {
|
||||
Ok(Self {
|
||||
key: mc_types::get_string(&mut data)?,
|
||||
has_payload: mc_types::get_bool(&mut data),
|
||||
payload_load: mc_types::get_var_int(&mut data)?,
|
||||
payload: data.clone(),
|
||||
})
|
||||
}
|
||||
|
||||
fn convert(&self) -> Vec<u8> {
|
||||
let mut data: Vec<u8> = vec![];
|
||||
data.append(&mut mc_types::convert_var_int(Self::packet_id()));
|
||||
data.append(&mut mc_types::convert_string(&self.key));
|
||||
data.append(&mut mc_types::convert_bool(self.has_payload));
|
||||
data.append(&mut mc_types::convert_var_int(self.payload_load));
|
||||
data.append(&mut self.payload.clone());
|
||||
|
||||
data
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
}
|
||||
|
|
|
@ -20,8 +20,8 @@ use crate::play::Play;
|
|||
|
||||
pub type Result<T> = std::result::Result<T, Box<dyn Error>>;
|
||||
|
||||
pub const VERSION_NAME: &str = "1.19.4";
|
||||
pub const VERSION_PROTOCOL: i32 = 762;
|
||||
pub const VERSION_NAME: &str = "1.21";
|
||||
pub const VERSION_PROTOCOL: i32 = 767;
|
||||
|
||||
const SEGMENT_BITS: u8 = 0x7F;
|
||||
const CONTINUE_BIT: u8 = 0x80;
|
||||
|
|
|
@ -2,7 +2,9 @@
|
|||
|
||||
pub mod clientbound {
|
||||
|
||||
use serde::{Serialize, Deserialize};
|
||||
use serde::{Serialize, Deserialize, Serializer, Deserializer};
|
||||
use serde::de::{self, Visitor, MapAccess};
|
||||
use std::fmt;
|
||||
|
||||
use crate::mc_types::{self, Result, Packet, PacketError};
|
||||
|
||||
|
@ -12,6 +14,64 @@ pub mod clientbound {
|
|||
pub protocol: i32,
|
||||
}
|
||||
|
||||
pub enum StatusDescription {
|
||||
String(String),
|
||||
Chat(mc_types::Chat),
|
||||
}
|
||||
|
||||
impl Serialize for StatusDescription {
|
||||
fn serialize<S>(&self, serializer: S)
|
||||
-> std::result::Result<S::Ok, S::Error>
|
||||
where
|
||||
S: Serializer,
|
||||
{
|
||||
match *self {
|
||||
StatusDescription::String(ref s) => serializer.serialize_str(s),
|
||||
StatusDescription::Chat(ref c) => c.serialize(serializer),
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
impl<'de> Deserialize<'de> for StatusDescription {
|
||||
fn deserialize<D>(deserializer: D) -> std::result::Result<Self, D::Error>
|
||||
where
|
||||
D: Deserializer<'de>,
|
||||
{
|
||||
struct StatusDescriptionVisitor;
|
||||
|
||||
impl<'de> Visitor<'de> for StatusDescriptionVisitor {
|
||||
type Value = StatusDescription;
|
||||
|
||||
fn expecting(&self, formatter: &mut fmt::Formatter)
|
||||
-> fmt::Result
|
||||
{
|
||||
formatter.write_str(
|
||||
"a string or a map representing a Chat object")
|
||||
}
|
||||
|
||||
fn visit_str<E>(self, value: &str)
|
||||
-> std::result::Result<StatusDescription, E>
|
||||
where
|
||||
E: de::Error,
|
||||
{
|
||||
Ok(StatusDescription::String(value.to_string()))
|
||||
}
|
||||
|
||||
fn visit_map<M>(self, map: M)
|
||||
-> std::result::Result<StatusDescription, M::Error>
|
||||
where
|
||||
M: MapAccess<'de>,
|
||||
{
|
||||
let chat = mc_types::Chat::deserialize(
|
||||
de::value::MapAccessDeserializer::new(map))?;
|
||||
Ok(StatusDescription::Chat(chat))
|
||||
}
|
||||
}
|
||||
|
||||
deserializer.deserialize_any(StatusDescriptionVisitor)
|
||||
}
|
||||
}
|
||||
|
||||
#[derive(Serialize, Deserialize)]
|
||||
pub struct StatusPlayerInfo {
|
||||
pub name: String,
|
||||
|
@ -30,7 +90,7 @@ pub mod clientbound {
|
|||
#[derive(Serialize, Deserialize)]
|
||||
pub struct StatusResponseData {
|
||||
pub version: StatusVersion,
|
||||
pub description: mc_types::Chat,
|
||||
pub description: StatusDescription,
|
||||
pub players: StatusPlayers,
|
||||
#[serde(skip_serializing_if = "Option::is_none")]
|
||||
pub favicon: Option<String>,
|
||||
|
|
Loading…
Reference in New Issue