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)]
|
|
|
|
pub struct BlockTypes(HashMap<String, BlockType>);
|
2023-03-01 21:54:40 +01:00
|
|
|
|
2023-03-01 22:45:56 +01:00
|
|
|
impl Default for BlockTypes {
|
|
|
|
fn default() -> Self {
|
|
|
|
BlockTypes(
|
|
|
|
block_types::BLOCK_TYPES
|
|
|
|
.iter()
|
|
|
|
.map(|(k, v)| (String::from(*k), *v))
|
|
|
|
.collect(),
|
|
|
|
)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
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:")?;
|
|
|
|
self.0.get(suffix).copied()
|
2023-03-01 21:54:40 +01:00
|
|
|
}
|
|
|
|
}
|