azalea_client/plugins/
disconnect.rs1use azalea_chat::FormattedText;
4use azalea_entity::{
5 EntityBundle, HasClientLoaded, InLoadedChunk, LocalEntity, metadata::PlayerMetadataBundle,
6};
7use bevy_app::{App, Plugin, PostUpdate};
8use bevy_ecs::prelude::*;
9use derive_more::Deref;
10use tracing::info;
11
12use super::login::IsAuthenticated;
13#[cfg(feature = "online-mode")]
14use crate::chat_signing;
15use crate::{
16 client::JoinedClientBundle, connection::RawConnection, local_player::WorldHolder, mining,
17 tick_counter::TicksConnected,
18};
19
20pub struct DisconnectPlugin;
21impl Plugin for DisconnectPlugin {
22 fn build(&self, app: &mut App) {
23 app.add_message::<DisconnectEvent>().add_systems(
24 PostUpdate,
25 (
26 update_read_packets_task_running_component,
27 remove_components_from_disconnected_players,
28 disconnect_on_connection_dead,
34 )
35 .chain(),
36 );
37 }
38}
39
40#[derive(Message)]
50pub struct DisconnectEvent {
51 pub entity: Entity,
52 pub reason: Option<FormattedText>,
53}
54
55#[derive(Bundle)]
60pub struct RemoveOnDisconnectBundle {
61 pub joined_client: JoinedClientBundle,
62
63 pub entity: EntityBundle,
64 pub world_holder: WorldHolder,
65 pub player_metadata: PlayerMetadataBundle,
66 pub in_loaded_chunk: InLoadedChunk,
67 pub raw_connection: RawConnection,
69 pub is_connection_alive: IsConnectionAlive,
71 #[cfg(feature = "online-mode")]
73 pub chat_signing_session: chat_signing::ChatSigningSession,
74 pub is_authenticated: IsAuthenticated,
76 pub has_client_loaded: HasClientLoaded,
78 pub ticks_alive: TicksConnected,
80
81 pub mining: mining::Mining,
84}
85
86pub fn remove_components_from_disconnected_players(
89 mut commands: Commands,
90 mut events: MessageReader<DisconnectEvent>,
91 mut loaded_by_query: Query<&mut azalea_entity::LoadedBy>,
92) {
93 for DisconnectEvent { entity, reason } in events.read() {
94 info!(
95 "A client {entity:?} was disconnected{}",
96 if let Some(reason) = reason {
97 format!(": {reason}")
98 } else {
99 "".to_owned()
100 }
101 );
102 commands
103 .entity(*entity)
104 .remove::<RemoveOnDisconnectBundle>();
105 for mut loaded_by in &mut loaded_by_query.iter_mut() {
112 loaded_by.remove(entity);
113 }
114 }
115}
116
117#[derive(Clone, Component, Copy, Debug, Deref)]
118pub struct IsConnectionAlive(bool);
119
120fn update_read_packets_task_running_component(
121 query: Query<(Entity, &RawConnection)>,
122 mut commands: Commands,
123) {
124 for (entity, raw_connection) in &query {
125 let running = raw_connection.is_alive();
126 commands.entity(entity).insert(IsConnectionAlive(running));
127 }
128}
129
130#[allow(clippy::type_complexity)]
131fn disconnect_on_connection_dead(
132 query: Query<(Entity, &IsConnectionAlive), (Changed<IsConnectionAlive>, With<LocalEntity>)>,
133 mut disconnect_events: MessageWriter<DisconnectEvent>,
134) {
135 for (entity, &is_connection_alive) in &query {
136 if !*is_connection_alive {
137 disconnect_events.write(DisconnectEvent {
138 entity,
139 reason: None,
140 });
141 }
142 }
143}