aboutsummaryrefslogtreecommitdiff
path: root/azalea-client/src/client.rs
diff options
context:
space:
mode:
authormat <27899617+mat-1@users.noreply.github.com>2025-05-02 15:55:58 -0500
committerGitHub <noreply@github.com>2025-05-02 15:55:58 -0500
commit9a40b65bc1912298a43de43fd6e8477a8622a832 (patch)
treec429c62489926d6bbfc1675fea5a1860378d7a00 /azalea-client/src/client.rs
parent52e34de95cd64a1c8ae1177cd7bc1d67fbab3c71 (diff)
downloadazalea-drasl-9a40b65bc1912298a43de43fd6e8477a8622a832.tar.xz
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
Diffstat (limited to 'azalea-client/src/client.rs')
-rw-r--r--azalea-client/src/client.rs65
1 files changed, 29 insertions, 36 deletions
diff --git a/azalea-client/src/client.rs b/azalea-client/src/client.rs
index 2ebf44b5..dc9a3d3e 100644
--- a/azalea-client/src/client.rs
+++ b/azalea-client/src/client.rs
@@ -54,7 +54,7 @@ use crate::{
events::Event,
interact::CurrentSequenceNumber,
inventory::Inventory,
- join::{StartJoinCallback, StartJoinServerEvent},
+ join::{ConnectOpts, StartJoinCallback, StartJoinServerEvent},
local_player::{
GameProfileComponent, Hunger, InstanceHolder, PermissionLevel, PlayerAbilities, TabList,
},
@@ -107,22 +107,20 @@ pub enum JoinError {
Disconnect { reason: FormattedText },
}
-pub struct StartClientOpts<'a> {
+pub struct StartClientOpts {
pub ecs_lock: Arc<Mutex<World>>,
- pub account: &'a Account,
- pub address: &'a ServerAddress,
- pub resolved_address: &'a SocketAddr,
- pub proxy: Option<Proxy>,
+ pub account: Account,
+ pub connect_opts: ConnectOpts,
pub event_sender: Option<mpsc::UnboundedSender<Event>>,
}
-impl<'a> StartClientOpts<'a> {
+impl StartClientOpts {
pub fn new(
- account: &'a Account,
- address: &'a ServerAddress,
- resolved_address: &'a SocketAddr,
+ account: Account,
+ address: ServerAddress,
+ resolved_address: SocketAddr,
event_sender: Option<mpsc::UnboundedSender<Event>>,
- ) -> StartClientOpts<'a> {
+ ) -> StartClientOpts {
let mut app = App::new();
app.add_plugins(DefaultPlugins);
@@ -132,15 +130,17 @@ impl<'a> StartClientOpts<'a> {
Self {
ecs_lock,
account,
- address,
- resolved_address,
- proxy: None,
+ connect_opts: ConnectOpts {
+ address,
+ resolved_address,
+ proxy: None,
+ },
event_sender,
}
}
pub fn proxy(mut self, proxy: Proxy) -> Self {
- self.proxy = Some(proxy);
+ self.connect_opts.proxy = Some(proxy);
self
}
}
@@ -173,14 +173,14 @@ impl Client {
/// #[tokio::main]
/// async fn main() -> Result<(), Box<dyn std::error::Error>> {
/// let account = Account::offline("bot");
- /// let (client, rx) = Client::join(&account, "localhost").await?;
+ /// let (client, rx) = Client::join(account, "localhost").await?;
/// client.chat("Hello, world!");
/// client.disconnect();
/// Ok(())
/// }
/// ```
pub async fn join(
- account: &Account,
+ account: Account,
address: impl TryInto<ServerAddress>,
) -> Result<(Self, mpsc::UnboundedReceiver<Event>), JoinError> {
let address: ServerAddress = address.try_into().map_err(|_| JoinError::InvalidAddress)?;
@@ -189,8 +189,8 @@ impl Client {
let client = Self::start_client(StartClientOpts::new(
account,
- &address,
- &resolved_address,
+ address,
+ resolved_address,
Some(tx),
))
.await?;
@@ -198,7 +198,7 @@ impl Client {
}
pub async fn join_with_proxy(
- account: &Account,
+ account: Account,
address: impl TryInto<ServerAddress>,
proxy: Proxy,
) -> Result<(Self, mpsc::UnboundedReceiver<Event>), JoinError> {
@@ -207,7 +207,7 @@ impl Client {
let (tx, rx) = mpsc::unbounded_channel();
let client = Self::start_client(
- StartClientOpts::new(account, &address, &resolved_address, Some(tx)).proxy(proxy),
+ StartClientOpts::new(account, address, resolved_address, Some(tx)).proxy(proxy),
)
.await?;
Ok((client, rx))
@@ -219,28 +219,21 @@ impl Client {
StartClientOpts {
ecs_lock,
account,
- address,
- resolved_address,
- proxy,
+ connect_opts,
event_sender,
- }: StartClientOpts<'_>,
+ }: StartClientOpts,
) -> Result<Self, JoinError> {
// send a StartJoinServerEvent
let (start_join_callback_tx, mut start_join_callback_rx) =
mpsc::unbounded_channel::<Result<Entity, JoinError>>();
- {
- let mut ecs = ecs_lock.lock();
- ecs.send_event(StartJoinServerEvent {
- account: account.clone(),
- address: address.clone(),
- resolved_address: *resolved_address,
- proxy,
- event_sender: event_sender.clone(),
- start_join_callback_tx: Some(StartJoinCallback(start_join_callback_tx)),
- });
- }
+ ecs_lock.lock().send_event(StartJoinServerEvent {
+ account,
+ connect_opts,
+ event_sender,
+ start_join_callback_tx: Some(StartJoinCallback(start_join_callback_tx)),
+ });
let entity = start_join_callback_rx.recv().await.expect(
"StartJoinCallback should not be dropped before sending a message, this is a bug in Azalea",