diff options
| author | mat <github@matdoes.dev> | 2022-06-23 19:17:04 -0500 |
|---|---|---|
| committer | mat <github@matdoes.dev> | 2022-06-23 19:17:04 -0500 |
| commit | 37c6618c16319a7f40fd2e165190407472598e84 (patch) | |
| tree | 6d712df8893041f0e49cfa6ca7adc9a411d39b84 /azalea-core/src | |
| parent | 1089aa7961bd9af67c94dec9c5dbc6bd9f275225 (diff) | |
| download | azalea-drasl-37c6618c16319a7f40fd2e165190407472598e84.tar.xz | |
Fix everything so azalea-buf works
Diffstat (limited to 'azalea-core/src')
| -rw-r--r-- | azalea-core/src/delta.rs | 1 | ||||
| -rwxr-xr-x | azalea-core/src/difficulty.rs | 15 | ||||
| -rw-r--r-- | azalea-core/src/direction.rs | 2 | ||||
| -rwxr-xr-x | azalea-core/src/game_type.rs | 48 | ||||
| -rwxr-xr-x | azalea-core/src/lib.rs | 12 | ||||
| -rw-r--r-- | azalea-core/src/particle/mod.rs | 3 | ||||
| -rw-r--r-- | azalea-core/src/position.rs | 18 | ||||
| -rwxr-xr-x | azalea-core/src/resource_location.rs | 16 | ||||
| -rwxr-xr-x | azalea-core/src/serializable_uuid.rs | 73 | ||||
| -rw-r--r-- | azalea-core/src/slot.rs | 24 |
10 files changed, 89 insertions, 123 deletions
diff --git a/azalea-core/src/delta.rs b/azalea-core/src/delta.rs index a4d02907..339e52cd 100644 --- a/azalea-core/src/delta.rs +++ b/azalea-core/src/delta.rs @@ -1,3 +1,4 @@ +use crate::EntityPos; pub use azalea_buf::McBuf; /// Only works for up to 8 blocks diff --git a/azalea-core/src/difficulty.rs b/azalea-core/src/difficulty.rs index 7568f8e2..ebbb7708 100755 --- a/azalea-core/src/difficulty.rs +++ b/azalea-core/src/difficulty.rs @@ -1,11 +1,16 @@ -use std::fmt::{Debug, Error, Formatter}; +use std::{ + fmt::{Debug, Error, Formatter}, + io::{Read, Write}, +}; + +use azalea_buf::{McBufReadable, McBufWritable}; #[derive(Hash, Clone, Debug, PartialEq)] pub enum Difficulty { - PEACEFUL, - EASY, - NORMAL, - HARD, + PEACEFUL = 0, + EASY = 1, + NORMAL = 2, + HARD = 3, } pub enum Err { diff --git a/azalea-core/src/direction.rs b/azalea-core/src/direction.rs index bb655bdb..d3083922 100644 --- a/azalea-core/src/direction.rs +++ b/azalea-core/src/direction.rs @@ -1,3 +1,5 @@ +use azalea_buf::McBuf; + #[derive(Clone, Copy, Debug, McBuf)] pub enum Direction { Down = 0, diff --git a/azalea-core/src/game_type.rs b/azalea-core/src/game_type.rs index 963048dd..67c392b2 100755 --- a/azalea-core/src/game_type.rs +++ b/azalea-core/src/game_type.rs @@ -1,4 +1,7 @@ -#[derive(Hash, Clone, Debug)] +use azalea_buf::{McBufReadable, McBufWritable}; +use std::io::{Read, Write}; + +#[derive(Hash, Copy, Clone, Debug)] pub enum GameType { SURVIVAL, CREATIVE, @@ -17,8 +20,8 @@ impl GameType { } /// Get the id of the game type, but return -1 if the game type is invalid. - pub fn to_optional_id(game_type: &Option<GameType>) -> i8 { - match game_type { + pub fn to_optional_id<T: Into<Option<GameType>>>(game_type: T) -> i8 { + match game_type.into() { Some(game_type) => game_type.to_id() as i8, None => -1, } @@ -34,11 +37,12 @@ impl GameType { }) } - pub fn from_optional_id(id: i8) -> Result<Option<GameType>, String> { + pub fn from_optional_id(id: i8) -> Result<OptionalGameType, String> { Ok(match id { -1 => None, id => Some(GameType::from_id(id as u8)?), - }) + } + .into()) } pub fn short_name(&self) -> &'static str { @@ -74,13 +78,7 @@ impl GameType { impl McBufReadable for GameType { fn read_into(buf: &mut impl Read) -> Result<Self, String> { - GameType::from_id(buf.read_byte()?) - } -} - -impl McBufReadable for Option<GameType> { - fn read_into(buf: &mut impl Read) -> Result<Self, String> { - GameType::from_optional_id(buf.read_byte()? as i8) + GameType::from_id(u8::read_into(buf)?) } } @@ -90,8 +88,30 @@ impl McBufWritable for GameType { } } -impl McBufWritable for Option<GameType> { +/// Rust doesn't let us `impl McBufReadable for Option<GameType>` so we have to make a new type :( +#[derive(Hash, Copy, Clone, Debug)] +pub struct OptionalGameType(Option<GameType>); + +impl From<Option<GameType>> for OptionalGameType { + fn from(game_type: Option<GameType>) -> Self { + OptionalGameType(game_type) + } +} + +impl From<OptionalGameType> for Option<GameType> { + fn from(optional_game_type: OptionalGameType) -> Self { + optional_game_type.0 + } +} + +impl McBufReadable for OptionalGameType { + fn read_into(buf: &mut impl Read) -> Result<Self, String> { + GameType::from_optional_id(i8::read_into(buf)?) + } +} + +impl McBufWritable for OptionalGameType { fn write_into(&self, buf: &mut impl Write) -> Result<(), std::io::Error> { - buf.write_byte(GameType::to_optional_id(self) as u8) + GameType::to_optional_id(*self).write_into(buf) } } diff --git a/azalea-core/src/lib.rs b/azalea-core/src/lib.rs index 41c901c8..a1fa1fca 100755 --- a/azalea-core/src/lib.rs +++ b/azalea-core/src/lib.rs @@ -2,10 +2,14 @@ #![feature(int_roundings)] -pub mod difficulty; -pub mod game_type; -pub mod resource_location; -pub mod serializable_uuid; +mod difficulty; +pub use difficulty::*; + +mod resource_location; +pub use resource_location::*; + +mod game_type; +pub use game_type::*; mod slot; pub use slot::{Slot, SlotData}; diff --git a/azalea-core/src/particle/mod.rs b/azalea-core/src/particle/mod.rs index 5ca8ac8e..6eb53955 100644 --- a/azalea-core/src/particle/mod.rs +++ b/azalea-core/src/particle/mod.rs @@ -1,4 +1,5 @@ -use azalea_buf::{McBufReadable, McBufWritable}; +use crate::{BlockPos, Slot}; +use azalea_buf::{McBuf, McBufReadable, McBufVarReadable, McBufWritable}; use std::io::{Read, Write}; #[derive(Debug, Clone, McBuf)] diff --git a/azalea-core/src/position.rs b/azalea-core/src/position.rs index 9e7aca20..8caa5799 100644 --- a/azalea-core/src/position.rs +++ b/azalea-core/src/position.rs @@ -1,6 +1,9 @@ -use std::ops::Rem; - -use crate::resource_location::ResourceLocation; +use crate::ResourceLocation; +use azalea_buf::{McBufReadable, McBufWritable}; +use std::{ + io::{Read, Write}, + ops::Rem, +}; #[derive(Clone, Copy, Debug, Default, Eq, PartialEq)] pub struct BlockPos { @@ -202,11 +205,10 @@ impl McBufReadable for ChunkSectionPos { impl McBufWritable for BlockPos { fn write_into(&self, buf: &mut impl Write) -> Result<(), std::io::Error> { - buf.write_long( - (((self.x & 0x3FFFFFF) as i64) << 38) - | (((self.z & 0x3FFFFFF) as i64) << 12) - | ((self.y & 0xFFF) as i64), - ) + let data = (((self.x & 0x3FFFFFF) as i64) << 38) + | (((self.z & 0x3FFFFFF) as i64) << 12) + | ((self.y & 0xFFF) as i64); + data.write_into(buf) } } diff --git a/azalea-core/src/resource_location.rs b/azalea-core/src/resource_location.rs index 6807714b..61ae8a20 100755 --- a/azalea-core/src/resource_location.rs +++ b/azalea-core/src/resource_location.rs @@ -1,5 +1,8 @@ //! A resource, like minecraft:stone +use azalea_buf::{McBufReadable, McBufWritable}; +use std::io::{Read, Write}; + #[derive(Hash, Clone, PartialEq, Eq)] pub struct ResourceLocation { pub namespace: String, @@ -44,18 +47,20 @@ impl std::fmt::Debug for ResourceLocation { impl McBufReadable for ResourceLocation { fn read_into(buf: &mut impl Read) -> Result<Self, String> { - let location_string = self.read_utf()?; + let location_string = String::read_into(buf)?; ResourceLocation::new(&location_string) } } impl McBufWritable for ResourceLocation { fn write_into(&self, buf: &mut impl Write) -> Result<(), std::io::Error> { - buf.write_utf(&self.to_string()) + self.to_string().write_into(buf) } } #[cfg(test)] mod tests { + use std::io::Cursor; + use super::*; #[test] @@ -86,13 +91,14 @@ mod tests { #[test] fn mcbuf_resource_location() { let mut buf = Vec::new(); - buf.write_resource_location(&ResourceLocation::new("minecraft:dirt").unwrap()) - .unwrap(); + ResourceLocation::new("minecraft:dirt") + .unwrap() + .write_into(&mut buf)?; let mut buf = Cursor::new(buf); assert_eq!( - buf.read_resource_location().unwrap(), + ResourceLocation::read_into(&mut buf).unwrap(), ResourceLocation::new("minecraft:dirt").unwrap() ); } diff --git a/azalea-core/src/serializable_uuid.rs b/azalea-core/src/serializable_uuid.rs deleted file mode 100755 index 2c7128ff..00000000 --- a/azalea-core/src/serializable_uuid.rs +++ /dev/null @@ -1,73 +0,0 @@ -use uuid::Uuid; - -pub trait SerializableUuid { - fn to_int_array(&self) -> [u32; 4]; - fn from_int_array(array: [u32; 4]) -> Self; -} - -fn least_most_to_int_array(most: u64, least: u64) -> [u32; 4] { - [ - (most >> 32) as u32, - most as u32, - (least >> 32) as u32, - least as u32, - ] -} - -impl SerializableUuid for Uuid { - fn to_int_array(&self) -> [u32; 4] { - let most_significant_bits = (self.as_u128() >> 64) as u64; - let least_significant_bits = (self.as_u128() & 0xffffffffffffffff) as u64; - - least_most_to_int_array(most_significant_bits, least_significant_bits) - } - - fn from_int_array(array: [u32; 4]) -> Self { - let most = ((array[0] as u64) << 32) | ((array[1] as u64) & 0xFFFFFFFF); - let least = ((array[2] as u64) << 32) | ((array[3] as u64) & 0xFFFFFFFF); - - Uuid::from_u128(((most as u128) << 64) | least as u128) - } -} - -impl McBufReadable for Uuid { - fn read_into(buf: &mut impl Read) -> Result<Self, String> { - Ok(Uuid::from_int_array([ - Readable::read_int(self)? as u32, - Readable::read_int(self)? as u32, - Readable::read_int(self)? as u32, - Readable::read_int(self)? as u32, - ])) - } -} - -impl McBufWritable for Uuid { - fn write_into(&self, buf: &mut impl Write) -> Result<(), std::io::Error> { - let [a, b, c, d] = self.to_int_array(); - a.write_into(buf)?; - b.write_into(buf)?; - c.write_into(buf)?; - d.write_into(buf)?; - Ok(()) - } -} - -#[cfg(tests)] -mod tests { - use super::*; - - #[test] - fn to_int_array() { - let u = Uuid::parse_str("6536bfed-8695-48fd-83a1-ecd24cf2a0fd").unwrap(); - assert_eq!( - u.to_int_array(), - [0x6536bfed, 0x869548fd, 0x83a1ecd2, 0x4cf2a0fd] - ); - } - - #[test] - fn from_int_array() { - let u = Uuid::from_int_array([0x6536bfed, 0x869548fd, 0x83a1ecd2, 0x4cf2a0fd]); - assert_eq!(u.to_string(), "6536bfed-8695-48fd-83a1-ecd24cf2a0fd"); - } -} diff --git a/azalea-core/src/slot.rs b/azalea-core/src/slot.rs index e3b78289..6e622872 100644 --- a/azalea-core/src/slot.rs +++ b/azalea-core/src/slot.rs @@ -1,13 +1,17 @@ // TODO: have an azalea-inventory or azalea-container crate and put this there +use azalea_buf::{McBuf, McBufReadable, McBufWritable}; +use std::io::{Read, Write}; + #[derive(Debug, Clone)] pub enum Slot { - Present(SlotData), Empty, + Present(SlotData), } -#[derive(Debug, Clone)] +#[derive(Debug, Clone, McBuf)] pub struct SlotData { + #[var] pub id: i32, pub count: u8, pub nbt: azalea_nbt::Tag, @@ -15,26 +19,20 @@ pub struct SlotData { impl McBufReadable for Slot { fn read_into(buf: &mut impl Read) -> Result<Self, String> { - let present = buf.read_boolean()?; + let present = bool::read_into(buf)?; if !present { return Ok(Slot::Empty); } - let id = buf.read_varint()?; - let count = buf.read_byte()?; - let nbt = buf.read_nbt()?; - Ok(Slot::Present(SlotData { id, count, nbt })) + let slot = SlotData::read_into(buf)?; + Ok(Slot::Present(slot)) } } impl McBufWritable for Slot { fn write_into(&self, buf: &mut impl Write) -> Result<(), std::io::Error> { match self { - Slot::Empty => buf.write_byte(0)?, - Slot::Present(i) => { - buf.write_varint(i.id)?; - buf.write_byte(i.count)?; - buf.write_nbt(&i.nbt)?; - } + Slot::Empty => 0u8.write_into(buf)?, + Slot::Present(i) => i.write_into(buf)?, } Ok(()) |
