From 40926b5179ae420b6d5c0ab66b3066a5620e6d7b Mon Sep 17 00:00:00 2001 From: ya7on Date: Sun, 25 Feb 2024 21:50:03 +0300 Subject: [PATCH] Add angle type --- src/types.rs | 2 ++ src/types/angle.rs | 24 ++++++++++++++++++++++++ 2 files changed, 26 insertions(+) create mode 100644 src/types/angle.rs diff --git a/src/types.rs b/src/types.rs index 2dbc70a..7a9dcb1 100644 --- a/src/types.rs +++ b/src/types.rs @@ -1,5 +1,6 @@ //! Minecraft data types +mod angle; pub(crate) mod base; mod bitset; mod boolean; @@ -20,6 +21,7 @@ mod uuid; mod varint; mod vec; +pub use angle::MCAngle; pub use bitset::MCBitSet; pub use boolean::MCBoolean; pub use byte::MCByte; diff --git a/src/types/angle.rs b/src/types/angle.rs new file mode 100644 index 0000000..513b335 --- /dev/null +++ b/src/types/angle.rs @@ -0,0 +1,24 @@ +use crate::utils::TcpUtils; +use crate::MCType; +use std::fmt::Debug; +use std::io::Read; + +#[derive(Debug, Clone)] +pub struct MCAngle(pub i8); + +impl MCAngle { + /// From -360 to 360 + pub fn from_degrees(deegree: i16) -> Self { + Self((deegree % 360 / 360 * 256) as i8) + } +} + +impl MCType for MCAngle { + fn pack(&self) -> Vec { + vec![self.0 as u8] + } + + fn unpack(src: &mut dyn Read) -> Self { + Self(src.read_byte() as i8) + } +}