diff --git a/src/api/entity_loot.rs b/src/api/loot.rs similarity index 54% rename from src/api/entity_loot.rs rename to src/api/loot.rs index a970ce9..02f46c8 100644 --- a/src/api/entity_loot.rs +++ b/src/api/loot.rs @@ -1,4 +1,5 @@ -use crate::data::{get_version_specific_file, ENTITY_LOOT_FILE}; +use crate::data::{get_version_specific_file, BLOCK_LOOT_FILE, ENTITY_LOOT_FILE}; +use crate::models::block_loot::BlockLoot; use crate::models::entity_loot::EntityLoot; use crate::models::version::Version; use crate::DataResult; @@ -24,11 +25,27 @@ impl Loot { Ok(loot) } - /// Returns the entity loot indexed by name + /// Returns the entity loot indexed by entity name pub fn entity_loot(&self) -> DataResult> { let loot = self.entity_loot_array()?; let loot_map = HashMap::from_iter(loot.into_iter().map(|l| (l.entity.clone(), l))); Ok(loot_map) } + + /// Returns the block loot in a list + pub fn block_loot_array(&self) -> DataResult> { + let content = get_version_specific_file(&self.version, BLOCK_LOOT_FILE)?; + let loot = serde_json::from_str::>(&content)?; + + Ok(loot) + } + + /// Returns the block loot indexed by block name + pub fn block_loot(&self) -> DataResult> { + let loot = self.block_loot_array()?; + let loot_map = HashMap::from_iter(loot.into_iter().map(|l| (l.block.clone(), l))); + + Ok(loot_map) + } } diff --git a/src/api/mod.rs b/src/api/mod.rs index 57aaaca..c8c022b 100644 --- a/src/api/mod.rs +++ b/src/api/mod.rs @@ -1,6 +1,6 @@ use crate::api::enchantments::Enchantments; -use crate::api::entity_loot::Loot; use crate::api::items::Items; +use crate::api::loot::Loot; use crate::api::recipes::Recipes; use crate::models::version::Version; use std::sync::Arc; @@ -9,8 +9,8 @@ use std::sync::Arc; mod tests; pub mod enchantments; -mod entity_loot; pub mod items; +mod loot; mod recipes; pub mod versions; diff --git a/src/api/tests/loot.rs b/src/api/tests/loot.rs index fd2e92a..0c452d0 100644 --- a/src/api/tests/loot.rs +++ b/src/api/tests/loot.rs @@ -21,3 +21,25 @@ pub fn test_entity_loot_by_name() { assert!(by_name.get("zombie").is_some()); } } + +#[test] +pub fn test_block_loot_array() { + let versions = get_test_versions(); + + for version in versions { + let api = get_api(version); + assert_ne!(api.loot.block_loot_array().unwrap().len(), 0) + } +} + +#[test] +pub fn test_block_loot_by_name() { + let versions = get_test_versions(); + + for version in versions { + let api = get_api(version); + let by_name = api.loot.block_loot().unwrap(); + assert!(by_name.get("poppy").is_some()); + assert!(by_name.get("stone").is_some()); + } +} diff --git a/src/models/block_loot.rs b/src/models/block_loot.rs new file mode 100644 index 0000000..7387a55 --- /dev/null +++ b/src/models/block_loot.rs @@ -0,0 +1,17 @@ +#[derive(Deserialize, Debug, Clone)] +#[serde(rename_all(deserialize = "camelCase", serialize = "snake_case"))] +pub struct BlockLoot { + pub block: String, + pub drops: Vec, +} + +#[derive(Deserialize, Debug, Clone)] +#[serde(rename_all(deserialize = "camelCase", serialize = "snake_case"))] +pub struct ItemDrop { + pub item: String, + pub drop_chance: f32, + pub stack_size_range: [Option; 2], + pub block_age: Option, + pub silk_touch: Option, + pub no_silk_touch: Option, +} diff --git a/src/models/mod.rs b/src/models/mod.rs index 9dedb6b..71f19a3 100644 --- a/src/models/mod.rs +++ b/src/models/mod.rs @@ -1,3 +1,4 @@ +pub mod block_loot; pub mod enchantment; pub mod entity_loot; pub mod item; diff --git a/src/models/version.rs b/src/models/version.rs index a80aab2..2bc7118 100644 --- a/src/models/version.rs +++ b/src/models/version.rs @@ -5,24 +5,3 @@ pub struct Version { pub minecraft_version: String, pub major_version: String, } - -impl Version { - /// Returns the first version of the current major version - pub(crate) fn major_first(&self) -> String { - format!("{}.1", self.major_version) - } - - /// Returns the previous major version - pub(crate) fn previous_major(&self) -> String { - let major = self.major_version.split('.').last().unwrap(); - let major_num = major.parse::().unwrap(); - - self.major_version - .replace(major, format!("{}", major_num - 1).as_str()) - } - - /// Returns the first version of the previous major version - pub(crate) fn previous_major_first(&self) -> String { - format!("{}.1", self.previous_major()) - } -}