diff options
| author | mat <git@matdoes.dev> | 2026-03-30 10:29:16 +0400 |
|---|---|---|
| committer | mat <git@matdoes.dev> | 2026-05-07 08:05:58 -1200 |
| commit | 4d1f430408dc6854c1af5fb14b2641e293a9cf43 (patch) | |
| tree | 3795983ae9fd789120e834fc3bfa36154c3b41b6 | |
| parent | 4af9762854aad30b7fdc40640618e465197f2148 (diff) | |
| download | azalea-drasl-4d1f430408dc6854c1af5fb14b2641e293a9cf43.tar.xz | |
add Client::nearest_entities and nearby_players
| -rw-r--r-- | azalea/src/client_impl/entity_query.rs | 24 |
1 files changed, 22 insertions, 2 deletions
diff --git a/azalea/src/client_impl/entity_query.rs b/azalea/src/client_impl/entity_query.rs index df942ef2..0c123b78 100644 --- a/azalea/src/client_impl/entity_query.rs +++ b/azalea/src/client_impl/entity_query.rs @@ -1,12 +1,12 @@ use std::{any, sync::Arc}; use azalea_core::position::Vec3; -use azalea_entity::Position; +use azalea_entity::{LocalEntity, Position, metadata}; use azalea_world::WorldName; use bevy_ecs::{ component::Component, entity::Entity, - query::{QueryData, QueryEntityError, QueryFilter, QueryItem, ROQueryItem}, + query::{QueryData, QueryEntityError, QueryFilter, QueryItem, ROQueryItem, With, Without}, world::World, }; use parking_lot::{ @@ -242,6 +242,26 @@ impl Client { .map(|e| self.entity_ref_for(e)) .collect() } + /// Returns an array of [`EntityRef`] for all known entities in the world + /// that match the given filter, sorted by nearest first. + /// + /// Also see [`Self::nearest_entities_by`]. + pub fn nearest_entities<F: QueryFilter>(&self) -> Box<[EntityRef]> { + self.nearest_entities_by::<(), F>(|_| true) + } + + /// Returns an array of [`EntityRef`]s for all nearby player entities + /// (including this bot), sorted by nearest first. + /// + /// This is a shortcut for calling [`Self::nearest_entities`] with the + /// filter `With<metadata::Player>`. + /// + /// If you're in a swarm, this includes all players that are visible by at + /// least one client. + pub fn nearby_players(&self) -> Box<[EntityRef]> { + self.nearest_entities::<(With<metadata::Player>, Without<LocalEntity>)>() + } + /// Returns an array of all [`Entity`]s in the world that match the /// predicate, sorted by nearest first. /// |
