aboutsummaryrefslogtreecommitdiff
path: root/azalea-block
diff options
context:
space:
mode:
authormat <27899617+mat-1@users.noreply.github.com>2025-12-12 00:56:02 -0600
committerGitHub <noreply@github.com>2025-12-12 00:56:02 -0600
commitf9c25665c203d6377ace62f1e95381d037d8fd9e (patch)
tree8b4131d20fe661d3cc1175ec27f801fe61df41ea /azalea-block
parent82ad975242292d5875780b4398b62637674bf50a (diff)
downloadazalea-drasl-f9c25665c203d6377ace62f1e95381d037d8fd9e.tar.xz
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
Diffstat (limited to 'azalea-block')
-rw-r--r--azalea-block/README.md15
-rw-r--r--azalea-block/azalea-block-macros/src/lib.rs29
-rw-r--r--azalea-block/src/block_state.rs15
-rw-r--r--azalea-block/src/fluid_state.rs10
-rw-r--r--azalea-block/src/lib.rs11
-rw-r--r--azalea-block/src/range.rs37
6 files changed, 65 insertions, 52 deletions
diff --git a/azalea-block/README.md b/azalea-block/README.md
index 8eca79ee..81e6b097 100644
--- a/azalea-block/README.md
+++ b/azalea-block/README.md
@@ -2,7 +2,7 @@ Representation of Minecraft block states.
There's three block types, used for different things. You can (mostly) convert between them with `.into()`.
-## BlockState struct
+## `BlockState` struct
[`BlockState`] is a struct containing the numerical protocol ID of a block state. This is how blocks are stored in the world.
@@ -20,29 +20,30 @@ let block_state: BlockState = azalea_block::blocks::CobblestoneWall {
```
```
# use azalea_block::BlockState;
-let block_state: BlockState = azalea_registry::Block::Jukebox.into();
+# use azalea_registry::builtin::BlockKind;
+let block_state: BlockState = BlockKind::Jukebox.into();
```
-## BlockTrait
+## `BlockTrait`
The [`BlockTrait`] trait represents a type of a block. With [`BlockTrait`], you can get some extra things like the string block ID and some information about the block's behavior. Also, the structs that implement the trait contain the block attributes as fields so it's more convenient to get them. Note that this is often used as `Box<dyn BlockTrait>`.
If for some reason you don't want `BlockTrait`, set `default-features = false`.
```
# use azalea_block::{BlockTrait, BlockState};
-# let block_state = BlockState::from(azalea_registry::Block::Jukebox);
+# let block_state = BlockState::from(azalea_registry::builtin::BlockKind::Jukebox);
let block = Box::<dyn BlockTrait>::from(block_state);
```
```
# use azalea_block::{BlockTrait, BlockState};
-# let block_state: BlockState = azalea_registry::Block::Jukebox.into();
+# let block_state: BlockState = azalea_registry::builtin::BlockKind::Jukebox.into();
if let Some(jukebox) = Box::<dyn BlockTrait>::from(block_state).downcast_ref::<azalea_block::blocks::Jukebox>() {
// ...
}
```
-## azalea_registry::Block enum
+## `azalea_registry::builtin::BlockKind` enum
-This one technically isn't from the `azalea-block` crate, but it's still very relevant. It's an enum that contains every block type as a variant *without* containing any state data (unlike `BlockState` and the `Block` trait). Converting this into any other block type will use the default state for that block.
+This one isn't from the `azalea-block` crate, but it's still very relevant. It's an enum that contains every block type as a variant *without* containing any state data (unlike `BlockState` and `BlockTrait`). Converting this into any other block type will use the default state for that block.
diff --git a/azalea-block/azalea-block-macros/src/lib.rs b/azalea-block/azalea-block-macros/src/lib.rs
index 843ea2ca..1abfacbb 100644
--- a/azalea-block/azalea-block-macros/src/lib.rs
+++ b/azalea-block/azalea-block-macros/src/lib.rs
@@ -424,13 +424,13 @@ pub fn make_block_states(input: TokenStream) -> TokenStream {
});
from_registry_block_to_block_match.extend(quote! {
- azalea_registry::Block::#block_name_pascal_case => Box::new(#block_struct_name::default()),
+ BlockKind::#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::new_const(#default_state_id),
+ BlockKind::#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),
+ BlockKind::#block_name_pascal_case => BlockStates::from(#first_state_id..=#last_state_id),
});
let mut property_map_inner = quote! {};
@@ -491,8 +491,8 @@ pub fn make_block_states(input: TokenStream) -> TokenStream {
fn as_block_state(&self) -> BlockState {
#as_block_state
}
- fn as_registry_block(&self) -> azalea_registry::Block {
- azalea_registry::Block::#block_name_pascal_case
+ fn as_registry_block(&self) -> BlockKind {
+ BlockKind::#block_name_pascal_case
}
fn property_map(&self) -> std::collections::HashMap<&'static str, &'static str> {
@@ -557,6 +557,7 @@ pub fn make_block_states(input: TokenStream) -> TokenStream {
pub mod blocks {
use super::*;
+ use azalea_registry::builtin::BlockKind;
#block_structs
@@ -569,27 +570,27 @@ pub fn make_block_states(input: TokenStream) -> TokenStream {
}
}
}
- impl From<azalea_registry::Block> for Box<dyn BlockTrait> {
- fn from(block: azalea_registry::Block) -> Self {
+ impl From<BlockKind> for Box<dyn BlockTrait> {
+ fn from(block: BlockKind) -> Self {
match block {
#from_registry_block_to_block_match
- _ => unreachable!("There should always be a block struct for every azalea_registry::Block variant")
+ _ => unreachable!("There should always be a block struct for every BlockKind variant")
}
}
}
- impl From<azalea_registry::Block> for BlockState {
- fn from(block: azalea_registry::Block) -> Self {
+ impl From<BlockKind> for BlockState {
+ fn from(block: BlockKind) -> Self {
match block {
#from_registry_block_to_blockstate_match
- _ => unreachable!("There should always be a block state for every azalea_registry::Block variant")
+ _ => unreachable!("There should always be a block state for every BlockKind variant")
}
}
}
- impl From<azalea_registry::Block> for BlockStates {
- fn from(block: azalea_registry::Block) -> Self {
+ impl From<BlockKind> for BlockStates {
+ fn from(block: BlockKind) -> Self {
match block {
#from_registry_block_to_blockstates_match
- _ => unreachable!("There should always be a block state for every azalea_registry::Block variant")
+ _ => unreachable!("There should always be a block state for every BlockKind variant")
}
}
}
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<BlockState> for azalea_registry::Block {
+impl From<BlockState> for BlockKind {
fn from(value: BlockState) -> Self {
Box::<dyn BlockTrait>::from(value).as_registry_block()
}
@@ -156,22 +157,16 @@ mod tests {
assert_eq!(block.id(), "air");
let block: Box<dyn BlockTrait> =
- Box::<dyn BlockTrait>::from(BlockState::from(azalea_registry::Block::FloweringAzalea));
+ Box::<dyn BlockTrait>::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<BlockState> 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::<crate::properties::WaterLevel>()
.expect("water block should always have WaterLevel");
@@ -92,7 +94,7 @@ impl From<BlockState> for FluidState {
falling: false,
};
}
- azalea_registry::Block::Lava => {
+ BlockKind::Lava => {
let level = state
.property::<crate::properties::LavaLevel>()
.expect("lava block should always have LavaLevel");
@@ -102,7 +104,7 @@ impl From<BlockState> 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<HashSet<Block>> for BlockStates {
- fn from(set: HashSet<Block>) -> Self {
+impl From<HashSet<BlockKind>> for BlockStates {
+ fn from(set: HashSet<BlockKind>) -> Self {
Self::from(&set)
}
}
-impl From<&HashSet<Block>> for BlockStates {
- fn from(set: &HashSet<Block>) -> Self {
+impl From<&HashSet<BlockKind>> for BlockStates {
+ fn from(set: &HashSet<BlockKind>) -> 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<Block>> for BlockStates {
}
}
-impl<const N: usize> 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<const N: usize> From<[BlockKind; N]> for BlockStates {
+ fn from(arr: [BlockKind; N]) -> Self {
+ Self::from(&arr[..])
+ }
+}
+impl From<&RegistryTag<BlockKind>> for BlockStates {
+ fn from(tag: &RegistryTag<BlockKind>) -> Self {
+ Self::from(&**tag)
+ }
+}
+// allows users to do like `BlockStates::from(&tags::blocks::LOGS)` instead of
+// `BlockStates::from(&&tags::blocks::LOGS)`
+impl From<&LazyLock<RegistryTag<BlockKind>>> for BlockStates {
+ fn from(tag: &LazyLock<RegistryTag<BlockKind>>) -> Self {
+ Self::from(&**tag)
+ }
+}