From 7b84235a9be5bdc7c05873467ad8310b57448d79 Mon Sep 17 00:00:00 2001 From: mat Date: Mon, 29 Dec 2025 13:53:54 -1345 Subject: fix EntityRef::is_alive being able to panic, and add EntityRef::exists --- azalea/src/entity_ref/shared_impls.rs | 20 ++++++++++++++++++-- 1 file changed, 18 insertions(+), 2 deletions(-) (limited to 'azalea/src/entity_ref/shared_impls.rs') diff --git a/azalea/src/entity_ref/shared_impls.rs b/azalea/src/entity_ref/shared_impls.rs index 3c24b836..1dafbde2 100644 --- a/azalea/src/entity_ref/shared_impls.rs +++ b/azalea/src/entity_ref/shared_impls.rs @@ -164,6 +164,8 @@ impl_entity_functions! { /// You should avoid using this if you have auto-respawn enabled (which is /// the default), instead consider watching for /// [`Event::Death`](crate::Event::Death) instead. + /// + /// Also see [`Self::exists`]. EntityRef: /// Returns whether the entity is alive and hasn't despawned. /// @@ -171,8 +173,22 @@ impl_entity_functions! { /// entity is despawned. Because of this, it may be useful to check `is_alive` /// before calling functions that request data from the world. /// - /// Also see [`Client::is_alive`]. + /// Also see [`Client::is_alive`] and [`Self::exists`]. pub fn is_alive(&self) -> bool { - self.query_self::, _>(|dead| dead.is_none()) + self.try_query_self::, _>(|dead| dead.is_none()).unwrap_or(false) + } + + Client: + /// Returns whether the client is in the world (has been assigned an entity ID). + /// + /// Like [`Self::is_alive`], this will not panic. + EntityRef: + /// Returns whether the entity is in the world and hasn't despawned. + /// + /// Like [`Self::is_alive`], this will not panic. + /// + /// Also see [`Client::exists`]. + pub fn exists(&self) -> bool { + self.try_query_self::, _>(|entity_id| entity_id.is_some()).unwrap_or(false) } } -- cgit v1.2.3