Merge pull request #10 from firejoust/main

Get blocks by state ID
pull/11/head^2
Julius Riegel 2 years ago committed by GitHub
commit a7d1ced784
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23

@ -23,11 +23,18 @@ impl Blocks {
Ok(blocks)
}
/// Returns the blocks indexed by ID
pub fn blocks(&self) -> DataResult<HashMap<u32, Block>> {
// Returns the blocks indexed by state ID
pub fn blocks_by_state_id(&self) -> DataResult<HashMap<u32, Block>> {
let blocks = self.blocks_array()?;
let blocks_map = blocks.into_iter().map(|b| (b.id, b)).collect();
let mut blocks_map = HashMap::new();
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.clone());
});
});
Ok(blocks_map)
}
@ -39,6 +46,14 @@ impl Blocks {
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
pub fn block_collision_shapes(&self) -> DataResult<BlockCollisionShapes> {
let content = get_version_specific_file(&self.version, BLOCK_COLLISION_SHAPES_FILE)?;

@ -20,7 +20,7 @@ impl Enchantments {
pub fn enchantments_array(&self) -> DataResult<Vec<Enchantment>> {
let content = get_version_specific_file(&self.version, ENCHANTMENTS_FILE)?;
serde_json::from_str::<Vec<Enchantment>>(&*content).map_err(DataError::from)
serde_json::from_str::<Vec<Enchantment>>(&content).map_err(DataError::from)
}
/// Returns a map of enchantments indexed by ID

@ -19,7 +19,7 @@ impl Items {
pub fn items_array(&self) -> DataResult<Vec<Item>> {
let content = get_version_specific_file(&self.version, ITEMS_FILE)?;
serde_json::from_str::<Vec<Item>>(&*content).map_err(DataError::from)
serde_json::from_str::<Vec<Item>>(&content).map_err(DataError::from)
}
/// Returns the items indexed by name

@ -18,6 +18,6 @@ impl Recipes {
/// Returns a list of recipes indexed by item ID
pub fn recipes(&self) -> DataResult<HashMap<u32, Vec<Recipe>>> {
let content = get_version_specific_file(&self.version, RECIPES_FILE)?;
serde_json::from_str::<HashMap<u32, Vec<Recipe>>>(&*content).map_err(DataError::from)
serde_json::from_str::<HashMap<u32, Vec<Recipe>>>(&content).map_err(DataError::from)
}
}

@ -11,6 +11,17 @@ 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 by_state = api.blocks.blocks_by_state_id();
assert!(by_state.is_ok());
}
}
#[test]
pub fn test_blocks_by_name() {
let versions = get_test_versions();

@ -7,7 +7,7 @@ use std::collections::HashMap;
/// Returns the unsorted list of versions
pub fn versions() -> DataResult<Vec<Version>> {
let content = get_common_file(PROTOCOL_VERSIONS_FILE)?;
let versions = serde_json::from_str::<Vec<Version>>(&*content)?;
let versions = serde_json::from_str::<Vec<Version>>(&content)?;
Ok(versions)
}
@ -52,5 +52,5 @@ pub fn latest_stable() -> DataResult<Version> {
/// Returns a list of available version information
pub fn available_versions() -> DataResult<Vec<String>> {
let content = get_common_file(VERSIONS_FILE)?;
serde_json::from_str::<Vec<String>>(&*content).map_err(DataError::from)
serde_json::from_str::<Vec<String>>(&content).map_err(DataError::from)
}

Loading…
Cancel
Save