From 9ffd0e80bbb3feace231553d6539124585b03e3c Mon Sep 17 00:00:00 2001 From: mat Date: Wed, 6 May 2026 03:36:16 -0100 Subject: change panicking functions in Client and EntityRef to return an AzaleaResult instead --- azalea/src/entity_ref/mod.rs | 52 +++++++++++++++++++++++++++++--------------- 1 file changed, 34 insertions(+), 18 deletions(-) (limited to 'azalea/src/entity_ref/mod.rs') diff --git a/azalea/src/entity_ref/mod.rs b/azalea/src/entity_ref/mod.rs index e09852c1..db22f555 100644 --- a/azalea/src/entity_ref/mod.rs +++ b/azalea/src/entity_ref/mod.rs @@ -2,7 +2,7 @@ pub mod shared_impls; use std::fmt::Debug; -use azalea_entity::EntityKindComponent; +use azalea_entity::{EntityKindComponent, EntityUuid}; use azalea_registry::builtin::EntityKind; use bevy_ecs::{ component::Component, @@ -10,8 +10,12 @@ use bevy_ecs::{ query::{QueryData, QueryEntityError, QueryItem}, }; use parking_lot::MappedRwLockReadGuard; +use uuid::Uuid; -use crate::Client; +use crate::{ + Client, + client_impl::error::{AzaleaResult, MissingComponentError}, +}; /// A reference to an entity in a world. /// @@ -61,7 +65,9 @@ impl EntityRef { /// # fn example(client: &azalea::Client) { /// let world_name = client.component::(); /// # } - pub fn component(&self) -> MappedRwLockReadGuard<'_, T> { + pub fn component( + &self, + ) -> Result, MissingComponentError> { self.client.entity_component(self.entity) } @@ -81,23 +87,24 @@ impl EntityRef { /// /// Also see [`Client::query_self`] and [`Client::query_entity`]. /// - /// # Panics + /// # Errors /// - /// This will panic if the entity doesn't exist or is missing a component - /// required by the query. Consider using [`Self::try_query_self`] to - /// avoid this. - pub fn query_self(&self, f: impl FnOnce(QueryItem) -> R) -> R { + /// This will return an error if the entity doesn't exist or is missing a + /// component required by the query. + pub fn query_self( + &self, + f: impl FnOnce(QueryItem) -> R, + ) -> AzaleaResult { self.client.query_entity(self.entity, f) } - /// Query the ECS for data from the entity, or return an error if the query - /// fails. - /// - /// Also see [`Self::query_self`]. + #[doc(hidden)] + #[deprecated = "replaced with `Self::query_self`."] pub fn try_query_self( &self, f: impl FnOnce(QueryItem) -> R, ) -> Result { + #[allow(deprecated)] self.client.try_query_entity(self.entity, f) } } @@ -113,8 +120,17 @@ impl Debug for EntityRef { impl EntityRef { /// Returns the type of entity that this is. - pub fn kind(&self) -> EntityKind { - **self.component::() + pub fn kind(&self) -> AzaleaResult { + Ok(**self.component::()?) + } + + /// Get the Minecraft UUID of this entity. + /// + /// Also see [`Client::uuid`]. + pub fn uuid(&self) -> AzaleaResult { + // note: this isn't in shared_impls because the Client counterpart isn't + // fallible + Ok(**self.component::()?) } } @@ -134,13 +150,13 @@ impl EntityRef { } /// Look at this entity from the client that created the `EntityRef`. - pub fn look_at(&self) { - self.client.look_at(self.eye_position()); + pub fn look_at(&self) -> AzaleaResult<()> { + Ok(self.client.look_at(self.eye_position()?)) } /// Returns the distance between the client's feet position and this /// entity's feet position. - pub fn distance_to_client(&self) -> f64 { - self.position().distance_to(self.client.position()) + pub fn distance_to_client(&self) -> AzaleaResult { + Ok(self.position()?.distance_to(self.client.position()?)) } } -- cgit v1.2.3