resource: rename BlockTypeMap to BlockTypes

The block_types() function it turned into a Default implementation.
This commit is contained in:
Matthias Schiffer 2023-03-01 22:45:56 +01:00
parent 95e4e45974
commit 4c2fd6c1a9
Signed by: neocturne
GPG key ID: 16EF3F64CB201D9C
3 changed files with 18 additions and 15 deletions

View file

@ -50,14 +50,14 @@ impl Paths {
/// Type with methods for processing the regions of a Minecraft save directory /// Type with methods for processing the regions of a Minecraft save directory
struct RegionProcessor<'a> { struct RegionProcessor<'a> {
block_types: resource::BlockTypeMap, block_types: resource::BlockTypes,
paths: &'a Paths, paths: &'a Paths,
} }
impl<'a> RegionProcessor<'a> { impl<'a> RegionProcessor<'a> {
fn new(paths: &'a Paths) -> Self { fn new(paths: &'a Paths) -> Self {
RegionProcessor { RegionProcessor {
block_types: resource::block_types(), block_types: resource::BlockTypes::default(),
paths, paths,
} }
} }

View file

@ -53,21 +53,24 @@ impl BlockType {
} }
} }
pub struct BlockTypeMap(HashMap<String, BlockType>); #[derive(Debug)]
pub struct BlockTypes(HashMap<String, BlockType>);
impl BlockTypeMap { impl Default for BlockTypes {
fn default() -> Self {
BlockTypes(
block_types::BLOCK_TYPES
.iter()
.map(|(k, v)| (String::from(*k), *v))
.collect(),
)
}
}
impl BlockTypes {
#[inline] #[inline]
pub fn get(&self, id: &str) -> Option<BlockType> { pub fn get(&self, id: &str) -> Option<BlockType> {
let suffix = id.strip_prefix("minecraft:")?; let suffix = id.strip_prefix("minecraft:")?;
self.0.get(suffix).copied() self.0.get(suffix).copied()
} }
} }
pub fn block_types() -> BlockTypeMap {
BlockTypeMap(
block_types::BLOCK_TYPES
.iter()
.map(|(k, v)| (String::from(*k), *v))
.collect(),
)
}

View file

@ -4,7 +4,7 @@ use serde::{Deserialize, Serialize};
use super::chunk::Chunk; use super::chunk::Chunk;
use crate::{ use crate::{
resource::{BlockFlag, BlockType, BlockTypeMap}, resource::{BlockFlag, BlockType, BlockTypes},
types::*, types::*,
}; };
@ -92,7 +92,7 @@ pub type BlockInfoArray = LayerBlockArray<Option<BlockInfo>>;
/// determined as the block that should be visible on the rendered /// determined as the block that should be visible on the rendered
/// map. For water blocks, the height of the first non-water block /// map. For water blocks, the height of the first non-water block
/// is additionally filled in as the water depth. /// is additionally filled in as the water depth.
pub fn top_layer(chunk: &Chunk, block_types: &BlockTypeMap) -> Result<Box<BlockInfoArray>> { pub fn top_layer(chunk: &Chunk, block_types: &BlockTypes) -> Result<Box<BlockInfoArray>> {
use BLOCKS_PER_CHUNK as N; use BLOCKS_PER_CHUNK as N;
let mut done = 0; let mut done = 0;