From 5a21a687d81014a61c0ff82c9ab08ced296e30b7 Mon Sep 17 00:00:00 2001 From: DaniD3v Date: Sat, 24 Aug 2024 23:34:47 +0200 Subject: [PATCH] Block structs refractor + added comments renamed a few fields for clarity. added examples for fields that might not immediately be obvious. removed some `Option`s that are not necessary. renamed `BlockElement` to `BlockType` because you could mistake `BlockElement` for the main way to represent a Block (e.g. in Chunks). --- pumpkin-world/src/block/block_registry.rs | 37 +++++++++++++++++------ 1 file changed, 28 insertions(+), 9 deletions(-) diff --git a/pumpkin-world/src/block/block_registry.rs b/pumpkin-world/src/block/block_registry.rs index 1a36df8d5..0d79d6d03 100644 --- a/pumpkin-world/src/block/block_registry.rs +++ b/pumpkin-world/src/block/block_registry.rs @@ -9,27 +9,46 @@ const BLOCKS_JSON: &str = include_str!("../../assets/blocks.json"); #[derive(Deserialize, Debug, Clone, PartialEq, Eq)] pub struct BlockDefinition { + /// e.g. minecraft:door or minecraft:button #[serde(rename = "type")] - kind: String, - block_set_type: Option, + category: String, + + /// Specifies the variant of the blocks category. + /// e.g. minecraft:iron_door has the variant iron + #[serde(rename = "block_set_type")] + variant: Option, } +/// One possible state of a Block. +/// This could e.g. be an extended piston facing left. #[derive(Deserialize, Debug, Clone, PartialEq, Eq)] pub struct BlockState { - default: Option, id: i64, - properties: Option>, + + /// Whether this is the default state of the Block + #[serde(default, rename = "default")] + is_default: bool, + + /// The propertise active for this `BlockState`. + #[serde(default)] + properties: HashMap, } +/// A fully-fledged block definition. +/// Stores the category, variant, all of the possible states and all of the possible properties. #[derive(Deserialize, Debug, Clone, PartialEq, Eq)] -pub struct BlocksElement { +pub struct BlockType { definition: BlockDefinition, - properties: Option>>, states: Vec, + + // TODO is this safe to remove? It's currently not used in the Project. @lukas0008 @Snowiiii + /// A list of valid property keys/values for a block. + #[serde(default, rename = "properties")] + valid_properties: HashMap>, } lazy_static! { - pub static ref BLOCKS: HashMap = + pub static ref BLOCKS: HashMap = serde_json::from_str(BLOCKS_JSON).expect("Could not parse block.json registry."); } @@ -45,13 +64,13 @@ pub fn block_id_and_properties_to_block_state_id( None => Ok(block .states .iter() - .find(|state| state.default.unwrap_or(false)) + .find(|state| state.is_default) .expect("Each block should have at least one default state") .id), Some(properties) => block .states .iter() - .find(|state| state.properties.as_ref() == Some(properties)) + .find(|state| &state.properties == properties) .map(|state| state.id) .ok_or(WorldError::BlockStateIdNotFound), };