diff --git a/src/api/blocks.rs b/src/api/blocks.rs index 2b14dc6..ace1e2f 100644 --- a/src/api/blocks.rs +++ b/src/api/blocks.rs @@ -23,11 +23,18 @@ impl Blocks { Ok(blocks) } - /// Returns the blocks indexed by ID - pub fn blocks(&self) -> DataResult> { + // Returns the blocks indexed by state ID + pub fn blocks_by_state_id(&self) -> DataResult> { 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> { + 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 { let content = get_version_specific_file(&self.version, BLOCK_COLLISION_SHAPES_FILE)?; diff --git a/src/api/enchantments.rs b/src/api/enchantments.rs index 026fd48..1b5d89b 100644 --- a/src/api/enchantments.rs +++ b/src/api/enchantments.rs @@ -20,7 +20,7 @@ impl Enchantments { pub fn enchantments_array(&self) -> DataResult> { let content = get_version_specific_file(&self.version, ENCHANTMENTS_FILE)?; - serde_json::from_str::>(&*content).map_err(DataError::from) + serde_json::from_str::>(&content).map_err(DataError::from) } /// Returns a map of enchantments indexed by ID diff --git a/src/api/items.rs b/src/api/items.rs index 9c62989..e454b14 100644 --- a/src/api/items.rs +++ b/src/api/items.rs @@ -19,7 +19,7 @@ impl Items { pub fn items_array(&self) -> DataResult> { let content = get_version_specific_file(&self.version, ITEMS_FILE)?; - serde_json::from_str::>(&*content).map_err(DataError::from) + serde_json::from_str::>(&content).map_err(DataError::from) } /// Returns the items indexed by name diff --git a/src/api/recipes.rs b/src/api/recipes.rs index 2c802af..7b95a27 100644 --- a/src/api/recipes.rs +++ b/src/api/recipes.rs @@ -18,6 +18,6 @@ impl Recipes { /// Returns a list of recipes indexed by item ID pub fn recipes(&self) -> DataResult>> { let content = get_version_specific_file(&self.version, RECIPES_FILE)?; - serde_json::from_str::>>(&*content).map_err(DataError::from) + serde_json::from_str::>>(&content).map_err(DataError::from) } } diff --git a/src/api/tests/blocks.rs b/src/api/tests/blocks.rs index 5570dbe..9e13a4c 100644 --- a/src/api/tests/blocks.rs +++ b/src/api/tests/blocks.rs @@ -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(); diff --git a/src/api/versions.rs b/src/api/versions.rs index 4a1fe57..1cbac3e 100644 --- a/src/api/versions.rs +++ b/src/api/versions.rs @@ -7,7 +7,7 @@ use std::collections::HashMap; /// Returns the unsorted list of versions pub fn versions() -> DataResult> { let content = get_common_file(PROTOCOL_VERSIONS_FILE)?; - let versions = serde_json::from_str::>(&*content)?; + let versions = serde_json::from_str::>(&content)?; Ok(versions) } @@ -52,5 +52,5 @@ pub fn latest_stable() -> DataResult { /// Returns a list of available version information pub fn available_versions() -> DataResult> { let content = get_common_file(VERSIONS_FILE)?; - serde_json::from_str::>(&*content).map_err(DataError::from) + serde_json::from_str::>(&content).map_err(DataError::from) }