Skip to content

Commit 53e630f

Browse files
committed
add Event::ConnectionFailed and add more plugins by default when using Client::join
1 parent b7ad0e6 commit 53e630f

File tree

4 files changed

+37
-8
lines changed

4 files changed

+37
-8
lines changed

azalea-client/src/plugins/join.rs

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -86,7 +86,8 @@ pub struct ConnectOpts {
8686
#[derive(Message)]
8787
pub struct ConnectionFailedEvent {
8888
pub entity: Entity,
89-
pub error: ConnectionError,
89+
// wrap it in Arc so it can be cloned
90+
pub error: Arc<ConnectionError>,
9091
}
9192

9293
pub fn handle_start_join_server_event(
@@ -197,7 +198,10 @@ pub fn poll_create_connection_task(
197198
Ok(conn) => conn,
198199
Err(error) => {
199200
warn!("failed to create connection: {error}");
200-
connection_failed_events.write(ConnectionFailedEvent { entity, error });
201+
connection_failed_events.write(ConnectionFailedEvent {
202+
entity,
203+
error: Arc::new(error),
204+
});
201205
return;
202206
}
203207
};

azalea/src/client_impl/mod.rs

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -33,8 +33,10 @@ use tokio::sync::mpsc;
3333
use uuid::Uuid;
3434

3535
use crate::{
36+
bot::DefaultBotPlugins,
3637
entity_ref::EntityRef,
3738
events::{Event, LocalPlayerEvents},
39+
swarm::DefaultSwarmPlugins,
3840
};
3941

4042
pub mod attack;
@@ -86,7 +88,7 @@ impl StartClientOpts {
8688
event_sender: Option<mpsc::UnboundedSender<Event>>,
8789
) -> StartClientOpts {
8890
let mut app = App::new();
89-
app.add_plugins(DefaultPlugins);
91+
app.add_plugins((DefaultPlugins, DefaultBotPlugins, DefaultSwarmPlugins));
9092

9193
// appexit_rx is unused here since the user should be able to handle it
9294
// themselves if they're using StartClientOpts::new

azalea/src/events.rs

Lines changed: 23 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,9 +4,12 @@
44
use std::sync::Arc;
55

66
use azalea_chat::FormattedText;
7+
use azalea_client::join::ConnectionFailedEvent;
78
use azalea_core::{entity_id::MinecraftEntityId, position::ChunkPos, tick::GameTick};
89
use azalea_entity::{Dead, InLoadedChunk};
9-
use azalea_protocol::packets::game::c_player_combat_kill::ClientboundPlayerCombatKill;
10+
use azalea_protocol::{
11+
connect::ConnectionError, packets::game::c_player_combat_kill::ClientboundPlayerCombatKill,
12+
};
1013
use azalea_world::WorldName;
1114
use bevy_app::{App, Plugin, PreUpdate, Update};
1215
use bevy_ecs::prelude::*;
@@ -122,7 +125,14 @@ pub enum Event {
122125
/// A `KeepAlive` packet was sent by the server.
123126
KeepAlive(u64),
124127
/// The client disconnected from the server.
128+
///
129+
/// Also see [`Event::ConnectionFailed`].
125130
Disconnect(Option<FormattedText>),
131+
/// The initial connection to the server failed.
132+
///
133+
/// Also see [`Event::Disconnect`], and the related ECS event
134+
/// [`ConnectionFailedEvent`].
135+
ConnectionFailed(Arc<ConnectionError>),
126136
ReceiveChunk(ChunkPos),
127137
}
128138

@@ -151,6 +161,7 @@ impl Plugin for EventsPlugin {
151161
keepalive_listener,
152162
death_listener.after(azalea_client::packet::death_event_on_0_health),
153163
disconnect_listener,
164+
connection_failed_listener,
154165
receive_chunk_listener,
155166
),
156167
)
@@ -301,6 +312,17 @@ pub fn disconnect_listener(
301312
}
302313
}
303314

315+
pub fn connection_failed_listener(
316+
query: Query<&LocalPlayerEvents>,
317+
mut events: MessageReader<ConnectionFailedEvent>,
318+
) {
319+
for event in events.read() {
320+
if let Ok(local_player_events) = query.get(event.entity) {
321+
let _ = local_player_events.send(Event::ConnectionFailed(event.error.clone()));
322+
}
323+
}
324+
}
325+
304326
pub fn receive_chunk_listener(
305327
query: Query<&LocalPlayerEvents>,
306328
mut events: MessageReader<ReceiveChunkEvent>,

azalea/src/swarm/builder.rs

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -75,10 +75,11 @@ impl SwarmBuilder<NoState, NoSwarmState, (), ()> {
7575
/// Start creating the swarm.
7676
#[must_use]
7777
pub fn new() -> Self {
78-
Self::new_without_plugins()
79-
.add_plugins(DefaultPlugins)
80-
.add_plugins(DefaultBotPlugins)
81-
.add_plugins(DefaultSwarmPlugins)
78+
Self::new_without_plugins().add_plugins((
79+
DefaultPlugins,
80+
DefaultBotPlugins,
81+
DefaultSwarmPlugins,
82+
))
8283
}
8384

8485
/// [`Self::new`] but without adding the plugins by default.

0 commit comments

Comments
 (0)