diff options
Diffstat (limited to 'azalea/src/entity_ref')
| -rw-r--r-- | azalea/src/entity_ref/mod.rs | 52 | ||||
| -rw-r--r-- | azalea/src/entity_ref/shared_impls.rs | 55 |
2 files changed, 56 insertions, 51 deletions
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::<WorldName>(); /// # } - pub fn component<T: Component>(&self) -> MappedRwLockReadGuard<'_, T> { + pub fn component<T: Component>( + &self, + ) -> Result<MappedRwLockReadGuard<'_, T>, 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<D: QueryData, R>(&self, f: impl FnOnce(QueryItem<D>) -> 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<D: QueryData, R>( + &self, + f: impl FnOnce(QueryItem<D>) -> R, + ) -> AzaleaResult<R> { 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<D: QueryData, R>( &self, f: impl FnOnce(QueryItem<D>) -> R, ) -> Result<R, QueryEntityError> { + #[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::<EntityKindComponent>() + pub fn kind(&self) -> AzaleaResult<EntityKind> { + Ok(**self.component::<EntityKindComponent>()?) + } + + /// Get the Minecraft UUID of this entity. + /// + /// Also see [`Client::uuid`]. + pub fn uuid(&self) -> AzaleaResult<Uuid> { + // note: this isn't in shared_impls because the Client counterpart isn't + // fallible + Ok(**self.component::<EntityUuid>()?) } } @@ -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<f64> { + Ok(self.position()?.distance_to(self.client.position()?)) } } diff --git a/azalea/src/entity_ref/shared_impls.rs b/azalea/src/entity_ref/shared_impls.rs index ae3ef42a..5841aba9 100644 --- a/azalea/src/entity_ref/shared_impls.rs +++ b/azalea/src/entity_ref/shared_impls.rs @@ -1,12 +1,11 @@ use azalea_core::{entity_id::MinecraftEntityId, position::Vec3}; use azalea_entity::{ - Attributes, Dead, EntityUuid, Physics, Position, dimensions::EntityDimensions, metadata::Health, + Attributes, Dead, Physics, Position, dimensions::EntityDimensions, metadata::Health, }; use azalea_world::WorldName; -use uuid::Uuid; use super::EntityRef; -use crate::Client; +use crate::{Client, client_impl::error::AzaleaResult}; macro_rules! impl_entity_functions { ( $( @@ -50,8 +49,8 @@ impl_entity_functions! { /// To get the client's eye position, use [`Self::eye_position`]. /// /// Also see [`Client::position`]. - pub fn position(&self) -> Vec3 { - **self.component::<Position>() + pub fn position(&self) -> AzaleaResult<Vec3> { + Ok(**self.component::<Position>()?) } Client: @@ -65,8 +64,8 @@ impl_entity_functions! { /// width, height, and eye height. /// /// Also see [`Client::dimensions`] - pub fn dimensions(&self) -> EntityDimensions { - self.component::<EntityDimensions>().clone() + pub fn dimensions(&self) -> AzaleaResult<EntityDimensions> { + Ok(self.component::<EntityDimensions>()?.clone()) } Client: @@ -80,7 +79,7 @@ impl_entity_functions! { /// Get the position of this entity's eyes. /// /// Also see [`Client::eye_position`]. - pub fn eye_position(&self) -> Vec3 { + pub fn eye_position(&self) -> AzaleaResult<Vec3> { self.query_self::<(&Position, &EntityDimensions), _>(|(pos, dim)| { pos.up(dim.eye_height as f64) }) @@ -94,20 +93,8 @@ impl_entity_functions! { /// Get the health of this entity, typically in the range `0..=20`. /// /// Also see [`Client::health`]. - pub fn health(&self) -> f32 { - **self.component::<Health>() - } - - Client: - /// Get the Minecraft UUID of this client. - /// - /// This is a shortcut for `**self.component::<EntityUuid>()`. - EntityRef: - /// Get the Minecraft UUID of this entity. - /// - /// Also see [`Client::uuid`]. - pub fn uuid(&self) -> Uuid { - **self.component::<EntityUuid>() + pub fn health(&self) -> AzaleaResult<f32> { + Ok(**self.component::<Health>()?) } Client: @@ -124,8 +111,8 @@ impl_entity_functions! { /// consider using [`Self::uuid`] instead. /// /// Also see [`Client::minecraft_id`]. - pub fn minecraft_id(&self) -> MinecraftEntityId { - *self.component::<MinecraftEntityId>() + pub fn minecraft_id(&self) -> AzaleaResult<MinecraftEntityId> { + Ok(*self.component::<MinecraftEntityId>()?) } Client: @@ -134,17 +121,19 @@ impl_entity_functions! { EntityRef: /// Returns the attribute values of the entity, which can be used to /// determine things like its movement speed. - pub fn attributes(&self) -> Attributes { + pub fn attributes(&self) -> AzaleaResult<Attributes> { // this *could* return a mapped read guard for performance but that rarely // matters and it's just easier for the user if it doesn't. - self.component::<Attributes>().clone() + Ok(self.component::<Attributes>()?.clone()) } Client: + #[doc(hidden)] #[deprecated = "renamed to `world_name`."] EntityRef: + #[doc(hidden)] #[deprecated = "renamed to `world_name`."] - pub fn instance_name(&self) -> WorldName { + pub fn instance_name(&self) -> AzaleaResult<WorldName> { self.world_name() } @@ -162,8 +151,8 @@ impl_entity_functions! { /// /// Also see [`Client::world_name`], #[doc(alias("dimension_name"))] - pub fn world_name(&self) -> WorldName { - (*self.component::<WorldName>()).clone() + pub fn world_name(&self) -> AzaleaResult<WorldName> { + Ok((*self.component::<WorldName>()?).clone()) } Client: @@ -183,7 +172,7 @@ impl_entity_functions! { /// /// Also see [`Client::is_alive`] and [`Self::exists`]. pub fn is_alive(&self) -> bool { - self.try_query_self::<Option<&Dead>, _>(|dead| dead.is_none()).unwrap_or(false) + self.query_self::<Option<&Dead>, _>(|dead| dead.is_none()).unwrap_or(false) } Client: @@ -197,7 +186,7 @@ impl_entity_functions! { /// /// Also see [`Client::exists`]. pub fn exists(&self) -> bool { - self.try_query_self::<Option<&MinecraftEntityId>, _>(|entity_id| entity_id.is_some()).unwrap_or(false) + self.query_self::<Option<&MinecraftEntityId>, _>(|entity_id| entity_id.is_some()).unwrap_or(false) } Client: @@ -208,7 +197,7 @@ impl_entity_functions! { /// collisions, etc. /// /// Also see [`Client::physics`]. - pub fn physics(&self) -> Physics { - self.component::<Physics>().clone() + pub fn physics(&self) -> AzaleaResult<Physics> { + Ok(self.component::<Physics>()?.clone()) } } |
