1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
|
//! Swarms are a way to conveniently control many bots.
//!
//! See [`Swarm`] for more information.
mod builder;
mod chat;
mod events;
pub mod prelude;
use std::sync::{
Arc,
atomic::{self, AtomicBool},
};
use azalea_client::{account::Account, client_chat::ChatPacket, join::ConnectOpts};
use azalea_entity::LocalEntity;
use azalea_protocol::address::ResolvedAddr;
use azalea_world::Worlds;
use bevy_app::{AppExit, PluginGroup, PluginGroupBuilder};
use bevy_ecs::prelude::*;
pub use builder::SwarmBuilder;
use futures::future::BoxFuture;
use parking_lot::RwLock;
use tokio::{sync::mpsc, task};
use tracing::{debug, error, warn};
use crate::{Client, JoinOpts, client_impl::StartClientOpts};
/// A swarm is a way to conveniently control many bots at once, while also
/// being able to control bots at an individual level when desired.
///
/// It can safely be cloned, so there should be no need to wrap them in a Mutex.
///
/// Swarms are created from [`SwarmBuilder`].
///
/// Clients can be added to the swarm later via [`Swarm::add`], and can be
/// removed with [`Client::disconnect`].
#[derive(Clone, Resource)]
pub struct Swarm {
/// 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>>,
pub worlds: Arc<RwLock<Worlds>>,
/// This is used internally to make the client handler function work.
pub(crate) bots_tx: mpsc::UnboundedSender<(Option<crate::Event>, Client)>,
/// This is used internally to make the swarm handler function work.
pub(crate) swarm_tx: mpsc::UnboundedSender<SwarmEvent>,
}
/// An event about something that doesn't have to do with a single bot.
#[derive(Clone, Debug)]
#[non_exhaustive]
pub enum SwarmEvent {
/// All the bots in the swarm have successfully joined the server.
Login,
/// The swarm was created.
///
/// This is only fired once, and it's guaranteed to be the first event to
/// fire.
Init,
/// This is fired every Minecraft tick (in the [`GameTick`] schedule).
Tick,
/// A bot got disconnected from the server.
///
/// If you'd like to implement special auto-reconnect behavior beyond what's
/// built-in, you can disable that with [`SwarmBuilder::reconnect_delay`]
/// and then call [`Swarm::add_with_opts`] with the account and options
/// from this event.
///
/// [`SwarmBuilder::reconnect_delay`]: crate::swarm::SwarmBuilder::reconnect_after
Disconnect(Box<Account>, Box<JoinOpts>),
/// At least one bot received a chat message.
Chat(ChatPacket),
}
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 + Sync>;
/// Make a bot [`Swarm`].
///
/// [`Swarm`]: struct.Swarm.html
///
/// # Examples
/// ```rust,no_run
/// use azalea::{prelude::*, swarm::prelude::*};
/// use std::time::Duration;
///
/// #[derive(Clone, Component, Default)]
/// struct State {}
///
/// #[derive(Clone, Default, Resource)]
/// struct SwarmState {}
///
/// #[tokio::main]
/// async fn main() -> AppExit {
/// let mut accounts = Vec::new();
/// let mut states = Vec::new();
///
/// for i in 0..10 {
/// accounts.push(Account::offline(&format!("bot{i}")));
/// states.push(State::default());
/// }
///
/// SwarmBuilder::new()
/// .add_accounts(accounts.clone())
/// .set_handler(handle)
/// .set_swarm_handler(swarm_handle)
/// .join_delay(Duration::from_millis(1000))
/// .start("localhost")
/// .await
/// }
///
/// async fn handle(bot: Client, event: Event, _state: State) -> eyre::Result<()> {
/// match &event {
/// _ => {}
/// }
/// Ok(())
/// }
///
/// async fn swarm_handle(
/// mut swarm: Swarm,
/// event: SwarmEvent,
/// _state: SwarmState,
/// ) -> eyre::Result<()> {
/// match &event {
/// SwarmEvent::Chat(m) => {
/// println!("{}", m.message().to_ansi());
/// }
/// _ => {}
/// }
/// Ok(())
/// }
impl Swarm {
/// Add a new account to the swarm.
///
/// You can remove it later by calling [`Client::disconnect`].
///
/// # Errors
///
/// Returns an error if the server's address could not be resolved.
pub async fn add<S: Component + Clone>(&self, account: &Account, state: S) -> Client {
self.add_with_opts(account, state, &JoinOpts::default())
.await
}
/// Add a new account to the swarm, using custom options.
///
/// This is useful if you want bots in the same swarm to connect to
/// different addresses. Usually you'll just want [`Self::add`] though.
///
/// # Errors
///
/// Returns an error if the server's address could not be resolved.
pub async fn add_with_opts<S: Component + Clone>(
&self,
account: &Account,
state: S,
join_opts: &JoinOpts,
) -> Client {
debug!(
"add_with_opts called for account {} with opts {join_opts:?}",
account.username()
);
let mut address = self.address.read().clone();
if let Some(custom_server_addr) = join_opts.custom_server_addr.clone() {
address.server = custom_server_addr;
}
if let Some(custom_socket_addr) = join_opts.custom_socket_addr {
address.socket = custom_socket_addr;
}
let server_proxy = join_opts.server_proxy.clone();
let sessionserver_proxy = join_opts.sessionserver_proxy.clone();
let (tx, rx) = mpsc::unbounded_channel();
let client = Client::start_client(StartClientOpts {
ecs_lock: self.ecs.clone(),
account: account.clone(),
connect_opts: ConnectOpts {
address,
server_proxy,
sessionserver_proxy,
},
event_sender: Some(tx),
})
.await;
// add the state to the client
{
let mut ecs = self.ecs.write();
ecs.entity_mut(client.entity).insert(state);
}
let cloned_bot = client.clone();
let swarm_tx = self.swarm_tx.clone();
let bots_tx = self.bots_tx.clone();
let join_opts = join_opts.clone();
task::spawn_local(Self::event_copying_task(
rx, swarm_tx, bots_tx, cloned_bot, join_opts,
));
client
}
/// Copy the events from a client's receiver into bots_tx, until the bot is
/// removed from the ECS.
async fn event_copying_task(
mut rx: mpsc::UnboundedReceiver<crate::Event>,
swarm_tx: mpsc::UnboundedSender<SwarmEvent>,
bots_tx: mpsc::UnboundedSender<(Option<crate::Event>, Client)>,
bot: Client,
join_opts: JoinOpts,
) {
while let Some(event) = rx.recv().await {
if rx.len() > 1_000 {
static WARNED_1_000: AtomicBool = AtomicBool::new(false);
if !WARNED_1_000.swap(true, atomic::Ordering::Relaxed) {
warn!(
"The client's Event channel has more than 1,000 items! If you don't need it, consider disabling the `packet-event` feature for `azalea`."
)
}
if rx.len() > 10_000 {
static WARNED_10_000: AtomicBool = AtomicBool::new(false);
if !WARNED_10_000.swap(true, atomic::Ordering::Relaxed) {
warn!("The client's Event channel has more than 10,000 items!!")
}
if rx.len() > 100_000 {
static WARNED_100_000: AtomicBool = AtomicBool::new(false);
if !WARNED_100_000.swap(true, atomic::Ordering::Relaxed) {
warn!("The client's Event channel has more than 100,000 items!!!")
}
if rx.len() > 1_000_000 {
static WARNED_1_000_000: AtomicBool = AtomicBool::new(false);
if !WARNED_1_000_000.swap(true, atomic::Ordering::Relaxed) {
warn!(
"The client's Event channel has more than 1,000,000 items!!!! your code is almost certainly leaking memory"
)
}
}
}
}
}
if let crate::Event::Disconnect(_) = event {
debug!(
"Sending SwarmEvent::Disconnect due to receiving an Event::Disconnect from client {}",
bot.entity
);
let account = bot.account();
swarm_tx
.send(SwarmEvent::Disconnect(
Box::new(account),
Box::new(join_opts.clone()),
))
.unwrap();
}
// we can't handle events here (since we can't copy the handler),
// they're handled above in SwarmBuilder::start
if let Err(e) = bots_tx.send((Some(event), bot.clone())) {
error!(
"Error sending event to swarm, aborting event_copying_task for {}: {e}",
bot.entity
);
break;
}
}
debug!(
"client sender ended for {}, this won't trigger SwarmEvent::Disconnect unless the client already sent its own disconnect event",
bot.entity
);
}
/// Get an array of ECS [`Entity`]s for all [`LocalEntity`]s in our world.
/// This will include clients that were disconnected without being removed
/// from the ECS.
///
/// [`LocalEntity`]: azalea_entity::LocalEntity
pub fn client_entities(&self) -> Box<[Entity]> {
let mut ecs = self.ecs.write();
let mut query = ecs.query_filtered::<Entity, With<LocalEntity>>();
query.iter(&ecs).collect::<Box<[Entity]>>()
}
/// End the entire swarm and return from [`SwarmBuilder::start`].
///
/// You should typically avoid calling this if you intend on creating the
/// swarm again, because creating an entirely new swarm can be a
/// relatively expensive process.
///
/// If you only want to change the server that the bots are connecting to,
/// it may be better to call [`Swarm::add_with_opts`] with a different
/// server address.
///
/// This is also implemented on [`Client`] as [`Client::exit`].
pub fn exit(&self) {
self.ecs.write().write_message(AppExit::Success);
}
}
impl IntoIterator for Swarm {
type Item = Client;
type IntoIter = std::vec::IntoIter<Self::Item>;
/// Iterate over the bots in this swarm.
///
/// ```rust,no_run
/// # use azalea::{prelude::*, swarm::prelude::*};
/// #[derive(Clone, Component)]
/// # pub struct State;
/// # fn example(swarm: Swarm) {
/// for bot in swarm {
/// let state = bot.component::<State>();
/// // ...
/// }
/// # }
/// ```
fn into_iter(self) -> Self::IntoIter {
let client_entities = self.client_entities();
client_entities
.into_iter()
.map(|entity| Client::new(entity, self.ecs.clone()))
.collect::<Box<[Client]>>()
.into_iter()
}
}
/// This plugin group will add all the default plugins necessary for swarms to
/// work.
pub struct DefaultSwarmPlugins;
impl PluginGroup for DefaultSwarmPlugins {
fn build(self) -> PluginGroupBuilder {
PluginGroupBuilder::start::<Self>()
.add(chat::SwarmChatPlugin)
.add(events::SwarmPlugin)
}
}
/// A marker that can be used in place of a SwarmState in [`SwarmBuilder`].
///
/// You probably don't need to use this manually since the compiler will infer
/// it for you.
#[derive(Clone, Default, Resource)]
pub struct NoSwarmState;
|