Skip to content

Commit

Permalink
Added compile time BlockState (#347)
Browse files Browse the repository at this point in the history
* added block_state! macro

* Update block_state! macro
  • Loading branch information
Alvsch authored Dec 7, 2024
1 parent b82a4e1 commit a04b23f
Show file tree
Hide file tree
Showing 11 changed files with 465 additions and 387 deletions.
93 changes: 93 additions & 0 deletions pumpkin-macros/src/block_state.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,93 @@
use std::{collections::HashMap, sync::LazyLock};

use quote::quote;
use serde::Deserialize;

static BLOCKS: LazyLock<TopLevel> = LazyLock::new(|| {
serde_json::from_str(include_str!("../../assets/blocks.json"))
.expect("Could not parse blocks.json registry.")
});

static STATE_BY_REGISTRY_ID: LazyLock<HashMap<String, Block>> = LazyLock::new(|| {
let mut map = HashMap::new();
for block in &BLOCKS.blocks {
map.insert(block.name.clone(), block.clone());
}
map
});

pub(crate) fn block_state_impl(item: proc_macro::TokenStream) -> proc_macro::TokenStream {
let input_string = item.to_string();
let registry_id = input_string.trim_matches('"');

let state = STATE_BY_REGISTRY_ID
.get(registry_id)
.expect("Invalid registry id");

let default_state_id = state.default_state_id;
let block_id = state.id;

if std::env::var("CARGO_PKG_NAME").unwrap() == "pumpkin-world" {
quote! {
crate::block::BlockState {
state_id: #default_state_id,
block_id: #block_id,
}
}
.into()
} else {
quote! {
pumpkin_world::block::BlockState {
state_id: #default_state_id,
block_id: #block_id,
}
}
.into()
}
}

#[expect(dead_code)]
#[derive(Deserialize, Clone, Debug)]
struct TopLevel {
block_entity_types: Vec<String>,
shapes: Vec<Shape>,
pub blocks: Vec<Block>,
}

#[expect(dead_code)]
#[derive(Deserialize, Clone, Debug)]
struct Block {
pub id: u16,
pub item_id: u16,
pub hardness: f32,
pub wall_variant_id: Option<u16>,
pub translation_key: String,
pub name: String,
pub properties: Vec<Property>,
pub default_state_id: u16,
pub states: Vec<State>,
}
#[expect(dead_code)]
#[derive(Deserialize, Clone, Debug)]
pub struct Property {
name: String,
values: Vec<String>,
}
#[expect(dead_code)]
#[derive(Deserialize, Clone, Debug)]
pub struct State {
pub id: u16,
pub air: bool,
pub luminance: u8,
pub burnable: bool,
pub opacity: Option<u32>,
pub replaceable: bool,
pub collision_shapes: Vec<u16>,
pub block_entity_type: Option<u32>,
}
#[expect(dead_code)]
#[derive(Deserialize, Clone, Debug)]
struct Shape {
min: [f32; 3],
max: [f32; 3],
}
6 changes: 6 additions & 0 deletions pumpkin-macros/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -84,3 +84,9 @@ mod particle;
pub fn particle(item: TokenStream) -> TokenStream {
particle::particle_impl(item)
}

mod block_state;
#[proc_macro]
pub fn block_state(item: TokenStream) -> TokenStream {
block_state::block_state_impl(item)
}
4 changes: 2 additions & 2 deletions pumpkin-world/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -46,8 +46,8 @@ pub fn bench_create_chunk_noise_overworld() {
let config = NoiseConfig::new(0, &OVERWORLD_NOISE_ROUTER);
let generation_shape = GenerationShape::SURFACE;
let sampler = FluidLevelSampler::Chunk(StandardChunkFluidLevelSampler::new(
FluidLevel::new(63, *WATER_BLOCK),
FluidLevel::new(-54, *LAVA_BLOCK),
FluidLevel::new(63, WATER_BLOCK),
FluidLevel::new(-54, LAVA_BLOCK),
));

ChunkNoiseGenerator::new(
Expand Down
Loading

0 comments on commit a04b23f

Please sign in to comment.