diff --git a/purple_cello_mc_derive/src/lib.rs b/purple_cello_mc_derive/src/lib.rs index 2bb18b7..ffef8d8 100644 --- a/purple_cello_mc_derive/src/lib.rs +++ b/purple_cello_mc_derive/src/lib.rs @@ -50,7 +50,7 @@ pub fn derive_packet(input: TokenStream) -> TokenStream { let read_fields = fields.named.iter().map(|f| { let fname = &f.ident; let fty = &f.ty; - quote! { #fname: <#fty as McType>::get(data)? } + quote! { #fname: <#fty as mc_types::McType>::get(data)? } }); let write_fields = fields.named.iter().map(|f| { @@ -63,9 +63,9 @@ pub fn derive_packet(input: TokenStream) -> TokenStream { let expanded = quote! { impl Packet for #name { - fn packet_id() -> i32 { #packet_id } + fn packet_id() -> mc_types::VarInt { mc_types::VarInt(#packet_id) } - fn get(data: &mut Vec) -> Result { + fn get(data: &mut Vec) -> mc_types::Result { Ok(Self { #(#read_fields),* }) @@ -73,7 +73,7 @@ pub fn derive_packet(input: TokenStream) -> TokenStream { fn convert(&self) -> Vec { let mut out = Vec::new(); - out.extend(convert_var_int(Self::packet_id())); + out.extend(mc_types::convert_var_int(Self::packet_id())); #(#write_fields)* out } @@ -84,13 +84,13 @@ pub fn derive_packet(input: TokenStream) -> TokenStream { } impl #enum_name { - pub async fn read(conn: &mut T) -> Result { + pub async fn read(conn: &mut T) -> mc_types::Result { let mut data = conn.read_data().await?; - let packet_id = get_var_int(&mut data)?; + let packet_id = mc_types::get_var_int(&mut data)?; if packet_id == #name::packet_id() { Ok(Self::#enum_variant_ident(#name::get(&mut data)?)) } else { - Err(Box::new(PacketError::InvalidPacketId(packet_id))) + Err(Box::new(mc_types::PacketError::InvalidPacketId(packet_id))) } } } @@ -119,7 +119,7 @@ pub fn derive_mctype_struct(input: TokenStream) -> TokenStream { let read_fields = fields.named.iter().map(|f| { let fname = &f.ident; let fty = &f.ty; - quote! { #fname: <#fty as McType>::get(data)? } + quote! { #fname: <#fty as mc_types::McType>::get(data)? } }); let write_fields = fields.named.iter().map(|f| { @@ -128,8 +128,8 @@ pub fn derive_mctype_struct(input: TokenStream) -> TokenStream { }); let expanded = quote! { - impl McType for #name { - fn get(data: &mut Vec) -> Result { + impl mc_types::McType for #name { + fn get(data: &mut Vec) -> mc_types::Result { Ok(Self { #(#read_fields),* }) diff --git a/src/handshake.rs b/src/handshake.rs index 5a23a45..4d2abc4 100644 --- a/src/handshake.rs +++ b/src/handshake.rs @@ -3,8 +3,8 @@ pub mod serverbound { - use mc_derive::Packet; - use crate::mc_types::{self, Result, Packet}; + use purple_cello_mc_derive::Packet; + use crate::mc_types::{self, Packet, McType}; #[derive(Packet)] #[packet(id = 0, name = "Handshake")] diff --git a/src/login/clientbound.rs b/src/login/clientbound.rs index 98c6d91..3c962ac 100644 --- a/src/login/clientbound.rs +++ b/src/login/clientbound.rs @@ -1,6 +1,7 @@ // Yeahbut September 2025 -use crate::mc_types::{self, Result, Packet}; +use purple_cello_mc_derive::{Packet, McType}; +use crate::mc_types::{self, Packet, McType}; #[derive(Packet)] #[packet(id = 0, name = "Disconnect")] diff --git a/src/login/serverbound.rs b/src/login/serverbound.rs index bfb6cd9..031117d 100644 --- a/src/login/serverbound.rs +++ b/src/login/serverbound.rs @@ -1,6 +1,7 @@ // Yeahbut September 2025 -use crate::mc_types::{self, Result, Packet}; +use purple_cello_mc_derive::Packet; +use crate::mc_types::{self, Packet, McType}; #[derive(Packet)] #[packet(id = 0, name = "LoginStart")] diff --git a/src/mc_types.rs b/src/mc_types.rs index 5778e37..67102ba 100644 --- a/src/mc_types.rs +++ b/src/mc_types.rs @@ -11,6 +11,7 @@ use serde::{Serialize, Deserialize}; pub use crate::mc_types::error::PacketError; pub use crate::mc_types::protocol::{ProtocolRead, ProtocolConnection}; pub use crate::mc_types::packet::{Result, Packet}; +pub use crate::mc_types::helpers::{convert_var_int, get_var_int}; use crate::mc_types::helpers::*; pub const VERSION_NAME: &str = "1.21"; @@ -106,23 +107,23 @@ impl UUID { } impl McType for VarInt { - fn get(data: &mut Vec) -> Result { Ok(get_var_int(data)) } + fn get(data: &mut Vec) -> Result { get_var_int(data) } fn convert(&self) -> Vec { convert_var_int(*self) } } impl McType for VarLong { - fn get(data: &mut Vec) -> Result { Ok(get_var_long(data)) } + fn get(data: &mut Vec) -> Result { get_var_long(data) } fn convert(&self) -> Vec { convert_var_long(*self) } } impl McType for String { - fn get(data: &mut Vec) -> Result { Ok(get_string(data)) } - fn convert(&self) -> Vec { convert_string(*self) } + fn get(data: &mut Vec) -> Result { get_string(data) } + fn convert(&self) -> Vec { convert_string(self) } } impl McType for Vec { fn get(data: &mut Vec) -> Result { - let length = get_var_int(data)?; + let length = get_var_int(data)?.0; let mut out = Vec::with_capacity(length as usize); for _ in 0..length { out.push(T::get(data)?); @@ -131,7 +132,7 @@ impl McType for Vec { } fn convert(&self) -> Vec { - let mut data = convert_var_int(self.len() as i32); + let mut data = convert_var_int(VarInt(self.len() as i32)); for el in self { data.extend(el.convert()); } diff --git a/src/mc_types/error.rs b/src/mc_types/error.rs index c0dacad..05f52f4 100644 --- a/src/mc_types/error.rs +++ b/src/mc_types/error.rs @@ -3,11 +3,13 @@ use std::fmt; use std::error::Error; +use crate::mc_types::VarInt; + #[derive(Debug)] pub enum PacketError { ValueTooLarge, RanOutOfBytes, - InvalidPacketId(i32), + InvalidPacketId(VarInt), InvalidUUIDString, EncryptionError, } @@ -20,7 +22,7 @@ impl fmt::Display for PacketError { PacketError::RanOutOfBytes => write!(f, "Ran out of bytes while reading VarInt"), PacketError::InvalidPacketId(packet_id) => - write!(f, "Invalid packet id: {}", packet_id), + write!(f, "Invalid packet id: {}", packet_id.0), PacketError::InvalidUUIDString => write!(f, "Invalid UUID format"), PacketError::EncryptionError => diff --git a/src/mc_types/helpers.rs b/src/mc_types/helpers.rs index 30202f4..cd4620b 100644 --- a/src/mc_types/helpers.rs +++ b/src/mc_types/helpers.rs @@ -107,45 +107,47 @@ pub fn convert_f64(value: f64) -> Vec { convert_u64(value as u64) } -pub fn get_uuid(data: &mut Vec) -> u128 { - ((data.remove(0) as u128) << 120) | - ((data.remove(0) as u128) << 112) | - ((data.remove(0) as u128) << 104) | - ((data.remove(0) as u128) << 96) | - ((data.remove(0) as u128) << 88) | - ((data.remove(0) as u128) << 80) | - ((data.remove(0) as u128) << 72) | - ((data.remove(0) as u128) << 64) | - ((data.remove(0) as u128) << 56) | - ((data.remove(0) as u128) << 48) | - ((data.remove(0) as u128) << 40) | - ((data.remove(0) as u128) << 32) | - ((data.remove(0) as u128) << 24) | - ((data.remove(0) as u128) << 16) | - ((data.remove(0) as u128) << 8) | - (data.remove(0) as u128) +pub fn get_uuid(data: &mut Vec) -> UUID { + UUID( + ((data.remove(0) as u128) << 120) | + ((data.remove(0) as u128) << 112) | + ((data.remove(0) as u128) << 104) | + ((data.remove(0) as u128) << 96) | + ((data.remove(0) as u128) << 88) | + ((data.remove(0) as u128) << 80) | + ((data.remove(0) as u128) << 72) | + ((data.remove(0) as u128) << 64) | + ((data.remove(0) as u128) << 56) | + ((data.remove(0) as u128) << 48) | + ((data.remove(0) as u128) << 40) | + ((data.remove(0) as u128) << 32) | + ((data.remove(0) as u128) << 24) | + ((data.remove(0) as u128) << 16) | + ((data.remove(0) as u128) << 8) | + (data.remove(0) as u128) + ) } -pub fn convert_uuid(value: u128) -> Vec { +pub fn convert_uuid(value: UUID) -> Vec { vec![ - ((value & 0xFF000000000000000000000000000000) >> 120) as u8, - ((value & 0xFF0000000000000000000000000000) >> 112) as u8, - ((value & 0xFF00000000000000000000000000) >> 104) as u8, - ((value & 0xFF000000000000000000000000) >> 96) as u8, - ((value & 0xFF0000000000000000000000) >> 88) as u8, - ((value & 0xFF00000000000000000000) >> 80) as u8, - ((value & 0xFF000000000000000000) >> 72) as u8, - ((value & 0xFF0000000000000000) >> 64) as u8, - ((value & 0xFF00000000000000) >> 56) as u8, - ((value & 0xFF000000000000) >> 48) as u8, - ((value & 0xFF0000000000) >> 40) as u8, - ((value & 0xFF00000000) >> 32) as u8, - ((value & 0xFF000000) >> 24) as u8, - ((value & 0xFF0000) >> 16) as u8, - ((value & 0xFF00) >> 8) as u8, - (value & 0xFF) as u8, + ((value.0 & 0xFF000000000000000000000000000000) >> 120) as u8, + ((value.0 & 0xFF0000000000000000000000000000) >> 112) as u8, + ((value.0 & 0xFF00000000000000000000000000) >> 104) as u8, + ((value.0 & 0xFF000000000000000000000000) >> 96) as u8, + ((value.0 & 0xFF0000000000000000000000) >> 88) as u8, + ((value.0 & 0xFF00000000000000000000) >> 80) as u8, + ((value.0 & 0xFF000000000000000000) >> 72) as u8, + ((value.0 & 0xFF0000000000000000) >> 64) as u8, + ((value.0 & 0xFF00000000000000) >> 56) as u8, + ((value.0 & 0xFF000000000000) >> 48) as u8, + ((value.0 & 0xFF0000000000) >> 40) as u8, + ((value.0 & 0xFF00000000) >> 32) as u8, + ((value.0 & 0xFF000000) >> 24) as u8, + ((value.0 & 0xFF0000) >> 16) as u8, + ((value.0 & 0xFF00) >> 8) as u8, + ( value.0 & 0xFF) as u8, ] } -pub fn uuid_u128_to_string(uuid: u128) -> String { +pub fn uuid_u128_to_string(uuid: UUID) -> String { let uuid_bytes = convert_uuid(uuid); format!( "{:08x}-{:04x}-{:04x}-{:04x}-{:012x}", @@ -170,26 +172,26 @@ pub fn uuid_u128_to_string(uuid: u128) -> String { ]), ) } -pub fn uuid_string_to_u128(uuid: &str) -> Result { +pub fn uuid_string_to_u128(uuid: &str) -> Result { let cleaned_uuid = uuid.replace("-", ""); if cleaned_uuid.len() != 32 { return Err(Box::new(PacketError::InvalidUUIDString)); } - Ok(u128::from_str_radix(&cleaned_uuid, 16)?) + Ok(UUID(u128::from_str_radix(&cleaned_uuid, 16)?)) } -pub fn get_var_int(data: &mut Vec) -> Result { - Ok(get_var(data, 32)? as i32) +pub fn get_var_int(data: &mut Vec) -> Result { + Ok(VarInt(get_var(data, 32)? as i32)) } -pub fn convert_var_int(value: i32) -> Vec { - convert_var(value as i64) +pub fn convert_var_int(value: VarInt) -> Vec { + convert_var(value.0 as i64) } -pub fn get_var_long(data: &mut Vec) -> Result { - get_var(data, 64) +pub fn get_var_long(data: &mut Vec) -> Result { + Ok(VarLong(get_var(data, 32)?)) } -pub fn convert_var_long(value: i64) -> Vec { - convert_var(value) +pub fn convert_var_long(value: VarLong) -> Vec { + convert_var(value.0) } fn get_var(data: &mut Vec, size: u8) -> Result { @@ -231,13 +233,13 @@ fn convert_var(mut value: i64) -> Vec { } pub fn get_string(data: &mut Vec) -> Result { - let length = get_var_int(data)? as usize; + let length = get_var_int(data)?.0 as usize; let buffer = data[..length].to_vec(); for _ in 0..length { data.remove(0); } Ok(String::from_utf8_lossy(&buffer).to_string()) } pub fn convert_string(s: &str) -> Vec { - let length = s.len() as i32; + let length = VarInt(s.len() as i32); let mut data = convert_var_int(length); data.append(&mut s.as_bytes().to_vec()); data diff --git a/src/mc_types/packet.rs b/src/mc_types/packet.rs index 74b825f..b52f2f0 100644 --- a/src/mc_types/packet.rs +++ b/src/mc_types/packet.rs @@ -12,7 +12,7 @@ pub type Result = std::result::Result>; #[async_trait] pub trait Packet: Sized { - fn packet_id() -> i32; + fn packet_id() -> VarInt; fn get(data: &mut Vec) -> Result; fn convert(&self) -> Vec; diff --git a/src/mc_types/protocol.rs b/src/mc_types/protocol.rs index d59b957..67998d1 100644 --- a/src/mc_types/protocol.rs +++ b/src/mc_types/protocol.rs @@ -217,7 +217,7 @@ impl<'a> ProtocolRead for ProtocolConnection<'a> { #[async_trait] impl<'a> ProtocolWrite for ProtocolConnection<'a> { async fn write_data(&mut self, data: &mut Vec) -> Result<()> { - let mut out_data = convert_var_int(data.len() as i32); + let mut out_data = convert_var_int(VarInt(data.len() as i32)); out_data.append(data); match &mut self.aes_cipher { Some(aes_cipher) => { @@ -256,7 +256,7 @@ unsafe impl<'a> Send for WriteHaftProtocolConnection<'a> {} #[async_trait] impl<'a> ProtocolWrite for WriteHaftProtocolConnection<'a> { async fn write_data(&mut self, data: &mut Vec) -> Result<()> { - let mut out_data = convert_var_int(data.len() as i32); + let mut out_data = convert_var_int(VarInt(data.len() as i32)); out_data.append(data); match &mut self.aes_cipher { Some(aes_cipher) => { @@ -342,7 +342,7 @@ async fn read_var_int_stream(stream: &mut OwnedReadHalf) -> Result { } } - let varint = get_var_int(&mut data)?; + let varint = get_var_int(&mut data)?.0; Ok(varint) } @@ -363,7 +363,7 @@ async fn read_var_int_stream_encrypted( } } - let varint = get_var_int(&mut data)?; + let varint = get_var_int(&mut data)?.0; Ok(varint) } diff --git a/src/play.rs b/src/play.rs index 7313ab1..8d1d9bf 100644 --- a/src/play.rs +++ b/src/play.rs @@ -21,7 +21,7 @@ pub struct PlayPacket { impl Packet for PlayPacket { - fn packet_id() -> i32 {0} + fn packet_id() -> mc_types::VarInt {mc_types::VarInt(0)} fn get(data: &mut Vec) -> Result { Ok(Self { diff --git a/src/status/clientbound.rs b/src/status/clientbound.rs index bff3c94..8667cab 100644 --- a/src/status/clientbound.rs +++ b/src/status/clientbound.rs @@ -4,7 +4,8 @@ use serde::{Serialize, Deserialize, Serializer, Deserializer}; use serde::de::{self, Visitor, MapAccess}; use std::fmt; -use crate::mc_types::{self, Result, Packet, PacketError}; +use purple_cello_mc_derive::Packet; +use crate::mc_types::{self, Packet, McType}; #[derive(Serialize, Deserialize)] pub struct StatusVersion { @@ -106,13 +107,13 @@ pub struct Status { impl Status { - pub fn from_json(data: StatusResponseData) -> Result { + pub fn from_json(data: StatusResponseData) -> mc_types::Result { Ok(Self { response: serde_json::to_string(&data)? }) } - pub fn get_json(&self) -> Result { + pub fn get_json(&self) -> mc_types::Result { Ok(serde_json::from_str(&self.response)?) } diff --git a/src/status/serverbound.rs b/src/status/serverbound.rs index ae3ce94..8e57d29 100644 --- a/src/status/serverbound.rs +++ b/src/status/serverbound.rs @@ -1,6 +1,7 @@ // Yeahbut September 2025 -use crate::mc_types::{self, Result, Packet}; +use purple_cello_mc_derive::Packet; +use crate::mc_types::{self, Packet, McType}; #[derive(Packet)] #[packet(id = 0, name = "Status")]