parent
273902873d
commit
b815a9b06b
@ -0,0 +1,41 @@
|
||||
use crate::data::{get_version_specific_file, BLOCKS_FILE};
|
||||
use crate::models::block::Block;
|
||||
use crate::models::version::Version;
|
||||
use crate::DataResult;
|
||||
use std::collections::HashMap;
|
||||
use std::iter::FromIterator;
|
||||
use std::sync::Arc;
|
||||
|
||||
pub struct Blocks {
|
||||
version: Arc<Version>,
|
||||
}
|
||||
|
||||
impl Blocks {
|
||||
pub fn new(version: Arc<Version>) -> Self {
|
||||
Self { version }
|
||||
}
|
||||
|
||||
/// Returns the list of blocks
|
||||
pub fn blocks_array(&self) -> DataResult<Vec<Block>> {
|
||||
let content = get_version_specific_file(&self.version, BLOCKS_FILE)?;
|
||||
let blocks = serde_json::from_str::<Vec<Block>>(&content)?;
|
||||
|
||||
Ok(blocks)
|
||||
}
|
||||
|
||||
/// Returns the blocks indexed by ID
|
||||
pub fn blocks(&self) -> DataResult<HashMap<u32, Block>> {
|
||||
let blocks = self.blocks_array()?;
|
||||
let blocks_map = HashMap::from_iter(blocks.into_iter().map(|b| (b.id, b)));
|
||||
|
||||
Ok(blocks_map)
|
||||
}
|
||||
|
||||
/// Returns the blocks indexed by name
|
||||
pub fn blocks_by_name(&self) -> DataResult<HashMap<String, Block>> {
|
||||
let blocks = self.blocks_array()?;
|
||||
let blocks_map = HashMap::from_iter(blocks.into_iter().map(|b| (b.name.clone(), b)));
|
||||
|
||||
Ok(blocks_map)
|
||||
}
|
||||
}
|
@ -0,0 +1,36 @@
|
||||
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.blocks.blocks_array().unwrap().len(), 0)
|
||||
}
|
||||
}
|
||||
|
||||
#[test]
|
||||
pub fn test_blocks_by_name() {
|
||||
let versions = get_test_versions();
|
||||
|
||||
for version in versions {
|
||||
let api = get_api(version);
|
||||
let by_name = api.blocks.blocks_by_name().unwrap();
|
||||
assert!(by_name.get("dirt").is_some());
|
||||
assert!(by_name.get("stone").is_some());
|
||||
assert_eq!(by_name.get("grass").unwrap().stack_size, 64)
|
||||
}
|
||||
}
|
||||
|
||||
#[test]
|
||||
pub fn test_blocks_by_id() {
|
||||
let versions = get_test_versions();
|
||||
|
||||
for version in versions {
|
||||
let api = get_api(version);
|
||||
let by_name = api.blocks.blocks().unwrap();
|
||||
assert!(by_name.get(&1).is_some());
|
||||
assert!(by_name.get(&5).is_some());
|
||||
}
|
||||
}
|
@ -0,0 +1,58 @@
|
||||
use std::collections::HashMap;
|
||||
|
||||
#[derive(Deserialize, Debug, Clone)]
|
||||
#[serde(rename_all(deserialize = "camelCase", serialize = "snake_case"))]
|
||||
pub struct Block {
|
||||
pub id: u32,
|
||||
pub display_name: String,
|
||||
pub name: String,
|
||||
pub hardness: Option<f32>,
|
||||
pub stack_size: u8,
|
||||
pub diggable: bool,
|
||||
pub bounding_box: BoundingBox,
|
||||
pub material: Option<String>,
|
||||
pub harvest_tool: Option<HashMap<u32, bool>>,
|
||||
pub variations: Option<Vec<Variation>>,
|
||||
pub drops: Vec<u32>,
|
||||
pub transparent: bool,
|
||||
pub emit_light: u8,
|
||||
pub filter_light: u8,
|
||||
pub min_state_id: Option<u32>,
|
||||
pub max_state_id: Option<u32>,
|
||||
pub default_state: Option<u32>,
|
||||
#[serde(alias = "resistance")]
|
||||
pub blast_resistance: Option<f32>,
|
||||
}
|
||||
|
||||
#[derive(Deserialize, Debug, Clone)]
|
||||
#[serde(rename_all(deserialize = "camelCase", serialize = "snake_case"))]
|
||||
pub enum BoundingBox {
|
||||
Block,
|
||||
Empty,
|
||||
}
|
||||
|
||||
#[derive(Deserialize, Debug, Clone)]
|
||||
#[serde(rename_all(deserialize = "camelCase", serialize = "snake_case"))]
|
||||
pub struct Variation {
|
||||
metadata: u32,
|
||||
display_name: String,
|
||||
description: Option<String>,
|
||||
}
|
||||
|
||||
#[derive(Deserialize, Debug, Clone)]
|
||||
#[serde(rename_all(deserialize = "camelCase", serialize = "snake_case"))]
|
||||
pub struct State {
|
||||
pub name: String,
|
||||
#[serde(alias = "type")]
|
||||
pub state_type: StateType,
|
||||
pub values: Option<Vec<String>>,
|
||||
pub num_values: u32,
|
||||
}
|
||||
|
||||
#[derive(Deserialize, Debug, Clone)]
|
||||
#[serde(rename_all(deserialize = "camelCase", serialize = "snake_case"))]
|
||||
pub enum StateType {
|
||||
Enum,
|
||||
Bool,
|
||||
Int,
|
||||
}
|
Loading…
Reference in New Issue