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

fixed macro for blocks with no properties

This commit is contained in:
mat 2022-05-28 21:38:30 -05:00
parent 9c1c286236
commit e832c84eb8
4 changed files with 53 additions and 18 deletions

View file

@ -230,21 +230,32 @@ pub fn make_block_states(input: TokenStream) -> TokenStream {
pub #property_name_snake: #property, pub #property_name_snake: #property,
}) })
} }
let block_name_pascal_case = Ident::new(
&to_pascal_case(&block.name.to_string()),
proc_macro2::Span::call_site(),
);
let block_struct_name = Ident::new( let block_struct_name = Ident::new(
&format!("{}Block", to_pascal_case(&block.name.to_string())), &format!("{}Block", block_name_pascal_case),
proc_macro2::Span::call_site(), proc_macro2::Span::call_site(),
); );
let mut from_block_to_state_match = quote! {}; let mut from_block_to_state_match_inner = quote! {};
let first_state_id = state_id; let first_state_id = state_id;
// if there's no properties, then the block is just a single state
if block_properties_vec.len() == 0 {
block_state_enum_variants.extend(quote! {
#block_name_pascal_case,
});
state_id += 1;
}
for combination in combinations_of(&block_properties_vec) { for combination in combinations_of(&block_properties_vec) {
state_id += 1; state_id += 1;
let variant_name = Ident::new( let variant_name = Ident::new(
&format!( &format!(
"{}_{}", "{}_{}",
to_pascal_case(&block.name.to_string()), block_name_pascal_case,
combination combination
.iter() .iter()
.map(|v| v.to_string()) .map(|v| v.to_string())
@ -260,7 +271,7 @@ pub fn make_block_states(input: TokenStream) -> TokenStream {
// face: properties::Face::Floor, // face: properties::Face::Floor,
// facing: properties::Facing::North, // facing: properties::Facing::North,
// powered: properties::Powered::True, // powered: properties::Powered::True,
let mut from_block_to_state_match_inner = quote! {}; let mut from_block_to_state_combination_match_inner = quote! {};
for i in 0..block_property_names.len() { for i in 0..block_property_names.len() {
let property_name = &block_property_names[i]; let property_name = &block_property_names[i];
let property_name_ident = Ident::new(property_name, proc_macro2::Span::call_site()); let property_name_ident = Ident::new(property_name, proc_macro2::Span::call_site());
@ -269,14 +280,14 @@ pub fn make_block_states(input: TokenStream) -> TokenStream {
let variant = let variant =
Ident::new(&combination[i].to_string(), proc_macro2::Span::call_site()); Ident::new(&combination[i].to_string(), proc_macro2::Span::call_site());
from_block_to_state_match_inner.extend(quote! { from_block_to_state_combination_match_inner.extend(quote! {
#property_name_ident: #property_name_snake::#variant, #property_name_ident: #property_name_snake::#variant,
}); });
} }
from_block_to_state_match.extend(quote! { from_block_to_state_match_inner.extend(quote! {
#block_struct_name { #block_struct_name {
#from_block_to_state_match_inner #from_block_to_state_combination_match_inner
} => BlockState::#variant_name, } => BlockState::#variant_name,
}); });
} }
@ -329,6 +340,17 @@ pub fn make_block_states(input: TokenStream) -> TokenStream {
let block_behavior = &block.behavior; let block_behavior = &block.behavior;
let block_id = block.name.to_string(); let block_id = block.name.to_string();
let from_block_to_state_match = if block.properties_and_defaults.len() > 0 {
quote! {
match b {
#from_block_to_state_match_inner
}
}
} else {
quote! { BlockState::#block_name_pascal_case }
};
let block_struct = quote! { let block_struct = quote! {
#[derive(Debug)] #[derive(Debug)]
pub struct #block_struct_name { pub struct #block_struct_name {
@ -346,9 +368,7 @@ pub fn make_block_states(input: TokenStream) -> TokenStream {
impl From<#block_struct_name> for BlockState { impl From<#block_struct_name> for BlockState {
fn from(b: #block_struct_name) -> Self { fn from(b: #block_struct_name) -> Self {
match b { #from_block_to_state_match
#from_block_to_state_match
}
} }
} }

View file

@ -1,5 +1,8 @@
pub fn combinations_of<T: Clone>(items: &[Vec<T>]) -> Vec<Vec<T>> { pub fn combinations_of<T: Clone>(items: &[Vec<T>]) -> Vec<Vec<T>> {
let mut combinations = Vec::new(); let mut combinations = Vec::new();
if items.len() == 0 {
return combinations;
};
if items.len() == 1 { if items.len() == 1 {
for item in &items[0] { for item in &items[0] {
combinations.push(vec![item.clone()]); combinations.push(vec![item.clone()]);

View file

@ -43,11 +43,6 @@ make_block_states! {
Powered=True Powered=True
}, },
acacia_door => BlockBehavior::default(), { acacia_door => BlockBehavior::default(), {
Facing=North,
Half=Upper,
Hinge=Left,
Open=True,
Powered=True
}, },
} }
} }
@ -168,3 +163,4 @@ make_block_states! {
// assert_eq!(block.id(), "acacia_button"); // assert_eq!(block.id(), "acacia_button");
// } // }
// } // }
// }

View file

@ -22,7 +22,7 @@ def generate_blocks(blocks: dict):
new_make_block_states_macro_code.append(' Properties => {') new_make_block_states_macro_code.append(' Properties => {')
for property_name, property_variants in properties.items(): for property_name, property_variants in properties.items():
new_make_block_states_macro_code.append( new_make_block_states_macro_code.append(
f' {to_camel_case(property_name)} => {{') f' {to_camel_case(property_name)} {{')
for variant in property_variants: for variant in property_variants:
new_make_block_states_macro_code.append( new_make_block_states_macro_code.append(
@ -51,6 +51,22 @@ def generate_blocks(blocks: dict):
new_make_block_states_macro_code.append( new_make_block_states_macro_code.append(
f' {to_camel_case(property)}={to_camel_case(property_default)},') f' {to_camel_case(property)}={to_camel_case(property_default)},')
new_make_block_states_macro_code.append(' },') new_make_block_states_macro_code.append(' },')
new_make_block_states_macro_code.append(' },') new_make_block_states_macro_code.append(' }')
new_make_block_states_macro_code.append('}')
print('\n'.join(new_make_block_states_macro_code)) new_code = []
in_macro = False
for line in existing_code:
if line == 'make_block_states! {':
in_macro = True
elif line == '}':
if in_macro:
in_macro = False
new_code.extend(new_make_block_states_macro_code)
continue
if in_macro:
continue
new_code.append(line)
with open(BLOCKS_RS_DIR, 'w') as f:
f.write('\n'.join(new_code))