Files
Pumpkin/pumpkin-world/src/global_registry.rs
T
2024-08-20 13:23:32 +02:00

49 lines
1.3 KiB
Rust

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>,
pub entries: HashMap<String, HashMap<String, u32>>,
}
lazy_static! {
pub static ref REGISTRY: HashMap<String, RegistryElement> =
serde_json::from_str(REGISTRY_JSON).expect("Could not parse registry.json registry.");
}
pub fn get_protocol_id(category: &str, entry: &str) -> u32 {
*REGISTRY
.get(category)
.expect("Invalid Category in registry")
.entries
.get(entry)
.map(|p| p.get("protocol_id").unwrap())
.expect("No Entry found")
}
#[allow(dead_code)]
pub fn get_default<'a>(category: &str) -> Option<&'a str> {
REGISTRY
.get(category)
.expect("Invalid Category in registry")
.default
.as_deref()
}
pub fn find_minecraft_id(category: &str, protocol_id: u32) -> Option<&str> {
REGISTRY
.get(category)?
.entries
.iter()
.find(|(_, other_protocol_id)| {
*other_protocol_id.get("protocol_id").unwrap() == protocol_id
})
.map(|(id, _)| id.as_str())
}