Skip to content

Commit

Permalink
addd disconnect reason (#114)
Browse files Browse the repository at this point in the history
  • Loading branch information
1zun4 authored Dec 2, 2023
1 parent d21e496 commit fbee81e
Show file tree
Hide file tree
Showing 5 changed files with 18 additions and 3 deletions.
1 change: 1 addition & 0 deletions azalea-client/src/client.rs
Original file line number Diff line number Diff line change
Expand Up @@ -422,6 +422,7 @@ impl Client {
pub fn disconnect(&self) {
self.ecs.lock().send_event(DisconnectEvent {
entity: self.entity,
reason: None,
});
}

Expand Down
5 changes: 3 additions & 2 deletions azalea-client/src/disconnect.rs
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,7 @@ impl Plugin for DisconnectPlugin {
#[derive(Event)]
pub struct DisconnectEvent {
pub entity: Entity,
pub reason: Option<String>,
}

/// System that removes the [`JoinedClientBundle`] from the entity when it
Expand All @@ -41,7 +42,7 @@ pub fn remove_components_from_disconnected_players(
mut commands: Commands,
mut events: EventReader<DisconnectEvent>,
) {
for DisconnectEvent { entity } in events.read() {
for DisconnectEvent { entity, .. } in events.read() {
commands.entity(*entity).remove::<JoinedClientBundle>();
}
}
Expand All @@ -64,7 +65,7 @@ fn disconnect_on_connection_dead(
) {
for (entity, &is_connection_alive) in &query {
if !*is_connection_alive {
disconnect_events.send(DisconnectEvent { entity });
disconnect_events.send(DisconnectEvent { entity, reason: None });
}
}
}
13 changes: 12 additions & 1 deletion azalea-client/src/events.rs
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@ use crate::{
AddPlayerEvent, DeathEvent, KeepAliveEvent, PacketEvent, RemovePlayerEvent,
UpdatePlayerEvent,
},
PlayerInfo,
PlayerInfo, disconnect::DisconnectEvent,
};

// (for contributors):
Expand Down Expand Up @@ -93,6 +93,8 @@ pub enum Event {
Death(Option<Arc<ClientboundPlayerCombatKillPacket>>),
/// A `KeepAlive` packet was sent by the server.
KeepAlive(u64),
/// The client disconnected from the server.
Disconnect(Option<String>),
}

/// A component that contains an event sender for events that are only
Expand All @@ -117,6 +119,7 @@ impl Plugin for EventPlugin {
remove_player_listener,
keepalive_listener,
death_listener,
disconnect_listener,
),
)
.add_systems(
Expand Down Expand Up @@ -229,3 +232,11 @@ fn keepalive_listener(query: Query<&LocalPlayerEvents>, mut events: EventReader<
.unwrap();
}
}

fn disconnect_listener(query: Query<&LocalPlayerEvents>, mut events: EventReader<DisconnectEvent>) {
for event in events.read() {
if let Ok(local_player_events) = query.get(event.entity) {
let _ = local_player_events.send(Event::Disconnect(event.reason.clone()));
}
}
}
1 change: 1 addition & 0 deletions azalea-client/src/packet_handling/configuration.rs
Original file line number Diff line number Diff line change
Expand Up @@ -104,6 +104,7 @@ pub fn process_packet_events(ecs: &mut World) {
let mut disconnect_events = system_state.get_mut(ecs);
disconnect_events.send(DisconnectEvent {
entity: player_entity,
reason: Some(p.reason.to_ansi()),
});
}
ClientboundConfigurationPacket::FinishConfiguration(p) => {
Expand Down
1 change: 1 addition & 0 deletions azalea-client/src/packet_handling/game.rs
Original file line number Diff line number Diff line change
Expand Up @@ -401,6 +401,7 @@ pub fn process_packet_events(ecs: &mut World) {
let mut disconnect_events = system_state.get_mut(ecs);
disconnect_events.send(DisconnectEvent {
entity: player_entity,
reason: Some(p.reason.to_ansi()),
});
}
ClientboundGamePacket::UpdateRecipes(_p) => {
Expand Down

0 comments on commit fbee81e

Please sign in to comment.