diff options
| author | mat <27899617+mat-1@users.noreply.github.com> | 2025-12-12 00:56:02 -0600 |
|---|---|---|
| committer | GitHub <noreply@github.com> | 2025-12-12 00:56:02 -0600 |
| commit | f9c25665c203d6377ace62f1e95381d037d8fd9e (patch) | |
| tree | 8b4131d20fe661d3cc1175ec27f801fe61df41ea /azalea-core/src | |
| parent | 82ad975242292d5875780b4398b62637674bf50a (diff) | |
| download | azalea-drasl-f9c25665c203d6377ace62f1e95381d037d8fd9e.tar.xz | |
Refactor azalea-registry (#294)
* move registries in azalea-registry into separate modules
* rename Item and Block to ItemKind and BlockKind
* remove 'extra' registries from azalea-registry
* hide deprecated items from docs
* use DamageKindKey instead of Identifier when parsing registries
* store tag entries as a Vec instead of a HashSet
* sort tag values by protocol id
* update changelog
Diffstat (limited to 'azalea-core/src')
| -rw-r--r-- | azalea-core/src/checksum.rs | 3 | ||||
| -rw-r--r-- | azalea-core/src/data_registry.rs | 52 | ||||
| -rw-r--r-- | azalea-core/src/identifier.rs | 184 | ||||
| -rw-r--r-- | azalea-core/src/lib.rs | 14 | ||||
| -rw-r--r-- | azalea-core/src/position.rs | 3 | ||||
| -rw-r--r-- | azalea-core/src/registry_holder/components.rs | 2 | ||||
| -rw-r--r-- | azalea-core/src/registry_holder/dimension_type.rs | 13 | ||||
| -rw-r--r-- | azalea-core/src/registry_holder/enchantment.rs | 2 | ||||
| -rw-r--r-- | azalea-core/src/registry_holder/entity_effect.rs | 14 | ||||
| -rw-r--r-- | azalea-core/src/registry_holder/float_provider.rs | 2 | ||||
| -rw-r--r-- | azalea-core/src/registry_holder/mod.rs | 8 | ||||
| -rw-r--r-- | azalea-core/src/registry_holder/value.rs | 8 | ||||
| -rw-r--r-- | azalea-core/src/sound.rs | 3 | ||||
| -rw-r--r-- | azalea-core/src/tier.rs | 6 |
14 files changed, 78 insertions, 236 deletions
diff --git a/azalea-core/src/checksum.rs b/azalea-core/src/checksum.rs index ac13fcc7..490d5ea4 100644 --- a/azalea-core/src/checksum.rs +++ b/azalea-core/src/checksum.rs @@ -1,12 +1,13 @@ use std::{cmp::Ordering, fmt, hash::Hasher}; use azalea_buf::AzBuf; +use azalea_registry::identifier::Identifier; use crc32c::Crc32cHasher; use serde::{Serialize, ser}; use thiserror::Error; use tracing::error; -use crate::{identifier::Identifier, registry_holder::RegistryHolder}; +use crate::registry_holder::RegistryHolder; #[derive(Debug, Clone, Copy, PartialEq, Eq, PartialOrd, Ord, Default, AzBuf)] pub struct Checksum(pub u32); diff --git a/azalea-core/src/data_registry.rs b/azalea-core/src/data_registry.rs index cf82772a..07837b19 100644 --- a/azalea-core/src/data_registry.rs +++ b/azalea-core/src/data_registry.rs @@ -1,16 +1,30 @@ -use azalea_registry::DataRegistry; -use simdnbt::owned::NbtCompound; - -use crate::{ +use azalea_registry::{ + DataRegistry, DataRegistryKey, DataRegistryKeyRef, + data::{self}, identifier::Identifier, - registry_holder::{self, RegistryDeserializesTo, RegistryHolder}, }; +use simdnbt::owned::NbtCompound; + +use crate::registry_holder::{self, RegistryDeserializesTo, RegistryHolder}; + +pub trait DataRegistryWithKey: DataRegistry { + fn key<'s, 'a: 's>( + &'s self, + registries: &'a RegistryHolder, + ) -> Option<<Self::Key as DataRegistryKey>::Borrow<'s>> { + registries + .protocol_id_to_identifier(Identifier::from(Self::NAME), self.protocol_id()) + .map(DataRegistryKeyRef::from_ident) + } +} +impl<R: DataRegistry> DataRegistryWithKey for R {} pub trait ResolvableDataRegistry: DataRegistry { type DeserializesTo: RegistryDeserializesTo; + #[doc(hidden)] + #[deprecated = "use `DataRegistryWithKey::key` instead."] fn resolve_name<'a>(&self, registries: &'a RegistryHolder) -> Option<&'a Identifier> { - // self.resolve(registries).map(|(name, _)| name.clone()) registries.protocol_id_to_identifier(Identifier::from(Self::NAME), self.protocol_id()) } @@ -42,20 +56,20 @@ macro_rules! define_default_deserializes_to { } define_deserializes_to! { - azalea_registry::DimensionType => registry_holder::dimension_type::DimensionTypeElement, - azalea_registry::Enchantment => registry_holder::enchantment::EnchantmentData, + data::DimensionKind => registry_holder::dimension_type::DimensionKindElement, + data::Enchantment => registry_holder::enchantment::EnchantmentData, } define_default_deserializes_to! { - azalea_registry::DamageKind, - azalea_registry::Dialog, - azalea_registry::WolfSoundVariant, - azalea_registry::CowVariant, - azalea_registry::ChickenVariant, - azalea_registry::FrogVariant, - azalea_registry::CatVariant, - azalea_registry::PigVariant, - azalea_registry::PaintingVariant, - azalea_registry::WolfVariant, - azalea_registry::Biome, + data::DamageKind, + data::Dialog, + data::WolfSoundVariant, + data::CowVariant, + data::ChickenVariant, + data::FrogVariant, + data::CatVariant, + data::PigVariant, + data::PaintingVariant, + data::WolfVariant, + data::Biome, } diff --git a/azalea-core/src/identifier.rs b/azalea-core/src/identifier.rs deleted file mode 100644 index a3c886c3..00000000 --- a/azalea-core/src/identifier.rs +++ /dev/null @@ -1,184 +0,0 @@ -//! An arbitrary identifier or resource location. - -use std::{ - fmt::{self, Debug, Display}, - io::{self, Cursor, Write}, - num::NonZeroUsize, - str::FromStr, -}; - -use azalea_buf::{AzaleaRead, AzaleaWrite, BufReadError}; -use serde::{Deserialize, Deserializer, Serialize, Serializer, de}; -use simdnbt::{FromNbtTag, ToNbtTag, owned::NbtTag}; - -/// An identifier, like `minecraft:stone` or `brigadier:number`. -/// -/// This was formerly called a `ResourceLocation`. -#[doc(alias = "ResourceLocation")] -#[derive(Hash, Clone, PartialEq, Eq, Default)] -pub struct Identifier { - // empty namespaces aren't allowed so NonZero is fine. - colon_index: Option<NonZeroUsize>, - inner: Box<str>, -} - -static DEFAULT_NAMESPACE: &str = "minecraft"; -// static REALMS_NAMESPACE: &str = "realms"; - -impl Identifier { - pub fn new(resource_string: impl Into<String>) -> Identifier { - let mut resource_string = resource_string.into(); - - let colon_index = resource_string.find(':'); - let colon_index = if let Some(colon_index) = colon_index { - if colon_index == 0 { - resource_string = resource_string.split_off(1); - } - NonZeroUsize::new(colon_index) - } else { - None - }; - - Self { - colon_index, - inner: resource_string.into(), - } - } - - pub fn namespace(&self) -> &str { - if let Some(colon_index) = self.colon_index { - &self.inner[0..colon_index.get()] - } else { - DEFAULT_NAMESPACE - } - } - pub fn path(&self) -> &str { - if let Some(colon_index) = self.colon_index { - &self.inner[(colon_index.get() + 1)..] - } else { - &self.inner - } - } -} - -impl Display for Identifier { - fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result { - if self.colon_index.is_some() { - write!(f, "{}", self.inner) - } else { - write!(f, "{DEFAULT_NAMESPACE}:{}", self.inner) - } - } -} -impl Debug for Identifier { - fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result { - write!(f, "{self}") - } -} -impl FromStr for Identifier { - type Err = &'static str; - - fn from_str(s: &str) -> Result<Self, Self::Err> { - Ok(Identifier::new(s)) - } -} -impl From<&str> for Identifier { - fn from(s: &str) -> Self { - Identifier::new(s) - } -} - -impl AzaleaRead for Identifier { - fn azalea_read(buf: &mut Cursor<&[u8]>) -> Result<Self, BufReadError> { - let location_string = String::azalea_read(buf)?; - Ok(Identifier::new(&location_string)) - } -} -impl AzaleaWrite for Identifier { - fn azalea_write(&self, buf: &mut impl Write) -> io::Result<()> { - self.to_string().azalea_write(buf) - } -} - -impl Serialize for Identifier { - fn serialize<S>(&self, serializer: S) -> Result<S::Ok, S::Error> - where - S: Serializer, - { - serializer.serialize_str(&self.to_string()) - } -} - -impl<'de> Deserialize<'de> for Identifier { - fn deserialize<D>(deserializer: D) -> Result<Self, D::Error> - where - D: Deserializer<'de>, - { - let s = String::deserialize(deserializer)?; - if s.contains(':') { - Ok(Identifier::new(&s)) - } else { - Err(de::Error::invalid_value( - de::Unexpected::Str(&s), - &"a valid Identifier", - )) - } - } -} - -impl FromNbtTag for Identifier { - fn from_nbt_tag(tag: simdnbt::borrow::NbtTag) -> Option<Self> { - tag.string().and_then(|s| s.to_str().parse().ok()) - } -} - -impl ToNbtTag for Identifier { - fn to_nbt_tag(self) -> NbtTag { - NbtTag::String(self.to_string().into()) - } -} - -#[cfg(test)] -mod tests { - use super::*; - - #[test] - fn basic_identifier() { - let r = Identifier::new("abcdef:ghijkl"); - assert_eq!(r.namespace(), "abcdef"); - assert_eq!(r.path(), "ghijkl"); - } - #[test] - fn no_namespace() { - let r = Identifier::new("azalea"); - assert_eq!(r.namespace(), "minecraft"); - assert_eq!(r.path(), "azalea"); - } - #[test] - fn colon_start() { - let r = Identifier::new(":azalea"); - assert_eq!(r.namespace(), "minecraft"); - assert_eq!(r.path(), "azalea"); - } - #[test] - fn colon_end() { - let r = Identifier::new("azalea:"); - assert_eq!(r.namespace(), "azalea"); - assert_eq!(r.path(), ""); - } - - #[test] - fn azbuf_identifier() { - let mut buf = Vec::new(); - Identifier::new("minecraft:dirt") - .azalea_write(&mut buf) - .unwrap(); - - let mut buf = Cursor::new(&buf[..]); - - assert_eq!( - Identifier::azalea_read(&mut buf).unwrap(), - Identifier::new("minecraft:dirt") - ); - } -} diff --git a/azalea-core/src/lib.rs b/azalea-core/src/lib.rs index 16b23c01..d61690aa 100644 --- a/azalea-core/src/lib.rs +++ b/azalea-core/src/lib.rs @@ -16,15 +16,21 @@ pub mod direction; pub mod filterable; pub mod game_type; pub mod hit_result; -pub mod identifier; pub mod math; pub mod objectives; pub mod position; pub mod registry_holder; +#[doc(hidden)] pub mod resource_location { - #![deprecated(note = "renamed to `identifier`.")] - #[deprecated(note = "renamed to `identifier::Identifier`.")] - pub type ResourceLocation = crate::identifier::Identifier; + #![deprecated(note = "moved to `azalea_registry::identifier`.")] + #[deprecated(note = "moved to `azalea_registry::identifier::Identifier`.")] + pub type ResourceLocation = azalea_registry::identifier::Identifier; +} +#[doc(hidden)] +pub mod identifier { + #![deprecated(note = "moved to `azalea_registry::identifier`.")] + #[deprecated(note = "moved to `azalea_registry::identifier::Identifier`.")] + pub type Identifier = azalea_registry::identifier::Identifier; } pub mod sound; #[cfg(feature = "bevy_ecs")] diff --git a/azalea-core/src/position.rs b/azalea-core/src/position.rs index 7cd86a03..1deb1892 100644 --- a/azalea-core/src/position.rs +++ b/azalea-core/src/position.rs @@ -13,10 +13,11 @@ use std::{ }; use azalea_buf::{AzBuf, AzaleaRead, AzaleaWrite, BufReadError}; +use azalea_registry::identifier::Identifier; use serde::{Serialize, Serializer}; use simdnbt::borrow::NbtTag; -use crate::{codec_utils::IntArray, direction::Direction, identifier::Identifier, math}; +use crate::{codec_utils::IntArray, direction::Direction, math}; macro_rules! vec3_impl { ($name:ident, $type:ty) => { diff --git a/azalea-core/src/registry_holder/components.rs b/azalea-core/src/registry_holder/components.rs index e7f94cd8..cbd6384d 100644 --- a/azalea-core/src/registry_holder/components.rs +++ b/azalea-core/src/registry_holder/components.rs @@ -1,6 +1,6 @@ use std::{any::Any, fmt::Debug, mem::ManuallyDrop, str::FromStr}; -use azalea_registry::{EnchantmentEffectComponentKind, SoundEvent}; +use azalea_registry::builtin::{EnchantmentEffectComponentKind, SoundEvent}; use simdnbt::{ DeserializeError, borrow::{NbtCompound, NbtList, NbtTag}, diff --git a/azalea-core/src/registry_holder/dimension_type.rs b/azalea-core/src/registry_holder/dimension_type.rs index ca158277..86dd2b3e 100644 --- a/azalea-core/src/registry_holder/dimension_type.rs +++ b/azalea-core/src/registry_holder/dimension_type.rs @@ -1,12 +1,13 @@ use std::collections::HashMap; use azalea_buf::AzBuf; +use azalea_registry::{builtin::SoundEvent, identifier::Identifier}; use simdnbt::{ Deserialize, FromNbtTag, Serialize, ToNbtTag, owned::{NbtCompound, NbtTag}, }; -use crate::{codec_utils::*, identifier::Identifier}; +use crate::codec_utils::*; #[derive(Debug, Clone, Serialize, Deserialize)] #[cfg_attr(feature = "strict_registry", simdnbt(deny_unknown_fields))] @@ -51,7 +52,7 @@ pub struct ChatTypeStyle { #[cfg(feature = "strict_registry")] #[derive(Debug, Clone, Serialize, Deserialize)] #[simdnbt(deny_unknown_fields)] -pub struct DimensionTypeElement { +pub struct DimensionKindElement { pub ambient_light: f32, pub bed_works: bool, pub coordinate_scale: f32, @@ -75,7 +76,7 @@ pub struct DimensionTypeElement { /// Dimension attributes. #[cfg(not(feature = "strict_registry"))] #[derive(Debug, Clone, Serialize, Deserialize)] -pub struct DimensionTypeElement { +pub struct DimensionKindElement { pub height: u32, pub min_y: i32, pub ultrawarm: Option<bool>, @@ -208,7 +209,7 @@ pub struct BiomeMusic { pub replace_current_music: bool, pub max_delay: u32, pub min_delay: u32, - pub sound: azalea_registry::SoundEvent, + pub sound: SoundEvent, } #[derive(Debug, Clone, Serialize, Deserialize)] @@ -217,14 +218,14 @@ pub struct BiomeMoodSound { pub tick_delay: u32, pub block_search_extent: u32, pub offset: f32, - pub sound: azalea_registry::SoundEvent, + pub sound: SoundEvent, } #[derive(Debug, Clone, Serialize, Deserialize)] #[cfg_attr(feature = "strict_registry", simdnbt(deny_unknown_fields))] pub struct AdditionsSound { pub tick_chance: f32, - pub sound: azalea_registry::SoundEvent, + pub sound: SoundEvent, } /// Biome particles. diff --git a/azalea-core/src/registry_holder/enchantment.rs b/azalea-core/src/registry_holder/enchantment.rs index 0f78843c..8d825baf 100644 --- a/azalea-core/src/registry_holder/enchantment.rs +++ b/azalea-core/src/registry_holder/enchantment.rs @@ -1,6 +1,6 @@ use std::{any::Any, fmt::Debug, str::FromStr}; -use azalea_registry::EnchantmentEffectComponentKind; +use azalea_registry::builtin::EnchantmentEffectComponentKind; use indexmap::IndexMap; use simdnbt::{DeserializeError, borrow::NbtCompound}; diff --git a/azalea-core/src/registry_holder/entity_effect.rs b/azalea-core/src/registry_holder/entity_effect.rs index 442b0e1a..ef08b7f0 100644 --- a/azalea-core/src/registry_holder/entity_effect.rs +++ b/azalea-core/src/registry_holder/entity_effect.rs @@ -1,7 +1,12 @@ use std::{collections::HashMap, str::FromStr}; use azalea_registry::{ - EnchantmentEntityEffectKind as EntityEffectKind, GameEvent, Holder, ParticleKind, SoundEvent, + Holder, + builtin::{ + EnchantmentEntityEffectKind as EntityEffectKind, GameEvent, ParticleKind, SoundEvent, + }, + data::DamageKindKey, + identifier::Identifier, }; use simdnbt::{ Deserialize, DeserializeError, @@ -9,7 +14,6 @@ use simdnbt::{ }; use crate::{ - identifier::Identifier, position::{Vec3, Vec3i}, registry_holder::{ block_predicate::BlockPredicate, block_state_provider::BlockStateProvider, @@ -129,17 +133,15 @@ pub struct ChangeItemDamage { pub struct DamageEntity { pub min_damage: LevelBasedValue, pub max_damage: LevelBasedValue, - // TODO: convert to a DamageKind after azalea-registry refactor #[simdnbt(rename = "damage_type")] - pub damage_kind: Identifier, + pub damage_kind: DamageKindKey, } #[derive(Debug, Clone, simdnbt::Deserialize)] pub struct Explode { pub attribute_to_user: Option<bool>, - // TODO: convert to a DamageKind after azalea-registry refactor #[simdnbt(rename = "damage_type")] - pub damage_kind: Option<Identifier>, + pub damage_kind: Option<DamageKindKey>, pub knockback_multiplier: Option<LevelBasedValue>, pub immune_blocks: Option<HomogeneousList>, pub offset: Option<Vec3>, diff --git a/azalea-core/src/registry_holder/float_provider.rs b/azalea-core/src/registry_holder/float_provider.rs index 6ce6b26d..c7d56bf9 100644 --- a/azalea-core/src/registry_holder/float_provider.rs +++ b/azalea-core/src/registry_holder/float_provider.rs @@ -1,6 +1,6 @@ use std::ops::Range; -use azalea_registry::FloatProviderKind; +use azalea_registry::builtin::FloatProviderKind; use simdnbt::{ DeserializeError, FromNbtTag, borrow::{NbtCompound, NbtTag}, diff --git a/azalea-core/src/registry_holder/mod.rs b/azalea-core/src/registry_holder/mod.rs index 269cf454..886c75e1 100644 --- a/azalea-core/src/registry_holder/mod.rs +++ b/azalea-core/src/registry_holder/mod.rs @@ -16,13 +16,12 @@ pub mod value; use std::{collections::HashMap, io::Cursor}; +use azalea_registry::identifier::Identifier; use indexmap::IndexMap; use simdnbt::{DeserializeError, FromNbtTag, borrow, owned::NbtCompound}; use thiserror::Error; use tracing::error; -use crate::identifier::Identifier; - /// The base of the registry. /// /// This is the registry that is sent to the client upon login. @@ -39,7 +38,7 @@ pub struct RegistryHolder { #[rustfmt::skip] // allow empty line /// Attributes about the dimension. - pub dimension_type: RegistryType<dimension_type::DimensionTypeElement>, + pub dimension_type: RegistryType<dimension_type::DimensionKindElement>, pub enchantment: RegistryType<enchantment::EnchantmentData>, @@ -93,7 +92,6 @@ macro_rules! registry_holder { ) -> Option<&Identifier> { let index = protocol_id as usize; - if registry.namespace() == "minecraft" { match registry.path() { $( @@ -189,7 +187,7 @@ impl RegistryDeserializesTo for NbtCompound { .get_index(protocol_id as usize) } } -impl RegistryDeserializesTo for dimension_type::DimensionTypeElement { +impl RegistryDeserializesTo for dimension_type::DimensionKindElement { fn get_for_registry<'a>( registries: &'a RegistryHolder, registry_name: &'static str, diff --git a/azalea-core/src/registry_holder/value.rs b/azalea-core/src/registry_holder/value.rs index ccd7f9ea..964d1e5a 100644 --- a/azalea-core/src/registry_holder/value.rs +++ b/azalea-core/src/registry_holder/value.rs @@ -1,6 +1,9 @@ use azalea_registry::{ - Attribute, EnchantmentLevelBasedValueKind as LevelBasedValueKind, - EnchantmentValueEffectKind as ValueEffectKind, + builtin::{ + Attribute, EnchantmentLevelBasedValueKind as LevelBasedValueKind, + EnchantmentValueEffectKind as ValueEffectKind, + }, + identifier::Identifier, }; use simdnbt::{ DeserializeError, FromNbtTag, @@ -9,7 +12,6 @@ use simdnbt::{ use crate::{ attribute_modifier_operation::AttributeModifierOperation, - identifier::Identifier, registry_holder::{components::impl_from_effect_nbt_tag, get_in_compound}, }; diff --git a/azalea-core/src/sound.rs b/azalea-core/src/sound.rs index a3b74b43..9ac568b6 100644 --- a/azalea-core/src/sound.rs +++ b/azalea-core/src/sound.rs @@ -1,8 +1,7 @@ use azalea_buf::AzBuf; +use azalea_registry::identifier::Identifier; use serde::Serialize; -use crate::identifier::Identifier; - #[derive(Clone, Debug, PartialEq, AzBuf, Serialize, simdnbt::Deserialize)] pub struct CustomSound { pub sound_id: Identifier, diff --git a/azalea-core/src/tier.rs b/azalea-core/src/tier.rs index e921899f..a25a3c1a 100644 --- a/azalea-core/src/tier.rs +++ b/azalea-core/src/tier.rs @@ -1,5 +1,7 @@ -pub fn get_item_tier(item: azalea_registry::Item) -> Option<Tier> { - use azalea_registry::Item::*; +use azalea_registry::builtin::ItemKind; + +pub fn get_item_tier(item: ItemKind) -> Option<Tier> { + use ItemKind::*; Some(match item { WoodenPickaxe | WoodenShovel | WoodenAxe | WoodenHoe | WoodenSword => Tier::Wood, StonePickaxe | StoneShovel | StoneAxe | StoneHoe | StoneSword => Tier::Stone, |
