1
2
Fork 0
mirror of https://github.com/mat-1/azalea.git synced 2025-08-02 23:44:38 +00:00

rename the Block trait to BlockTrait to disambiguate with azalea_registry::Block

This commit is contained in:
mat 2025-06-02 19:23:59 -03:30
commit 7517a207db
15 changed files with 40 additions and 41 deletions

View file

@ -23,20 +23,20 @@ let block_state: BlockState = azalea_block::blocks::CobblestoneWall {
let block_state: BlockState = azalea_registry::Block::Jukebox.into(); let block_state: BlockState = azalea_registry::Block::Jukebox.into();
``` ```
## Block trait ## BlockTrait
The [`Block`] trait represents a type of a block. With the the [`Block`] trait, 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 Block>`. 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 the `Block` trait, set default-features to false. If for some reason you don't want `BlockTrait`, set `default-features = false`.
``` ```
# use azalea_block::{Block, BlockState}; # use azalea_block::{BlockTrait, BlockState};
# let block_state = BlockState::from(azalea_registry::Block::Jukebox); # let block_state = BlockState::from(azalea_registry::Block::Jukebox);
let block = Box::<dyn Block>::from(block_state); let block = Box::<dyn BlockTrait>::from(block_state);
``` ```
``` ```
# use azalea_block::{Block, BlockState}; # use azalea_block::{BlockTrait, BlockState};
# let block_state: BlockState = azalea_registry::Block::Jukebox.into(); # let block_state: BlockState = azalea_registry::Block::Jukebox.into();
if let Some(jukebox) = Box::<dyn Block>::from(block_state).downcast_ref::<azalea_block::blocks::Jukebox>() { if let Some(jukebox) = Box::<dyn BlockTrait>::from(block_state).downcast_ref::<azalea_block::blocks::Jukebox>() {
// ... // ...
} }
``` ```

View file

@ -660,7 +660,7 @@ pub fn make_block_states(input: TokenStream) -> TokenStream {
#block_struct_fields #block_struct_fields
} }
impl Block for #block_struct_name { impl BlockTrait for #block_struct_name {
fn behavior(&self) -> BlockBehavior { fn behavior(&self) -> BlockBehavior {
#block_behavior #block_behavior
} }
@ -785,7 +785,7 @@ pub fn make_block_states(input: TokenStream) -> TokenStream {
#block_structs #block_structs
impl From<BlockState> for Box<dyn Block> { impl From<BlockState> for Box<dyn BlockTrait> {
fn from(block_state: BlockState) -> Self { fn from(block_state: BlockState) -> Self {
let b = block_state.id(); let b = block_state.id();
match b { match b {
@ -794,7 +794,7 @@ pub fn make_block_states(input: TokenStream) -> TokenStream {
} }
} }
} }
impl From<azalea_registry::Block> for Box<dyn Block> { impl From<azalea_registry::Block> for Box<dyn BlockTrait> {
fn from(block: azalea_registry::Block) -> Self { fn from(block: azalea_registry::Block) -> Self {
match block { match block {
#from_registry_block_to_block_match #from_registry_block_to_block_match

View file

@ -5,7 +5,7 @@ use std::{
use azalea_buf::{AzaleaRead, AzaleaReadVar, AzaleaWrite, AzaleaWriteVar, BufReadError}; use azalea_buf::{AzaleaRead, AzaleaReadVar, AzaleaWrite, AzaleaWriteVar, BufReadError};
use crate::Block; use crate::BlockTrait;
/// The type that's used internally to represent a block state ID. /// The type that's used internally to represent a block state ID.
/// ///
@ -121,14 +121,14 @@ impl Debug for BlockState {
f, f,
"BlockState(id: {}, {:?})", "BlockState(id: {}, {:?})",
self.id, self.id,
Box::<dyn Block>::from(*self) Box::<dyn BlockTrait>::from(*self)
) )
} }
} }
impl From<BlockState> for azalea_registry::Block { impl From<BlockState> for azalea_registry::Block {
fn from(value: BlockState) -> Self { fn from(value: BlockState) -> Self {
Box::<dyn Block>::from(value).as_registry_block() Box::<dyn BlockTrait>::from(value).as_registry_block()
} }
} }
@ -149,11 +149,11 @@ mod tests {
#[test] #[test]
fn test_from_blockstate() { fn test_from_blockstate() {
let block: Box<dyn Block> = Box::<dyn Block>::from(BlockState::AIR); let block: Box<dyn BlockTrait> = Box::<dyn BlockTrait>::from(BlockState::AIR);
assert_eq!(block.id(), "air"); assert_eq!(block.id(), "air");
let block: Box<dyn Block> = let block: Box<dyn BlockTrait> =
Box::<dyn Block>::from(BlockState::from(azalea_registry::Block::FloweringAzalea)); Box::<dyn BlockTrait>::from(BlockState::from(azalea_registry::Block::FloweringAzalea));
assert_eq!(block.id(), "flowering_azalea"); assert_eq!(block.id(), "flowering_azalea");
} }

View file

@ -2,7 +2,7 @@ use std::fmt::Debug;
use azalea_block_macros::make_block_states; use azalea_block_macros::make_block_states;
use crate::{Block, BlockBehavior, BlockState, BlockStates, Property}; use crate::{BlockTrait, BlockBehavior, BlockState, BlockStates, Property};
make_block_states! { make_block_states! {
Properties => { Properties => {

View file

@ -15,7 +15,7 @@ pub use block_state::BlockState;
pub use generated::{blocks, properties}; pub use generated::{blocks, properties};
pub use range::BlockStates; pub use range::BlockStates;
pub trait Block: Debug + Any { pub trait BlockTrait: Debug + Any {
fn behavior(&self) -> BlockBehavior; fn behavior(&self) -> BlockBehavior;
/// Get the Minecraft ID for this block. For example `stone` or /// Get the Minecraft ID for this block. For example `stone` or
/// `grass_block`. /// `grass_block`.
@ -27,8 +27,8 @@ pub trait Block: Debug + Any {
/// `azalea_registry::Block` doesn't contain any state data. /// `azalea_registry::Block` doesn't contain any state data.
fn as_registry_block(&self) -> azalea_registry::Block; fn as_registry_block(&self) -> azalea_registry::Block;
} }
impl dyn Block { impl dyn BlockTrait {
pub fn downcast_ref<T: Block>(&self) -> Option<&T> { pub fn downcast_ref<T: BlockTrait>(&self) -> Option<&T> {
(self as &dyn Any).downcast_ref::<T>() (self as &dyn Any).downcast_ref::<T>()
} }
} }

View file

@ -1,7 +1,6 @@
use std::{ use std::{
collections::{HashSet, hash_set}, collections::{HashSet, hash_set},
ops::{Add, RangeInclusive}, ops::{Add, RangeInclusive},
sync::LazyLock,
}; };
use crate::{BlockState, block_state::BlockStateIntegerRepr}; use crate::{BlockState, block_state::BlockStateIntegerRepr};

View file

@ -1,4 +1,4 @@
use azalea_block::{Block, BlockState, fluid_state::FluidState}; use azalea_block::{BlockTrait, BlockState, fluid_state::FluidState};
use azalea_core::{direction::Direction, game_type::GameMode, position::BlockPos, tick::GameTick}; use azalea_core::{direction::Direction, game_type::GameMode, position::BlockPos, tick::GameTick};
use azalea_entity::{FluidOnEyes, Physics, mining::get_mine_progress}; use azalea_entity::{FluidOnEyes, Physics, mining::get_mine_progress};
use azalea_inventory::ItemStack; use azalea_inventory::ItemStack;
@ -300,7 +300,7 @@ fn handle_mining_queued(
}); });
} }
let block = Box::<dyn Block>::from(target_block_state); let block = Box::<dyn BlockTrait>::from(target_block_state);
let held_item = inventory.held_item(); let held_item = inventory.held_item();
@ -469,7 +469,7 @@ pub fn handle_finish_mining_block_observer(
return; return;
}; };
let registry_block = Box::<dyn Block>::from(block_state).as_registry_block(); let registry_block = Box::<dyn BlockTrait>::from(block_state).as_registry_block();
if !can_use_game_master_blocks(abilities, permission_level) if !can_use_game_master_blocks(abilities, permission_level)
&& matches!( && matches!(
registry_block, registry_block,
@ -603,7 +603,7 @@ pub fn continue_mining_block(
commands.entity(entity).remove::<Mining>(); commands.entity(entity).remove::<Mining>();
continue; continue;
} }
let block = Box::<dyn Block>::from(target_block_state); let block = Box::<dyn BlockTrait>::from(target_block_state);
**mine_progress += get_mine_progress( **mine_progress += get_mine_progress(
block.as_ref(), block.as_ref(),
current_mining_item.kind(), current_mining_item.kind(),

View file

@ -1,4 +1,4 @@
use azalea_block::{Block, BlockBehavior}; use azalea_block::{BlockTrait, BlockBehavior};
use azalea_core::tier::get_item_tier; use azalea_core::tier::get_item_tier;
use azalea_registry as registry; use azalea_registry as registry;
@ -13,7 +13,7 @@ use crate::{FluidOnEyes, Physics, effects};
/// The player inventory is needed to check your armor and offhand for modifiers /// The player inventory is needed to check your armor and offhand for modifiers
/// to your mining speed. /// to your mining speed.
pub fn get_mine_progress( pub fn get_mine_progress(
block: &dyn Block, block: &dyn BlockTrait,
held_item: registry::Item, held_item: registry::Item,
player_inventory: &azalea_inventory::Menu, player_inventory: &azalea_inventory::Menu,
fluid_on_eyes: &FluidOnEyes, fluid_on_eyes: &FluidOnEyes,
@ -41,7 +41,7 @@ pub fn get_mine_progress(
/ divider as f32 / divider as f32
} }
fn has_correct_tool_for_drops(block: &dyn Block, tool: registry::Item) -> bool { fn has_correct_tool_for_drops(block: &dyn BlockTrait, tool: registry::Item) -> bool {
if !block.behavior().requires_correct_tool_for_drops { if !block.behavior().requires_correct_tool_for_drops {
return true; return true;
} }

View file

@ -135,7 +135,7 @@ pub fn update_on_climbable(
let block_pos = BlockPos::from(position); let block_pos = BlockPos::from(position);
let block_state_at_feet = instance.get_block_state(&block_pos).unwrap_or_default(); let block_state_at_feet = instance.get_block_state(&block_pos).unwrap_or_default();
let block_at_feet = Box::<dyn azalea_block::Block>::from(block_state_at_feet); let block_at_feet = Box::<dyn azalea_block::BlockTrait>::from(block_state_at_feet);
let registry_block_at_feet = block_at_feet.as_registry_block(); let registry_block_at_feet = block_at_feet.as_registry_block();
**on_climbable = azalea_registry::tags::blocks::CLIMBABLE.contains(&registry_block_at_feet) **on_climbable = azalea_registry::tags::blocks::CLIMBABLE.contains(&registry_block_at_feet)
@ -162,7 +162,7 @@ fn is_trapdoor_useable_as_ladder(
.get_block_state(&block_pos.down(1)) .get_block_state(&block_pos.down(1))
.unwrap_or_default(); .unwrap_or_default();
let registry_block_below = let registry_block_below =
Box::<dyn azalea_block::Block>::from(block_below).as_registry_block(); Box::<dyn azalea_block::BlockTrait>::from(block_below).as_registry_block();
if registry_block_below != azalea_registry::Block::Ladder { if registry_block_below != azalea_registry::Block::Ladder {
return false; return false;
} }

View file

@ -379,7 +379,7 @@ pub fn legacy_blocks_motion(block: BlockState) -> bool {
pub fn legacy_calculate_solid(block: BlockState) -> bool { pub fn legacy_calculate_solid(block: BlockState) -> bool {
// force_solid has to be checked before anything else // force_solid has to be checked before anything else
let block_trait = Box::<dyn azalea_block::Block>::from(block); let block_trait = Box::<dyn azalea_block::BlockTrait>::from(block);
if let Some(solid) = block_trait.behavior().force_solid { if let Some(solid) = block_trait.behavior().force_solid {
return solid; return solid;
} }

View file

@ -8,7 +8,7 @@ pub mod travel;
use std::collections::HashSet; use std::collections::HashSet;
use azalea_block::{Block, BlockState, fluid_state::FluidState, properties}; use azalea_block::{BlockTrait, BlockState, fluid_state::FluidState, properties};
use azalea_core::{ use azalea_core::{
math, math,
position::{BlockPos, Vec3}, position::{BlockPos, Vec3},
@ -492,7 +492,7 @@ fn block_jump_factor(world: &Instance, position: &Position) -> f32 {
.get_block_state(&get_block_pos_below_that_affects_movement(position)); .get_block_state(&get_block_pos_below_that_affects_movement(position));
let block_at_pos_jump_factor = if let Some(block) = block_at_pos { let block_at_pos_jump_factor = if let Some(block) = block_at_pos {
Box::<dyn Block>::from(block).behavior().jump_factor Box::<dyn BlockTrait>::from(block).behavior().jump_factor
} else { } else {
1. 1.
}; };
@ -501,7 +501,7 @@ fn block_jump_factor(world: &Instance, position: &Position) -> f32 {
} }
if let Some(block) = block_below { if let Some(block) = block_below {
Box::<dyn Block>::from(block).behavior().jump_factor Box::<dyn BlockTrait>::from(block).behavior().jump_factor
} else { } else {
1. 1.
} }

View file

@ -1,4 +1,4 @@
use azalea_block::{Block, BlockState, fluid_state::FluidState}; use azalea_block::{BlockTrait, BlockState, fluid_state::FluidState};
use azalea_core::{ use azalea_core::{
aabb::AABB, aabb::AABB,
position::{BlockPos, Vec3}, position::{BlockPos, Vec3},
@ -124,7 +124,7 @@ fn travel_in_air(
.chunks .chunks
.get_block_state(&block_pos_below) .get_block_state(&block_pos_below)
.unwrap_or(BlockState::AIR); .unwrap_or(BlockState::AIR);
let block_below: Box<dyn Block> = block_state_below.into(); let block_below: Box<dyn BlockTrait> = block_state_below.into();
let block_friction = block_below.behavior().friction; let block_friction = block_below.behavior().friction;
let inertia = if physics.on_ground() { let inertia = if physics.on_ground() {

View file

@ -278,10 +278,10 @@ enum Attribute {
registry! { registry! {
/// An enum of every type of block in the game. To represent a block *state*, /// An enum of every type of block in the game. To represent a block *state*,
/// use [`azalea_block::BlockState`] or the [`azalea_block::Block`] trait. /// use [`azalea_block::BlockState`] or [`azalea_block::BlockTrait`].
/// ///
/// [`azalea_block::BlockState`]: https://docs.rs/azalea-block/latest/azalea_block/struct.BlockState.html /// [`azalea_block::BlockState`]: https://docs.rs/azalea-block/latest/azalea_block/struct.BlockState.html
/// [`azalea_block::Block`]: https://docs.rs/azalea-block/latest/azalea_block/trait.Block.html /// [`azalea_block::BlockTrait`]: https://docs.rs/azalea-block/latest/azalea_block/trait.BlockTrait.html
enum Block { enum Block {
Air => "minecraft:air", Air => "minecraft:air",
Stone => "minecraft:stone", Stone => "minecraft:stone",

View file

@ -44,7 +44,7 @@ fn motion_blocking(block_state: BlockState) -> bool {
impl HeightmapKind { impl HeightmapKind {
pub fn is_opaque(self, block_state: BlockState) -> bool { pub fn is_opaque(self, block_state: BlockState) -> bool {
let block = Box::<dyn azalea_block::Block>::from(block_state); let block = Box::<dyn azalea_block::BlockTrait>::from(block_state);
let registry_block = block.as_registry_block(); let registry_block = block.as_registry_block();
match self { match self {
HeightmapKind::WorldSurfaceWg => !block_state.is_air(), HeightmapKind::WorldSurfaceWg => !block_state.is_air(),

View file

@ -1,4 +1,4 @@
use azalea_block::{Block, BlockState, fluid_state::FluidKind}; use azalea_block::{BlockState, BlockTrait, fluid_state::FluidKind};
use azalea_client::{Client, inventory::Inventory}; use azalea_client::{Client, inventory::Inventory};
use azalea_entity::{FluidOnEyes, Physics}; use azalea_entity::{FluidOnEyes, Physics};
use azalea_inventory::{ItemStack, Menu, components}; use azalea_inventory::{ItemStack, Menu, components};
@ -52,7 +52,7 @@ pub fn accurate_best_tool_in_hotbar_for_block(
let mut best_speed = 0.; let mut best_speed = 0.;
let mut best_slot = None; let mut best_slot = None;
let block = Box::<dyn Block>::from(block); let block = Box::<dyn BlockTrait>::from(block);
let registry_block = block.as_registry_block(); let registry_block = block.as_registry_block();
if matches!( if matches!(