Add block_loot api

Signed-off-by: trivernis <trivernis@protonmail.com>
pull/1/head
trivernis 4 years ago
parent 2a09971903
commit 273902873d
Signed by: Trivernis
GPG Key ID: DFFFCC2C7A02DB45

@ -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<HashMap<String, EntityLoot>> {
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<Vec<BlockLoot>> {
let content = get_version_specific_file(&self.version, BLOCK_LOOT_FILE)?;
let loot = serde_json::from_str::<Vec<BlockLoot>>(&content)?;
Ok(loot)
}
/// Returns the block loot indexed by block name
pub fn block_loot(&self) -> DataResult<HashMap<String, BlockLoot>> {
let loot = self.block_loot_array()?;
let loot_map = HashMap::from_iter(loot.into_iter().map(|l| (l.block.clone(), l)));
Ok(loot_map)
}
}

@ -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;

@ -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());
}
}

@ -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<ItemDrop>,
}
#[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<usize>; 2],
pub block_age: Option<usize>,
pub silk_touch: Option<bool>,
pub no_silk_touch: Option<bool>,
}

@ -1,3 +1,4 @@
pub mod block_loot;
pub mod enchantment;
pub mod entity_loot;
pub mod item;

@ -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::<i32>().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())
}
}

Loading…
Cancel
Save