From ed15c00307d7bea5d3418e7fbc3d09aa5ff5a153 Mon Sep 17 00:00:00 2001 From: Alexander Medvedev Date: Wed, 1 Apr 2026 23:21:52 +0200 Subject: [PATCH] chore: surface rules: parse some block/states directly --- pumpkin-codegen/src/block.rs | 2 +- pumpkin-codegen/src/configured_feature.rs | 28 ++++++++++++------- pumpkin-codegen/src/placed_feature.rs | 18 ++++++------ .../configured_features_generated.rs | 15 +++++----- .../generated/placed_features_generated.rs | 2 +- .../src/generation/rule/block_match.rs | 7 +++-- .../src/generation/rule/block_state_match.rs | 8 ++++-- .../src/generation/rule/random_block_match.rs | 7 ++--- .../rule/random_block_state_match.rs | 7 +++-- 9 files changed, 54 insertions(+), 40 deletions(-) diff --git a/pumpkin-codegen/src/block.rs b/pumpkin-codegen/src/block.rs index 87d0332ba..2b53fa87b 100644 --- a/pumpkin-codegen/src/block.rs +++ b/pumpkin-codegen/src/block.rs @@ -373,7 +373,7 @@ impl ToTokens for BlockPropertyStruct { for (key, value) in props { match *key { #(#from_props_values),*, - _ => {}, // + _ => {}, // } } block_props diff --git a/pumpkin-codegen/src/configured_feature.rs b/pumpkin-codegen/src/configured_feature.rs index 7d46f198b..121a3c115 100644 --- a/pumpkin-codegen/src/configured_feature.rs +++ b/pumpkin-codegen/src/configured_feature.rs @@ -859,7 +859,10 @@ fn value_to_rule_test(v: &Value) -> TokenStream { "minecraft:always_true" | "" => quote! { RuleTest::AlwaysTrue }, "minecraft:block_match" => { let block = v["block"].as_str().unwrap_or("minecraft:stone"); - quote! { RuleTest::BlockMatch(BlockMatchRuleTest { block: #block.to_string() }) } + let name_stripped = block.strip_prefix("minecraft:").unwrap_or(block); + let block_ident = + quote::format_ident!("{}", name_stripped.to_uppercase().replace([':', '-'], "_")); + quote! { RuleTest::BlockMatch(BlockMatchRuleTest { block: pumpkin_data::Block::#block_ident }) } } "minecraft:blockstate_match" => { let state = value_to_block_state(&v["block_state"]); @@ -872,7 +875,10 @@ fn value_to_rule_test(v: &Value) -> TokenStream { "minecraft:random_block_match" => { let block = v["block"].as_str().unwrap_or("minecraft:stone"); let prob = v["probability"].as_f64().unwrap_or(0.5) as f32; - quote! { RuleTest::RandomBlockMatch(RandomBlockMatchRuleTest { block: #block.to_string(), probability: #prob }) } + let name_stripped = block.strip_prefix("minecraft:").unwrap_or(block); + let block_ident = + quote::format_ident!("{}", block.to_uppercase().replace([':', '-'], "_")); + quote! { RuleTest::RandomBlockMatch(RandomBlockMatchRuleTest { block: pumpkin_data::Block::#block_ident, probability: #prob }) } } "minecraft:random_blockstate_match" => { let state = value_to_block_state(&v["block_state"]); @@ -1271,17 +1277,19 @@ fn value_to_placement_modifier_cf(v: &Value) -> TokenStream { let positions = v["positions"] .as_array() .map(|arr| { - arr.iter().map(|p| { - let coords = p.as_array().unwrap(); - let x = coords[0].as_i64().unwrap_or(0) as i32; - let y = coords[1].as_i64().unwrap_or(0) as i32; - let z = coords[2].as_i64().unwrap_or(0) as i32; - quote! { BlockPos::new(#x, #y, #z) } - }).collect::>() + arr.iter() + .map(|p| { + let coords = p.as_array().unwrap(); + let x = coords[0].as_i64().unwrap_or(0) as i32; + let y = coords[1].as_i64().unwrap_or(0) as i32; + let z = coords[2].as_i64().unwrap_or(0) as i32; + quote! { BlockPos::new(#x, #y, #z) } + }) + .collect::>() }) .unwrap_or_default(); quote! { PlacementModifier::FixedPlacement(vec![#(#positions),*]) } - }, + } "minecraft:heightmap" => { let hm = crate::placed_feature::value_to_height_map( v["heightmap"].as_str().unwrap_or("MOTION_BLOCKING"), diff --git a/pumpkin-codegen/src/placed_feature.rs b/pumpkin-codegen/src/placed_feature.rs index 41c78208e..695898ea9 100644 --- a/pumpkin-codegen/src/placed_feature.rs +++ b/pumpkin-codegen/src/placed_feature.rs @@ -112,17 +112,19 @@ fn value_to_placement_modifier(v: &Value) -> TokenStream { let positions = v["positions"] .as_array() .map(|arr| { - arr.iter().map(|p| { - let coords = p.as_array().unwrap(); - let x = coords[0].as_i64().unwrap_or(0) as i32; - let y = coords[1].as_i64().unwrap_or(0) as i32; - let z = coords[2].as_i64().unwrap_or(0) as i32; - quote! { BlockPos::new(#x, #y, #z) } - }).collect::>() + arr.iter() + .map(|p| { + let coords = p.as_array().unwrap(); + let x = coords[0].as_i64().unwrap_or(0) as i32; + let y = coords[1].as_i64().unwrap_or(0) as i32; + let z = coords[2].as_i64().unwrap_or(0) as i32; + quote! { BlockPos::new(#x, #y, #z) } + }) + .collect::>() }) .unwrap_or_default(); quote! { PlacementModifier::FixedPlacement(vec![#(#positions),*]) } - }, + } "minecraft:heightmap" => { let heightmap = value_to_height_map(v["heightmap"].as_str().unwrap_or("MOTION_BLOCKING")); diff --git a/pumpkin-data/src/generated/configured_features_generated.rs b/pumpkin-data/src/generated/configured_features_generated.rs index 436554732..895dba5d6 100644 --- a/pumpkin-data/src/generated/configured_features_generated.rs +++ b/pumpkin-data/src/generated/configured_features_generated.rs @@ -2614,7 +2614,8 @@ fn build_configured_features() -> std::collections::HashMap std::collections::HashMap std::collections::HashMap std::collections::HashMap std::collections::HashMap std::collections::HashMap std::collections::HashMap std::collections::HashMap { PlacedFeature { feature: Feature::Named("end_platform".to_string()), placement: vec![ - PlacementModifier::FixedPlacement(vec![BlockPos::new(100, 50, 0)]), + PlacementModifier::FixedPlacement(vec![BlockPos::new(100i32, 49i32, 0i32)]), PlacementModifier::Biome(BiomePlacementModifier), ], }, diff --git a/pumpkin-world/src/generation/rule/block_match.rs b/pumpkin-world/src/generation/rule/block_match.rs index f2639040d..c256f32fa 100644 --- a/pumpkin-world/src/generation/rule/block_match.rs +++ b/pumpkin-world/src/generation/rule/block_match.rs @@ -1,13 +1,14 @@ +use pumpkin_data::Block; + use crate::block::RawBlockState; pub struct BlockMatchRuleTest { - // This should be a Block codec, so this is wrong - pub block: String, + pub block: Block, } impl BlockMatchRuleTest { #[must_use] pub fn test(&self, state: RawBlockState) -> bool { - state.to_block().name == self.block.strip_prefix("minecraft:").unwrap_or(&self.block) + state.to_block().name == self.block.name } } diff --git a/pumpkin-world/src/generation/rule/block_state_match.rs b/pumpkin-world/src/generation/rule/block_state_match.rs index 73098b4e0..5c0fc111e 100644 --- a/pumpkin-world/src/generation/rule/block_state_match.rs +++ b/pumpkin-world/src/generation/rule/block_state_match.rs @@ -1,12 +1,14 @@ -use crate::block::{BlockStateCodec, RawBlockState}; +use pumpkin_data::BlockState; + +use crate::block::RawBlockState; pub struct BlockStateMatchRuleTest { - pub block_state: BlockStateCodec, + pub block_state: BlockState, } impl BlockStateMatchRuleTest { #[must_use] pub fn test(&self, state: RawBlockState) -> bool { - state.0 == self.block_state.get_state_id() + state.0 == self.block_state.id } } diff --git a/pumpkin-world/src/generation/rule/random_block_match.rs b/pumpkin-world/src/generation/rule/random_block_match.rs index a3c1dd5dc..487e0d48f 100644 --- a/pumpkin-world/src/generation/rule/random_block_match.rs +++ b/pumpkin-world/src/generation/rule/random_block_match.rs @@ -1,16 +1,15 @@ +use pumpkin_data::Block; use pumpkin_util::random::{RandomGenerator, RandomImpl}; use crate::block::RawBlockState; pub struct RandomBlockMatchRuleTest { - // This should be a Block codec, so this is wrong - pub block: String, + pub block: Block, pub probability: f32, } impl RandomBlockMatchRuleTest { pub fn test(&self, state: RawBlockState, random: &mut RandomGenerator) -> bool { - state.to_block().name == self.block.strip_prefix("minecraft:").unwrap_or(&self.block) - && random.next_f32() < self.probability + state.to_block().name == self.block.name && random.next_f32() < self.probability } } diff --git a/pumpkin-world/src/generation/rule/random_block_state_match.rs b/pumpkin-world/src/generation/rule/random_block_state_match.rs index 2cc188860..8a6dfe64c 100644 --- a/pumpkin-world/src/generation/rule/random_block_state_match.rs +++ b/pumpkin-world/src/generation/rule/random_block_state_match.rs @@ -1,14 +1,15 @@ +use pumpkin_data::BlockState; use pumpkin_util::random::{RandomGenerator, RandomImpl}; -use crate::block::{BlockStateCodec, RawBlockState}; +use crate::block::RawBlockState; pub struct RandomBlockStateMatchRuleTest { - pub block_state: BlockStateCodec, + pub block_state: BlockState, pub probability: f32, } impl RandomBlockStateMatchRuleTest { pub fn test(&self, state: RawBlockState, random: &mut RandomGenerator) -> bool { - state.0 == self.block_state.get_state_id() && random.next_f32() < self.probability + state.0 == self.block_state.id && random.next_f32() < self.probability } }