From ec97d1aa80deba74476d925fa2fb3410d69e2ff9 Mon Sep 17 00:00:00 2001 From: firejoust <42525540+firejoust@users.noreply.github.com> Date: Wed, 30 Nov 2022 15:36:14 +1100 Subject: [PATCH 1/3] Get blocks by state ID --- src/api/blocks.rs | 24 +++++++++++++++++++----- src/api/tests/blocks.rs | 12 ++++++++++++ 2 files changed, 31 insertions(+), 5 deletions(-) diff --git a/src/api/blocks.rs b/src/api/blocks.rs index 2b14dc6..373584c 100644 --- a/src/api/blocks.rs +++ b/src/api/blocks.rs @@ -23,11 +23,17 @@ impl Blocks { Ok(blocks) } - /// 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(); - + // Returns the references of blocks indexed by state ID + pub fn blocks_by_state_id<'a>(&self, blocks: &'a Vec) -> DataResult> { + 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); + }); + }); + Ok(blocks_map) } @@ -39,6 +45,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/tests/blocks.rs b/src/api/tests/blocks.rs index 5570dbe..fb27a91 100644 --- a/src/api/tests/blocks.rs +++ b/src/api/tests/blocks.rs @@ -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] pub fn test_blocks_by_name() { let versions = get_test_versions(); From 1ae46eb09a3e9eb74df6f736ee367d5f2f2c06dd Mon Sep 17 00:00:00 2001 From: firejoust <42525540+firejoust@users.noreply.github.com> Date: Mon, 5 Dec 2022 20:10:35 +1100 Subject: [PATCH 2/3] Use self/clone --- src/api/blocks.rs | 7 ++++--- src/api/tests/blocks.rs | 3 +-- 2 files changed, 5 insertions(+), 5 deletions(-) diff --git a/src/api/blocks.rs b/src/api/blocks.rs index 373584c..ace1e2f 100644 --- a/src/api/blocks.rs +++ b/src/api/blocks.rs @@ -23,14 +23,15 @@ impl Blocks { Ok(blocks) } - // Returns the references of blocks indexed by state ID - pub fn blocks_by_state_id<'a>(&self, blocks: &'a Vec) -> DataResult> { + // Returns the blocks indexed by state ID + pub fn blocks_by_state_id(&self) -> DataResult> { + let blocks = self.blocks_array()?; 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); + blocks_map.insert(s, b.clone()); }); }); diff --git a/src/api/tests/blocks.rs b/src/api/tests/blocks.rs index fb27a91..9e13a4c 100644 --- a/src/api/tests/blocks.rs +++ b/src/api/tests/blocks.rs @@ -17,8 +17,7 @@ pub fn test_blocks_by_state_id() { 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); + let by_state = api.blocks.blocks_by_state_id(); assert!(by_state.is_ok()); } } From 7ce26821e9262d46575a209b7b018d6256e4a422 Mon Sep 17 00:00:00 2001 From: firejoust <42525540+firejoust@users.noreply.github.com> Date: Sat, 17 Dec 2022 11:28:32 +1100 Subject: [PATCH 3/3] fix CI lint --- src/api/enchantments.rs | 2 +- src/api/items.rs | 2 +- src/api/recipes.rs | 2 +- src/api/versions.rs | 4 ++-- 4 files changed, 5 insertions(+), 5 deletions(-) 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/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) }