diff options
| author | mat <27899617+mat-1@users.noreply.github.com> | 2025-12-28 04:31:29 -0600 |
|---|---|---|
| committer | GitHub <noreply@github.com> | 2025-12-28 04:31:29 -0600 |
| commit | 20c7e27250148f62bab9e7b99e4f0cd6deb82325 (patch) | |
| tree | 163b312ef4dc72c5c23be923f8ca17cdf2a7278c /azalea/src/swarm | |
| parent | 7ab3b8924f64f7eadb6b8928b6fae73cb06e4c2f (diff) | |
| download | azalea-drasl-20c7e27250148f62bab9e7b99e4f0cd6deb82325.tar.xz | |
Change Client::component to return a reference (#298)
* change Client::component to return a reference
* write docs
* merge main
* remove unused parking_lot feature
Diffstat (limited to 'azalea/src/swarm')
| -rw-r--r-- | azalea/src/swarm/builder.rs | 6 | ||||
| -rw-r--r-- | azalea/src/swarm/mod.rs | 21 |
2 files changed, 15 insertions, 12 deletions
diff --git a/azalea/src/swarm/builder.rs b/azalea/src/swarm/builder.rs index 860e47be..4312a485 100644 --- a/azalea/src/swarm/builder.rs +++ b/azalea/src/swarm/builder.rs @@ -448,7 +448,7 @@ where let (ecs_lock, start_running_systems, appexit_rx) = start_ecs_runner(&mut self.app); let swarm = Swarm { - ecs_lock: ecs_lock.clone(), + ecs: ecs_lock.clone(), address: Arc::new(RwLock::new(address)), instance_container, @@ -460,7 +460,7 @@ where // run the main schedule so the startup systems run { - let mut ecs = ecs_lock.lock(); + let mut ecs = ecs_lock.write(); ecs.insert_resource(swarm.clone()); ecs.insert_resource(self.swarm_state.clone()); if let Some(reconnect_after) = self.reconnect_after { @@ -545,7 +545,7 @@ where if let Some(handler) = &self.handler { let ecs_mutex = first_bot.ecs.clone(); - let mut ecs = ecs_mutex.lock(); + let mut ecs = ecs_mutex.write(); let mut query = ecs.query::<Option<&S>>(); let Ok(Some(first_bot_state)) = query.get(&ecs, first_bot.entity) else { error!( diff --git a/azalea/src/swarm/mod.rs b/azalea/src/swarm/mod.rs index 48ac20a3..90fcf4dd 100644 --- a/azalea/src/swarm/mod.rs +++ b/azalea/src/swarm/mod.rs @@ -20,7 +20,7 @@ use bevy_app::{PluginGroup, PluginGroupBuilder}; use bevy_ecs::prelude::*; pub use builder::SwarmBuilder; use futures::future::BoxFuture; -use parking_lot::{Mutex, RwLock}; +use parking_lot::RwLock; use tokio::{sync::mpsc, task}; use tracing::{debug, error, warn}; @@ -37,7 +37,12 @@ use crate::{Client, JoinOpts, client_impl::StartClientOpts}; /// removed with [`Client::disconnect`]. #[derive(Clone, Resource)] pub struct Swarm { - pub ecs_lock: Arc<Mutex<World>>, + /// A way to directly access the ECS. + /// + /// This will not work if called within a system, as the ECS is already + /// locked. + #[doc(alias = "ecs_lock")] // former type name + pub ecs: Arc<RwLock<World>>, // the address is public and mutable so plugins can change it pub address: Arc<RwLock<ResolvedAddr>>, @@ -176,7 +181,7 @@ impl Swarm { let (tx, rx) = mpsc::unbounded_channel(); let client = Client::start_client(StartClientOpts { - ecs_lock: self.ecs_lock.clone(), + ecs_lock: self.ecs.clone(), account: account.clone(), connect_opts: ConnectOpts { address, @@ -188,7 +193,7 @@ impl Swarm { .await; // add the state to the client { - let mut ecs = self.ecs_lock.lock(); + let mut ecs = self.ecs.write(); ecs.entity_mut(client.entity).insert(state); } @@ -251,9 +256,7 @@ impl Swarm { "Sending SwarmEvent::Disconnect due to receiving an Event::Disconnect from client {}", bot.entity ); - let account = bot - .get_component::<Account>() - .expect("bot is missing required Account component"); + let account = bot.component::<Account>().clone(); swarm_tx .send(SwarmEvent::Disconnect( Box::new(account), @@ -284,7 +287,7 @@ impl Swarm { /// /// [`LocalEntity`]: azalea_entity::LocalEntity pub fn client_entities(&self) -> Box<[Entity]> { - let mut ecs = self.ecs_lock.lock(); + let mut ecs = self.ecs.write(); let mut query = ecs.query_filtered::<Entity, With<LocalEntity>>(); query.iter(&ecs).collect::<Box<[Entity]>>() } @@ -312,7 +315,7 @@ impl IntoIterator for Swarm { client_entities .into_iter() - .map(|entity| Client::new(entity, self.ecs_lock.clone())) + .map(|entity| Client::new(entity, self.ecs.clone())) .collect::<Box<[Client]>>() .into_iter() } |
