From 9dacd90abcfaa62ade1e4f130ed53da2c9facdbc Mon Sep 17 00:00:00 2001 From: mat Date: Sun, 1 May 2022 13:51:59 -0500 Subject: clientbound_add_entity_packet & clientbound_set_entity_data_packet --- azalea-protocol/src/mc_buf/read.rs | 57 ++++++++++++++++++++++++------------- azalea-protocol/src/mc_buf/write.rs | 37 +++++++++++++----------- 2 files changed, 59 insertions(+), 35 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 b345aac9..53c137b3 100755 --- a/azalea-protocol/src/mc_buf/read.rs +++ b/azalea-protocol/src/mc_buf/read.rs @@ -2,7 +2,7 @@ use async_trait::async_trait; use azalea_chat::component::Component; use azalea_core::{ difficulty::Difficulty, game_type::GameType, resource_location::ResourceLocation, - serializable_uuid::SerializableUuid, Slot, SlotData, + serializable_uuid::SerializableUuid, BlockPos, Direction, Slot, SlotData, }; use serde::Deserialize; use tokio::io::{AsyncRead, AsyncReadExt}; @@ -473,29 +473,14 @@ 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 +impl McBufReadable for Option { + default 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?) + Some(T::read_into(buf).await?) } else { None }) @@ -567,3 +552,37 @@ impl McBufReadable for Uuid { buf.read_uuid().await } } + +// BlockPos +#[async_trait] +impl McBufReadable for BlockPos { + async fn read_into(buf: &mut R) -> Result + where + R: AsyncRead + std::marker::Unpin + std::marker::Send, + { + let val = u64::read_into(buf).await?; + let x = (val >> 38) as i32; + let y = (val & 0xFFF) as i32; + let z = ((val >> 12) & 0x3FFFFFF) as i32; + Ok(BlockPos { x, y, z }) + } +} + +// Direction +#[async_trait] +impl McBufReadable for Direction { + async fn read_into(buf: &mut R) -> Result + where + R: AsyncRead + std::marker::Unpin + std::marker::Send, + { + match buf.read_varint().await? { + 0 => Ok(Self::Down), + 1 => Ok(Self::Up), + 2 => Ok(Self::North), + 3 => Ok(Self::South), + 4 => Ok(Self::West), + 5 => Ok(Self::East), + _ => Err("Invalid direction".to_string()), + } + } +} diff --git a/azalea-protocol/src/mc_buf/write.rs b/azalea-protocol/src/mc_buf/write.rs index 4c7ac60c..e5437ae8 100755 --- a/azalea-protocol/src/mc_buf/write.rs +++ b/azalea-protocol/src/mc_buf/write.rs @@ -3,7 +3,7 @@ use async_trait::async_trait; use azalea_chat::component::Component; use azalea_core::{ difficulty::Difficulty, game_type::GameType, resource_location::ResourceLocation, - serializable_uuid::SerializableUuid, Slot, + serializable_uuid::SerializableUuid, BlockPos, Direction, Slot, }; use byteorder::{BigEndian, WriteBytesExt}; use std::io::Write; @@ -336,21 +336,8 @@ 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> { +impl McBufWritable for Option { + default fn write_into(&self, buf: &mut Vec) -> Result<(), std::io::Error> { if let Some(s) = self { buf.write_boolean(true)?; s.write_into(buf)?; @@ -418,3 +405,21 @@ impl McBufWritable for Uuid { Ok(()) } } + +// BlockPos +impl McBufWritable for BlockPos { + fn write_into(&self, buf: &mut Vec) -> Result<(), std::io::Error> { + buf.write_long( + (((self.x & 0x3FFFFFF) as i64) << 38) + | (((self.z & 0x3FFFFFF) as i64) << 12) + | ((self.y & 0xFFF) as i64), + ) + } +} + +// Direction +impl McBufWritable for Direction { + fn write_into(&self, buf: &mut Vec) -> Result<(), std::io::Error> { + buf.write_varint(*self as i32) + } +} -- cgit v1.2.3