azalea_block/
lib.rs

1#![doc = include_str!("../README.md")]
2
3mod behavior;
4pub mod block_state;
5pub mod fluid_state;
6mod generated;
7mod range;
8
9use core::fmt::Debug;
10use std::{any::Any, collections::HashMap};
11
12use azalea_registry::builtin::BlockKind;
13pub use behavior::BlockBehavior;
14// re-exported for convenience
15pub use block_state::BlockState;
16pub use generated::{blocks, properties};
17pub use range::BlockStates;
18
19pub trait BlockTrait: Debug + Any {
20    fn behavior(&self) -> BlockBehavior;
21    /// Get the Minecraft string ID for this block.
22    ///
23    /// For example, `stone` or `grass_block`.
24    fn id(&self) -> &'static str;
25    /// Convert the block struct to a [`BlockState`].
26    ///
27    /// This is a lossless conversion, as [`BlockState`] also contains state
28    /// data.
29    fn as_block_state(&self) -> BlockState;
30    /// Convert the block struct to a [`BlockKind`].
31    ///
32    /// This is a lossy conversion, as [`BlockKind`] doesn't contain any state
33    /// data.
34    fn as_registry_block(&self) -> BlockKind;
35
36    /// Returns a map of property names on this block to their values as
37    /// strings.
38    ///
39    /// Consider using [`Self::get_property`] if you only need a single
40    /// property.
41    fn property_map(&self) -> HashMap<&'static str, &'static str>;
42    /// Get a property's value as a string by its name, or `None` if the block
43    /// has no property with that name.
44    ///
45    /// To get all properties, you may use [`Self::property_map`].
46    fn get_property(&self, name: &str) -> Option<&'static str>;
47}
48
49impl dyn BlockTrait {
50    pub fn downcast_ref<T: BlockTrait>(&self) -> Option<&T> {
51        (self as &dyn Any).downcast_ref::<T>()
52    }
53}
54
55pub trait Property {
56    type Value;
57
58    fn try_from_block_state(state: BlockState) -> Option<Self::Value>;
59
60    /// Convert the value of the property to a string, like "x" or "true".
61    fn to_static_str(&self) -> &'static str;
62}
63
64#[cfg(test)]
65mod tests {
66    use crate::BlockTrait;
67
68    #[test]
69    pub fn roundtrip_block_state() {
70        let block = crate::blocks::OakTrapdoor {
71            facing: crate::properties::FacingCardinal::East,
72            half: crate::properties::TopBottom::Bottom,
73            open: true,
74            powered: false,
75            waterlogged: false,
76        };
77        let block_state = block.as_block_state();
78        let block_from_state = Box::<dyn BlockTrait>::from(block_state);
79        let block_from_state = *block_from_state
80            .downcast_ref::<crate::blocks::OakTrapdoor>()
81            .unwrap();
82        assert_eq!(block, block_from_state);
83    }
84
85    #[test]
86    pub fn test_property_map() {
87        let block = crate::blocks::OakTrapdoor {
88            facing: crate::properties::FacingCardinal::East,
89            half: crate::properties::TopBottom::Bottom,
90            open: true,
91            powered: false,
92            waterlogged: false,
93        };
94
95        let property_map = block.property_map();
96
97        assert_eq!(property_map.len(), 5);
98        assert_eq!(property_map.get("facing"), Some(&"east"));
99        assert_eq!(property_map.get("half"), Some(&"bottom"));
100        assert_eq!(property_map.get("open"), Some(&"true"));
101        assert_eq!(property_map.get("powered"), Some(&"false"));
102        assert_eq!(property_map.get("waterlogged"), Some(&"false"));
103    }
104
105    #[test]
106    pub fn test_integer_properties() {
107        // Test with oak sapling that has an integer-like stage property
108        let sapling_stage_0 = crate::blocks::OakSapling {
109            stage: crate::properties::OakSaplingStage::_0,
110        };
111
112        let sapling_stage_1 = crate::blocks::OakSapling {
113            stage: crate::properties::OakSaplingStage::_1,
114        };
115
116        // Test stage 0
117        let properties_0 = sapling_stage_0.property_map();
118        assert_eq!(properties_0.len(), 1);
119        assert_eq!(properties_0.get("stage"), Some(&"0"));
120        assert_eq!(sapling_stage_0.get_property("stage"), Some("0"));
121
122        // Test stage 1
123        let properties_1 = sapling_stage_1.property_map();
124        assert_eq!(properties_1.len(), 1);
125        assert_eq!(properties_1.get("stage"), Some(&"1"));
126        assert_eq!(sapling_stage_1.get_property("stage"), Some("1"));
127
128        // Test non-existent property
129        assert_eq!(sapling_stage_0.get_property("nonexistent"), None);
130    }
131}