Add entity_loot api

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

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

@ -1,4 +1,5 @@
use crate::api::enchantments::Enchantments; use crate::api::enchantments::Enchantments;
use crate::api::entity_loot::Loot;
use crate::api::items::Items; use crate::api::items::Items;
use crate::api::recipes::Recipes; use crate::api::recipes::Recipes;
use crate::models::version::Version; use crate::models::version::Version;
@ -8,6 +9,7 @@ use std::sync::Arc;
mod tests; mod tests;
pub mod enchantments; pub mod enchantments;
mod entity_loot;
pub mod items; pub mod items;
mod recipes; mod recipes;
pub mod versions; pub mod versions;
@ -17,6 +19,7 @@ pub struct Api {
pub items: Items, pub items: Items,
pub recipes: Recipes, pub recipes: Recipes,
pub enchantments: Enchantments, pub enchantments: Enchantments,
pub loot: Loot,
} }
impl Api { impl Api {
@ -27,6 +30,7 @@ impl Api {
items: Items::new(Arc::clone(&version)), items: Items::new(Arc::clone(&version)),
recipes: Recipes::new(Arc::clone(&version)), recipes: Recipes::new(Arc::clone(&version)),
enchantments: Enchantments::new(Arc::clone(&version)), enchantments: Enchantments::new(Arc::clone(&version)),
loot: Loot::new(Arc::clone(&version)),
} }
} }
} }

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

@ -4,6 +4,7 @@ use crate::models::version::Version;
mod enchantments; mod enchantments;
mod items; mod items;
mod loot;
mod recipes; mod recipes;
mod versions; mod versions;
@ -17,5 +18,6 @@ fn get_test_versions() -> Vec<Version> {
.unwrap() .unwrap()
.into_iter() .into_iter()
.filter(|v| available.contains(&v.minecraft_version)) .filter(|v| available.contains(&v.minecraft_version))
.filter(|v| v.version >= 477) // datapaths < 1.14 are incomplete
.collect() .collect()
} }

@ -1,4 +1,3 @@
use serde_json::Value;
use std::collections::HashMap; use std::collections::HashMap;
#[derive(Deserialize, Debug, Clone)] #[derive(Deserialize, Debug, Clone)]

@ -4,7 +4,6 @@ use crate::data::datapaths::Datapaths;
use crate::models::version::Version; use crate::models::version::Version;
use crate::{DataError, DataResult}; use crate::{DataError, DataResult};
use include_dir::Dir; use include_dir::Dir;
use std::collections::HashMap;
pub static MINECRAFT_DATA: Dir = include_dir::include_dir!("minecraft-data/data"); pub static MINECRAFT_DATA: Dir = include_dir::include_dir!("minecraft-data/data");
@ -54,7 +53,7 @@ pub fn get_version_specific_file(version: &Version, filename: &str) -> DataResul
/// Returns the data path for a given file /// Returns the data path for a given file
pub fn get_path(version: &Version, filename: &str) -> DataResult<String> { pub fn get_path(version: &Version, filename: &str) -> DataResult<String> {
lazy_static::lazy_static! { lazy_static::lazy_static! {
static ref PATHS: Datapaths = getDatapaths().unwrap(); static ref PATHS: Datapaths = get_datapaths().unwrap();
}; };
PATHS PATHS
.pc .pc
@ -66,7 +65,7 @@ pub fn get_path(version: &Version, filename: &str) -> DataResult<String> {
} }
/// Returns the parsed data paths /// Returns the parsed data paths
fn getDatapaths() -> DataResult<Datapaths> { fn get_datapaths() -> DataResult<Datapaths> {
let content = MINECRAFT_DATA let content = MINECRAFT_DATA
.get_file("dataPaths.json") .get_file("dataPaths.json")
.ok_or(DataError::NotFoundError("dataPaths.json".to_string()))? .ok_or(DataError::NotFoundError("dataPaths.json".to_string()))?

@ -1,9 +1,6 @@
#[macro_use] #[macro_use]
extern crate serde_derive; extern crate serde_derive;
#[macro_use]
extern crate include_dir;
pub mod api; pub mod api;
pub(crate) mod data; pub(crate) mod data;
pub mod models; pub mod models;

@ -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 enchantment;
pub mod entity_loot;
pub mod item; pub mod item;
pub mod recipe; pub mod recipe;
pub mod version; pub mod version;

Loading…
Cancel
Save