1
2
Fork 0
mirror of https://github.com/mat-1/azalea.git synced 2025-08-02 14:26:04 +00:00

add trait feature to az-block

This commit is contained in:
mat 2022-08-24 22:42:54 -05:00
parent ae64d316e3
commit 881d3cd228
5 changed files with 55 additions and 41 deletions

View file

@ -9,3 +9,7 @@ version = "0.1.0"
[dependencies]
block-macros = {path = "./block-macros"}
[features]
default = ["trait"]
trait = []

View file

@ -6,3 +6,5 @@ There's two main things here, the `BlockState` enum and the `Block` trait.
`BlockState` is a simple enum with every possible block state as variant, and `Block` is a heavier trait which lets you access information about a block more easily.
Every block is a struct that implements `Block`. You can freely convert between `BlockState` and `Block` with .into().
If you don't want the `Block` trait, set default-features to false.

View file

@ -426,6 +426,7 @@ pub fn make_block_states(input: TokenStream) -> TokenStream {
quote! { BlockState::#block_name_pascal_case }
};
if cfg!(feature = "trait") {
let block_struct = quote! {
#[derive(Debug)]
pub struct #block_struct_name {
@ -458,9 +459,10 @@ pub fn make_block_states(input: TokenStream) -> TokenStream {
block_structs.extend(block_struct);
}
}
let last_state_id = (state_id - 1) as u32;
quote! {
let mut generated = quote! {
#property_enums
#[repr(u32)]
@ -469,6 +471,17 @@ pub fn make_block_states(input: TokenStream) -> TokenStream {
#block_state_enum_variants
}
impl BlockState {
/// Returns the highest possible state
#[inline]
pub fn max_state() -> u32 {
#last_state_id
}
}
};
if cfg!(feature = "trait") {
generated.extend(quote! {
#block_structs
impl From<BlockState> for Box<dyn Block> {
@ -480,15 +493,8 @@ pub fn make_block_states(input: TokenStream) -> TokenStream {
}
}
}
impl BlockState {
/// Returns the highest possible state
#[inline]
pub fn max_state() -> u32 {
#last_state_id
}
});
}
}
.into()
generated.into()
}

View file

@ -1,6 +1,8 @@
#[cfg(feature = "trait")]
mod behavior;
mod blocks;
#[cfg(feature = "trait")]
pub use behavior::BlockBehavior;
pub use blocks::*;

View file

@ -6,7 +6,7 @@ version = "0.1.0"
# See more keys and their definitions at https://doc.rust-lang.org/cargo/reference/manifest.html
[dependencies]
azalea-block = {path = "../azalea-block"}
azalea-block = {path = "../azalea-block", default-features = false}
azalea-buf = {path = "../azalea-buf"}
azalea-core = {path = "../azalea-core"}
azalea-entity = {path = "../azalea-entity"}