Skip to content

Commit

Permalink
Make block state macro comptime-again
Browse files Browse the repository at this point in the history
  • Loading branch information
lukas0008 committed Oct 19, 2024
1 parent c3dcec2 commit 7300bda
Show file tree
Hide file tree
Showing 2 changed files with 53 additions and 16 deletions.
63 changes: 50 additions & 13 deletions pumpkin-macros/src/block_state.rs
Original file line number Diff line number Diff line change
Expand Up @@ -238,24 +238,61 @@ pub fn block_state_impl(item: TokenStream) -> TokenStream {
};

let id = state.id;
let category_name = block_info.definition.category.clone();

if std::env::var("CARGO_PKG_NAME").unwrap() == "pumpkin-world" {
let category_name: proc_macro2::TokenStream = format!(
"crate::block::BlockCategory::{}",
&pascal_case(
&block_info
.definition
.category
.split_once(':')
.expect("Bad minecraft id")
.1,
)
)
.parse()
.unwrap();
let block_name: proc_macro2::TokenStream = format!(
"crate::block::Block::{}",
&pascal_case(&block_name.split_once(':').expect("Bad minecraft id").1,)
)
.parse()
.unwrap();
quote! {
crate::block::block_state::BlockState::new_unchecked(
#id as u16,
crate::block::Block::from_registry_id(#block_name),
crate::block::BlockCategory::from_registry_id(#category_name),
)
crate::block::BlockState {
state_id: #id as u16,
category: #category_name,
block: #block_name,
}
}
.into()
} else {
let category_name: proc_macro2::TokenStream = format!(
"pumpkin_world::block::BlockCategory::{}",
&pascal_case(
&block_info
.definition
.category
.split_once(':')
.expect("Bad minecraft id")
.1,
)
)
.parse()
.unwrap();
let block_name: proc_macro2::TokenStream = format!(
"pumpkin_world::block::Block::{}",
&pascal_case(&block_name.split_once(':').expect("Bad minecraft id").1,)
)
.parse()
.unwrap();
quote! {
pumpkin_world::block::block_id::BlockStateId::new_unchecked(
#id as u16,
pumpkin_world::block::Block::from_registry_id(#block_name),
pumpkin_world::block::BlockCategory::from_registry_id(#category_name),
)
pumpkin_world::block::BlockState {
state_id: #id as u16,
category: #category_name,
block: #block_name,
}
}
.into()
}
.into()
}
6 changes: 3 additions & 3 deletions pumpkin-world/src/block/block_state.rs
Original file line number Diff line number Diff line change
Expand Up @@ -6,9 +6,9 @@ use super::block_registry::{Block, BlockCategory, BLOCKS};

#[derive(Clone)]
pub struct BlockState {
state_id: u16,
block: Block,
category: BlockCategory,
pub state_id: u16,
pub block: Block,
pub category: BlockCategory,
}

impl BlockState {
Expand Down

0 comments on commit 7300bda

Please sign in to comment.