diff options
Diffstat (limited to 'azalea-block/src')
| -rwxr-xr-x | azalea-block/src/blocks.rs | 11 | ||||
| -rwxr-xr-x | azalea-block/src/lib.rs | 43 |
2 files changed, 38 insertions, 16 deletions
diff --git a/azalea-block/src/blocks.rs b/azalea-block/src/blocks.rs index 226542dc..e6923d59 100755 --- a/azalea-block/src/blocks.rs +++ b/azalea-block/src/blocks.rs @@ -1,9 +1,18 @@ +use std::any::Any; + use crate::BlockBehavior; use azalea_block_macros::make_block_states; +use std::fmt::Debug; -pub trait Block { +pub trait Block: Debug + Any { fn behavior(&self) -> BlockBehavior; fn id(&self) -> &'static str; + fn as_blockstate(&self) -> BlockState; +} +impl dyn Block { + pub fn downcast_ref<T: Block>(&self) -> Option<&T> { + (self as &dyn Any).downcast_ref::<T>() + } } make_block_states! { diff --git a/azalea-block/src/lib.rs b/azalea-block/src/lib.rs index 4a35be00..7a62e588 100755 --- a/azalea-block/src/lib.rs +++ b/azalea-block/src/lib.rs @@ -1,4 +1,5 @@ #![doc = include_str!("../README.md")] +#![feature(trait_upcasting)] mod behavior; mod blocks; @@ -6,10 +7,7 @@ mod blocks; use azalea_buf::{BufReadError, McBufReadable, McBufVarReadable, McBufVarWritable, McBufWritable}; pub use behavior::BlockBehavior; pub use blocks::*; -use std::{ - io::{Cursor, Write}, - mem, -}; +use std::io::{Cursor, Write}; impl BlockState { /// Transmutes a u32 to a block state. @@ -17,8 +15,8 @@ impl BlockState { /// # Safety /// The `state_id` should be a valid block state. #[inline] - pub unsafe fn from_u32_unsafe(state_id: u32) -> Self { - mem::transmute::<u32, BlockState>(state_id) + pub unsafe fn from_u32_unchecked(state_id: u32) -> Self { + BlockState { id: state_id } } #[inline] @@ -33,7 +31,7 @@ impl TryFrom<u32> for BlockState { /// Safely converts a state id to a block state. fn try_from(state_id: u32) -> Result<Self, Self::Error> { if Self::is_valid_state(state_id) { - Ok(unsafe { Self::from_u32_unsafe(state_id) }) + Ok(unsafe { Self::from_u32_unchecked(state_id) }) } else { Err(()) } @@ -50,7 +48,7 @@ impl McBufReadable for BlockState { } impl McBufWritable for BlockState { fn write_into(&self, buf: &mut impl Write) -> Result<(), std::io::Error> { - u32::var_write_into(&(*self as u32), buf) + u32::var_write_into(&self.id, buf) } } @@ -60,7 +58,7 @@ mod tests { #[test] fn test_from_u32() { - assert_eq!(BlockState::try_from(0).unwrap(), BlockState::Air); + assert_eq!(BlockState::try_from(0).unwrap(), BlockState::AIR); assert!(BlockState::try_from(BlockState::max_state()).is_ok()); assert!(BlockState::try_from(BlockState::max_state() + 1).is_err()); @@ -68,19 +66,34 @@ mod tests { #[test] fn test_from_blockstate() { - let block: Box<dyn Block> = Box::<dyn Block>::from(BlockState::Air); + let block: Box<dyn Block> = Box::<dyn Block>::from(BlockState::AIR); assert_eq!(block.id(), "air"); - let block: Box<dyn Block> = Box::<dyn Block>::from(BlockState::FloweringAzalea); + let block: Box<dyn Block> = + Box::<dyn Block>::from(BlockState::from(azalea_registry::Block::FloweringAzalea)); assert_eq!(block.id(), "flowering_azalea"); } - #[cfg(not(feature = "full-debug"))] #[test] fn test_debug_blockstate() { - assert_eq!( - format!("{:?}", BlockState::FloweringAzalea), - "BlockState (flowering_azalea)" + let formatted = format!( + "{:?}", + BlockState::from(azalea_registry::Block::FloweringAzalea) + ); + assert!( + formatted.ends_with(", FloweringAzaleaBlock)"), + "{}", + formatted + ); + + let formatted = format!( + "{:?}", + BlockState::from(azalea_registry::Block::BigDripleafStem) + ); + assert!( + formatted.ends_with(", BigDripleafStemBlock { facing: North, waterlogged: false })"), + "{}", + formatted ); } } |
