parent
c2a7fd2ff1
commit
2a09971903
@ -0,0 +1,34 @@
|
||||
use crate::data::{get_version_specific_file, ENTITY_LOOT_FILE};
|
||||
use crate::models::entity_loot::EntityLoot;
|
||||
use crate::models::version::Version;
|
||||
use crate::DataResult;
|
||||
use std::collections::HashMap;
|
||||
use std::iter::FromIterator;
|
||||
use std::sync::Arc;
|
||||
|
||||
/// API to access item information
|
||||
pub struct Loot {
|
||||
version: Arc<Version>,
|
||||
}
|
||||
|
||||
impl Loot {
|
||||
pub fn new(version: Arc<Version>) -> Self {
|
||||
Self { version }
|
||||
}
|
||||
|
||||
/// Returns the entity loot in a list
|
||||
pub fn entity_loot_array(&self) -> DataResult<Vec<EntityLoot>> {
|
||||
let content = get_version_specific_file(&self.version, ENTITY_LOOT_FILE)?;
|
||||
let loot = serde_json::from_str::<Vec<EntityLoot>>(&content)?;
|
||||
|
||||
Ok(loot)
|
||||
}
|
||||
|
||||
/// Returns the entity loot indexed by 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)
|
||||
}
|
||||
}
|
@ -0,0 +1,23 @@
|
||||
use crate::api::tests::{get_api, get_test_versions};
|
||||
|
||||
#[test]
|
||||
pub fn test_entity_loot_array() {
|
||||
let versions = get_test_versions();
|
||||
|
||||
for version in versions {
|
||||
let api = get_api(version);
|
||||
assert_ne!(api.loot.entity_loot_array().unwrap().len(), 0)
|
||||
}
|
||||
}
|
||||
|
||||
#[test]
|
||||
pub fn test_entity_loot_by_name() {
|
||||
let versions = get_test_versions();
|
||||
|
||||
for version in versions {
|
||||
let api = get_api(version);
|
||||
let by_name = api.loot.entity_loot().unwrap();
|
||||
assert!(by_name.get("sheep").is_some());
|
||||
assert!(by_name.get("zombie").is_some());
|
||||
}
|
||||
}
|
@ -0,0 +1,15 @@
|
||||
#[derive(Deserialize, Debug, Clone)]
|
||||
#[serde(rename_all(deserialize = "camelCase", serialize = "snake_case"))]
|
||||
pub struct EntityLoot {
|
||||
pub entity: 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: [usize; 2],
|
||||
pub player_kill: Option<bool>,
|
||||
}
|
@ -1,4 +1,5 @@
|
||||
pub mod enchantment;
|
||||
pub mod entity_loot;
|
||||
pub mod item;
|
||||
pub mod recipe;
|
||||
pub mod version;
|
||||
|
Loading…
Reference in New Issue