aboutsummaryrefslogtreecommitdiff
path: root/azalea-client/src/entity_query.rs
diff options
context:
space:
mode:
authormat <git@matdoes.dev>2025-09-28 13:10:04 -0545
committermat <git@matdoes.dev>2025-09-28 13:10:04 -0545
commit2c8b7c5c2c9297273abfba8f7743f1bc25f166b1 (patch)
tree3d3aded400100c136287fa59293ce26c61644d00 /azalea-client/src/entity_query.rs
parente2ed19c1ed92f0dccc881d835d9ac6e0f7f834c0 (diff)
downloadazalea-drasl-2c8b7c5c2c9297273abfba8f7743f1bc25f166b1.tar.xz
upgrade bevy to 0.17.0-rc.2
Diffstat (limited to 'azalea-client/src/entity_query.rs')
-rw-r--r--azalea-client/src/entity_query.rs31
1 files changed, 17 insertions, 14 deletions
diff --git a/azalea-client/src/entity_query.rs b/azalea-client/src/entity_query.rs
index 55b86d6c..c21380fc 100644
--- a/azalea-client/src/entity_query.rs
+++ b/azalea-client/src/entity_query.rs
@@ -6,7 +6,7 @@ use azalea_world::InstanceName;
use bevy_ecs::{
component::Component,
entity::Entity,
- query::{QueryData, QueryFilter, ROQueryItem},
+ query::{QueryData, QueryFilter, QueryItem, ROQueryItem},
world::World,
};
use parking_lot::Mutex;
@@ -14,26 +14,29 @@ use parking_lot::Mutex;
use crate::Client;
impl Client {
- /// A convenience function for getting components of our player's entity.
+ /// A convenience function for getting components from our client's entity.
///
/// # Examples
/// ```
/// # use azalea_world::InstanceName;
/// # fn example(mut client: azalea_client::Client) {
- /// let is_logged_in = client
- /// .query::<Option<&InstanceName>>(&mut client.ecs.lock())
- /// .is_some();
+ /// let is_logged_in = client.query_self::<Option<&InstanceName>, _>(|ins| ins.is_some());
/// # }
/// ```
- pub fn query<'w, D: QueryData>(&self, ecs: &'w mut World) -> D::Item<'w> {
- ecs.query::<D>()
- .get_mut(ecs, self.entity)
- .unwrap_or_else(|_| {
- panic!(
- "Our client is missing a required component {:?}",
- any::type_name::<D>()
- )
- })
+ ///
+ /// # Panics
+ ///
+ /// This will panic if the component doesn't exist on the client.
+ pub fn query_self<D: QueryData, R>(&self, 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, self.entity).unwrap_or_else(|_| {
+ panic!(
+ "Our client is missing a required component {:?}",
+ any::type_name::<D>()
+ )
+ });
+ f(res)
}
/// Quickly returns a lightweight [`Entity`] for an arbitrary entity that