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, str::FromStr};
11
12use azalea_registry::builtin::BlockKind;
13pub use behavior::BlockBehavior;
14pub use block_state::BlockState;
16pub use generated::{blocks, properties};
17pub use range::BlockStates;
18
19pub trait BlockTrait: Debug + Any {
23 fn behavior(&self) -> BlockBehavior;
24 fn id(&self) -> &'static str;
28 fn as_block_state(&self) -> BlockState;
33 fn as_block_kind(&self) -> BlockKind;
38 #[deprecated = "renamed to as_block_kind"]
39 #[doc(hidden)]
40 fn as_registry_block(&self) -> BlockKind {
41 self.as_block_kind()
42 }
43
44 fn property_map(&self) -> HashMap<&'static str, &'static str>;
50 fn get_property(&self, name: &str) -> Option<&'static str>;
57 fn set_property(&mut self, name: &str, new_value: &str) -> Result<(), InvalidPropertyError>;
64}
65
66#[derive(Debug)]
67pub struct InvalidPropertyError;
68
69impl dyn BlockTrait {
70 pub fn downcast_ref<T: BlockTrait>(&self) -> Option<&T> {
71 (self as &dyn Any).downcast_ref::<T>()
72 }
73}
74
75pub trait Property: FromStr {
76 type Value;
77
78 fn try_from_block_state(state: BlockState) -> Option<Self::Value>;
79
80 fn to_static_str(&self) -> &'static str;
82}
83
84#[cfg(test)]
85mod tests {
86 use crate::BlockTrait;
87
88 #[test]
89 pub fn roundtrip_block_state() {
90 let block = crate::blocks::OakTrapdoor {
91 facing: crate::properties::FacingCardinal::East,
92 half: crate::properties::TopBottom::Bottom,
93 open: true,
94 powered: false,
95 waterlogged: false,
96 };
97 let block_state = block.as_block_state();
98 let block_from_state = Box::<dyn BlockTrait>::from(block_state);
99 let block_from_state = *block_from_state
100 .downcast_ref::<crate::blocks::OakTrapdoor>()
101 .unwrap();
102 assert_eq!(block, block_from_state);
103 }
104
105 #[test]
106 pub fn test_property_map() {
107 let block = crate::blocks::OakTrapdoor {
108 facing: crate::properties::FacingCardinal::East,
109 half: crate::properties::TopBottom::Bottom,
110 open: true,
111 powered: false,
112 waterlogged: false,
113 };
114
115 let property_map = block.property_map();
116
117 assert_eq!(property_map.len(), 5);
118 assert_eq!(property_map.get("facing"), Some(&"east"));
119 assert_eq!(property_map.get("half"), Some(&"bottom"));
120 assert_eq!(property_map.get("open"), Some(&"true"));
121 assert_eq!(property_map.get("powered"), Some(&"false"));
122 assert_eq!(property_map.get("waterlogged"), Some(&"false"));
123 }
124
125 #[test]
126 pub fn test_integer_properties() {
127 let sapling_stage_0 = crate::blocks::OakSapling {
129 stage: crate::properties::Stage::_0,
130 };
131
132 let sapling_stage_1 = crate::blocks::OakSapling {
133 stage: crate::properties::Stage::_1,
134 };
135
136 let properties_0 = sapling_stage_0.property_map();
138 assert_eq!(properties_0.len(), 1);
139 assert_eq!(properties_0.get("stage"), Some(&"0"));
140 assert_eq!(sapling_stage_0.get_property("stage"), Some("0"));
141
142 let properties_1 = sapling_stage_1.property_map();
144 assert_eq!(properties_1.len(), 1);
145 assert_eq!(properties_1.get("stage"), Some(&"1"));
146 assert_eq!(sapling_stage_1.get_property("stage"), Some("1"));
147
148 assert_eq!(sapling_stage_0.get_property("nonexistent"), None);
150 }
151}