aboutsummaryrefslogtreecommitdiff
path: root/azalea-protocol/src/mc_buf
diff options
context:
space:
mode:
authormat <github@matdoes.dev>2022-04-27 18:00:50 +0000
committermat <github@matdoes.dev>2022-04-27 18:00:50 +0000
commit9b50886c30f3e9129e054b019581264c9e6cadaf (patch)
tree8dfcc5bb8980a86d650a0d3d7afb705624b818c6 /azalea-protocol/src/mc_buf
parent60d1fa50c32c202dd04895caa51172948a4760b6 (diff)
downloadazalea-drasl-9b50886c30f3e9129e054b019581264c9e6cadaf.tar.xz
player info packet
Diffstat (limited to 'azalea-protocol/src/mc_buf')
-rwxr-xr-xazalea-protocol/src/mc_buf/read.rs57
-rwxr-xr-xazalea-protocol/src/mc_buf/write.rs49
2 files changed, 103 insertions, 3 deletions
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<i16, String>;
async fn read_float(&mut self) -> Result<f32, String>;
async fn read_double(&mut self) -> Result<f64, String>;
+ async fn read_uuid(&mut self) -> Result<Uuid, String>;
}
#[async_trait]
@@ -207,6 +209,15 @@ where
Err(_) => Err("Error reading double".to_string()),
}
}
+
+ async fn read_uuid(&mut self) -> Result<Uuid, String> {
+ 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<GameType> {
}
}
+// Option<String>
+#[async_trait]
+impl McBufReadable for Option<String> {
+ async fn read_into<R>(buf: &mut R) -> Result<Self, String>
+ 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<Component>
+#[async_trait]
+impl McBufReadable for Option<Component> {
+ async fn read_into<R>(buf: &mut R) -> Result<Self, String>
+ 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<R>(buf: &mut R) -> Result<Self, String>
+ 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<u8> {
) -> 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<GameType> {
}
}
+// Option<String>
+impl McBufWritable for Option<String> {
+ fn write_into(&self, buf: &mut Vec<u8>) -> Result<(), std::io::Error> {
+ if let Some(s) = self {
+ buf.write_boolean(true)?;
+ buf.write_utf(s)?;
+ } else {
+ buf.write_boolean(false)?;
+ };
+ Ok(())
+ }
+}
+
+// Option<Component>
+impl McBufWritable for Option<Component> {
+ fn write_into(&self, buf: &mut Vec<u8>) -> 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<u8>) -> 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<u8>) -> Result<(), std::io::Error> {
+ buf.write_uuid(self)?;
+
+ Ok(())
+ }
+}