aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
-rw-r--r--azalea-client/src/disconnect.rs21
-rw-r--r--azalea-client/src/events.rs6
-rw-r--r--azalea-client/src/packet_handling/configuration.rs2
-rw-r--r--azalea-client/src/packet_handling/game.rs2
-rw-r--r--azalea-client/src/raw_connection.rs5
-rw-r--r--azalea/examples/testbot.rs7
6 files changed, 32 insertions, 11 deletions
diff --git a/azalea-client/src/disconnect.rs b/azalea-client/src/disconnect.rs
index ac663a66..35334199 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<String>,
+ pub reason: Option<FormattedText>,
}
/// System that removes the [`JoinedClientBundle`] from the entity when it
@@ -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>();
}
}
@@ -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>)>,
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,
+ });
}
}
}
diff --git a/azalea-client/src/events.rs b/azalea-client/src/events.rs
index 0441ae98..9f2632a8 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<String>),
+ Disconnect(Option<FormattedText>),
}
/// 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 82016fe6..0285884e 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 db8da69a..cffa0d2b 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 e2daaba2..bcb1ada0 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 b2f09dfe..74ecfd8a 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");
+ }
+ }
_ => {}
}