From 19881c4612fe8372d8ddffa72c33b4339704f3b6 Mon Sep 17 00:00:00 2001 From: mat Date: Wed, 11 Oct 2023 00:02:12 -0500 Subject: fix State incorrectly being reused when calling handlers in swarm --- azalea-core/src/position.rs | 2 +- azalea/src/swarm/mod.rs | 13 ++++++++++--- 2 files changed, 11 insertions(+), 4 deletions(-) diff --git a/azalea-core/src/position.rs b/azalea-core/src/position.rs index e61756b0..cc99b684 100755 --- a/azalea-core/src/position.rs +++ b/azalea-core/src/position.rs @@ -168,7 +168,7 @@ impl Vec3 { f64::sqrt(self.x * self.x + self.y * self.y + self.z * self.z) } - /// Get the squared distance from this position to another position. + /// Get the distance from this position to another position. /// Equivalent to `(self - other).length()`. pub fn distance_to(&self, other: &Self) -> f64 { (self - other).length() diff --git a/azalea/src/swarm/mod.rs b/azalea/src/swarm/mod.rs index 18c92438..af381c0b 100644 --- a/azalea/src/swarm/mod.rs +++ b/azalea/src/swarm/mod.rs @@ -387,12 +387,19 @@ where }); // bot events - while let Some((Some(event), bot)) = bots_rx.recv().await { + while let Some((Some(first_event), first_bot)) = bots_rx.recv().await { if let Some(handler) = &self.handler { - let state = bot.component::(); - tokio::spawn((handler)(bot, event, state.clone())); + let first_bot_state = first_bot.component::(); + let first_bot_entity = first_bot.entity; + tokio::spawn((handler)(first_bot, first_event, first_bot_state.clone())); + // this makes it not have to keep locking the ecs + let mut states = HashMap::new(); + states.insert(first_bot_entity, first_bot_state); while let Ok((Some(event), bot)) = bots_rx.try_recv() { + let state = states + .entry(bot.entity) + .or_insert_with(|| bot.component::().clone()); tokio::spawn((handler)(bot, event, state.clone())); } } -- cgit v1.2.3