aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
-rw-r--r--CHANGELOG.md1
-rw-r--r--azalea-core/src/data_registry.rs7
-rw-r--r--azalea-registry/src/data.rs3
-rw-r--r--azalea-registry/src/identifier.rs17
-rw-r--r--azalea-registry/src/lib.rs1
-rw-r--r--azalea/src/client_impl/mod.rs15
6 files changed, 43 insertions, 1 deletions
diff --git a/CHANGELOG.md b/CHANGELOG.md
index 3ecbdb6c..584035be 100644
--- a/CHANGELOG.md
+++ b/CHANGELOG.md
@@ -27,6 +27,7 @@ is breaking anyways, semantic versioning is not followed.
- Serializing `FormattedText` with serde was writing `extra` twice.
- Attack cooldowns were being applied incorrectly for tools.
+- `Identifier` had an incorrect `Hash` and `PartialEq` implementation.
## [0.15.0+mc1.21.11] - 2025-12-18
diff --git a/azalea-core/src/data_registry.rs b/azalea-core/src/data_registry.rs
index 07837b19..90628f47 100644
--- a/azalea-core/src/data_registry.rs
+++ b/azalea-core/src/data_registry.rs
@@ -16,6 +16,13 @@ pub trait DataRegistryWithKey: DataRegistry {
.protocol_id_to_identifier(Identifier::from(Self::NAME), self.protocol_id())
.map(DataRegistryKeyRef::from_ident)
}
+
+ fn key_owned<'s, 'a: 's>(&'s self, registries: &'a RegistryHolder) -> Option<Self::Key> {
+ registries
+ .protocol_id_to_identifier(Identifier::from(Self::NAME), self.protocol_id())
+ .cloned()
+ .map(DataRegistryKey::from_ident)
+ }
}
impl<R: DataRegistry> DataRegistryWithKey for R {}
diff --git a/azalea-registry/src/data.rs b/azalea-registry/src/data.rs
index b2c3691b..a8e2d304 100644
--- a/azalea-registry/src/data.rs
+++ b/azalea-registry/src/data.rs
@@ -72,6 +72,9 @@ macro_rules! data_registry {
impl crate::DataRegistryKey for $enum_name {
type Borrow<'a> = $enum_name<&'a Identifier>;
+ fn from_ident(ident: Identifier) -> Self {
+ Self::from(ident)
+ }
fn into_ident(self) -> Identifier {
match self {
$(
diff --git a/azalea-registry/src/identifier.rs b/azalea-registry/src/identifier.rs
index 635c5935..4f906181 100644
--- a/azalea-registry/src/identifier.rs
+++ b/azalea-registry/src/identifier.rs
@@ -2,6 +2,7 @@
use std::{
fmt::{self, Debug, Display},
+ hash::{Hash, Hasher},
io::{self, Cursor, Write},
num::NonZeroUsize,
str::FromStr,
@@ -18,7 +19,7 @@ use simdnbt::{FromNbtTag, ToNbtTag, owned::NbtTag};
///
/// This was formerly called a `ResourceLocation`.
#[doc(alias = "ResourceLocation")]
-#[derive(Clone, Default, Eq, Hash, PartialEq)]
+#[derive(Clone, Default, Eq)]
pub struct Identifier {
// empty namespaces aren't allowed so NonZero is fine.
colon_index: Option<NonZeroUsize>,
@@ -63,6 +64,20 @@ impl Identifier {
}
}
}
+impl PartialEq for Identifier {
+ fn eq(&self, other: &Self) -> bool {
+ self.namespace() == other.namespace() && self.path() == other.path()
+ }
+}
+impl Hash for Identifier {
+ fn hash<H: Hasher>(&self, state: &mut H) {
+ let namespace = self.namespace();
+ if namespace != DEFAULT_NAMESPACE {
+ namespace.hash(state);
+ }
+ self.path().hash(state);
+ }
+}
impl Display for Identifier {
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
diff --git a/azalea-registry/src/lib.rs b/azalea-registry/src/lib.rs
index e5157e4f..1d146508 100644
--- a/azalea-registry/src/lib.rs
+++ b/azalea-registry/src/lib.rs
@@ -319,6 +319,7 @@ pub trait DataRegistry:
pub trait DataRegistryKey {
type Borrow<'a>: DataRegistryKeyRef<'a>;
+ fn from_ident(ident: Identifier) -> Self;
fn into_ident(self) -> Identifier;
}
pub trait DataRegistryKeyRef<'a> {
diff --git a/azalea/src/client_impl/mod.rs b/azalea/src/client_impl/mod.rs
index dc4ab90f..fd6506d0 100644
--- a/azalea/src/client_impl/mod.rs
+++ b/azalea/src/client_impl/mod.rs
@@ -425,12 +425,27 @@ impl Client {
/// This is necessary for data-driven registries like [`Enchantment`].
///
/// [`Enchantment`]: azalea_registry::data::Enchantment
+ #[deprecated = "use `bot.resolve_registry_key(registry).map(|r| r.into_ident())` instead."]
pub fn resolve_registry_name(
&self,
registry: &impl ResolvableDataRegistry,
) -> Option<Identifier> {
self.with_registry_holder(|registries| registry.key(registries).map(|r| r.into_ident()))
}
+
+ /// Resolve the given registry entry to its key (aka name).
+ ///
+ /// This is necessary for data-driven registries like [`Enchantment`] and
+ /// [`Biome`](azalea_registry::data::Biome).
+ ///
+ /// To get the key as an [`Identifier`], you can map the return value like
+ /// `.map(|r| r.into_ident())`.
+ ///
+ /// [`Enchantment`]: azalea_registry::data::Enchantment
+ pub fn resolve_registry_key<R: ResolvableDataRegistry>(&self, registry: &R) -> Option<R::Key> {
+ self.with_registry_holder(|registries| registry.key_owned(registries))
+ }
+
/// Resolve the given registry to its name and data and call the given
/// function with it.
///