diff --git a/src/mc_types.rs b/src/mc_types.rs index adc1091..01cf4a3 100644 --- a/src/mc_types.rs +++ b/src/mc_types.rs @@ -2,18 +2,13 @@ mod error; mod protocol; - -use std::error::Error; +mod packet; use serde::{Serialize, Deserialize}; -use async_trait::async_trait; - -use crate::mc_types::protocol::ProtocolWrite; pub use crate::mc_types::error::PacketError; pub use crate::mc_types::protocol::{ProtocolRead, ProtocolConnection}; - -pub type Result = std::result::Result>; +pub use crate::mc_types::packet::{Result, Packet, PacketArray}; pub const VERSION_NAME: &str = "1.21"; pub const VERSION_PROTOCOL: i32 = 767; @@ -26,50 +21,6 @@ pub struct Chat { pub text: String, } -#[async_trait] -pub trait Packet: Sized { - fn packet_id() -> i32; - fn get(data: &mut Vec) -> Result; - fn convert(&self) -> Vec; - - async fn read(conn: &mut T) -> Result { - let mut data = conn.read_data().await?; - let packet_id = get_var_int(&mut data)?; - if packet_id == Self::packet_id() { - return Ok(Self::get(&mut data)?) - } else { - return Err(Box::new(PacketError::InvalidPacketId(packet_id))) - } - } - - async fn write(&self, conn: &mut T) -> Result<()> { - conn.write_data(&mut self.convert()).await - } -} - -pub trait PacketArray: Sized { - fn get(data: &mut Vec) -> Result; - fn convert(&self) -> Vec; - - fn get_array(data: &mut Vec) -> Result> { - let length = get_var_int(data)?; - let mut out_data: Vec = vec![]; - for _ in 0..length { - out_data.push(Self::get(data)?); - } - Ok(out_data) - } - - fn convert_array(array: &mut Vec) -> Vec { - let length = array.len() as i32; - let mut data: Vec = convert_var_int(length); - for element in array { - data.append(&mut Self::convert(element)); - } - data - } -} - pub fn get_bool(data: &mut Vec) -> bool { data.remove(0) != 0 } diff --git a/src/mc_types/packet.rs b/src/mc_types/packet.rs new file mode 100644 index 0000000..70843be --- /dev/null +++ b/src/mc_types/packet.rs @@ -0,0 +1,55 @@ +// Yeahbut September 2025 + +use std::error::Error; + +use async_trait::async_trait; + +use crate::mc_types::protocol::ProtocolWrite; + +use crate::mc_types::*; + +pub type Result = std::result::Result>; + +#[async_trait] +pub trait Packet: Sized { + fn packet_id() -> i32; + fn get(data: &mut Vec) -> Result; + fn convert(&self) -> Vec; + + async fn read(conn: &mut T) -> Result { + let mut data = conn.read_data().await?; + let packet_id = get_var_int(&mut data)?; + if packet_id == Self::packet_id() { + return Ok(Self::get(&mut data)?) + } else { + return Err(Box::new(PacketError::InvalidPacketId(packet_id))) + } + } + + async fn write(&self, conn: &mut T) -> Result<()> { + conn.write_data(&mut self.convert()).await + } +} + +pub trait PacketArray: Sized { + fn get(data: &mut Vec) -> Result; + fn convert(&self) -> Vec; + + fn get_array(data: &mut Vec) -> Result> { + let length = get_var_int(data)?; + let mut out_data: Vec = vec![]; + for _ in 0..length { + out_data.push(Self::get(data)?); + } + Ok(out_data) + } + + fn convert_array(array: &mut Vec) -> Vec { + let length = array.len() as i32; + let mut data: Vec = convert_var_int(length); + for element in array { + data.append(&mut Self::convert(element)); + } + data + } +}