From 839d536e8020b291bc1d213a5e8e823dc513a940 Mon Sep 17 00:00:00 2001 From: mat Date: Sun, 28 Dec 2025 09:06:47 -0330 Subject: add a few more functions for getting common components to Client --- azalea/src/client_impl/entity_query.rs | 31 ++++++++++++++++++------------- 1 file changed, 18 insertions(+), 13 deletions(-) (limited to 'azalea/src/client_impl/entity_query.rs') diff --git a/azalea/src/client_impl/entity_query.rs b/azalea/src/client_impl/entity_query.rs index ae623dc7..683d755e 100644 --- a/azalea/src/client_impl/entity_query.rs +++ b/azalea/src/client_impl/entity_query.rs @@ -21,8 +21,9 @@ impl Client { /// /// This returns a reference to the component wrapped by a read guard. This /// makes the component cheap to access, but means that the ECS cannot be - /// mutated while it's in scope. In some cases, it may be simpler for you to - /// immediately clone the component after accessing it. + /// mutated while it's in scope (it will cause a deadlock). In some cases, + /// it may be simpler for you to immediately clone the component after + /// accessing it. /// /// If the component isn't guaranteed to be present, consider using /// [`Self::get_component`] instead. @@ -227,15 +228,19 @@ impl Client { pub fn nearest_entities_by( &self, predicate: impl EntityPredicate, - ) -> Vec { - let Some(instance_name) = self.get_component::() else { - return vec![]; - }; - let Some(position) = self.get_component::() else { - return vec![]; + ) -> Box<[Entity]> { + let (instance_name, position) = { + let Some(instance_name) = self.get_component::() else { + return Box::new([]); + }; + let Some(position) = self.get_component::() else { + return Box::new([]); + }; + + (instance_name.clone(), **position) }; - let (instance_name, position) = (instance_name.clone(), *position); - predicate.find_all_sorted(self.ecs.clone(), &instance_name, (&position).into()) + + predicate.find_all_sorted(self.ecs.clone(), &instance_name, position) } /// Get a component from an entity. @@ -291,7 +296,7 @@ pub trait EntityPredicate { ecs_lock: Arc>, instance_name: &InstanceName, nearest_to: Vec3, - ) -> Vec; + ) -> Box<[Entity]>; } impl EntityPredicate for F where @@ -316,7 +321,7 @@ where ecs_lock: Arc>, instance_name: &InstanceName, nearest_to: Vec3, - ) -> Vec { + ) -> Box<[Entity]> { let mut ecs = ecs_lock.write(); let mut query = ecs.query_filtered::<(Entity, &InstanceName, &Position, Q), Filter>(); let mut entities = query @@ -333,6 +338,6 @@ where entities .into_iter() .map(|(e, _)| e) - .collect::>() + .collect::>() } } -- cgit v1.2.3