diff options
| author | mat <git@matdoes.dev> | 2025-04-17 22:17:18 +0200 |
|---|---|---|
| committer | mat <git@matdoes.dev> | 2025-04-17 11:09:14 -0930 |
| commit | 2aa046c4b50a0de850eb567cd8bced03e8f99bd6 (patch) | |
| tree | 2fda147226a725b588ef0e7ff36b22cad6509bd4 /azalea-block | |
| parent | 6a83a6fa387170ae71fbe06791cf3afa20aac1df (diff) | |
| download | azalea-drasl-2aa046c4b50a0de850eb567cd8bced03e8f99bd6.tar.xz | |
make BlockState::id private
Diffstat (limited to 'azalea-block')
| -rw-r--r-- | azalea-block/azalea-block-macros/src/lib.rs | 10 | ||||
| -rw-r--r-- | azalea-block/src/block_state.rs | 21 | ||||
| -rw-r--r-- | azalea-block/src/range.rs | 2 |
3 files changed, 25 insertions, 8 deletions
diff --git a/azalea-block/azalea-block-macros/src/lib.rs b/azalea-block/azalea-block-macros/src/lib.rs index 7952a8d1..f1b5c10f 100644 --- a/azalea-block/azalea-block-macros/src/lib.rs +++ b/azalea-block/azalea-block-macros/src/lib.rs @@ -552,7 +552,7 @@ pub fn make_block_states(input: TokenStream) -> TokenStream { from_block_to_state_match_inner.extend(quote! { #block_struct_name { #from_block_to_state_combination_match_inner - } => BlockState { id: #state_id }, + } => BlockState::new_const(#state_id), }); if is_default { @@ -626,7 +626,7 @@ pub fn make_block_states(input: TokenStream) -> TokenStream { azalea_registry::Block::#block_name_pascal_case => Box::new(#block_struct_name::default()), }); from_registry_block_to_blockstate_match.extend(quote! { - azalea_registry::Block::#block_name_pascal_case => BlockState { id: #default_state_id }, + azalea_registry::Block::#block_name_pascal_case => BlockState::new_const(#default_state_id), }); from_registry_block_to_blockstates_match.extend(quote! { azalea_registry::Block::#block_name_pascal_case => BlockStates::from(#first_state_id..=#last_state_id), @@ -646,7 +646,7 @@ pub fn make_block_states(input: TokenStream) -> TokenStream { let block_id = block.name.to_string(); let from_block_to_state_match = if block.properties_and_defaults.is_empty() { - quote! { BlockState { id: #first_state_id } } + quote! { BlockState::new_const(#first_state_id) } } else { quote! { match self { @@ -762,7 +762,7 @@ pub fn make_block_states(input: TokenStream) -> TokenStream { type Value = #value; fn try_from_block_state(block_state: BlockState) -> Option<Self::Value> { - match block_state.id { + match block_state.id() { #enum_inner_generated _ => None } @@ -788,7 +788,7 @@ pub fn make_block_states(input: TokenStream) -> TokenStream { impl From<BlockState> for Box<dyn Block> { fn from(block_state: BlockState) -> Self { - let b = block_state.id; + let b = block_state.id(); match b { #from_state_to_block_match _ => panic!("Invalid block state: {}", b), diff --git a/azalea-block/src/block_state.rs b/azalea-block/src/block_state.rs index 6a185ee4..36a01863 100644 --- a/azalea-block/src/block_state.rs +++ b/azalea-block/src/block_state.rs @@ -1,5 +1,6 @@ use std::{ fmt::{self, Debug}, + hint::assert_unchecked, io::{self, Cursor, Write}, }; @@ -27,7 +28,7 @@ pub type BlockStateIntegerRepr = u16; pub struct BlockState { /// The protocol ID for the block state. IDs may change every /// version, so you shouldn't hard-code them or store them in databases. - pub id: BlockStateIntegerRepr, + id: BlockStateIntegerRepr, } impl BlockState { @@ -35,12 +36,21 @@ impl BlockState { /// 0. pub const AIR: BlockState = BlockState { id: 0 }; + /// Create a new BlockState and panic if the block is not a valid state. + /// + /// You should probably use [`BlockState::try_from`] instead. + #[inline] + pub(crate) const fn new_const(id: BlockStateIntegerRepr) -> Self { + assert!(Self::is_valid_state(id)); + Self { id } + } + /// Whether the block state is possible to exist in vanilla Minecraft. /// /// It's equivalent to checking that the state ID is not greater than /// [`Self::MAX_STATE`]. #[inline] - pub fn is_valid_state(state_id: BlockStateIntegerRepr) -> bool { + pub const fn is_valid_state(state_id: BlockStateIntegerRepr) -> bool { state_id <= Self::MAX_STATE } @@ -50,6 +60,13 @@ impl BlockState { pub fn is_air(&self) -> bool { self == &Self::AIR } + + /// Returns the protocol ID for the block state. IDs may change every + /// version, so you shouldn't hard-code them or store them in databases. + #[inline] + pub const fn id(&self) -> BlockStateIntegerRepr { + self.id + } } impl TryFrom<u32> for BlockState { diff --git a/azalea-block/src/range.rs b/azalea-block/src/range.rs index 18e74c88..cbe77284 100644 --- a/azalea-block/src/range.rs +++ b/azalea-block/src/range.rs @@ -14,7 +14,7 @@ impl From<RangeInclusive<BlockStateIntegerRepr>> for BlockStates { fn from(range: RangeInclusive<BlockStateIntegerRepr>) -> Self { let mut set = HashSet::with_capacity((range.end() - range.start() + 1) as usize); for id in range { - set.insert(BlockState { id }); + set.insert(BlockState::try_from(id).unwrap_or_default()); } Self { set } } |
