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:
parent
03fcdc2e32
commit
5a21a687d8
@ -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<String>,
|
||||
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<String>,
|
||||
}
|
||||
|
||||
/// 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<bool>,
|
||||
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)]
|
||||
pub struct BlocksElement {
|
||||
pub struct BlockType {
|
||||
definition: BlockDefinition,
|
||||
properties: Option<HashMap<String, Vec<String>>>,
|
||||
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! {
|
||||
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.");
|
||||
}
|
||||
|
||||
@ -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),
|
||||
};
|
||||
|
||||
Loading…
Reference in New Issue
Block a user