1
2
Fork 0
mirror of https://github.com/mat-1/azalea.git synced 2025-08-02 14:26:04 +00:00

add ClientBuilder/SwarmBuilder::reconnect_after

This commit is contained in:
mat 2025-05-02 21:14:04 +02:00
parent b617a96c72
commit 4c810c7577
3 changed files with 63 additions and 6 deletions

View file

@ -1,6 +1,6 @@
//! Auto-reconnect to the server when the client is kicked.
//!
//! This is enabled by default.
//! See [`AutoReconnectPlugin`] for more information.
use std::time::{Duration, Instant};
@ -14,10 +14,20 @@ use super::{
};
use crate::Account;
/// The default delay that Azalea will use for reconnecting our clients. See
/// [`AutoReconnectPlugin`] for more information.
pub const DEFAULT_RECONNECT_DELAY: Duration = Duration::from_secs(5);
/// A default plugin that makes clients automatically rejoin the server when
/// they're disconnected. The reconnect delay is configurable globally or
/// per-client with the [`AutoReconnectDelay`] resource/component. Auto
/// reconnecting can be disabled by removing the resource from the ECS.
///
/// The delay defaults to [`DEFAULT_RECONNECT_DELAY`].
pub struct AutoReconnectPlugin;
impl Plugin for AutoReconnectPlugin {
fn build(&self, app: &mut App) {
app.insert_resource(AutoReconnectDelay::new(Duration::from_secs(5)))
app.insert_resource(AutoReconnectDelay::new(DEFAULT_RECONNECT_DELAY))
.add_systems(
Update,
(start_rejoin_on_disconnect, rejoin_after_delay)

View file

@ -14,6 +14,7 @@ pub mod prelude;
pub mod swarm;
use std::net::SocketAddr;
use std::time::Duration;
use app::Plugins;
pub use azalea_auth as auth;
@ -174,6 +175,22 @@ where
self
}
/// Configures the auto-reconnection behavior for our bot.
///
/// If this is `Some`, then it'll set the default reconnection delay for our
/// bot (how long it'll wait after being kicked before it tries
/// rejoining). if it's `None`, then auto-reconnecting will be disabled.
///
/// If this function isn't called, then our client will reconnect after
/// [`DEFAULT_RECONNECT_DELAY`].
///
/// [`DEFAULT_RECONNECT_DELAY`]: azalea_client::auto_reconnect::DEFAULT_RECONNECT_DELAY
#[must_use]
pub fn reconnect_after(mut self, delay: impl Into<Option<Duration>>) -> Self {
self.swarm.reconnect_after = delay.into();
self
}
/// Build this `ClientBuilder` into an actual [`Client`] and join the given
/// server. If the client can't join, it'll keep retrying forever until it
/// can.

View file

@ -18,8 +18,11 @@ use std::{
};
use azalea_client::{
Account, Client, DefaultPlugins, Event, JoinError, StartClientOpts, chat::ChatPacket,
join::ConnectOpts, start_ecs_runner,
Account, Client, DefaultPlugins, Event, JoinError, StartClientOpts,
auto_reconnect::{AutoReconnectDelay, DEFAULT_RECONNECT_DELAY},
chat::ChatPacket,
join::ConnectOpts,
start_ecs_runner,
};
use azalea_entity::LocalEntity;
use azalea_protocol::{ServerAddress, resolver};
@ -90,7 +93,11 @@ where
/// None to have every bot connect at the same time. None is different than
/// a duration of 0, since if a duration is present the bots will wait for
/// the previous one to be ready.
pub(crate) join_delay: Option<std::time::Duration>,
pub(crate) join_delay: Option<Duration>,
/// The default reconnection delay for our bots. This will change the value
/// of the `AutoReconnectDelay` resource.
pub(crate) reconnect_after: Option<Duration>,
}
impl SwarmBuilder<NoState, NoSwarmState, (), ()> {
/// Start creating the swarm.
@ -139,6 +146,7 @@ impl SwarmBuilder<NoState, NoSwarmState, (), ()> {
handler: None,
swarm_handler: None,
join_delay: None,
reconnect_after: Some(DEFAULT_RECONNECT_DELAY),
}
}
}
@ -257,6 +265,7 @@ where
Box::pin(handler(swarm, event, state))
})),
join_delay: self.join_delay,
reconnect_after: self.reconnect_after,
}
}
}
@ -352,11 +361,25 @@ where
/// field, however, the bots will wait for the previous one to have
/// connected and *then* they'll wait the given duration.
#[must_use]
pub fn join_delay(mut self, delay: std::time::Duration) -> Self {
pub fn join_delay(mut self, delay: Duration) -> Self {
self.join_delay = Some(delay);
self
}
/// Configures the auto-reconnection behavior for our bots.
///
/// If this is `Some`, then it'll set the default reconnection delay for our
/// bots (how long they'll wait after being kicked before they try
/// rejoining). if it's `None`, then auto-reconnecting will be disabled.
///
/// If this function isn't called, then our clients will reconnect after
/// [`DEFAULT_RECONNECT_DELAY`].
#[must_use]
pub fn reconnect_after(mut self, delay: impl Into<Option<Duration>>) -> Self {
self.reconnect_after = delay.into();
self
}
/// Build this `SwarmBuilder` into an actual [`Swarm`] and join the given
/// server.
///
@ -432,6 +455,13 @@ where
let mut ecs = ecs_lock.lock();
ecs.insert_resource(swarm.clone());
ecs.insert_resource(self.swarm_state.clone());
if let Some(reconnect_after) = self.reconnect_after {
ecs.insert_resource(AutoReconnectDelay {
delay: reconnect_after,
});
} else {
ecs.remove_resource::<AutoReconnectDelay>();
}
ecs.run_schedule(main_schedule_label);
ecs.clear_trackers();
}