From 89af4aaee29b6a45f114bbc81f606ce12aa1f1a7 Mon Sep 17 00:00:00 2001 From: Matthias Schiffer Date: Thu, 2 Mar 2023 00:03:39 +0100 Subject: [PATCH] resource: extend BlockTypes with legacy block type data Allow to directly map from id/data values to BlockType. --- src/resource/mod.rs | 34 ++++++++++++++++++++++++++-------- 1 file changed, 26 insertions(+), 8 deletions(-) diff --git a/src/resource/mod.rs b/src/resource/mod.rs index ee409e6..812a0e7 100644 --- a/src/resource/mod.rs +++ b/src/resource/mod.rs @@ -54,16 +54,29 @@ impl BlockType { } #[derive(Debug)] -pub struct BlockTypes(HashMap); +pub struct BlockTypes { + block_type_map: HashMap, + legacy_block_types: Box<[[BlockType; 16]; 256]>, +} impl Default for BlockTypes { fn default() -> Self { - BlockTypes( - block_types::BLOCK_TYPES - .iter() - .map(|(k, v)| (String::from(*k), *v)) - .collect(), - ) + 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, + } } } @@ -71,6 +84,11 @@ impl BlockTypes { #[inline] pub fn get(&self, id: &str) -> Option { let suffix = id.strip_prefix("minecraft:")?; - self.0.get(suffix).copied() + self.block_type_map.get(suffix).copied() + } + + #[inline] + pub fn get_legacy(&self, id: u8, data: u8) -> Option { + Some(self.legacy_block_types[id as usize][data as usize]) } }