diff options
| author | mat <git@matdoes.dev> | 2025-06-02 04:12:07 -0100 |
|---|---|---|
| committer | mat <git@matdoes.dev> | 2025-06-02 04:12:07 -0100 |
| commit | 8da179b221ae4a7bfd02245b3cb6357464adbc43 (patch) | |
| tree | 5f94f8dd7f4b9bf62b66d3d5468de6449da05070 /azalea-client/src/plugins/join.rs | |
| parent | 3d121722d7b995de1346f9838df15386aea5acc8 (diff) | |
| download | azalea-drasl-8da179b221ae4a7bfd02245b3cb6357464adbc43.tar.xz | |
simplify some join logic so the Entity is returned even on connection error
Diffstat (limited to 'azalea-client/src/plugins/join.rs')
| -rw-r--r-- | azalea-client/src/plugins/join.rs | 54 |
1 files changed, 11 insertions, 43 deletions
diff --git a/azalea-client/src/plugins/join.rs b/azalea-client/src/plugins/join.rs index a3447782..09eeff59 100644 --- a/azalea-client/src/plugins/join.rs +++ b/azalea-client/src/plugins/join.rs @@ -1,4 +1,4 @@ -use std::{io, net::SocketAddr, sync::Arc}; +use std::{net::SocketAddr, sync::Arc}; use azalea_entity::{LocalEntity, indexing::EntityUuidIndex}; use azalea_protocol::{ @@ -20,7 +20,7 @@ use tracing::{debug, warn}; use super::events::LocalPlayerEvents; use crate::{ - Account, JoinError, LocalPlayerBundle, + Account, LocalPlayerBundle, connection::RawConnection, packet::login::{InLoginState, SendLoginPacketEvent}, }; @@ -36,7 +36,6 @@ impl Plugin for JoinPlugin { ( handle_start_join_server_event.before(super::login::poll_auth_task), poll_create_connection_task, - handle_connection_failed_events, ) .chain(), ); @@ -53,7 +52,8 @@ pub struct StartJoinServerEvent { pub connect_opts: ConnectOpts, pub event_sender: Option<mpsc::UnboundedSender<crate::Event>>, - pub start_join_callback_tx: Option<StartJoinCallback>, + // this is mpsc instead of oneshot so it can be cloned (since it's sent in an event) + pub start_join_callback_tx: Option<mpsc::UnboundedSender<Entity>>, } /// Options for how the connection to the server will be made. These are @@ -79,11 +79,6 @@ pub struct ConnectionFailedEvent { pub error: ConnectionError, } -// this is mpsc instead of oneshot so it can be cloned (since it's sent in an -// event) -#[derive(Component, Debug, Clone)] -pub struct StartJoinCallback(pub mpsc::UnboundedSender<Result<Entity, JoinError>>); - pub fn handle_start_join_server_event( mut commands: Commands, mut events: EventReader<StartJoinServerEvent>, @@ -103,7 +98,7 @@ pub fn handle_start_join_server_event( warn!( "Received StartJoinServerEvent for {entity:?} but it's already connected. Ignoring the event but replying with Ok." ); - let _ = start_join_callback_tx.0.send(Ok(entity)); + let _ = start_join_callback_tx.send(entity); } else { warn!( "Received StartJoinServerEvent for {entity:?} but it's already connected. Ignoring the event." @@ -121,6 +116,10 @@ pub fn handle_start_join_server_event( entity }; + if let Some(start_join_callback) = &event.start_join_callback_tx { + let _ = start_join_callback.send(entity); + } + let mut entity_mut = commands.entity(entity); entity_mut.insert(( @@ -141,9 +140,6 @@ pub fn handle_start_join_server_event( // handle receiving packets entity_mut.insert(LocalPlayerEvents(event_sender.clone())); } - if let Some(start_join_callback) = &event.start_join_callback_tx { - entity_mut.insert(start_join_callback.clone()); - } let task_pool = IoTaskPool::get(); let connect_opts = event.connect_opts.clone(); @@ -184,15 +180,10 @@ pub struct CreateConnectionTask(pub Task<Result<LoginConn, ConnectionError>>); pub fn poll_create_connection_task( mut commands: Commands, - mut query: Query<( - Entity, - &mut CreateConnectionTask, - &Account, - Option<&StartJoinCallback>, - )>, + mut query: Query<(Entity, &mut CreateConnectionTask, &Account)>, mut connection_failed_events: EventWriter<ConnectionFailedEvent>, ) { - for (entity, mut task, account, mut start_join_callback) in query.iter_mut() { + for (entity, mut task, account) in query.iter_mut() { if let Some(poll_res) = future::block_on(future::poll_once(&mut task.0)) { let mut entity_mut = commands.entity(entity); entity_mut.remove::<CreateConnectionTask>(); @@ -238,29 +229,6 @@ pub fn poll_create_connection_task( profile_id: account.uuid_or_offline(), }, )); - - if let Some(cb) = start_join_callback.take() { - let _ = cb.0.send(Ok(entity)); - } } } } - -pub fn handle_connection_failed_events( - mut events: EventReader<ConnectionFailedEvent>, - query: Query<&StartJoinCallback>, -) { - for event in events.read() { - let Ok(start_join_callback) = query.get(event.entity) else { - // the StartJoinCallback isn't required to be present, so this is fine - continue; - }; - - // io::Error isn't clonable, so we create a new one based on the `kind` and - // `to_string`, - let ConnectionError::Io(err) = &event.error; - let cloned_err = ConnectionError::Io(io::Error::new(err.kind(), err.to_string())); - - let _ = start_join_callback.0.send(Err(cloned_err.into())); - } -} |
