From 2aa046c4b50a0de850eb567cd8bced03e8f99bd6 Mon Sep 17 00:00:00 2001 From: mat Date: Thu, 17 Apr 2025 22:17:18 +0200 Subject: make BlockState::id private --- azalea-block/src/block_state.rs | 21 +++++++++++++++++++-- azalea-block/src/range.rs | 2 +- 2 files changed, 20 insertions(+), 3 deletions(-) (limited to 'azalea-block/src') 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 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> for BlockStates { fn from(range: RangeInclusive) -> 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 } } -- cgit v1.2.3