From 9a40b65bc1912298a43de43fd6e8477a8622a832 Mon Sep 17 00:00:00 2001 From: mat <27899617+mat-1@users.noreply.github.com> Date: Fri, 2 May 2025 15:55:58 -0500 Subject: Add AutoReconnectPlugin (#221) * add AutoReconnectPlugin * merge main * start simplifying swarm internals * fix Swarm::into_iter, handler functions, DisconnectEvent, and add some more docs * add ClientBuilder/SwarmBuilder::reconnect_after * fix a doctest * reword SwarmEvent::Disconnect doc * better behavior when we try to join twice * reconnect on ConnectionFailedEvent too * autoreconnect is less breaking now --- azalea/src/lib.rs | 26 +++++++++++++++++++++++++- 1 file changed, 25 insertions(+), 1 deletion(-) (limited to 'azalea/src/lib.rs') diff --git a/azalea/src/lib.rs b/azalea/src/lib.rs index d63ea6c3..3f388e42 100644 --- a/azalea/src/lib.rs +++ b/azalea/src/lib.rs @@ -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; @@ -126,7 +127,12 @@ impl ClientBuilder { /// Set the function that's called every time a bot receives an [`Event`]. /// This is the way to handle normal per-bot events. /// - /// Currently you can have up to one client handler. + /// Currently, you can have up to one client handler. + /// + /// Note that if you're creating clients directly from the ECS using + /// [`StartJoinServerEvent`] and the client wasn't already in the ECS, then + /// the handler function won't be called for that client. This shouldn't be + /// a concern for most bots, though. /// /// ``` /// # use azalea::prelude::*; @@ -139,6 +145,8 @@ impl ClientBuilder { /// Ok(()) /// } /// ``` + /// + /// [`StartJoinServerEvent`]: azalea_client::join::StartJoinServerEvent #[must_use] pub fn set_handler(self, handler: HandleFn) -> ClientBuilder where @@ -169,6 +177,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>) -> 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. -- cgit v1.2.3