From fbaae39cdf9c5a7a34005a51a37b85f7cdd5ea00 Mon Sep 17 00:00:00 2001 From: mat Date: Thu, 13 Nov 2025 12:34:47 +0930 Subject: rename ResourceLocation to Identifier ahead of mojmap changes --- azalea-core/src/checksum.rs | 4 +- azalea-core/src/data_registry.rs | 14 +-- azalea-core/src/identifier.rs | 164 +++++++++++++++++++++++++++++++++++ azalea-core/src/lib.rs | 7 +- azalea-core/src/position.rs | 10 +-- azalea-core/src/registry_holder.rs | 28 +++--- azalea-core/src/resource_location.rs | 160 ---------------------------------- azalea-core/src/sound.rs | 4 +- 8 files changed, 197 insertions(+), 194 deletions(-) create mode 100644 azalea-core/src/identifier.rs delete mode 100644 azalea-core/src/resource_location.rs (limited to 'azalea-core/src') diff --git a/azalea-core/src/checksum.rs b/azalea-core/src/checksum.rs index 4661d171..df94d58e 100644 --- a/azalea-core/src/checksum.rs +++ b/azalea-core/src/checksum.rs @@ -6,7 +6,7 @@ use serde::{Serialize, ser}; use thiserror::Error; use tracing::error; -use crate::{registry_holder::RegistryHolder, resource_location::ResourceLocation}; +use crate::{identifier::Identifier, registry_holder::RegistryHolder}; #[derive(Debug, Clone, Copy, PartialEq, Eq, PartialOrd, Ord, Default, AzBuf)] pub struct Checksum(pub u32); @@ -200,7 +200,7 @@ impl<'a, 'r> ser::Serializer for ChecksumSerializer<'a, 'r> { let value = self .registries .map - .get(&ResourceLocation::from(name)) + .get(&Identifier::from(name)) .and_then(|r| r.get_index(variant_index as usize)) .map(|r| r.0.to_string()) .unwrap_or_default(); diff --git a/azalea-core/src/data_registry.rs b/azalea-core/src/data_registry.rs index 6e2c29ff..d3fae125 100644 --- a/azalea-core/src/data_registry.rs +++ b/azalea-core/src/data_registry.rs @@ -3,23 +3,23 @@ use std::{io::Cursor, str::FromStr}; use azalea_registry::DataRegistry; use simdnbt::owned::NbtCompound; -use crate::{registry_holder::RegistryHolder, resource_location::ResourceLocation}; +use crate::{identifier::Identifier, registry_holder::RegistryHolder}; pub trait ResolvableDataRegistry: DataRegistry { - fn resolve_name(&self, registries: &RegistryHolder) -> Option { + 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(|_| { + ) -> Option<(&'a Identifier, &'a NbtCompound)> { + let name_ident = Identifier::from_str(Self::NAME).unwrap_or_else(|_| { panic!( - "Name for registry should be a valid ResourceLocation: {}", + "Name for registry should be a valid Identifier: {}", Self::NAME ) }); - let registry_values = registries.map.get(&name_resourcelocation)?; + let registry_values = registries.map.get(&name_ident)?; let resolved = registry_values.get_index(self.protocol_id() as usize)?; Some(resolved) } @@ -27,7 +27,7 @@ pub trait ResolvableDataRegistry: DataRegistry { fn resolve_and_deserialize( &self, registries: &RegistryHolder, - ) -> Option> { + ) -> Option> { let (name, value) = self.resolve(registries)?; let mut nbt_bytes = Vec::new(); diff --git a/azalea-core/src/identifier.rs b/azalea-core/src/identifier.rs new file mode 100644 index 00000000..d1e46aef --- /dev/null +++ b/azalea-core/src/identifier.rs @@ -0,0 +1,164 @@ +//! An arbitrary string identifier. + +use std::{ + fmt, + io::{self, Cursor, Write}, + 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 { + pub namespace: String, + pub path: String, +} + +static DEFAULT_NAMESPACE: &str = "minecraft"; +// static REALMS_NAMESPACE: &str = "realms"; + +impl Identifier { + pub fn new(resource_string: &str) -> Identifier { + let sep_byte_position_option = resource_string.chars().position(|c| c == ':'); + let (namespace, path) = if let Some(sep_byte_position) = sep_byte_position_option { + if sep_byte_position == 0 { + (DEFAULT_NAMESPACE, &resource_string[1..]) + } else { + ( + &resource_string[..sep_byte_position], + &resource_string[sep_byte_position + 1..], + ) + } + } else { + (DEFAULT_NAMESPACE, resource_string) + }; + Identifier { + namespace: namespace.to_string(), + path: path.to_string(), + } + } +} + +impl fmt::Display for Identifier { + fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result { + write!(f, "{}:{}", self.namespace, self.path) + } +} +impl fmt::Debug for Identifier { + fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result { + write!(f, "{}:{}", self.namespace, self.path) + } +} +impl FromStr for Identifier { + type Err = &'static str; + + fn from_str(s: &str) -> Result { + 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 { + 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(&self, serializer: S) -> Result + where + S: Serializer, + { + serializer.serialize_str(&self.to_string()) + } +} + +impl<'de> Deserialize<'de> for Identifier { + fn deserialize(deserializer: D) -> Result + 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 { + 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_resource_location() { + 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_resource_location() { + 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 9fdf4b6c..b87e6143 100644 --- a/azalea-core/src/lib.rs +++ b/azalea-core/src/lib.rs @@ -15,11 +15,16 @@ 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; -pub mod resource_location; +pub mod resource_location { + #![deprecated(note = "renamed to `identifier`.")] + #[deprecated(note = "renamed to `identifier::Identifier`.")] + pub type ResourceLocation = crate::identifier::Identifier; +} pub mod sound; #[cfg(feature = "bevy_ecs")] pub mod tick; diff --git a/azalea-core/src/position.rs b/azalea-core/src/position.rs index 1686a7ad..03ea49ec 100644 --- a/azalea-core/src/position.rs +++ b/azalea-core/src/position.rs @@ -16,9 +16,7 @@ use azalea_buf::{AzBuf, AzaleaRead, AzaleaWrite, BufReadError}; use serde::{Serialize, Serializer}; use simdnbt::Deserialize; -use crate::{ - codec_utils::IntArray, direction::Direction, math, resource_location::ResourceLocation, -}; +use crate::{codec_utils::IntArray, direction::Direction, identifier::Identifier, math}; macro_rules! vec3_impl { ($name:ident, $type:ty) => { @@ -723,7 +721,7 @@ impl nohash_hasher::IsEnabled for ChunkSectionBlockPos {} #[derive(Debug, Clone, PartialEq, Serialize)] pub struct GlobalPos { // this is actually a ResourceKey in Minecraft, but i don't think it matters? - pub dimension: ResourceLocation, + pub dimension: Identifier, pub pos: BlockPos, } @@ -958,7 +956,7 @@ impl AzaleaRead for BlockPos { impl AzaleaRead for GlobalPos { fn azalea_read(buf: &mut Cursor<&[u8]>) -> Result { Ok(GlobalPos { - dimension: ResourceLocation::azalea_read(buf)?, + dimension: Identifier::azalea_read(buf)?, pos: BlockPos::azalea_read(buf)?, }) } @@ -987,7 +985,7 @@ impl AzaleaWrite for BlockPos { impl AzaleaWrite for GlobalPos { fn azalea_write(&self, buf: &mut impl Write) -> io::Result<()> { - ResourceLocation::azalea_write(&self.dimension, buf)?; + Identifier::azalea_write(&self.dimension, buf)?; BlockPos::azalea_write(&self.pos, buf)?; Ok(()) diff --git a/azalea-core/src/registry_holder.rs b/azalea-core/src/registry_holder.rs index 7c5bfede..6730cd20 100644 --- a/azalea-core/src/registry_holder.rs +++ b/azalea-core/src/registry_holder.rs @@ -14,7 +14,7 @@ use simdnbt::{ }; use tracing::error; -use crate::resource_location::ResourceLocation; +use crate::identifier::Identifier; /// The base of the registry. /// @@ -26,15 +26,11 @@ use crate::resource_location::ResourceLocation; /// world. #[derive(Default, Debug, Clone)] pub struct RegistryHolder { - pub map: HashMap>, + pub map: HashMap>, } impl RegistryHolder { - pub fn append( - &mut self, - id: ResourceLocation, - entries: Vec<(ResourceLocation, Option)>, - ) { + pub fn append(&mut self, id: Identifier, entries: Vec<(Identifier, Option)>) { let map = self.map.entry(id).or_default(); for (key, value) in entries { if let Some(value) = value { @@ -49,7 +45,7 @@ impl RegistryHolder { /// /// You should do some type of error handling if this returns `None`. pub fn dimension_type(&self) -> Option> { - let name = ResourceLocation::new("minecraft:dimension_type"); + let name = Identifier::new("minecraft:dimension_type"); match self.get(&name) { Some(Ok(registry)) => Some(registry), Some(Err(err)) => { @@ -65,7 +61,7 @@ impl RegistryHolder { fn get( &self, - name: &ResourceLocation, + name: &Identifier, ) -> Option, simdnbt::DeserializeError>> { // this is suboptimal, ideally simdnbt should just have a way to get the // owned::NbtCompound as a borrow::NbtCompound @@ -95,14 +91,14 @@ impl RegistryHolder { /// A collection of values for a certain type of registry data. #[derive(Debug, Clone)] pub struct RegistryType { - pub map: HashMap, + pub map: HashMap, } #[derive(Debug, Clone, Serialize, Deserialize)] #[cfg_attr(feature = "strict_registry", simdnbt(deny_unknown_fields))] pub struct TrimMaterialElement { pub asset_name: String, - pub ingredient: ResourceLocation, + pub ingredient: Identifier, pub item_model_index: f32, pub override_armor_materials: HashMap, pub description: Option, @@ -145,13 +141,13 @@ pub struct DimensionTypeElement { pub ambient_light: f32, pub bed_works: bool, pub coordinate_scale: f32, - pub effects: ResourceLocation, + pub effects: Identifier, pub fixed_time: Option, pub has_ceiling: bool, pub has_raids: bool, pub has_skylight: bool, pub height: u32, - pub infiniburn: ResourceLocation, + pub infiniburn: Identifier, pub logical_height: u32, pub min_y: i32, pub monster_spawn_block_light_limit: u32, @@ -185,7 +181,7 @@ pub enum MonsterSpawnLightLevel { /// A complex value with a type, minimum, and maximum. /// Vanilla minecraft only uses one type, "minecraft:uniform". Complex { - kind: ResourceLocation, + kind: Identifier, value: MonsterSpawnLightLevelValues, }, } @@ -195,7 +191,7 @@ impl FromNbtTag for MonsterSpawnLightLevel { if let Some(value) = tag.int() { Some(Self::Simple(value as u32)) } else if let Some(value) = tag.compound() { - let kind = ResourceLocation::from_nbt_tag(value.get("type")?)?; + let kind = Identifier::from_nbt_tag(value.get("type")?)?; let value = MonsterSpawnLightLevelValues::from_nbt_tag(value.get("value")?)?; Some(Self::Complex { kind, value }) } else { @@ -285,7 +281,7 @@ pub struct BiomeEffects { pub music: Option, pub mood_sound: BiomeMoodSound, pub additions_sound: Option, - pub ambient_sound: Option, + pub ambient_sound: Option, pub particle: Option, } diff --git a/azalea-core/src/resource_location.rs b/azalea-core/src/resource_location.rs deleted file mode 100644 index 1591f678..00000000 --- a/azalea-core/src/resource_location.rs +++ /dev/null @@ -1,160 +0,0 @@ -//! A resource, like minecraft:stone - -use std::{ - fmt, - io::{self, Cursor, Write}, - str::FromStr, -}; - -use azalea_buf::{AzaleaRead, AzaleaWrite, BufReadError}; -use serde::{Deserialize, Deserializer, Serialize, Serializer, de}; -use simdnbt::{FromNbtTag, ToNbtTag, owned::NbtTag}; - -#[derive(Hash, Clone, PartialEq, Eq, Default)] -pub struct ResourceLocation { - pub namespace: String, - pub path: String, -} - -static DEFAULT_NAMESPACE: &str = "minecraft"; -// static REALMS_NAMESPACE: &str = "realms"; - -impl ResourceLocation { - pub fn new(resource_string: &str) -> ResourceLocation { - let sep_byte_position_option = resource_string.chars().position(|c| c == ':'); - let (namespace, path) = if let Some(sep_byte_position) = sep_byte_position_option { - if sep_byte_position == 0 { - (DEFAULT_NAMESPACE, &resource_string[1..]) - } else { - ( - &resource_string[..sep_byte_position], - &resource_string[sep_byte_position + 1..], - ) - } - } else { - (DEFAULT_NAMESPACE, resource_string) - }; - ResourceLocation { - namespace: namespace.to_string(), - path: path.to_string(), - } - } -} - -impl fmt::Display for ResourceLocation { - fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result { - write!(f, "{}:{}", self.namespace, self.path) - } -} -impl fmt::Debug for ResourceLocation { - fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result { - write!(f, "{}:{}", self.namespace, self.path) - } -} -impl FromStr for ResourceLocation { - type Err = &'static str; - - fn from_str(s: &str) -> Result { - Ok(ResourceLocation::new(s)) - } -} -impl From<&str> for ResourceLocation { - fn from(s: &str) -> Self { - ResourceLocation::new(s) - } -} - -impl AzaleaRead for ResourceLocation { - fn azalea_read(buf: &mut Cursor<&[u8]>) -> Result { - let location_string = String::azalea_read(buf)?; - Ok(ResourceLocation::new(&location_string)) - } -} -impl AzaleaWrite for ResourceLocation { - fn azalea_write(&self, buf: &mut impl Write) -> io::Result<()> { - self.to_string().azalea_write(buf) - } -} - -impl Serialize for ResourceLocation { - fn serialize(&self, serializer: S) -> Result - where - S: Serializer, - { - serializer.serialize_str(&self.to_string()) - } -} - -impl<'de> Deserialize<'de> for ResourceLocation { - fn deserialize(deserializer: D) -> Result - where - D: Deserializer<'de>, - { - let s = String::deserialize(deserializer)?; - if s.contains(':') { - Ok(ResourceLocation::new(&s)) - } else { - Err(de::Error::invalid_value( - de::Unexpected::Str(&s), - &"a valid ResourceLocation", - )) - } - } -} - -impl FromNbtTag for ResourceLocation { - fn from_nbt_tag(tag: simdnbt::borrow::NbtTag) -> Option { - tag.string().and_then(|s| s.to_str().parse().ok()) - } -} - -impl ToNbtTag for ResourceLocation { - fn to_nbt_tag(self) -> NbtTag { - NbtTag::String(self.to_string().into()) - } -} - -#[cfg(test)] -mod tests { - use super::*; - - #[test] - fn basic_resource_location() { - let r = ResourceLocation::new("abcdef:ghijkl"); - assert_eq!(r.namespace, "abcdef"); - assert_eq!(r.path, "ghijkl"); - } - #[test] - fn no_namespace() { - let r = ResourceLocation::new("azalea"); - assert_eq!(r.namespace, "minecraft"); - assert_eq!(r.path, "azalea"); - } - #[test] - fn colon_start() { - let r = ResourceLocation::new(":azalea"); - assert_eq!(r.namespace, "minecraft"); - assert_eq!(r.path, "azalea"); - } - #[test] - fn colon_end() { - let r = ResourceLocation::new("azalea:"); - assert_eq!(r.namespace, "azalea"); - assert_eq!(r.path, ""); - } - - #[test] - fn azbuf_resource_location() { - let mut buf = Vec::new(); - ResourceLocation::new("minecraft:dirt") - .azalea_write(&mut buf) - .unwrap(); - - let mut buf = Cursor::new(&buf[..]); - - assert_eq!( - ResourceLocation::azalea_read(&mut buf).unwrap(), - ResourceLocation::new("minecraft:dirt") - ); - } -} diff --git a/azalea-core/src/sound.rs b/azalea-core/src/sound.rs index 3f7b86c3..ebc18928 100644 --- a/azalea-core/src/sound.rs +++ b/azalea-core/src/sound.rs @@ -1,10 +1,10 @@ use azalea_buf::AzBuf; use serde::Serialize; -use crate::resource_location::ResourceLocation; +use crate::identifier::Identifier; #[derive(Clone, Debug, PartialEq, AzBuf, Serialize)] pub struct CustomSound { - pub location: ResourceLocation, + pub location: Identifier, pub fixed_range: Option, } -- cgit v1.2.3