2023-02-07 22:09:14 +01:00
|
|
|
mod block_types;
|
2023-02-12 19:02:00 +01:00
|
|
|
mod legacy_block_types;
|
2023-02-07 22:09:14 +01:00
|
|
|
|
|
|
|
use std::collections::HashMap;
|
|
|
|
|
|
|
|
use enumflags2::{bitflags, BitFlags};
|
|
|
|
|
2023-02-12 19:02:00 +01:00
|
|
|
pub use legacy_block_types::LEGACY_BLOCK_TYPES;
|
2023-02-25 23:23:00 +01:00
|
|
|
use serde::{de, Deserialize, Deserializer, Serialize, Serializer};
|
2023-02-12 19:02:00 +01:00
|
|
|
|
2023-02-07 22:09:14 +01:00
|
|
|
#[bitflags]
|
|
|
|
#[repr(u8)]
|
|
|
|
#[derive(Debug, Clone, Copy, PartialEq)]
|
2023-02-13 00:11:36 +01:00
|
|
|
pub enum BlockFlag {
|
2023-02-07 22:09:14 +01:00
|
|
|
Opaque,
|
|
|
|
Grass,
|
|
|
|
Foliage,
|
|
|
|
Birch,
|
|
|
|
Spruce,
|
|
|
|
Water,
|
|
|
|
}
|
|
|
|
|
2023-02-25 23:23:00 +01:00
|
|
|
fn serialize_block_flags<S>(flags: &BitFlags<BlockFlag>, serializer: S) -> Result<S::Ok, S::Error>
|
|
|
|
where
|
|
|
|
S: Serializer,
|
|
|
|
{
|
|
|
|
flags.bits().serialize(serializer)
|
|
|
|
}
|
|
|
|
fn deserialize_block_flags<'de, D>(deserializer: D) -> Result<BitFlags<BlockFlag>, D::Error>
|
|
|
|
where
|
|
|
|
D: Deserializer<'de>,
|
|
|
|
{
|
|
|
|
let bits = u8::deserialize(deserializer)?;
|
|
|
|
BitFlags::<BlockFlag>::from_bits(bits).map_err(de::Error::custom)
|
|
|
|
}
|
|
|
|
|
|
|
|
#[derive(Debug, Clone, Copy, Serialize, Deserialize)]
|
2023-02-07 22:09:14 +01:00
|
|
|
pub struct BlockColor(pub u8, pub u8, pub u8);
|
|
|
|
|
2023-02-25 23:23:00 +01:00
|
|
|
#[derive(Debug, Clone, Copy, Serialize, Deserialize)]
|
2023-02-07 22:09:14 +01:00
|
|
|
pub struct BlockType {
|
2023-02-25 23:23:00 +01:00
|
|
|
#[serde(
|
|
|
|
serialize_with = "serialize_block_flags",
|
|
|
|
deserialize_with = "deserialize_block_flags"
|
|
|
|
)]
|
2023-02-13 00:11:36 +01:00
|
|
|
pub flags: BitFlags<BlockFlag>,
|
2023-02-07 22:09:14 +01:00
|
|
|
pub color: BlockColor,
|
|
|
|
}
|
|
|
|
|
2023-02-15 00:20:15 +01:00
|
|
|
impl BlockType {
|
|
|
|
pub fn is(&self, flag: BlockFlag) -> bool {
|
|
|
|
self.flags.contains(flag)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2023-03-01 22:45:56 +01:00
|
|
|
#[derive(Debug)]
|
2023-03-02 00:03:39 +01:00
|
|
|
pub struct BlockTypes {
|
|
|
|
block_type_map: HashMap<String, BlockType>,
|
|
|
|
legacy_block_types: Box<[[BlockType; 16]; 256]>,
|
|
|
|
}
|
2023-03-01 21:54:40 +01:00
|
|
|
|
2023-03-01 22:45:56 +01:00
|
|
|
impl Default for BlockTypes {
|
|
|
|
fn default() -> Self {
|
2023-03-02 00:03:39 +01:00
|
|
|
let block_type_map: HashMap<_, _> = block_types::BLOCK_TYPES
|
|
|
|
.iter()
|
|
|
|
.map(|(k, v)| (String::from(*k), *v))
|
|
|
|
.collect();
|
|
|
|
let legacy_block_types = Box::new(LEGACY_BLOCK_TYPES.map(|inner| {
|
|
|
|
inner.map(|id| {
|
|
|
|
*id.strip_prefix("minecraft:")
|
|
|
|
.and_then(|suffix| block_type_map.get(suffix))
|
|
|
|
.expect("Unknown legacy block type")
|
|
|
|
})
|
|
|
|
}));
|
|
|
|
|
|
|
|
BlockTypes {
|
|
|
|
block_type_map,
|
|
|
|
legacy_block_types,
|
|
|
|
}
|
2023-03-01 22:45:56 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
impl BlockTypes {
|
2023-03-01 21:54:40 +01:00
|
|
|
#[inline]
|
|
|
|
pub fn get(&self, id: &str) -> Option<BlockType> {
|
2023-03-01 21:58:43 +01:00
|
|
|
let suffix = id.strip_prefix("minecraft:")?;
|
2023-03-02 00:03:39 +01:00
|
|
|
self.block_type_map.get(suffix).copied()
|
|
|
|
}
|
|
|
|
|
|
|
|
#[inline]
|
|
|
|
pub fn get_legacy(&self, id: u8, data: u8) -> Option<BlockType> {
|
|
|
|
Some(self.legacy_block_types[id as usize][data as usize])
|
2023-03-01 21:54:40 +01:00
|
|
|
}
|
|
|
|
}
|