Skip to content

Commit

Permalink
Fix: kick in player state when client fails
Browse files Browse the repository at this point in the history
  • Loading branch information
Snowiiii committed Oct 13, 2024
1 parent 033648b commit 533357c
Show file tree
Hide file tree
Showing 3 changed files with 9 additions and 5 deletions.
8 changes: 7 additions & 1 deletion pumpkin/src/client/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -17,9 +17,10 @@ use crossbeam::atomic::AtomicCell;
use mio::{event::Event, net::TcpStream, Token};
use parking_lot::Mutex;
use pumpkin_config::compression::CompressionInfo;
use pumpkin_core::text::TextComponent;
use pumpkin_protocol::{
bytebuf::{packet_id::Packet, DeserializerError},
client::{config::CConfigDisconnect, login::CLoginDisconnect},
client::{config::CConfigDisconnect, login::CLoginDisconnect, play::CPlayDisconnect},
packet_decoder::PacketDecoder,
packet_encoder::PacketEncoder,
server::{
Expand Down Expand Up @@ -416,6 +417,11 @@ impl Client {
self.try_send_packet(&CConfigDisconnect::new(reason))
.unwrap_or_else(|_| self.close());
}
// This way players get kicked when players using client functions (e.g. poll, send_packet)
ConnectionState::Play => {
self.try_send_packet(&CPlayDisconnect::new(&TextComponent::text(reason)))
.unwrap_or_else(|_| self.close());
}
_ => {
log::warn!("Can't kick in {:?} State", self.connection_state)
}
Expand Down
3 changes: 1 addition & 2 deletions pumpkin/src/entity/player.rs
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@ use pumpkin_protocol::{
SPlayerPositionRotation, SPlayerRotation, SSetCreativeSlot, SSetHeldItem, SSetPlayerGround,
SSwingArm, SUseItem, SUseItemOn,
},
ConnectionState, RawPacket, ServerPacket, VarInt,
RawPacket, ServerPacket, VarInt,
};

use pumpkin_protocol::server::play::{SCloseContainer, SKeepAlive};
Expand Down Expand Up @@ -215,7 +215,6 @@ impl Player {

/// Kicks the Client with a reason depending on the connection state
pub fn kick(&self, reason: TextComponent) {
assert!(self.client.connection_state.load() == ConnectionState::Play);
assert!(!self
.client
.closed
Expand Down
3 changes: 1 addition & 2 deletions pumpkin/src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -234,7 +234,7 @@ fn main() -> io::Result<()> {
}
clients.insert(token, client);
},

// Maybe received an event for a TCP connection.
token => {
// Poll Players
if let Some(player) = players.get_mut(&token) {
Expand All @@ -256,7 +256,6 @@ fn main() -> io::Result<()> {
};

// Poll current Clients (non players)
// Maybe received an event for a TCP connection.
let (done, make_player) = if let Some(client) = clients.get_mut(&token) {
client.poll(event).await;
let closed = client.closed.load(std::sync::atomic::Ordering::Relaxed);
Expand Down

0 comments on commit 533357c

Please sign in to comment.