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-client/src | |
| parent | cdb68dfb702f442135dadedc53ce29b1cc2b9c14 (diff) | |
| download | azalea-drasl-b08d3d55d7351eff6e27a09d732078c038539958.tar.xz | |
start implementing data driven registries
Diffstat (limited to 'azalea-client/src')
| -rw-r--r-- | azalea-client/src/client.rs | 52 |
1 files changed, 51 insertions, 1 deletions
diff --git a/azalea-client/src/client.rs b/azalea-client/src/client.rs index 37569d6b..2221cee7 100644 --- a/azalea-client/src/client.rs +++ b/azalea-client/src/client.rs @@ -9,7 +9,10 @@ use std::{ use azalea_auth::{game_profile::GameProfile, sessionserver::ClientSessionServerError}; use azalea_chat::FormattedText; -use azalea_core::{position::Vec3, tick::GameTick}; +use azalea_core::{ + data_registry::ResolvableDataRegistry, position::Vec3, resource_location::ResourceLocation, + tick::GameTick, +}; use azalea_entity::{ indexing::{EntityIdIndex, EntityUuidIndex}, metadata::Health, @@ -48,6 +51,7 @@ use bevy_ecs::{ use bevy_time::TimePlugin; use derive_more::Deref; use parking_lot::{Mutex, RwLock}; +use simdnbt::owned::NbtCompound; use thiserror::Error; use tokio::{ sync::{broadcast, mpsc}, @@ -715,6 +719,52 @@ impl Client { pub fn tab_list(&self) -> HashMap<Uuid, PlayerInfo> { self.component::<TabList>().deref().clone() } + + /// Call the given function with the client's [`RegistryHolder`]. + /// + /// The player's instance (aka world) will be locked during this time, which + /// may result in a deadlock if you try to access the instance again while + /// in the function. + pub fn with_registry_holder<R>( + &self, + f: impl FnOnce(&azalea_core::registry_holder::RegistryHolder) -> R, + ) -> R { + let instance = self.world(); + let registries = &instance.read().registries; + f(registries) + } + + /// Resolve the given registry to its name. + /// + /// This is necessary for data-driven registries like [`Enchantment`]. + /// + /// [`Enchantment`]: azalea_registry::Enchantment + pub fn resolve_registry_name( + &self, + registry: &impl ResolvableDataRegistry, + ) -> Option<ResourceLocation> { + self.with_registry_holder(|registries| registry.resolve_name(registries)) + } + /// Resolve the given registry to its name and data and call the given + /// function with it. + /// + /// This is necessary for data-driven registries like [`Enchantment`]. + /// + /// If you just want the value name, use [`Self::resolve_registry_name`] + /// instead. + /// + /// [`Enchantment`]: azalea_registry::Enchantment + pub fn with_resolved_registry<R>( + &self, + registry: impl ResolvableDataRegistry, + f: impl FnOnce(&ResourceLocation, &NbtCompound) -> R, + ) -> Option<R> { + self.with_registry_holder(|registries| { + registry + .resolve(registries) + .map(|(name, data)| f(name, data)) + }) + } } /// The bundle of components that's shared when we're either in the |
