Add Item registry

This commit is contained in:
Snowiiii
2024-08-17 13:00:35 +02:00
parent 9d3ee1de9b
commit d6fb4ed121
6 changed files with 38127 additions and 0 deletions

File diff suppressed because it is too large Load Diff

File diff suppressed because it is too large Load Diff

View File

@@ -0,0 +1,35 @@
use std::collections::HashMap;
use lazy_static::lazy_static;
pub const ITEM_REGISTRY: &str = "minecraft:item";
const REGISTRY_JSON: &str = include_str!("../assets/registries.json");
#[derive(serde::Deserialize, Debug, Clone, PartialEq, Eq)]
pub struct RegistryElement {
default: Option<String>,
entries: HashMap<String, u32>,
}
lazy_static! {
static ref REGISTRY: HashMap<String, RegistryElement> =
serde_json::from_str(REGISTRY_JSON).expect("Could not parse items.json registry.");
}
pub fn get_protocol_id(category: &str, entry: &str) -> u32 {
*REGISTRY
.get(category)
.expect("Invalid Category in registry")
.entries
.get(entry)
.expect("No Entry found")
}
pub fn get_default<'a>(category: &str) -> Option<&'a str> {
REGISTRY
.get(category)
.expect("Invalid Category in registry")
.default
.as_deref()
}

View File

@@ -0,0 +1,41 @@
use std::collections::HashMap;
use lazy_static::lazy_static;
use crate::global_registry::{self, ITEM_REGISTRY};
use super::Raritiy;
const ITEMS_JSON: &str = include_str!("../../assets/items.json");
#[derive(serde::Deserialize, Debug, Clone, PartialEq, Eq)]
pub struct ItemComponents {
// TODO: attribute_modifiers
// TODO: enchantments: HashMap<>
#[serde(rename = "minecraft:lore")]
lore: Vec<String>,
#[serde(rename = "minecraft:max_stack_size")]
max_stack_size: u32,
#[serde(rename = "minecraft:rarity")]
rarity: Raritiy,
#[serde(rename = "minecraft:repair_cost")]
repair_cost: u32,
}
#[derive(serde::Deserialize, Debug, Clone, PartialEq, Eq)]
struct ItemElement {
components: ItemComponents,
}
lazy_static! {
static ref ITEMS: HashMap<String, ItemElement> =
serde_json::from_str(ITEMS_JSON).expect("Could not parse items.json registry.");
}
pub fn get_item_element(item_id: &str) -> &ItemComponents {
&ITEMS.get(item_id).expect("Item not found").components
}
pub fn get_item_protocol_id(item_id: &str) -> u32 {
global_registry::get_protocol_id(ITEM_REGISTRY, item_id)
}

View File

@@ -0,0 +1,11 @@
mod item_registry;
#[derive(serde::Deserialize, Debug, Clone, PartialEq, Eq)]
#[serde(rename_all = "lowercase")]
/// Item Raritiy
pub enum Raritiy {
Common,
UnCommon,
Rare,
Epic,
}

View File

@@ -4,5 +4,7 @@ pub const WORLD_HEIGHT: usize = 384;
pub const WORLD_Y_START_AT: i32 = -64;
pub const DIRECT_PALETTE_BITS: u32 = 15;
mod block;
mod global_registry;
mod item;
pub mod radial_chunk_iterator;
mod world;