diff --git a/azalea-client/src/disconnect.rs b/azalea-client/src/disconnect.rs index ac663a662..35334199d 100644 --- a/azalea-client/src/disconnect.rs +++ b/azalea-client/src/disconnect.rs @@ -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 { @@ -33,7 +35,7 @@ impl Plugin for DisconnectPlugin { #[derive(Event)] pub struct DisconnectEvent { pub entity: Entity, - pub reason: Option, + pub reason: Option, } /// System that removes the [`JoinedClientBundle`] from the entity when it @@ -43,7 +45,11 @@ pub fn remove_components_from_disconnected_players( mut events: EventReader, ) { for DisconnectEvent { entity, .. } in events.read() { - commands.entity(*entity).remove::(); + commands + .entity(*entity) + .remove::() + // swarm detects when this tx gets dropped to fire SwarmEvent::Disconnect + .remove::(); } } @@ -60,12 +66,15 @@ fn update_read_packets_task_running_component( } } fn disconnect_on_connection_dead( - query: Query<(Entity, &IsConnectionAlive), Changed>, + query: Query<(Entity, &IsConnectionAlive), (Changed, With)>, mut disconnect_events: EventWriter, ) { 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, + }); } } } diff --git a/azalea-client/src/events.rs b/azalea-client/src/events.rs index 0441ae980..9f2632a8b 100644 --- a/azalea-client/src/events.rs +++ b/azalea-client/src/events.rs @@ -3,6 +3,7 @@ use std::sync::Arc; +use azalea_chat::FormattedText; use azalea_protocol::packets::game::{ clientbound_player_combat_kill_packet::ClientboundPlayerCombatKillPacket, ClientboundGamePacket, }; @@ -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): @@ -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), + Disconnect(Option), } /// A component that contains an event sender for events that are only diff --git a/azalea-client/src/packet_handling/configuration.rs b/azalea-client/src/packet_handling/configuration.rs index 82016fe66..0285884e3 100644 --- a/azalea-client/src/packet_handling/configuration.rs +++ b/azalea-client/src/packet_handling/configuration.rs @@ -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) => { diff --git a/azalea-client/src/packet_handling/game.rs b/azalea-client/src/packet_handling/game.rs index db8da69af..cffa0d2bc 100644 --- a/azalea-client/src/packet_handling/game.rs +++ b/azalea-client/src/packet_handling/game.rs @@ -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) => { diff --git a/azalea-client/src/raw_connection.rs b/azalea-client/src/raw_connection.rs index e2daaba2a..bcb1ada04 100644 --- a/azalea-client/src/raw_connection.rs +++ b/azalea-client/src/raw_connection.rs @@ -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) { diff --git a/azalea/examples/testbot.rs b/azalea/examples/testbot.rs index b2f09dfe9..74ecfd8ae 100644 --- a/azalea/examples/testbot.rs +++ b/azalea/examples/testbot.rs @@ -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"); + } + } _ => {} }