diff options
Diffstat (limited to 'azalea-core/src')
| -rwxr-xr-x | azalea-core/src/bitset.rs | 16 | ||||
| -rwxr-xr-x | azalea-core/src/delta.rs | 8 | ||||
| -rwxr-xr-x | azalea-core/src/difficulty.rs | 14 | ||||
| -rwxr-xr-x | azalea-core/src/direction.rs | 6 | ||||
| -rw-r--r-- | azalea-core/src/game_type.rs | 28 | ||||
| -rw-r--r-- | azalea-core/src/objectives.rs | 4 | ||||
| -rwxr-xr-x | azalea-core/src/position.rs | 165 | ||||
| -rwxr-xr-x | azalea-core/src/resource_location.rs | 20 |
8 files changed, 125 insertions, 136 deletions
diff --git a/azalea-core/src/bitset.rs b/azalea-core/src/bitset.rs index 0be04a57..e7c79467 100755 --- a/azalea-core/src/bitset.rs +++ b/azalea-core/src/bitset.rs @@ -1,9 +1,9 @@ use std::io::{Cursor, Write}; -use azalea_buf::{BufReadError, McBuf, McBufReadable, McBufWritable}; +use azalea_buf::{AzBuf, AzaleaRead, AzaleaWrite, BufReadError}; /// Represents Java's BitSet, a list of bits. -#[derive(Debug, Clone, PartialEq, Eq, Hash, Default, McBuf)] +#[derive(Debug, Clone, PartialEq, Eq, Hash, Default, AzBuf)] pub struct BitSet { data: Vec<u64>, } @@ -159,25 +159,25 @@ where } } -impl<const N: usize> McBufReadable for FixedBitSet<N> +impl<const N: usize> AzaleaRead for FixedBitSet<N> where [u8; N.div_ceil(8)]: Sized, { - fn read_from(buf: &mut Cursor<&[u8]>) -> Result<Self, BufReadError> { + fn azalea_read(buf: &mut Cursor<&[u8]>) -> Result<Self, BufReadError> { let mut data = [0; N.div_ceil(8)]; for item in data.iter_mut().take(N.div_ceil(8)) { - *item = u8::read_from(buf)?; + *item = u8::azalea_read(buf)?; } Ok(FixedBitSet { data }) } } -impl<const N: usize> McBufWritable for FixedBitSet<N> +impl<const N: usize> AzaleaWrite for FixedBitSet<N> where [u8; N.div_ceil(8)]: Sized, { - fn write_into(&self, buf: &mut impl Write) -> Result<(), std::io::Error> { + fn azalea_write(&self, buf: &mut impl Write) -> Result<(), std::io::Error> { for i in 0..N.div_ceil(8) { - self.data[i].write_into(buf)?; + self.data[i].azalea_write(buf)?; } Ok(()) } diff --git a/azalea-core/src/delta.rs b/azalea-core/src/delta.rs index 2dd67005..238262a2 100755 --- a/azalea-core/src/delta.rs +++ b/azalea-core/src/delta.rs @@ -1,4 +1,4 @@ -pub use azalea_buf::McBuf; +pub use azalea_buf::AzBuf; use crate::position::Vec3; @@ -9,7 +9,7 @@ pub trait PositionDeltaTrait { } /// Only works for up to 8 blocks -#[derive(Clone, Debug, McBuf, Default)] +#[derive(Clone, Debug, AzBuf, Default)] pub struct PositionDelta8 { pub xa: i16, pub ya: i16, @@ -49,10 +49,6 @@ impl Vec3 { } } - pub fn length_squared(&self) -> f64 { - self.x * self.x + self.y * self.y + self.z * self.z - } - pub fn normalize(&self) -> Vec3 { let length = f64::sqrt(self.x * self.x + self.y * self.y + self.z * self.z); if length < 1e-4 { diff --git a/azalea-core/src/difficulty.rs b/azalea-core/src/difficulty.rs index 750cfe1f..b907bbb3 100755 --- a/azalea-core/src/difficulty.rs +++ b/azalea-core/src/difficulty.rs @@ -3,7 +3,7 @@ use std::{ io::{Cursor, Write}, }; -use azalea_buf::{BufReadError, McBufReadable, McBufWritable}; +use azalea_buf::{AzaleaRead, AzaleaWrite, BufReadError}; #[derive(Hash, Clone, Copy, Debug, PartialEq, Eq)] pub enum Difficulty { @@ -66,15 +66,15 @@ impl Difficulty { } } -impl McBufReadable for Difficulty { - fn read_from(buf: &mut Cursor<&[u8]>) -> Result<Self, BufReadError> { - Ok(Difficulty::by_id(u8::read_from(buf)?)) +impl AzaleaRead for Difficulty { + fn azalea_read(buf: &mut Cursor<&[u8]>) -> Result<Self, BufReadError> { + Ok(Difficulty::by_id(u8::azalea_read(buf)?)) } } -impl McBufWritable for Difficulty { - fn write_into(&self, buf: &mut impl Write) -> Result<(), std::io::Error> { - u8::write_into(&self.id(), buf) +impl AzaleaWrite for Difficulty { + fn azalea_write(&self, buf: &mut impl Write) -> Result<(), std::io::Error> { + u8::azalea_write(&self.id(), buf) } } diff --git a/azalea-core/src/direction.rs b/azalea-core/src/direction.rs index 32e1a23a..b0b8c960 100755 --- a/azalea-core/src/direction.rs +++ b/azalea-core/src/direction.rs @@ -1,8 +1,8 @@ -use azalea_buf::McBuf; +use azalea_buf::AzBuf; use crate::position::Vec3; -#[derive(Clone, Copy, Debug, McBuf, Default, Eq, PartialEq)] +#[derive(Clone, Copy, Debug, AzBuf, Default, Eq, PartialEq)] pub enum Direction { #[default] Down = 0, @@ -62,7 +62,7 @@ impl Direction { } // TODO: make azalea_block use this instead of FacingCardinal -#[derive(Clone, Copy, Debug, McBuf, PartialEq, Eq, Hash)] +#[derive(Clone, Copy, Debug, AzBuf, PartialEq, Eq, Hash)] pub enum CardinalDirection { North, South, diff --git a/azalea-core/src/game_type.rs b/azalea-core/src/game_type.rs index 1e494035..5bac06d5 100644 --- a/azalea-core/src/game_type.rs +++ b/azalea-core/src/game_type.rs @@ -1,6 +1,6 @@ use std::io::{Cursor, Write}; -use azalea_buf::{BufReadError, McBufReadable, McBufVarReadable, McBufWritable}; +use azalea_buf::{AzaleaRead, AzaleaReadVar, AzaleaWrite, BufReadError}; use tracing::debug; /// A Minecraft gamemode, like survival or creative. @@ -93,9 +93,9 @@ impl GameMode { } } -impl McBufReadable for GameMode { - fn read_from(buf: &mut Cursor<&[u8]>) -> Result<Self, BufReadError> { - let id = u32::var_read_from(buf)?; +impl AzaleaRead for GameMode { + fn azalea_read(buf: &mut Cursor<&[u8]>) -> Result<Self, BufReadError> { + let id = u32::azalea_read_var(buf)?; let id = id.try_into().unwrap_or_else(|_| { debug!("Unknown game mode id {id}, defaulting to survival"); 0 @@ -107,13 +107,13 @@ impl McBufReadable for GameMode { } } -impl McBufWritable for GameMode { - fn write_into(&self, buf: &mut impl Write) -> Result<(), std::io::Error> { - u8::write_into(&self.to_id(), buf) +impl AzaleaWrite for GameMode { + fn azalea_write(&self, buf: &mut impl Write) -> Result<(), std::io::Error> { + u8::azalea_write(&self.to_id(), buf) } } -/// Rust doesn't let us `impl McBufReadable for Option<GameType>` so we have to +/// Rust doesn't let us `impl AzaleaRead for Option<GameType>` so we have to /// make a new type :( #[derive(Hash, Copy, Clone, Debug)] pub struct OptionalGameType(pub Option<GameMode>); @@ -130,15 +130,15 @@ impl From<OptionalGameType> for Option<GameMode> { } } -impl McBufReadable for OptionalGameType { - fn read_from(buf: &mut Cursor<&[u8]>) -> Result<Self, BufReadError> { - let id = i8::read_from(buf)?; +impl AzaleaRead for OptionalGameType { + fn azalea_read(buf: &mut Cursor<&[u8]>) -> Result<Self, BufReadError> { + let id = i8::azalea_read(buf)?; GameMode::from_optional_id(id).ok_or(BufReadError::UnexpectedEnumVariant { id: id as i32 }) } } -impl McBufWritable for OptionalGameType { - fn write_into(&self, buf: &mut impl Write) -> Result<(), std::io::Error> { - GameMode::to_optional_id(*self).write_into(buf) +impl AzaleaWrite for OptionalGameType { + fn azalea_write(&self, buf: &mut impl Write) -> Result<(), std::io::Error> { + GameMode::to_optional_id(*self).azalea_write(buf) } } diff --git a/azalea-core/src/objectives.rs b/azalea-core/src/objectives.rs index dd1534f2..a2f0d517 100644 --- a/azalea-core/src/objectives.rs +++ b/azalea-core/src/objectives.rs @@ -3,9 +3,9 @@ use std::{ str::FromStr, }; -use azalea_buf::McBuf; +use azalea_buf::AzBuf; -#[derive(Clone, Copy, Debug, McBuf)] +#[derive(Clone, Copy, Debug, AzBuf)] pub enum ObjectiveCriteria { Integer, Hearts, diff --git a/azalea-core/src/position.rs b/azalea-core/src/position.rs index de0276a3..c280a85a 100755 --- a/azalea-core/src/position.rs +++ b/azalea-core/src/position.rs @@ -8,11 +8,9 @@ use std::{ hash::Hash, io::{Cursor, Write}, ops::{Add, AddAssign, Mul, Rem, Sub}, - str::FromStr, }; -use azalea_buf::{BufReadError, McBuf, McBufReadable, McBufWritable}; -#[cfg(feature = "serde")] +use azalea_buf::{AzBuf, AzaleaRead, AzaleaWrite, BufReadError}; use serde::{Deserialize, Serialize}; use crate::resource_location::ResourceLocation; @@ -28,25 +26,25 @@ macro_rules! vec3_impl { /// Get the distance of this vector to the origin by doing `x^2 + y^2 + /// z^2`. #[inline] - pub fn length_sqr(&self) -> $type { + pub fn length_squared(&self) -> $type { self.x * self.x + self.y * self.y + self.z * self.z } /// Get the squared distance from this position to another position. - /// Equivalent to `(self - other).length_sqr()`. + /// Equivalent to `(self - other).length_squared()`. #[inline] - pub fn distance_to_sqr(&self, other: &Self) -> $type { - (self - other).length_sqr() + pub fn distance_squared_to(&self, other: &Self) -> $type { + (self - other).length_squared() } #[inline] - pub fn horizontal_distance_sqr(&self) -> $type { + pub fn horizontal_distance_squared(&self) -> $type { self.x * self.x + self.z * self.z } #[inline] - pub fn horizontal_distance_to_sqr(&self, other: &Self) -> $type { - (self - other).horizontal_distance_sqr() + pub fn horizontal_distance_squared_to(&self, other: &Self) -> $type { + (self - other).horizontal_distance_squared() } /// Return a new instance of this position with the y coordinate @@ -214,7 +212,7 @@ macro_rules! vec3_impl { /// Used to represent an exact position in the world where an entity could be. /// For blocks, [`BlockPos`] is used instead. -#[derive(Clone, Copy, Debug, Default, PartialEq, McBuf)] +#[derive(Clone, Copy, Debug, Default, PartialEq, AzBuf)] #[cfg_attr(feature = "serde", derive(Deserialize, Serialize))] pub struct Vec3 { pub x: f64, @@ -272,6 +270,46 @@ impl BlockPos { pub fn length_manhattan(&self) -> u32 { (self.x.abs() + self.y.abs() + self.z.abs()) as u32 } + + /// Make a new BlockPos with the lower coordinates for each axis. + /// + /// ``` + /// # use azalea_core::position::BlockPos; + /// assert_eq!( + /// BlockPos::min( + /// &BlockPos::new(1, 20, 300), + /// &BlockPos::new(50, 40, 30), + /// ), + /// BlockPos::new(1, 20, 30), + /// ); + /// ``` + pub fn min(&self, other: &Self) -> Self { + Self { + x: self.x.min(other.x), + y: self.y.min(other.y), + z: self.z.min(other.z), + } + } + + /// Make a new BlockPos with the higher coordinates for each axis. + /// + /// ``` + /// # use azalea_core::position::BlockPos; + /// assert_eq!( + /// BlockPos::max( + /// &BlockPos::new(1, 20, 300), + /// &BlockPos::new(50, 40, 30), + /// ), + /// BlockPos::new(50, 40, 300), + /// ); + /// ``` + pub fn max(&self, other: &Self) -> Self { + Self { + x: self.x.max(other.x), + y: self.y.max(other.y), + z: self.z.max(other.z), + } + } } /// Chunk coordinates are used to represent where a chunk is in the world. You @@ -315,15 +353,15 @@ impl From<u64> for ChunkPos { } } } -impl McBufReadable for ChunkPos { - fn read_from(buf: &mut Cursor<&[u8]>) -> Result<Self, BufReadError> { - let long = u64::read_from(buf)?; +impl AzaleaRead for ChunkPos { + fn azalea_read(buf: &mut Cursor<&[u8]>) -> Result<Self, BufReadError> { + let long = u64::azalea_read(buf)?; Ok(ChunkPos::from(long)) } } -impl McBufWritable for ChunkPos { - fn write_into(&self, buf: &mut impl Write) -> Result<(), std::io::Error> { - u64::from(*self).write_into(buf)?; +impl AzaleaWrite for ChunkPos { + fn azalea_write(&self, buf: &mut impl Write) -> Result<(), std::io::Error> { + u64::from(*self).azalea_write(buf)?; Ok(()) } } @@ -586,9 +624,9 @@ const PACKED_Z_MASK: u64 = (1 << PACKED_Z_LENGTH) - 1; const Z_OFFSET: u64 = PACKED_Y_LENGTH; const X_OFFSET: u64 = PACKED_Y_LENGTH + PACKED_Z_LENGTH; -impl McBufReadable for BlockPos { - fn read_from(buf: &mut Cursor<&[u8]>) -> Result<Self, BufReadError> { - let val = i64::read_from(buf)?; +impl AzaleaRead for BlockPos { + fn azalea_read(buf: &mut Cursor<&[u8]>) -> Result<Self, BufReadError> { + let val = i64::azalea_read(buf)?; let x = (val << (64 - X_OFFSET - PACKED_X_LENGTH) >> (64 - PACKED_X_LENGTH)) as i32; let y = (val << (64 - PACKED_Y_LENGTH) >> (64 - PACKED_Y_LENGTH)) as i32; let z = (val << (64 - Z_OFFSET - PACKED_Z_LENGTH) >> (64 - PACKED_Z_LENGTH)) as i32; @@ -596,18 +634,18 @@ impl McBufReadable for BlockPos { } } -impl McBufReadable for GlobalPos { - fn read_from(buf: &mut Cursor<&[u8]>) -> Result<Self, BufReadError> { +impl AzaleaRead for GlobalPos { + fn azalea_read(buf: &mut Cursor<&[u8]>) -> Result<Self, BufReadError> { Ok(GlobalPos { - world: ResourceLocation::read_from(buf)?, - pos: BlockPos::read_from(buf)?, + world: ResourceLocation::azalea_read(buf)?, + pos: BlockPos::azalea_read(buf)?, }) } } -impl McBufReadable for ChunkSectionPos { - fn read_from(buf: &mut Cursor<&[u8]>) -> Result<Self, BufReadError> { - let long = i64::read_from(buf)?; +impl AzaleaRead for ChunkSectionPos { + fn azalea_read(buf: &mut Cursor<&[u8]>) -> Result<Self, BufReadError> { + let long = i64::azalea_read(buf)?; Ok(ChunkSectionPos { x: (long >> 42) as i32, y: (long << 44 >> 44) as i32, @@ -616,80 +654,35 @@ impl McBufReadable for ChunkSectionPos { } } -impl McBufWritable for BlockPos { - fn write_into(&self, buf: &mut impl Write) -> Result<(), std::io::Error> { +impl AzaleaWrite for BlockPos { + fn azalea_write(&self, buf: &mut impl Write) -> Result<(), std::io::Error> { let mut val: u64 = 0; val |= ((self.x as u64) & PACKED_X_MASK) << X_OFFSET; val |= (self.y as u64) & PACKED_Y_MASK; val |= ((self.z as u64) & PACKED_Z_MASK) << Z_OFFSET; - val.write_into(buf) + val.azalea_write(buf) } } -impl McBufWritable for GlobalPos { - fn write_into(&self, buf: &mut impl Write) -> Result<(), std::io::Error> { - ResourceLocation::write_into(&self.world, buf)?; - BlockPos::write_into(&self.pos, buf)?; +impl AzaleaWrite for GlobalPos { + fn azalea_write(&self, buf: &mut impl Write) -> Result<(), std::io::Error> { + ResourceLocation::azalea_write(&self.world, buf)?; + BlockPos::azalea_write(&self.pos, buf)?; Ok(()) } } -impl McBufWritable for ChunkSectionPos { - fn write_into(&self, buf: &mut impl Write) -> Result<(), std::io::Error> { +impl AzaleaWrite for ChunkSectionPos { + fn azalea_write(&self, buf: &mut impl Write) -> Result<(), std::io::Error> { let long = (((self.x & 0x3FFFFF) as i64) << 42) | (self.y & 0xFFFFF) as i64 | (((self.z & 0x3FFFFF) as i64) << 20); - long.write_into(buf)?; + long.azalea_write(buf)?; Ok(()) } } -fn parse_three_values<T>(s: &str) -> Result<[T; 3], &'static str> -where - T: FromStr, - <T as FromStr>::Err: fmt::Debug, -{ - let parts = s.split_whitespace().collect::<Vec<_>>(); - if parts.len() != 3 { - return Err("Expected three values"); - } - - let x = parts[0].parse().map_err(|_| "Invalid X value")?; - let y = parts[1].parse().map_err(|_| "Invalid Y value")?; - let z = parts[2].parse().map_err(|_| "Invalid Z value")?; - - Ok([x, y, z]) -} - -/// Parses a string in the format "X Y Z" into a BlockPos. -/// -/// The input string should contain three integer values separated by spaces, -/// representing the x, y, and z components of the vector respectively. -/// This can be used to parse user input or from `BlockPos::to_string`. -impl FromStr for BlockPos { - type Err = &'static str; - - fn from_str(s: &str) -> Result<Self, Self::Err> { - let [x, y, z] = parse_three_values::<i32>(s)?; - Ok(BlockPos { x, y, z }) - } -} - -/// Parses a string in the format "X Y Z" into a Vec3. -/// -/// The input string should contain three floating-point values separated by -/// spaces, representing the x, y, and z components of the vector respectively. -/// This can be used to parse user input or from `Vec3::to_string`. -impl FromStr for Vec3 { - type Err = &'static str; - - fn from_str(s: &str) -> Result<Self, Self::Err> { - let [x, y, z] = parse_three_values::<f64>(s)?; - Ok(Vec3 { x, y, z }) - } -} - #[cfg(test)] mod tests { use super::*; @@ -733,9 +726,9 @@ mod tests { #[test] fn test_read_blockpos_from() { let mut buf = Vec::new(); - 13743895338965u64.write_into(&mut buf).unwrap(); + 13743895338965u64.azalea_write(&mut buf).unwrap(); let mut buf = Cursor::new(&buf[..]); - let block_pos = BlockPos::read_from(&mut buf).unwrap(); + let block_pos = BlockPos::azalea_read(&mut buf).unwrap(); assert_eq!(block_pos, BlockPos::new(49, -43, -3)); } @@ -751,9 +744,9 @@ mod tests { #[test] fn test_read_chunk_pos_from() { let mut buf = Vec::new(); - ChunkPos::new(2, -1).write_into(&mut buf).unwrap(); + ChunkPos::new(2, -1).azalea_write(&mut buf).unwrap(); let mut buf = Cursor::new(&buf[..]); - let chunk_pos = ChunkPos::from(u64::read_from(&mut buf).unwrap()); + let chunk_pos = ChunkPos::from(u64::azalea_read(&mut buf).unwrap()); assert_eq!(chunk_pos, ChunkPos::new(2, -1)); } } diff --git a/azalea-core/src/resource_location.rs b/azalea-core/src/resource_location.rs index 7e1e48fe..c95e3109 100755 --- a/azalea-core/src/resource_location.rs +++ b/azalea-core/src/resource_location.rs @@ -6,7 +6,7 @@ use std::{ str::FromStr, }; -use azalea_buf::{BufReadError, McBufReadable, McBufWritable}; +use azalea_buf::{AzaleaRead, AzaleaWrite, BufReadError}; #[cfg(feature = "serde")] use serde::{de, Deserialize, Deserializer, Serialize, Serializer}; use simdnbt::{owned::NbtTag, FromNbtTag, ToNbtTag}; @@ -60,15 +60,15 @@ impl FromStr for ResourceLocation { } } -impl McBufReadable for ResourceLocation { - fn read_from(buf: &mut Cursor<&[u8]>) -> Result<Self, BufReadError> { - let location_string = String::read_from(buf)?; +impl AzaleaRead for ResourceLocation { + fn azalea_read(buf: &mut Cursor<&[u8]>) -> Result<Self, BufReadError> { + let location_string = String::azalea_read(buf)?; Ok(ResourceLocation::new(&location_string)) } } -impl McBufWritable for ResourceLocation { - fn write_into(&self, buf: &mut impl Write) -> Result<(), std::io::Error> { - self.to_string().write_into(buf) +impl AzaleaWrite for ResourceLocation { + fn azalea_write(&self, buf: &mut impl Write) -> Result<(), std::io::Error> { + self.to_string().azalea_write(buf) } } @@ -142,16 +142,16 @@ mod tests { } #[test] - fn mcbuf_resource_location() { + fn azbuf_resource_location() { let mut buf = Vec::new(); ResourceLocation::new("minecraft:dirt") - .write_into(&mut buf) + .azalea_write(&mut buf) .unwrap(); let mut buf = Cursor::new(&buf[..]); assert_eq!( - ResourceLocation::read_from(&mut buf).unwrap(), + ResourceLocation::azalea_read(&mut buf).unwrap(), ResourceLocation::new("minecraft:dirt") ); } |
