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/src | |
| parent | 6a83a6fa387170ae71fbe06791cf3afa20aac1df (diff) | |
| download | azalea-drasl-2aa046c4b50a0de850eb567cd8bced03e8f99bd6.tar.xz | |
make BlockState::id private
Diffstat (limited to 'azalea-block/src')
| -rw-r--r-- | azalea-block/src/block_state.rs | 21 | ||||
| -rw-r--r-- | azalea-block/src/range.rs | 2 |
2 files changed, 20 insertions, 3 deletions
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 } } |
