82 lines
2.2 KiB
Rust
82 lines
2.2 KiB
Rust
pub mod entities;
|
|
pub mod viewer;
|
|
|
|
use std::collections::HashMap;
|
|
|
|
use pumpkin_data::{Block, BlockState, BlockStateId};
|
|
use serde::{Deserialize, Deserializer, Serialize, Serializer};
|
|
|
|
#[derive(Serialize, Deserialize, Clone)]
|
|
#[serde(rename_all = "PascalCase")]
|
|
pub struct BlockStateCodec {
|
|
/// Block name
|
|
#[serde(
|
|
deserialize_with = "parse_block_name",
|
|
serialize_with = "block_to_string"
|
|
)]
|
|
pub name: &'static Block,
|
|
/// Key-value pairs of properties
|
|
#[serde(skip_serializing_if = "Option::is_none")]
|
|
pub properties: Option<HashMap<String, String>>,
|
|
}
|
|
|
|
fn parse_block_name<'de, D: Deserializer<'de>>(
|
|
deserializer: D,
|
|
) -> Result<&'static Block, D::Error> {
|
|
let s = String::deserialize(deserializer)?;
|
|
let block =
|
|
Block::from_name(s.as_str()).ok_or(serde::de::Error::custom("Invalid block name"))?;
|
|
Ok(block)
|
|
}
|
|
|
|
fn block_to_string<S: Serializer>(block: &'static Block, serializer: S) -> Result<S::Ok, S::Error> {
|
|
serializer.serialize_str(block.name)
|
|
}
|
|
|
|
impl BlockStateCodec {
|
|
#[must_use]
|
|
pub fn get_state(&self) -> &'static BlockState {
|
|
let state_id = self.get_state_id();
|
|
BlockState::from_id(state_id)
|
|
}
|
|
|
|
#[must_use]
|
|
pub const fn get_block(&self) -> &'static Block {
|
|
self.name
|
|
}
|
|
|
|
/// Prefer this over `get_state` when the only the state ID is needed
|
|
#[must_use]
|
|
pub fn get_state_id(&self) -> BlockStateId {
|
|
let block = self.name;
|
|
|
|
let Some(properties_map) = &self.properties else {
|
|
return block.default_state.id;
|
|
};
|
|
|
|
let props_iter = properties_map
|
|
.iter()
|
|
.map(|(k, v)| (k.as_str(), v.as_str()))
|
|
.collect::<Vec<(&str, &str)>>();
|
|
|
|
let block_properties = block.from_properties(&props_iter);
|
|
block_properties.to_state_id(block)
|
|
}
|
|
}
|
|
|
|
#[cfg(test)]
|
|
mod test {
|
|
use pumpkin_data::BlockStateId;
|
|
|
|
use crate::chunk::palette::BLOCK_NETWORK_MAX_BITS;
|
|
|
|
#[test]
|
|
fn proper_network_bits_per_entry() {
|
|
let id_to_test = 1 << BLOCK_NETWORK_MAX_BITS;
|
|
assert!(
|
|
BlockStateId::new_or_air(id_to_test) == BlockStateId::AIR,
|
|
"We need to update our constants!"
|
|
);
|
|
}
|
|
}
|