aboutsummaryrefslogtreecommitdiff
path: root/azalea/src/client_impl/entity_query.rs
diff options
context:
space:
mode:
authormat <git@matdoes.dev>2025-12-29 13:53:54 -1345
committermat <git@matdoes.dev>2025-12-29 13:53:54 -1345
commit7b84235a9be5bdc7c05873467ad8310b57448d79 (patch)
tree306be0aaed50969c8be281f827a1bcc7fc3b3d48 /azalea/src/client_impl/entity_query.rs
parent39488a6585ce969af93f43ece1ffb1174dc95e1d (diff)
downloadazalea-drasl-7b84235a9be5bdc7c05873467ad8310b57448d79.tar.xz
fix EntityRef::is_alive being able to panic, and add EntityRef::exists
Diffstat (limited to 'azalea/src/client_impl/entity_query.rs')
-rw-r--r--azalea/src/client_impl/entity_query.rs22
1 files changed, 16 insertions, 6 deletions
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<D: QueryData, R>(&self, f: impl FnOnce(QueryItem<D>) -> R) -> R {
- let mut ecs = self.ecs.write();
- let mut qs = ecs.query::<D>();
- let res = qs.get_mut(&mut ecs, self.entity).unwrap_or_else(|_| {
+ self.try_query_self::<D, R>(f).unwrap_or_else(|_| {
panic!(
"`Client::query_self` failed when querying for {:?}",
any::type_name::<D>()
)
- });
- 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<D: QueryData, R>(
+ &self,
+ f: impl FnOnce(QueryItem<D>) -> R,
+ ) -> Result<R, QueryEntityError> {
+ let mut ecs = self.ecs.write();
+ let mut qs = ecs.query::<D>();
+ qs.get_mut(&mut ecs, self.entity).map(f)
}
/// Query the ECS for data from an entity.