1
2
Fork 0
mirror of https://github.com/mat-1/azalea.git synced 2025-08-02 06:16:04 +00:00
azalea/azalea-core/src/data_registry.rs
mat 833f306e8b
Fix errors on switching dimensions (#204)
* Fix errors on switching dimensions

* fix other tests

* clippy

* fix log feature in test_simulation

* fix chunks oops
2025-02-21 13:43:56 -06:00

47 lines
1.7 KiB
Rust

use std::{io::Cursor, 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)
}
fn resolve_and_deserialize<T: simdnbt::Deserialize>(
&self,
registries: &RegistryHolder,
) -> Option<Result<(ResourceLocation, T), simdnbt::DeserializeError>> {
let (name, value) = self.resolve(registries)?;
let mut nbt_bytes = Vec::new();
value.write(&mut nbt_bytes);
let nbt_borrow_compound =
simdnbt::borrow::read_compound(&mut Cursor::new(&nbt_bytes)).ok()?;
let value = match T::from_compound((&nbt_borrow_compound).into()) {
Ok(value) => value,
Err(err) => {
return Some(Err(err));
}
};
Some(Ok((name.clone(), value)))
}
}
impl<T: DataRegistry> ResolvableDataRegistry for T {}