From 53e630f73f5fcc82a6818448cdb105abb75dfcc7 Mon Sep 17 00:00:00 2001 From: mat Date: Fri, 23 Jan 2026 18:09:59 -0930 Subject: add Event::ConnectionFailed and add more plugins by default when using Client::join --- azalea-client/src/plugins/join.rs | 8 ++++++-- azalea/src/client_impl/mod.rs | 4 +++- azalea/src/events.rs | 24 +++++++++++++++++++++++- azalea/src/swarm/builder.rs | 9 +++++---- 4 files changed, 37 insertions(+), 8 deletions(-) diff --git a/azalea-client/src/plugins/join.rs b/azalea-client/src/plugins/join.rs index a6c9c586..0f5747e9 100644 --- a/azalea-client/src/plugins/join.rs +++ b/azalea-client/src/plugins/join.rs @@ -86,7 +86,8 @@ pub struct ConnectOpts { #[derive(Message)] pub struct ConnectionFailedEvent { pub entity: Entity, - pub error: ConnectionError, + // wrap it in Arc so it can be cloned + pub error: Arc, } pub fn handle_start_join_server_event( @@ -197,7 +198,10 @@ pub fn poll_create_connection_task( Ok(conn) => conn, Err(error) => { warn!("failed to create connection: {error}"); - connection_failed_events.write(ConnectionFailedEvent { entity, error }); + connection_failed_events.write(ConnectionFailedEvent { + entity, + error: Arc::new(error), + }); return; } }; diff --git a/azalea/src/client_impl/mod.rs b/azalea/src/client_impl/mod.rs index d55e5a38..343b8aef 100644 --- a/azalea/src/client_impl/mod.rs +++ b/azalea/src/client_impl/mod.rs @@ -33,8 +33,10 @@ use tokio::sync::mpsc; use uuid::Uuid; use crate::{ + bot::DefaultBotPlugins, entity_ref::EntityRef, events::{Event, LocalPlayerEvents}, + swarm::DefaultSwarmPlugins, }; pub mod attack; @@ -86,7 +88,7 @@ impl StartClientOpts { event_sender: Option>, ) -> StartClientOpts { let mut app = App::new(); - app.add_plugins(DefaultPlugins); + app.add_plugins((DefaultPlugins, DefaultBotPlugins, DefaultSwarmPlugins)); // appexit_rx is unused here since the user should be able to handle it // themselves if they're using StartClientOpts::new diff --git a/azalea/src/events.rs b/azalea/src/events.rs index b0bc8cd1..fcd47e2f 100644 --- a/azalea/src/events.rs +++ b/azalea/src/events.rs @@ -4,9 +4,12 @@ use std::sync::Arc; use azalea_chat::FormattedText; +use azalea_client::join::ConnectionFailedEvent; use azalea_core::{entity_id::MinecraftEntityId, position::ChunkPos, tick::GameTick}; use azalea_entity::{Dead, InLoadedChunk}; -use azalea_protocol::packets::game::c_player_combat_kill::ClientboundPlayerCombatKill; +use azalea_protocol::{ + connect::ConnectionError, packets::game::c_player_combat_kill::ClientboundPlayerCombatKill, +}; use azalea_world::WorldName; use bevy_app::{App, Plugin, PreUpdate, Update}; use bevy_ecs::prelude::*; @@ -122,7 +125,14 @@ pub enum Event { /// A `KeepAlive` packet was sent by the server. KeepAlive(u64), /// The client disconnected from the server. + /// + /// Also see [`Event::ConnectionFailed`]. Disconnect(Option), + /// The initial connection to the server failed. + /// + /// Also see [`Event::Disconnect`], and the related ECS event + /// [`ConnectionFailedEvent`]. + ConnectionFailed(Arc), ReceiveChunk(ChunkPos), } @@ -151,6 +161,7 @@ impl Plugin for EventsPlugin { keepalive_listener, death_listener.after(azalea_client::packet::death_event_on_0_health), disconnect_listener, + connection_failed_listener, receive_chunk_listener, ), ) @@ -301,6 +312,17 @@ pub fn disconnect_listener( } } +pub fn connection_failed_listener( + query: Query<&LocalPlayerEvents>, + mut events: MessageReader, +) { + for event in events.read() { + if let Ok(local_player_events) = query.get(event.entity) { + let _ = local_player_events.send(Event::ConnectionFailed(event.error.clone())); + } + } +} + pub fn receive_chunk_listener( query: Query<&LocalPlayerEvents>, mut events: MessageReader, diff --git a/azalea/src/swarm/builder.rs b/azalea/src/swarm/builder.rs index f42b4d4a..0adb4f26 100644 --- a/azalea/src/swarm/builder.rs +++ b/azalea/src/swarm/builder.rs @@ -75,10 +75,11 @@ impl SwarmBuilder { /// Start creating the swarm. #[must_use] pub fn new() -> Self { - Self::new_without_plugins() - .add_plugins(DefaultPlugins) - .add_plugins(DefaultBotPlugins) - .add_plugins(DefaultSwarmPlugins) + Self::new_without_plugins().add_plugins(( + DefaultPlugins, + DefaultBotPlugins, + DefaultSwarmPlugins, + )) } /// [`Self::new`] but without adding the plugins by default. -- cgit v1.2.3