Get blocks by state ID

pull/10/head
firejoust 2 years ago
parent ff68115bf1
commit ec97d1aa80

@ -23,10 +23,16 @@ impl Blocks {
Ok(blocks) Ok(blocks)
} }
/// Returns the blocks indexed by ID // Returns the references of blocks indexed by state ID
pub fn blocks(&self) -> DataResult<HashMap<u32, Block>> { pub fn blocks_by_state_id<'a>(&self, blocks: &'a Vec<Block>) -> DataResult<HashMap<u32, &'a Block>> {
let blocks = self.blocks_array()?; let mut blocks_map = HashMap::new();
let blocks_map = blocks.into_iter().map(|b| (b.id, b)).collect(); blocks.iter().for_each(|b| {
let min_state_id = b.min_state_id.unwrap_or(b.id << 4);
let max_state_id = b.max_state_id.unwrap_or(min_state_id + 15);
(min_state_id..max_state_id).for_each(|s| {
blocks_map.insert(s, b);
});
});
Ok(blocks_map) Ok(blocks_map)
} }
@ -39,6 +45,14 @@ impl Blocks {
Ok(blocks_map) Ok(blocks_map)
} }
/// Returns the blocks indexed by ID
pub fn blocks(&self) -> DataResult<HashMap<u32, Block>> {
let blocks = self.blocks_array()?;
let blocks_map = blocks.into_iter().map(|b| (b.id, b)).collect();
Ok(blocks_map)
}
/// Returns the block collision shapes object /// Returns the block collision shapes object
pub fn block_collision_shapes(&self) -> DataResult<BlockCollisionShapes> { pub fn block_collision_shapes(&self) -> DataResult<BlockCollisionShapes> {
let content = get_version_specific_file(&self.version, BLOCK_COLLISION_SHAPES_FILE)?; let content = get_version_specific_file(&self.version, BLOCK_COLLISION_SHAPES_FILE)?;

@ -11,6 +11,18 @@ pub fn test_blocks_array() {
} }
} }
#[test]
pub fn test_blocks_by_state_id() {
let versions = get_test_versions();
for version in versions {
let api = get_api(version);
let blocks = api.blocks.blocks_array().unwrap();
let by_state = api.blocks.blocks_by_state_id(&blocks);
assert!(by_state.is_ok());
}
}
#[test] #[test]
pub fn test_blocks_by_name() { pub fn test_blocks_by_name() {
let versions = get_test_versions(); let versions = get_test_versions();

Loading…
Cancel
Save