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/client_impl/entity_query.rs | 22 ++++++++++++++++------ 1 file changed, 16 insertions(+), 6 deletions(-) (limited to 'azalea/src/client_impl') diff --git a/azalea/src/client_impl/entity_query.rs b/azalea/src/client_impl/entity_query.rs index 8de48478..268eaaf1 100644 --- a/azalea/src/client_impl/entity_query.rs +++ b/azalea/src/client_impl/entity_query.rs @@ -84,17 +84,27 @@ impl Client { /// # Panics /// /// This will panic if the client is missing a component required by the - /// query. + /// query. Consider using [`Self::try_query_self`] to avoid this. pub fn query_self(&self, f: impl FnOnce(QueryItem) -> R) -> R { - let mut ecs = self.ecs.write(); - let mut qs = ecs.query::(); - let res = qs.get_mut(&mut ecs, self.entity).unwrap_or_else(|_| { + self.try_query_self::(f).unwrap_or_else(|_| { panic!( "`Client::query_self` failed when querying for {:?}", any::type_name::() ) - }); - f(res) + }) + } + + /// Query the ECS for data from our client entity, or return `None` if the + /// query failed. + /// + /// Also see [`Self::query_self`]. + pub fn try_query_self( + &self, + f: impl FnOnce(QueryItem) -> R, + ) -> Result { + let mut ecs = self.ecs.write(); + let mut qs = ecs.query::(); + qs.get_mut(&mut ecs, self.entity).map(f) } /// Query the ECS for data from an entity. -- cgit v1.2.3