parent
72c9d913df
commit
f710ca318c
@ -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)
|
||||
}
|
||||
}
|
@ -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());
|
||||
}
|
||||
}
|
@ -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>,
|
||||
}
|
Loading…
Reference in New Issue