Skip to content

Commit

Permalink
disconnect fixes
Browse files Browse the repository at this point in the history
  • Loading branch information
mat-1 committed Dec 2, 2023
1 parent fbee81e commit 45f9d27
Show file tree
Hide file tree
Showing 6 changed files with 32 additions and 11 deletions.
21 changes: 15 additions & 6 deletions azalea-client/src/disconnect.rs
Original file line number Diff line number Diff line change
@@ -1,18 +1,20 @@
//! Disconnect a client from the server.
use azalea_chat::FormattedText;
use azalea_entity::LocalEntity;
use bevy_app::{App, Plugin, PostUpdate};
use bevy_ecs::{
component::Component,
entity::Entity,
event::{EventReader, EventWriter},
prelude::Event,
query::Changed,
query::{Changed, With},
schedule::IntoSystemConfigs,
system::{Commands, Query},
};
use derive_more::Deref;

use crate::{client::JoinedClientBundle, raw_connection::RawConnection};
use crate::{client::JoinedClientBundle, events::LocalPlayerEvents, raw_connection::RawConnection};

pub struct DisconnectPlugin;
impl Plugin for DisconnectPlugin {
Expand All @@ -33,7 +35,7 @@ impl Plugin for DisconnectPlugin {
#[derive(Event)]
pub struct DisconnectEvent {
pub entity: Entity,
pub reason: Option<String>,
pub reason: Option<FormattedText>,
}

/// System that removes the [`JoinedClientBundle`] from the entity when it
Expand All @@ -43,7 +45,11 @@ pub fn remove_components_from_disconnected_players(
mut events: EventReader<DisconnectEvent>,
) {
for DisconnectEvent { entity, .. } in events.read() {
commands.entity(*entity).remove::<JoinedClientBundle>();
commands
.entity(*entity)
.remove::<JoinedClientBundle>()
// swarm detects when this tx gets dropped to fire SwarmEvent::Disconnect
.remove::<LocalPlayerEvents>();
}
}

Expand All @@ -60,12 +66,15 @@ fn update_read_packets_task_running_component(
}
}
fn disconnect_on_connection_dead(
query: Query<(Entity, &IsConnectionAlive), Changed<IsConnectionAlive>>,
query: Query<(Entity, &IsConnectionAlive), (Changed<IsConnectionAlive>, With<LocalEntity>)>,

Check warning on line 69 in azalea-client/src/disconnect.rs

View workflow job for this annotation

GitHub Actions / clippy

very complex type used. Consider factoring parts into `type` definitions

warning: very complex type used. Consider factoring parts into `type` definitions --> azalea-client/src/disconnect.rs:69:12 | 69 | query: Query<(Entity, &IsConnectionAlive), (Changed<IsConnectionAlive>, With<LocalEntity>)>, | ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ | = help: for further information visit https://rust-lang.github.io/rust-clippy/master/index.html#type_complexity = note: `#[warn(clippy::type_complexity)]` on by default
mut disconnect_events: EventWriter<DisconnectEvent>,
) {
for (entity, &is_connection_alive) in &query {
if !*is_connection_alive {
disconnect_events.send(DisconnectEvent { entity, reason: None });
disconnect_events.send(DisconnectEvent {
entity,
reason: None,
});
}
}
}
6 changes: 4 additions & 2 deletions azalea-client/src/events.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
use std::sync::Arc;

use azalea_chat::FormattedText;
use azalea_protocol::packets::game::{
clientbound_player_combat_kill_packet::ClientboundPlayerCombatKillPacket, ClientboundGamePacket,
};
Expand All @@ -20,11 +21,12 @@ use tokio::sync::mpsc;

use crate::{
chat::{ChatPacket, ChatReceivedEvent},
disconnect::DisconnectEvent,
packet_handling::game::{
AddPlayerEvent, DeathEvent, KeepAliveEvent, PacketEvent, RemovePlayerEvent,
UpdatePlayerEvent,
},
PlayerInfo, disconnect::DisconnectEvent,
PlayerInfo,
};

// (for contributors):
Expand Down Expand Up @@ -94,7 +96,7 @@ pub enum Event {
/// A `KeepAlive` packet was sent by the server.
KeepAlive(u64),
/// The client disconnected from the server.
Disconnect(Option<String>),
Disconnect(Option<FormattedText>),
}

/// A component that contains an event sender for events that are only
Expand Down
2 changes: 1 addition & 1 deletion azalea-client/src/packet_handling/configuration.rs
Original file line number Diff line number Diff line change
Expand Up @@ -104,7 +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()),
reason: Some(p.reason.clone()),
});
}
ClientboundConfigurationPacket::FinishConfiguration(p) => {
Expand Down
2 changes: 1 addition & 1 deletion azalea-client/src/packet_handling/game.rs
Original file line number Diff line number Diff line change
Expand Up @@ -401,7 +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()),
reason: Some(p.reason.clone()),
});
}
ClientboundGamePacket::UpdateRecipes(_p) => {
Expand Down
5 changes: 4 additions & 1 deletion azalea-client/src/raw_connection.rs
Original file line number Diff line number Diff line change
Expand Up @@ -133,7 +133,10 @@ impl RawConnectionReader {
Ok(raw_packet) => {
self.incoming_packet_queue.lock().push(raw_packet);
// tell the client to run all the systems
self.run_schedule_sender.send(()).unwrap();
if self.run_schedule_sender.send(()).is_err() {
// the client was dropped
break;
}
}
Err(error) => {
if !matches!(*error, ReadPacketError::ConnectionClosed) {
Expand Down
7 changes: 7 additions & 0 deletions azalea/examples/testbot.rs
Original file line number Diff line number Diff line change
Expand Up @@ -354,6 +354,13 @@ async fn handle(mut bot: Client, event: Event, _state: State) -> anyhow::Result<
println!("login packet");
}
}
Event::Disconnect(reason) => {
if let Some(reason) = reason {
println!("bot got kicked for reason: {}", reason.to_ansi());
} else {
println!("bot got kicked");
}
}
_ => {}
}

Expand Down

0 comments on commit 45f9d27

Please sign in to comment.