aboutsummaryrefslogtreecommitdiff
path: root/azalea-client/src
diff options
context:
space:
mode:
authormat <git@matdoes.dev>2025-10-05 08:43:52 +0330
committermat <git@matdoes.dev>2025-10-05 08:43:52 +0330
commit682f3c0e95966c39b3d135fc6364f891f3dfb30a (patch)
treee13407411cecbada972284109d978460cc10cf8e /azalea-client/src
parent2b9c453ade60bf4a37e2d77059641dc81b011be0 (diff)
downloadazalea-drasl-682f3c0e95966c39b3d135fc6364f891f3dfb30a.tar.xz
add Client::query_entity
Diffstat (limited to 'azalea-client/src')
-rw-r--r--azalea-client/src/entity_query.rs25
1 files changed, 25 insertions, 0 deletions
diff --git a/azalea-client/src/entity_query.rs b/azalea-client/src/entity_query.rs
index c21380fc..43e99c55 100644
--- a/azalea-client/src/entity_query.rs
+++ b/azalea-client/src/entity_query.rs
@@ -16,6 +16,8 @@ use crate::Client;
impl Client {
/// A convenience function for getting components from our client's entity.
///
+ /// To query another entity, you can use [`Self::query_entity`].
+ ///
/// # Examples
/// ```
/// # use azalea_world::InstanceName;
@@ -39,6 +41,29 @@ impl Client {
f(res)
}
+ /// A convenience function for getting components from any entity.
+ ///
+ /// If you're querying the client, you should use [`Self::query_self`].
+ ///
+ /// # Panics
+ ///
+ /// This will panic if the component doesn't exist on the entity.
+ pub fn query_entity<D: QueryData, R>(
+ &self,
+ entity: Entity,
+ f: impl FnOnce(QueryItem<D>) -> R,
+ ) -> R {
+ let mut ecs = self.ecs.lock();
+ let mut qs = ecs.query::<D>();
+ let res = qs.get_mut(&mut ecs, entity).unwrap_or_else(|_| {
+ panic!(
+ "Entity is missing a required component {:?}",
+ any::type_name::<D>()
+ )
+ });
+ f(res)
+ }
+
/// Quickly returns a lightweight [`Entity`] for an arbitrary entity that
/// matches the given predicate function that is in the same
/// [`Instance`] as the client.