From 9b50886c30f3e9129e054b019581264c9e6cadaf Mon Sep 17 00:00:00 2001 From: mat Date: Wed, 27 Apr 2022 18:00:50 +0000 Subject: player info packet --- azalea-protocol/src/mc_buf/read.rs | 57 +++++++++++++++++++++++++++++++++++-- azalea-protocol/src/mc_buf/write.rs | 49 ++++++++++++++++++++++++++++++- 2 files changed, 103 insertions(+), 3 deletions(-) (limited to 'azalea-protocol/src/mc_buf') diff --git a/azalea-protocol/src/mc_buf/read.rs b/azalea-protocol/src/mc_buf/read.rs index d3382e0a..e90b4b10 100755 --- a/azalea-protocol/src/mc_buf/read.rs +++ b/azalea-protocol/src/mc_buf/read.rs @@ -1,11 +1,12 @@ use async_trait::async_trait; use azalea_chat::component::Component; use azalea_core::{ - difficulty::Difficulty, game_type::GameType, resource_location::ResourceLocation, Slot, - SlotData, + difficulty::Difficulty, game_type::GameType, resource_location::ResourceLocation, + serializable_uuid::SerializableUuid, Slot, SlotData, }; use serde::Deserialize; use tokio::io::{AsyncRead, AsyncReadExt}; +use uuid::Uuid; use super::{UnsizedByteArray, MAX_STRING_LENGTH}; @@ -29,6 +30,7 @@ pub trait Readable { async fn read_short(&mut self) -> Result; async fn read_float(&mut self) -> Result; async fn read_double(&mut self) -> Result; + async fn read_uuid(&mut self) -> Result; } #[async_trait] @@ -207,6 +209,15 @@ where Err(_) => Err("Error reading double".to_string()), } } + + async fn read_uuid(&mut self) -> Result { + Ok(Uuid::from_int_array([ + self.read_int().await? as u32, + self.read_int().await? as u32, + self.read_int().await? as u32, + self.read_int().await? as u32, + ])) + } } #[async_trait] @@ -439,6 +450,37 @@ impl McBufReadable for Option { } } +// Option +#[async_trait] +impl McBufReadable for Option { + async fn read_into(buf: &mut R) -> Result + where + R: AsyncRead + std::marker::Unpin + std::marker::Send, + { + let present = buf.read_boolean().await?; + Ok(if present { + Some(buf.read_utf().await?) + } else { + None + }) + } +} +// Option +#[async_trait] +impl McBufReadable for Option { + async fn read_into(buf: &mut R) -> Result + where + R: AsyncRead + std::marker::Unpin + std::marker::Send, + { + let present = buf.read_boolean().await?; + Ok(if present { + Some(Component::read_into(buf).await?) + } else { + None + }) + } +} + // azalea_nbt::Tag #[async_trait] impl McBufReadable for azalea_nbt::Tag { @@ -493,3 +535,14 @@ impl McBufReadable for Slot { Ok(Slot::Present(SlotData { id, count, nbt })) } } + +// Uuid +#[async_trait] +impl McBufReadable for Uuid { + async fn read_into(buf: &mut R) -> Result + where + R: AsyncRead + std::marker::Unpin + std::marker::Send, + { + buf.read_uuid().await + } +} diff --git a/azalea-protocol/src/mc_buf/write.rs b/azalea-protocol/src/mc_buf/write.rs index b57ad786..bd5e3f52 100755 --- a/azalea-protocol/src/mc_buf/write.rs +++ b/azalea-protocol/src/mc_buf/write.rs @@ -2,10 +2,12 @@ use super::{UnsizedByteArray, MAX_STRING_LENGTH}; use async_trait::async_trait; use azalea_chat::component::Component; use azalea_core::{ - difficulty::Difficulty, game_type::GameType, resource_location::ResourceLocation, Slot, + difficulty::Difficulty, game_type::GameType, resource_location::ResourceLocation, + serializable_uuid::SerializableUuid, Slot, }; use byteorder::{BigEndian, WriteBytesExt}; use std::io::Write; +use uuid::Uuid; #[async_trait] pub trait Writable { @@ -43,6 +45,7 @@ pub trait Writable { ) -> Result<(), std::io::Error>; fn write_float(&mut self, n: f32) -> Result<(), std::io::Error>; fn write_double(&mut self, n: f64) -> Result<(), std::io::Error>; + fn write_uuid(&mut self, uuid: &Uuid) -> Result<(), std::io::Error>; } #[async_trait] @@ -163,6 +166,15 @@ impl Writable for Vec { ) -> Result<(), std::io::Error> { self.write_utf(&location.to_string()) } + + fn write_uuid(&mut self, uuid: &Uuid) -> Result<(), std::io::Error> { + let [a, b, c, d] = uuid.to_int_array(); + a.write_into(self)?; + b.write_into(self)?; + c.write_into(self)?; + d.write_into(self)?; + Ok(()) + } } pub trait McBufWritable @@ -317,6 +329,32 @@ impl McBufWritable for Option { } } +// Option +impl McBufWritable for Option { + fn write_into(&self, buf: &mut Vec) -> Result<(), std::io::Error> { + if let Some(s) = self { + buf.write_boolean(true)?; + buf.write_utf(s)?; + } else { + buf.write_boolean(false)?; + }; + Ok(()) + } +} + +// Option +impl McBufWritable for Option { + fn write_into(&self, buf: &mut Vec) -> Result<(), std::io::Error> { + if let Some(s) = self { + buf.write_boolean(true)?; + s.write_into(buf)?; + } else { + buf.write_boolean(false)?; + }; + Ok(()) + } +} + // azalea_nbt::Tag impl McBufWritable for azalea_nbt::Tag { fn write_into(&self, buf: &mut Vec) -> Result<(), std::io::Error> { @@ -365,3 +403,12 @@ impl McBufWritable for Slot { Ok(()) } } + +// Slot +impl McBufWritable for Uuid { + fn write_into(&self, buf: &mut Vec) -> Result<(), std::io::Error> { + buf.write_uuid(self)?; + + Ok(()) + } +} -- cgit v1.2.3