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).
This commit is contained in:
1 parent
03fcdc2e32
commit
5a21a687d8
1 file changed
+28
-9
@@ -9,27 +9,46 @@ const BLOCKS_JSON: &str = include_str!("../../assets/blocks.json");
|
|||||||
|
|
||||||
#[derive(Deserialize, Debug, Clone, PartialEq, Eq)]
|
#[derive(Deserialize, Debug, Clone, PartialEq, Eq)]
|
||||||
pub struct BlockDefinition {
|
pub struct BlockDefinition {
|
||||||
|
/// e.g. minecraft:door or minecraft:button
|
||||||
#[serde(rename = "type")]
|
#[serde(rename = "type")]
|
||||||
kind: String,
|
category: String,
|
||||||
block_set_type: Option<String>,
|
|
||||||
|
/// Specifies the variant of the blocks category.
|
||||||
|
/// e.g. minecraft:iron_door has the variant iron
|
||||||
|
#[serde(rename = "block_set_type")]
|
||||||
|
variant: Option<String>,
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/// One possible state of a Block.
|
||||||
|
/// This could e.g. be an extended piston facing left.
|
||||||
#[derive(Deserialize, Debug, Clone, PartialEq, Eq)]
|
#[derive(Deserialize, Debug, Clone, PartialEq, Eq)]
|
||||||
pub struct BlockState {
|
pub struct BlockState {
|
||||||
default: Option<bool>,
|
|
||||||
id: i64,
|
id: i64,
|
||||||
properties: Option<HashMap<String, String>>,
|
|
||||||
|
/// 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<String, String>,
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/// 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)]
|
#[derive(Deserialize, Debug, Clone, PartialEq, Eq)]
|
||||||
pub struct BlocksElement {
|
pub struct BlockType {
|
||||||
definition: BlockDefinition,
|
definition: BlockDefinition,
|
||||||
properties: Option<HashMap<String, Vec<String>>>,
|
|
||||||
states: Vec<BlockState>,
|
states: Vec<BlockState>,
|
||||||
|
|
||||||
|
// 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<String, Vec<String>>,
|
||||||
}
|
}
|
||||||
|
|
||||||
lazy_static! {
|
lazy_static! {
|
||||||
pub static ref BLOCKS: HashMap<String, BlocksElement> =
|
pub static ref BLOCKS: HashMap<String, BlockType> =
|
||||||
serde_json::from_str(BLOCKS_JSON).expect("Could not parse block.json registry.");
|
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
|
None => Ok(block
|
||||||
.states
|
.states
|
||||||
.iter()
|
.iter()
|
||||||
.find(|state| state.default.unwrap_or(false))
|
.find(|state| state.is_default)
|
||||||
.expect("Each block should have at least one default state")
|
.expect("Each block should have at least one default state")
|
||||||
.id),
|
.id),
|
||||||
Some(properties) => block
|
Some(properties) => block
|
||||||
.states
|
.states
|
||||||
.iter()
|
.iter()
|
||||||
.find(|state| state.properties.as_ref() == Some(properties))
|
.find(|state| &state.properties == properties)
|
||||||
.map(|state| state.id)
|
.map(|state| state.id)
|
||||||
.ok_or(WorldError::BlockStateIdNotFound),
|
.ok_or(WorldError::BlockStateIdNotFound),
|
||||||
};
|
};
|
||||||
|
|||||||
Reference in new issue
Block a user