diff options
| author | mat <git@matdoes.dev> | 2025-02-02 21:15:45 +0000 |
|---|---|---|
| committer | mat <git@matdoes.dev> | 2025-02-02 21:15:45 +0000 |
| commit | b08d3d55d7351eff6e27a09d732078c038539958 (patch) | |
| tree | 3d511170be7d46bff418296245f62ebd18065453 /azalea-core/src | |
| parent | cdb68dfb702f442135dadedc53ce29b1cc2b9c14 (diff) | |
| download | azalea-drasl-b08d3d55d7351eff6e27a09d732078c038539958.tar.xz | |
start implementing data driven registries
Diffstat (limited to 'azalea-core/src')
| -rw-r--r-- | azalea-core/src/data_registry.rs | 27 | ||||
| -rwxr-xr-x | azalea-core/src/lib.rs | 1 | ||||
| -rw-r--r-- | azalea-core/src/registry_holder.rs | 7 |
3 files changed, 32 insertions, 3 deletions
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<ResourceLocation> { + 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<T: DataRegistry> 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<ResourceLocation, HashMap<ResourceLocation, NbtCompound>>, + pub map: HashMap<ResourceLocation, IndexMap<ResourceLocation, NbtCompound>>, } impl RegistryHolder { pub fn append( &mut self, id: ResourceLocation, - entries: HashMap<ResourceLocation, Option<NbtCompound>>, + entries: Vec<(ResourceLocation, Option<NbtCompound>)>, ) { 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); } } } |
