From f9c25665c203d6377ace62f1e95381d037d8fd9e Mon Sep 17 00:00:00 2001 From: mat <27899617+mat-1@users.noreply.github.com> Date: Fri, 12 Dec 2025 00:56:02 -0600 Subject: Refactor azalea-registry (#294) * move registries in azalea-registry into separate modules * rename Item and Block to ItemKind and BlockKind * remove 'extra' registries from azalea-registry * hide deprecated items from docs * use DamageKindKey instead of Identifier when parsing registries * store tag entries as a Vec instead of a HashSet * sort tag values by protocol id * update changelog --- azalea-block/src/block_state.rs | 15 +++++---------- azalea-block/src/fluid_state.rs | 10 ++++++---- azalea-block/src/lib.rs | 11 ++++++----- azalea-block/src/range.rs | 37 +++++++++++++++++++++++++------------ 4 files changed, 42 insertions(+), 31 deletions(-) (limited to 'azalea-block/src') diff --git a/azalea-block/src/block_state.rs b/azalea-block/src/block_state.rs index e3f894bf..d39961b3 100644 --- a/azalea-block/src/block_state.rs +++ b/azalea-block/src/block_state.rs @@ -4,6 +4,7 @@ use std::{ }; use azalea_buf::{AzaleaRead, AzaleaReadVar, AzaleaWrite, AzaleaWriteVar, BufReadError}; +use azalea_registry::builtin::BlockKind; use crate::BlockTrait; @@ -129,7 +130,7 @@ impl Debug for BlockState { } } -impl From for azalea_registry::Block { +impl From for BlockKind { fn from(value: BlockState) -> Self { Box::::from(value).as_registry_block() } @@ -156,22 +157,16 @@ mod tests { assert_eq!(block.id(), "air"); let block: Box = - Box::::from(BlockState::from(azalea_registry::Block::FloweringAzalea)); + Box::::from(BlockState::from(BlockKind::FloweringAzalea)); assert_eq!(block.id(), "flowering_azalea"); } #[test] fn test_debug_blockstate() { - let formatted = format!( - "{:?}", - BlockState::from(azalea_registry::Block::FloweringAzalea) - ); + let formatted = format!("{:?}", BlockState::from(BlockKind::FloweringAzalea)); assert!(formatted.ends_with(", FloweringAzalea)"), "{}", formatted); - let formatted = format!( - "{:?}", - BlockState::from(azalea_registry::Block::BigDripleafStem) - ); + let formatted = format!("{:?}", BlockState::from(BlockKind::BigDripleafStem)); assert!( formatted.ends_with(", BigDripleafStem { facing: North, waterlogged: false })"), "{}", diff --git a/azalea-block/src/fluid_state.rs b/azalea-block/src/fluid_state.rs index 80d0953b..0a8f7336 100644 --- a/azalea-block/src/fluid_state.rs +++ b/azalea-block/src/fluid_state.rs @@ -1,3 +1,5 @@ +use azalea_registry::builtin::BlockKind; + use crate::block_state::{BlockState, BlockStateIntegerRepr}; #[derive(Clone, Debug)] @@ -80,9 +82,9 @@ impl From for FluidState { }; } - let registry_block = azalea_registry::Block::from(state); + let registry_block = BlockKind::from(state); match registry_block { - azalea_registry::Block::Water => { + BlockKind::Water => { let level = state .property::() .expect("water block should always have WaterLevel"); @@ -92,7 +94,7 @@ impl From for FluidState { falling: false, }; } - azalea_registry::Block::Lava => { + BlockKind::Lava => { let level = state .property::() .expect("lava block should always have LavaLevel"); @@ -102,7 +104,7 @@ impl From for FluidState { falling: false, }; } - azalea_registry::Block::BubbleColumn => { + BlockKind::BubbleColumn => { return Self::new_source_block(FluidKind::Water, false); } _ => {} diff --git a/azalea-block/src/lib.rs b/azalea-block/src/lib.rs index 2cab022e..ad78102c 100644 --- a/azalea-block/src/lib.rs +++ b/azalea-block/src/lib.rs @@ -9,6 +9,7 @@ mod range; use core::fmt::Debug; use std::{any::Any, collections::HashMap}; +use azalea_registry::builtin::BlockKind; pub use behavior::BlockBehavior; // re-exported for convenience pub use block_state::BlockState; @@ -21,16 +22,16 @@ pub trait BlockTrait: Debug + Any { /// /// For example, `stone` or `grass_block`. fn id(&self) -> &'static str; - /// Convert the block to a block state. + /// Convert the block struct to a [`BlockState`]. /// /// This is a lossless conversion, as [`BlockState`] also contains state /// data. fn as_block_state(&self) -> BlockState; - /// Convert the block to an [`azalea_registry::Block`]. + /// Convert the block struct to a [`BlockKind`]. /// - /// This is a lossy conversion, as [`azalea_registry::Block`] doesn't - /// contain any state data. - fn as_registry_block(&self) -> azalea_registry::Block; + /// This is a lossy conversion, as [`BlockKind`] doesn't contain any state + /// data. + fn as_registry_block(&self) -> BlockKind; /// Returns a map of property names on this block to their values as /// strings. diff --git a/azalea-block/src/range.rs b/azalea-block/src/range.rs index 76b18079..53d2c10f 100644 --- a/azalea-block/src/range.rs +++ b/azalea-block/src/range.rs @@ -1,9 +1,10 @@ use std::{ collections::{HashSet, hash_set}, ops::{Add, RangeInclusive}, + sync::LazyLock, }; -use azalea_registry::Block; +use azalea_registry::{builtin::BlockKind, tags::RegistryTag}; use crate::{BlockState, block_state::BlockStateIntegerRepr}; @@ -47,14 +48,14 @@ impl Add for BlockStates { } } -impl From> for BlockStates { - fn from(set: HashSet) -> Self { +impl From> for BlockStates { + fn from(set: HashSet) -> Self { Self::from(&set) } } -impl From<&HashSet> for BlockStates { - fn from(set: &HashSet) -> Self { +impl From<&HashSet> for BlockStates { + fn from(set: &HashSet) -> Self { let mut block_states = HashSet::with_capacity(set.len()); for &block in set { block_states.extend(BlockStates::from(block)); @@ -63,13 +64,8 @@ impl From<&HashSet> for BlockStates { } } -impl From<[Block; N]> for BlockStates { - fn from(arr: [Block; N]) -> Self { - Self::from(&arr[..]) - } -} -impl From<&[Block]> for BlockStates { - fn from(arr: &[Block]) -> Self { +impl From<&[BlockKind]> for BlockStates { + fn from(arr: &[BlockKind]) -> Self { let mut block_states = HashSet::with_capacity(arr.len()); for &block in arr { block_states.extend(BlockStates::from(block)); @@ -77,3 +73,20 @@ impl From<&[Block]> for BlockStates { Self { set: block_states } } } +impl From<[BlockKind; N]> for BlockStates { + fn from(arr: [BlockKind; N]) -> Self { + Self::from(&arr[..]) + } +} +impl From<&RegistryTag> for BlockStates { + fn from(tag: &RegistryTag) -> Self { + Self::from(&**tag) + } +} +// allows users to do like `BlockStates::from(&tags::blocks::LOGS)` instead of +// `BlockStates::from(&&tags::blocks::LOGS)` +impl From<&LazyLock>> for BlockStates { + fn from(tag: &LazyLock>) -> Self { + Self::from(&**tag) + } +} -- cgit v1.2.3