From b08d3d55d7351eff6e27a09d732078c038539958 Mon Sep 17 00:00:00 2001 From: mat Date: Sun, 2 Feb 2025 21:15:45 +0000 Subject: start implementing data driven registries --- azalea-core/src/data_registry.rs | 27 +++++++++++++++++++++++++++ azalea-core/src/lib.rs | 1 + azalea-core/src/registry_holder.rs | 7 ++++--- 3 files changed, 32 insertions(+), 3 deletions(-) create mode 100644 azalea-core/src/data_registry.rs (limited to 'azalea-core/src') diff --git a/azalea-core/src/data_registry.rs b/azalea-core/src/data_registry.rs new file mode 100644 index 00000000..a72d9caf --- /dev/null +++ b/azalea-core/src/data_registry.rs @@ -0,0 +1,27 @@ +use std::str::FromStr; + +use azalea_registry::DataRegistry; +use simdnbt::owned::NbtCompound; + +use crate::{registry_holder::RegistryHolder, resource_location::ResourceLocation}; + +pub trait ResolvableDataRegistry: DataRegistry { + fn resolve_name(&self, registries: &RegistryHolder) -> Option { + self.resolve(registries).map(|(name, _)| name.clone()) + } + fn resolve<'a>( + &self, + registries: &'a RegistryHolder, + ) -> Option<(&'a ResourceLocation, &'a NbtCompound)> { + let name_resourcelocation = ResourceLocation::from_str(Self::NAME).unwrap_or_else(|_| { + panic!( + "Name for registry should be a valid ResourceLocation: {}", + Self::NAME + ) + }); + let registry_values = registries.map.get(&name_resourcelocation)?; + let resolved = registry_values.get_index(self.protocol_id() as usize)?; + Some(resolved) + } +} +impl ResolvableDataRegistry for T {} diff --git a/azalea-core/src/lib.rs b/azalea-core/src/lib.rs index 04422146..4a547252 100755 --- a/azalea-core/src/lib.rs +++ b/azalea-core/src/lib.rs @@ -7,6 +7,7 @@ pub mod bitset; pub mod block_hit_result; pub mod color; pub mod cursor3d; +pub mod data_registry; pub mod delta; pub mod difficulty; pub mod direction; diff --git a/azalea-core/src/registry_holder.rs b/azalea-core/src/registry_holder.rs index 41cf0d05..ec57eacc 100644 --- a/azalea-core/src/registry_holder.rs +++ b/azalea-core/src/registry_holder.rs @@ -7,6 +7,7 @@ use std::{collections::HashMap, io::Cursor}; +use indexmap::IndexMap; use simdnbt::{ owned::{NbtCompound, NbtTag}, Deserialize, FromNbtTag, Serialize, ToNbtTag, @@ -20,21 +21,21 @@ use crate::resource_location::ResourceLocation; /// This is the registry that is sent to the client upon login. #[derive(Default, Debug, Clone)] pub struct RegistryHolder { - pub map: HashMap>, + pub map: HashMap>, } impl RegistryHolder { pub fn append( &mut self, id: ResourceLocation, - entries: HashMap>, + entries: Vec<(ResourceLocation, Option)>, ) { let map = self.map.entry(id).or_default(); for (key, value) in entries { if let Some(value) = value { map.insert(key, value); } else { - map.remove(&key); + map.shift_remove(&key); } } } -- cgit v1.2.3