Add collision shapes and fix latest version

Signed-off-by: trivernis <trivernis@protonmail.com>
pull/6/head
trivernis 3 years ago
parent aa151453de
commit c2b1363560
Signed by: Trivernis
GPG Key ID: DFFFCC2C7A02DB45

@ -1,6 +1,6 @@
[package] [package]
name = "minecraft-data-rs" name = "minecraft-data-rs"
version = "0.4.3" version = "0.4.4"
authors = ["trivernis <trivernis@protonmail.com>"] authors = ["trivernis <trivernis@protonmail.com>"]
edition = "2018" edition = "2018"
readme = "README.md" readme = "README.md"
@ -12,10 +12,9 @@ repository = "https://github.com/Trivernis/minecraft-data-rs"
[dependencies] [dependencies]
thiserror = "1.0.30" thiserror = "1.0.30"
serde_json = "1.0.74" serde_json = "1.0.79"
serde_derive = "1.0.133" serde_derive = "1.0.136"
serde = "1.0.133" serde = "1.0.136"
include_dir = "0.7.2" include_dir = "0.7.2"
itertools = "0.10.3" itertools = "0.10.3"
lazy_static = "1.4.0" lazy_static = "1.4.0"
schemafy = "0.6.0"

@ -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::Block;
use crate::models::block_collision_shapes::BlockCollisionShapes;
use crate::models::version::Version; use crate::models::version::Version;
use crate::DataResult; use crate::DataResult;
use std::collections::HashMap; use std::collections::HashMap;
@ -38,4 +39,12 @@ impl Blocks {
Ok(blocks_map) Ok(blocks_map)
} }
/// Returns the block collision shapes object
pub fn block_collision_shapes(&self) -> DataResult<BlockCollisionShapes> {
let content = get_version_specific_file(&self.version, BLOCK_COLLISION_SHAPES_FILE)?;
let collision_shapes = serde_json::from_str(&content)?;
Ok(collision_shapes)
}
} }

@ -14,18 +14,29 @@ use std::sync::Arc;
#[cfg(test)] #[cfg(test)]
mod tests; mod tests;
pub mod biomes; mod biomes;
pub mod blocks; mod blocks;
pub mod enchantments; mod enchantments;
pub mod entities; mod entities;
pub mod foods; mod foods;
pub mod items; mod items;
pub mod loot; mod loot;
pub mod recipes; mod recipes;
pub mod versions; 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 /// A type wrapping access to all the metadata
/// about the selected minecraft version /// about the selected minecraft version
#[allow(missing_docs)]
pub struct Api { pub struct Api {
pub version: Arc<Version>, pub version: Arc<Version>,
pub items: Items, pub items: Items,

@ -1,4 +1,5 @@
use crate::api::tests::{get_api, get_test_versions}; use crate::api::tests::{get_api, get_test_versions};
use crate::models::block_collision_shapes::CollisionShapeIds;
#[test] #[test]
pub fn test_blocks_array() { 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());
}
}
}
}
}
}

@ -24,12 +24,13 @@ pub fn versions_by_minecraft_version() -> DataResult<HashMap<String, Version>> {
Ok(indexed_versions) 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<Version> { pub fn latest_stable() -> DataResult<Version> {
let latest = versions()? let latest = available_versions()?
.into_iter() .into_iter()
.filter_map(|v| { .filter_map(|v| {
let version_string = v.minecraft_version.clone(); let version_string = v.clone();
let mut parts = version_string.split("."); let mut parts = version_string.split(".");
Some(( Some((
@ -43,6 +44,7 @@ pub fn latest_stable() -> DataResult<Version> {
format!("{:#05}.{:#05}.{:#05}", maj, min, patch.unwrap_or(0)) format!("{:#05}.{:#05}.{:#05}", maj, min, patch.unwrap_or(0))
}) })
.map(|(v, _, _, _)| v) .map(|(v, _, _, _)| v)
.filter_map(|v| versions_by_minecraft_version().ok()?.remove(&v))
.rev() .rev()
.next(); .next();

@ -10,6 +10,7 @@ pub static MINECRAFT_DATA: Dir = include_dir::include_dir!("minecraft-data/data"
pub static BIOMES_FILE: &str = "biomes"; pub static BIOMES_FILE: &str = "biomes";
pub static BLOCK_LOOT_FILE: &str = "blockLoot"; pub static BLOCK_LOOT_FILE: &str = "blockLoot";
pub static BLOCKS_FILE: &str = "blocks"; pub static BLOCKS_FILE: &str = "blocks";
pub static BLOCK_COLLISION_SHAPES_FILE: &str = "blockCollisionShapes";
#[allow(unused)] #[allow(unused)]
pub static COMMANDS_FILE: &str = "commands"; pub static COMMANDS_FILE: &str = "commands";
pub static ENTITIES_FILE: &str = "entities"; pub static ENTITIES_FILE: &str = "entities";

@ -20,8 +20,10 @@
#[macro_use] #[macro_use]
extern crate serde_derive; extern crate serde_derive;
/// Provides data access methods
pub mod api; pub mod api;
pub(crate) mod data; pub(crate) mod data;
/// Contains the type definitions for the data
pub mod models; pub mod models;
pub(crate) mod utils; pub(crate) mod utils;

@ -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<String, CollisionShapeIds>,
pub shapes: HashMap<u16, CollisionShape>,
}
#[derive(Deserialize, Debug, Clone)]
#[serde(
rename_all(deserialize = "camelCase", serialize = "snake_case"),
untagged
)]
pub enum CollisionShapeIds {
Value(u16),
Array(Vec<u16>),
}
pub type CollisionShape = Vec<Vec<f32>>;

@ -1,10 +1,11 @@
pub mod biome; pub mod biome;
pub mod block; pub mod block;
pub mod block_collision_shapes;
pub mod block_loot; pub mod block_loot;
pub mod enchantment; pub mod enchantment;
pub mod entity;
pub mod entity_loot; pub mod entity_loot;
pub mod food; pub mod food;
pub mod item; pub mod item;
pub mod recipe; pub mod recipe;
pub mod version; pub mod version;
pub mod entity;

@ -1,7 +1,11 @@
/// A type wrapping a minecraft version
#[derive(Deserialize, Debug, Clone)] #[derive(Deserialize, Debug, Clone)]
#[serde(rename_all(deserialize = "camelCase", serialize = "snake_case"))] #[serde(rename_all(deserialize = "camelCase", serialize = "snake_case"))]
pub struct Version { pub struct Version {
/// API Version
pub version: i32, pub version: i32,
/// The full minecraft version (e.g. (`1.8.1`)
pub minecraft_version: String, pub minecraft_version: String,
/// The major version of this minecraft version (e.g. `1.8`)
pub major_version: String, pub major_version: String,
} }

@ -1,9 +1,11 @@
use std::io; use std::io;
use thiserror::Error; use thiserror::Error;
#[allow(missing_docs)]
pub type DataResult<T> = Result<T, DataError>; pub type DataResult<T> = Result<T, DataError>;
#[derive(Error, Debug)] #[derive(Error, Debug)]
#[allow(missing_docs)]
pub enum DataError { pub enum DataError {
#[error("IO Error: {0}")] #[error("IO Error: {0}")]
IOError(#[from] io::Error), IOError(#[from] io::Error),

Loading…
Cancel
Save