diff options
| author | mat <git@matdoes.dev> | 2025-10-12 22:10:02 -0300 |
|---|---|---|
| committer | mat <git@matdoes.dev> | 2025-10-12 22:10:02 -0300 |
| commit | 9aaf893588eaecd62f6b146897cbce8a3a5058b5 (patch) | |
| tree | eb4bd109480b8e6cfbcd89f1f309c4954d8c8f7f | |
| parent | 5f6ebb98c9ef5775ed5fe39662436b83a3108aa6 (diff) | |
| download | azalea-drasl-9aaf893588eaecd62f6b146897cbce8a3a5058b5.tar.xz | |
improve ux for entity matching functions
| -rw-r--r-- | CHANGELOG.md | 1 | ||||
| -rw-r--r-- | azalea-client/src/entity_query.rs | 21 | ||||
| -rw-r--r-- | azalea/examples/testbot/commands.rs | 4 | ||||
| -rw-r--r-- | azalea/examples/testbot/commands/debug.rs | 2 | ||||
| -rw-r--r-- | azalea/src/pathfinder/moves/mod.rs | 13 |
5 files changed, 17 insertions, 24 deletions
diff --git a/CHANGELOG.md b/CHANGELOG.md index 4e9bd24f..649a23f1 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -18,6 +18,7 @@ is breaking anyways, semantic versioning is not followed. - Update to Bevy 0.17. - `Client::query`, `map_component`, and `map_get_component` were replaced by `Client::query_self`. - Rename `SendPacketEvent` to `SendGamePacketEvent` and `PingEvent` to `GamePingEvent`. +- Swap the order of the type parameters in entity filtering functions so query is first, then filter. ### Fixed diff --git a/azalea-client/src/entity_query.rs b/azalea-client/src/entity_query.rs index 6945549d..b291fe7d 100644 --- a/azalea-client/src/entity_query.rs +++ b/azalea-client/src/entity_query.rs @@ -96,8 +96,8 @@ impl Client { /// use bevy_ecs::query::With; /// /// # fn example(mut bot: Client, sender_name: String) { - /// let entity = bot.any_entity_by::<With<Player>, (&GameProfileComponent,)>( - /// |(profile,): &(&GameProfileComponent,)| profile.name == sender_name, + /// let entity = bot.any_entity_by::<&GameProfileComponent, With<Player>>( + /// |profile: &GameProfileComponent| profile.name == sender_name, /// ); /// if let Some(entity) = entity { /// let position = bot.entity_component::<Position>(entity); @@ -108,7 +108,7 @@ impl Client { /// /// [`Entity`]: bevy_ecs::entity::Entity /// [`Instance`]: azalea_world::Instance - pub fn any_entity_by<F: QueryFilter, Q: QueryData>( + pub fn any_entity_by<Q: QueryData, F: QueryFilter>( &self, predicate: impl EntityPredicate<Q, F>, ) -> Option<Entity> { @@ -133,7 +133,7 @@ impl Client { /// # fn example(mut bot: azalea_client::Client, sender_name: String) { /// // get the position of the nearest player /// if let Some(nearest_player) = - /// bot.nearest_entity_by::<(With<Player>, Without<LocalEntity>), ()>(|_: &()| true) + /// bot.nearest_entity_by::<(), (With<Player>, Without<LocalEntity>)>(|_: ()| true) /// { /// let nearest_player_pos = *bot.entity_component::<Position>(nearest_player); /// bot.chat(format!("You are at {nearest_player_pos}")); @@ -142,7 +142,7 @@ impl Client { /// ``` /// /// [`Entity`]: bevy_ecs::entity::Entity - pub fn nearest_entity_by<F: QueryFilter, Q: QueryData>( + pub fn nearest_entity_by<Q: QueryData, F: QueryFilter>( &self, predicate: impl EntityPredicate<Q, F>, ) -> Option<Entity> { @@ -159,10 +159,10 @@ impl Client { /// # use bevy_ecs::query::{With, Without}; /// # fn example(mut bot: azalea_client::Client, sender_name: String) { /// let nearby_players = - /// bot.nearest_entities_by::<(With<Player>, Without<LocalEntity>), ()>(|_: &()| true); + /// bot.nearest_entities_by::<(), (With<Player>, Without<LocalEntity>)>(|_: ()| true); /// # } /// ``` - pub fn nearest_entities_by<F: QueryFilter, Q: QueryData>( + pub fn nearest_entities_by<Q: QueryData, F: QueryFilter>( &self, predicate: impl EntityPredicate<Q, F>, ) -> Vec<Entity> { @@ -218,7 +218,8 @@ pub trait EntityPredicate<Q: QueryData, Filter: QueryFilter> { } impl<F, Q: QueryData, Filter: QueryFilter> EntityPredicate<Q, Filter> for F where - F: Fn(&ROQueryItem<Q>) -> bool, + F: Fn(ROQueryItem<Q>) -> bool, + for<'w, 's> <<Q as QueryData>::ReadOnly as QueryData>::Item<'w, 's>: Copy, { fn find_any( &self, @@ -229,7 +230,7 @@ where let mut query = ecs.query_filtered::<(Entity, &InstanceName, Q), Filter>(); query .iter(&ecs) - .find(|(_, e_instance_name, q)| *e_instance_name == instance_name && (self)(q)) + .find(|(_, e_instance_name, q)| *e_instance_name == instance_name && (self)(*q)) .map(|(e, _, _)| e) } @@ -243,7 +244,7 @@ where let mut query = ecs.query_filtered::<(Entity, &InstanceName, &Position, Q), Filter>(); let mut entities = query .iter(&ecs) - .filter(|(_, e_instance_name, _, q)| *e_instance_name == instance_name && (self)(q)) + .filter(|(_, e_instance_name, _, q)| *e_instance_name == instance_name && (self)(*q)) .map(|(e, _, position, _)| (e, Vec3::from(position))) .collect::<Vec<(Entity, Vec3)>>(); diff --git a/azalea/examples/testbot/commands.rs b/azalea/examples/testbot/commands.rs index 1616b82e..9d9d9b8a 100644 --- a/azalea/examples/testbot/commands.rs +++ b/azalea/examples/testbot/commands.rs @@ -32,8 +32,8 @@ impl CommandSource { pub fn entity(&mut self) -> Option<Entity> { let username = self.chat.sender()?; self.bot - .any_entity_by::<With<Player>, &GameProfileComponent>( - |profile: &&GameProfileComponent| profile.name == username, + .any_entity_by::<&GameProfileComponent, With<Player>>( + |profile: &GameProfileComponent| profile.name == username, ) } } diff --git a/azalea/examples/testbot/commands/debug.rs b/azalea/examples/testbot/commands/debug.rs index d80ee3be..91f7dc61 100644 --- a/azalea/examples/testbot/commands/debug.rs +++ b/azalea/examples/testbot/commands/debug.rs @@ -218,7 +218,7 @@ pub fn register(commands: &mut CommandDispatcher<Mutex<CommandSource>>) { let source = ctx.source.lock(); let player_entities = source .bot - .nearest_entities_by::<With<metadata::Player>, ()>(|_: &()| true); + .nearest_entities_by::<(), With<metadata::Player>>(|_: ()| true); let tab_list = source.bot.tab_list(); for player_entity in player_entities { let uuid = source.bot.entity_component::<EntityUuid>(player_entity); diff --git a/azalea/src/pathfinder/moves/mod.rs b/azalea/src/pathfinder/moves/mod.rs index bb2354bf..fc574a4c 100644 --- a/azalea/src/pathfinder/moves/mod.rs +++ b/azalea/src/pathfinder/moves/mod.rs @@ -217,19 +217,10 @@ pub struct IsReachedCtx<'a> { #[must_use] pub fn default_is_reached( IsReachedCtx { - position, - target, - physics, - .. + position, target, .. }: IsReachedCtx, ) -> bool { - if BlockPos::from(position) == target { - return true; - } - - // this is to make it handle things like slabs correctly, if we're on the block - // below the target but on_ground - BlockPos::from(position).up(1) == target && physics.on_ground() + player_pos_to_block_pos(position) == target } pub struct PathfinderCtx<'a> { |
