From c2b13635601f11c04a374cd25a3aba2cb636c92a Mon Sep 17 00:00:00 2001 From: trivernis Date: Sat, 19 Feb 2022 11:58:26 +0100 Subject: [PATCH] Add collision shapes and fix latest version Signed-off-by: trivernis --- Cargo.toml | 9 ++++----- src/api/blocks.rs | 11 ++++++++++- src/api/mod.rs | 29 +++++++++++++++++++--------- src/api/tests/blocks.rs | 22 +++++++++++++++++++++ src/api/versions.rs | 8 +++++--- src/data/mod.rs | 1 + src/lib.rs | 2 ++ src/models/block_collision_shapes.rs | 20 +++++++++++++++++++ src/models/mod.rs | 3 ++- src/models/version.rs | 4 ++++ src/utils/error.rs | 2 ++ 11 files changed, 92 insertions(+), 19 deletions(-) create mode 100644 src/models/block_collision_shapes.rs diff --git a/Cargo.toml b/Cargo.toml index 252c0cb..3267fc8 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -1,6 +1,6 @@ [package] name = "minecraft-data-rs" -version = "0.4.3" +version = "0.4.4" authors = ["trivernis "] edition = "2018" readme = "README.md" @@ -12,10 +12,9 @@ repository = "https://github.com/Trivernis/minecraft-data-rs" [dependencies] thiserror = "1.0.30" -serde_json = "1.0.74" -serde_derive = "1.0.133" -serde = "1.0.133" +serde_json = "1.0.79" +serde_derive = "1.0.136" +serde = "1.0.136" include_dir = "0.7.2" itertools = "0.10.3" lazy_static = "1.4.0" -schemafy = "0.6.0" \ No newline at end of file diff --git a/src/api/blocks.rs b/src/api/blocks.rs index 21252cd..45c189f 100644 --- a/src/api/blocks.rs +++ b/src/api/blocks.rs @@ -1,5 +1,6 @@ -use crate::data::{get_version_specific_file, BLOCKS_FILE}; +use crate::data::{get_version_specific_file, BLOCKS_FILE, BLOCK_COLLISION_SHAPES_FILE}; use crate::models::block::Block; +use crate::models::block_collision_shapes::BlockCollisionShapes; use crate::models::version::Version; use crate::DataResult; use std::collections::HashMap; @@ -38,4 +39,12 @@ impl Blocks { Ok(blocks_map) } + + /// Returns the block collision shapes object + pub fn block_collision_shapes(&self) -> DataResult { + let content = get_version_specific_file(&self.version, BLOCK_COLLISION_SHAPES_FILE)?; + let collision_shapes = serde_json::from_str(&content)?; + + Ok(collision_shapes) + } } diff --git a/src/api/mod.rs b/src/api/mod.rs index 5bc230c..9d3ddcd 100644 --- a/src/api/mod.rs +++ b/src/api/mod.rs @@ -14,18 +14,29 @@ use std::sync::Arc; #[cfg(test)] mod tests; -pub mod biomes; -pub mod blocks; -pub mod enchantments; -pub mod entities; -pub mod foods; -pub mod items; -pub mod loot; -pub mod recipes; -pub mod versions; +mod biomes; +mod blocks; +mod enchantments; +mod entities; +mod foods; +mod items; +mod loot; +mod recipes; +mod versions; + +pub use biomes::*; +pub use blocks::*; +pub use enchantments::*; +pub use entities::*; +pub use foods::*; +pub use items::*; +pub use loot::*; +pub use recipes::*; +pub use versions::*; /// A type wrapping access to all the metadata /// about the selected minecraft version +#[allow(missing_docs)] pub struct Api { pub version: Arc, pub items: Items, diff --git a/src/api/tests/blocks.rs b/src/api/tests/blocks.rs index 74a5efc..5570dbe 100644 --- a/src/api/tests/blocks.rs +++ b/src/api/tests/blocks.rs @@ -1,4 +1,5 @@ use crate::api::tests::{get_api, get_test_versions}; +use crate::models::block_collision_shapes::CollisionShapeIds; #[test] pub fn test_blocks_array() { @@ -56,3 +57,24 @@ pub fn test_block_states() { } } } + +#[test] +pub fn test_block_collision_states() { + for version in get_test_versions() { + let api = get_api(version); + let shapes = api.blocks.block_collision_shapes().unwrap(); + + for (_block, ids) in shapes.blocks { + match ids { + CollisionShapeIds::Value(id) => { + assert!(shapes.shapes.get(&id).is_some()); + } + CollisionShapeIds::Array(ids) => { + for id in ids { + assert!(shapes.shapes.get(&id).is_some()); + } + } + } + } + } +} diff --git a/src/api/versions.rs b/src/api/versions.rs index 756b28b..61df53d 100644 --- a/src/api/versions.rs +++ b/src/api/versions.rs @@ -24,12 +24,13 @@ pub fn versions_by_minecraft_version() -> DataResult> { Ok(indexed_versions) } -/// Returns the latest stable version (hardcoded at the moment) +/// Returns the latest stable version for which data paths exists. +/// Patch versions using the same data path as the major version are ignored. pub fn latest_stable() -> DataResult { - let latest = versions()? + let latest = available_versions()? .into_iter() .filter_map(|v| { - let version_string = v.minecraft_version.clone(); + let version_string = v.clone(); let mut parts = version_string.split("."); Some(( @@ -43,6 +44,7 @@ pub fn latest_stable() -> DataResult { format!("{:#05}.{:#05}.{:#05}", maj, min, patch.unwrap_or(0)) }) .map(|(v, _, _, _)| v) + .filter_map(|v| versions_by_minecraft_version().ok()?.remove(&v)) .rev() .next(); diff --git a/src/data/mod.rs b/src/data/mod.rs index 5297c15..b686921 100644 --- a/src/data/mod.rs +++ b/src/data/mod.rs @@ -10,6 +10,7 @@ pub static MINECRAFT_DATA: Dir = include_dir::include_dir!("minecraft-data/data" pub static BIOMES_FILE: &str = "biomes"; pub static BLOCK_LOOT_FILE: &str = "blockLoot"; pub static BLOCKS_FILE: &str = "blocks"; +pub static BLOCK_COLLISION_SHAPES_FILE: &str = "blockCollisionShapes"; #[allow(unused)] pub static COMMANDS_FILE: &str = "commands"; pub static ENTITIES_FILE: &str = "entities"; diff --git a/src/lib.rs b/src/lib.rs index c0948d0..19bb012 100644 --- a/src/lib.rs +++ b/src/lib.rs @@ -20,8 +20,10 @@ #[macro_use] extern crate serde_derive; +/// Provides data access methods pub mod api; pub(crate) mod data; +/// Contains the type definitions for the data pub mod models; pub(crate) mod utils; diff --git a/src/models/block_collision_shapes.rs b/src/models/block_collision_shapes.rs new file mode 100644 index 0000000..2a372bc --- /dev/null +++ b/src/models/block_collision_shapes.rs @@ -0,0 +1,20 @@ +use std::collections::HashMap; + +#[derive(Deserialize, Debug, Clone)] +#[serde(rename_all(deserialize = "camelCase", serialize = "snake_case"))] +pub struct BlockCollisionShapes { + pub blocks: HashMap, + pub shapes: HashMap, +} + +#[derive(Deserialize, Debug, Clone)] +#[serde( + rename_all(deserialize = "camelCase", serialize = "snake_case"), + untagged +)] +pub enum CollisionShapeIds { + Value(u16), + Array(Vec), +} + +pub type CollisionShape = Vec>; diff --git a/src/models/mod.rs b/src/models/mod.rs index 88c47a3..5e61955 100644 --- a/src/models/mod.rs +++ b/src/models/mod.rs @@ -1,10 +1,11 @@ pub mod biome; pub mod block; +pub mod block_collision_shapes; pub mod block_loot; pub mod enchantment; +pub mod entity; pub mod entity_loot; pub mod food; pub mod item; pub mod recipe; pub mod version; -pub mod entity; diff --git a/src/models/version.rs b/src/models/version.rs index 2bc7118..7bc42a8 100644 --- a/src/models/version.rs +++ b/src/models/version.rs @@ -1,7 +1,11 @@ +/// A type wrapping a minecraft version #[derive(Deserialize, Debug, Clone)] #[serde(rename_all(deserialize = "camelCase", serialize = "snake_case"))] pub struct Version { + /// API Version pub version: i32, + /// The full minecraft version (e.g. (`1.8.1`) pub minecraft_version: String, + /// The major version of this minecraft version (e.g. `1.8`) pub major_version: String, } diff --git a/src/utils/error.rs b/src/utils/error.rs index 4735202..497b972 100644 --- a/src/utils/error.rs +++ b/src/utils/error.rs @@ -1,9 +1,11 @@ use std::io; use thiserror::Error; +#[allow(missing_docs)] pub type DataResult = Result; #[derive(Error, Debug)] +#[allow(missing_docs)] pub enum DataError { #[error("IO Error: {0}")] IOError(#[from] io::Error),