aboutsummaryrefslogtreecommitdiff
path: root/azalea/src
diff options
context:
space:
mode:
authormat <git@matdoes.dev>2025-05-02 02:41:14 +0000
committermat <git@matdoes.dev>2025-05-02 02:41:14 +0000
commit1d3f659c1d304b2a9820feaac063cac3109c2add (patch)
treef0a4798449c39f76469c988df04f9bcde2489a83 /azalea/src
parent881055e58711838893416bfc555e0643c5cfa515 (diff)
downloadazalea-drasl-1d3f659c1d304b2a9820feaac063cac3109c2add.tar.xz
make ClientBuilder/SwarmBuilder Send
Diffstat (limited to 'azalea/src')
-rw-r--r--azalea/src/lib.rs4
-rw-r--r--azalea/src/swarm/mod.rs20
2 files changed, 16 insertions, 8 deletions
diff --git a/azalea/src/lib.rs b/azalea/src/lib.rs
index 0a8300b1..d63ea6c3 100644
--- a/azalea/src/lib.rs
+++ b/azalea/src/lib.rs
@@ -43,7 +43,8 @@ use protocol::{ServerAddress, resolver::ResolverError};
use swarm::SwarmBuilder;
use thiserror::Error;
-pub type BoxHandleFn<S, R> = Box<dyn Fn(Client, azalea_client::Event, S) -> BoxFuture<'static, R>>;
+pub type BoxHandleFn<S, R> =
+ Box<dyn Fn(Client, azalea_client::Event, S) -> BoxFuture<'static, R> + Send>;
pub type HandleFn<S, Fut> = fn(Client, azalea_client::Event, S) -> Fut;
#[derive(Error, Debug)]
@@ -76,6 +77,7 @@ pub struct ClientBuilder<S, R>
where
S: Default + Send + Sync + Clone + Component + 'static,
R: Send + 'static,
+ Self: Send,
{
/// Internally, ClientBuilder is just a wrapper over SwarmBuilder since it's
/// technically just a subset of it so we can avoid duplicating code this
diff --git a/azalea/src/swarm/mod.rs b/azalea/src/swarm/mod.rs
index 2e33148e..4fd5120a 100644
--- a/azalea/src/swarm/mod.rs
+++ b/azalea/src/swarm/mod.rs
@@ -9,6 +9,7 @@ pub mod prelude;
use std::{
collections::{HashMap, hash_map},
future::Future,
+ mem,
net::SocketAddr,
sync::{
Arc,
@@ -23,7 +24,7 @@ use azalea_client::{
};
use azalea_protocol::{ServerAddress, resolver};
use azalea_world::InstanceContainer;
-use bevy_app::{App, PluginGroup, PluginGroupBuilder, Plugins};
+use bevy_app::{App, PluginGroup, PluginGroupBuilder, Plugins, SubApp};
use bevy_ecs::prelude::*;
use futures::future::{BoxFuture, join_all};
use parking_lot::{Mutex, RwLock};
@@ -70,8 +71,10 @@ pub struct SwarmBuilder<S, SS, R, SR>
where
S: Send + Sync + Clone + Component + 'static,
SS: Default + Send + Sync + Clone + Resource + 'static,
+ Self: Send,
{
- pub(crate) app: App,
+ // SubApp is used instead of App to make it Send
+ pub(crate) app: SubApp,
/// The accounts and proxies that are going to join the server.
pub(crate) accounts: Vec<(Account, JoinOpts)>,
/// The individual bot states. This must be the same length as `accounts`,
@@ -131,7 +134,10 @@ impl SwarmBuilder<NoState, NoSwarmState, (), ()> {
SwarmBuilder {
// we create the app here so plugins can add onto it.
// the schedules won't run until [`Self::start`] is called.
- app: App::new(),
+
+ // `App::new()` is used instead of `SubApp::new()` so the necessary resources are
+ // initialized
+ app: mem::take(App::new().main_mut()),
accounts: Vec::new(),
states: Vec::new(),
swarm_state: NoSwarmState,
@@ -361,7 +367,7 @@ where
/// Do the same as [`Self::start`], but allow passing in default join
/// options for the bots.
pub async fn start_with_default_opts(
- self,
+ mut self,
address: impl TryInto<ServerAddress>,
default_join_opts: JoinOpts,
) -> Result<!, StartError> {
@@ -394,9 +400,9 @@ where
swarm_tx.send(SwarmEvent::Init).unwrap();
- let main_schedule_label = self.app.main().update_schedule.unwrap();
+ let main_schedule_label = self.app.update_schedule.unwrap();
- let (ecs_lock, start_running_systems) = start_ecs_runner(self.app);
+ let (ecs_lock, start_running_systems) = start_ecs_runner(&mut self.app);
let swarm = Swarm {
ecs_lock: ecs_lock.clone(),
@@ -559,7 +565,7 @@ pub enum SwarmEvent {
pub type SwarmHandleFn<SS, Fut> = fn(Swarm, SwarmEvent, SS) -> Fut;
pub type BoxSwarmHandleFn<SS, R> =
- Box<dyn Fn(Swarm, SwarmEvent, SS) -> BoxFuture<'static, R> + Send>;
+ Box<dyn Fn(Swarm, SwarmEvent, SS) -> BoxFuture<'static, R> + Send + Sync>;
/// Make a bot [`Swarm`].
///