Add entities api

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

@ -0,0 +1,41 @@
use crate::data::{get_version_specific_file, ENTITIES_FILE};
use crate::models::entity::Entity;
use crate::models::version::Version;
use crate::DataResult;
use std::collections::HashMap;
use std::iter::FromIterator;
use std::sync::Arc;
pub struct Entities {
version: Arc<Version>,
}
impl Entities {
pub fn new(version: Arc<Version>) -> Self {
Self { version }
}
/// Returns an unordered array of entities
pub fn entities_array(&self) -> DataResult<Vec<Entity>> {
let content = get_version_specific_file(&self.version, ENTITIES_FILE)?;
let entities = serde_json::from_str(&content)?;
Ok(entities)
}
/// Returns entities indexed by name
pub fn entities_by_name(&self) -> DataResult<HashMap<String, Entity>> {
let entities = self.entities_array()?;
let entities_map = HashMap::from_iter(entities.into_iter().map(|e| (e.name.clone(), e)));
Ok(entities_map)
}
/// Returns entities indexed by id
pub fn entities(&self) -> DataResult<HashMap<u32, Entity>> {
let entities = self.entities_array()?;
let entities_map = HashMap::from_iter(entities.into_iter().map(|e| (e.id, e)));
Ok(entities_map)
}
}

@ -1,6 +1,7 @@
use crate::api::biomes::Biomes; use crate::api::biomes::Biomes;
use crate::api::blocks::Blocks; use crate::api::blocks::Blocks;
use crate::api::enchantments::Enchantments; use crate::api::enchantments::Enchantments;
use crate::api::entities::Entities;
use crate::api::foods::Foods; use crate::api::foods::Foods;
use crate::api::items::Items; use crate::api::items::Items;
use crate::api::loot::Loot; use crate::api::loot::Loot;
@ -14,6 +15,7 @@ mod tests;
pub mod biomes; pub mod biomes;
pub mod blocks; pub mod blocks;
pub mod enchantments; pub mod enchantments;
pub mod entities;
pub mod foods; pub mod foods;
pub mod items; pub mod items;
pub mod loot; pub mod loot;
@ -29,6 +31,7 @@ pub struct Api {
pub blocks: Blocks, pub blocks: Blocks,
pub foods: Foods, pub foods: Foods,
pub biomes: Biomes, pub biomes: Biomes,
pub entities: Entities,
} }
impl Api { impl Api {
@ -43,6 +46,7 @@ impl Api {
blocks: Blocks::new(Arc::clone(&version)), blocks: Blocks::new(Arc::clone(&version)),
foods: Foods::new(Arc::clone(&version)), foods: Foods::new(Arc::clone(&version)),
biomes: Biomes::new(Arc::clone(&version)), biomes: Biomes::new(Arc::clone(&version)),
entities: Entities::new(Arc::clone(&version)),
} }
} }
} }

@ -0,0 +1,35 @@
use crate::api::tests::{get_api, get_test_versions};
#[test]
pub fn test_blocks_array() {
let versions = get_test_versions();
for version in versions {
let api = get_api(version);
assert_ne!(api.entities.entities_array().unwrap().len(), 0)
}
}
#[test]
pub fn test_entities_by_name() {
let versions = get_test_versions();
for version in versions {
let api = get_api(version);
let by_name = api.entities.entities_by_name().unwrap();
assert!(by_name.get("cow").is_some());
assert!(by_name.get("armor_stand").is_some());
}
}
#[test]
pub fn test_entities_by_id() {
let versions = get_test_versions();
for version in versions {
let api = get_api(version);
let by_name = api.entities.entities().unwrap();
assert!(by_name.get(&1).is_some());
assert!(by_name.get(&5).is_some());
}
}

@ -5,6 +5,7 @@ use crate::models::version::Version;
mod biomes; mod biomes;
mod blocks; mod blocks;
mod enchantments; mod enchantments;
mod entities;
mod foods; mod foods;
mod items; mod items;
mod loot; mod loot;

@ -0,0 +1,13 @@
#[derive(Deserialize, Debug, Clone)]
#[serde(rename_all(deserialize = "camelCase", serialize = "snake_case"))]
pub struct Entity {
pub id: u32,
pub internal_id: Option<u32>,
pub display_name: String,
pub name: String,
#[serde(alias = "type")]
pub entity_type: String,
pub width: Option<f32>,
pub height: Option<f32>,
pub category: Option<String>,
}

@ -7,3 +7,4 @@ pub mod food;
pub mod item; pub mod item;
pub mod recipe; pub mod recipe;
pub mod version; pub mod version;
pub mod entity;

Loading…
Cancel
Save