From 30cdab5d09fccb0b6f510550ba80e4f6f0a8f255 Mon Sep 17 00:00:00 2001 From: Snowiiii Date: Sun, 4 Aug 2024 12:23:28 +0200 Subject: [PATCH] Add Clientbound Play Disconnect --- Cargo.lock | 1 + pumpkin-protocol/Cargo.toml | 3 +++ pumpkin-protocol/src/client/play/mod.rs | 20 +++++++++++++++++++- pumpkin/src/client/mod.rs | 21 +++++++++++++++++++-- pumpkin/src/server.rs | 2 +- 5 files changed, 43 insertions(+), 4 deletions(-) diff --git a/Cargo.lock b/Cargo.lock index a84f19387..47fed26f9 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -665,6 +665,7 @@ dependencies = [ "aes", "bytes", "cfb8", + "fastnbt 2.5.0 (git+https://github.com/owengage/fastnbt.git)", "log", "serde", "serde_json", diff --git a/pumpkin-protocol/Cargo.toml b/pumpkin-protocol/Cargo.toml index c58e78d53..eccbbff46 100644 --- a/pumpkin-protocol/Cargo.toml +++ b/pumpkin-protocol/Cargo.toml @@ -15,6 +15,9 @@ serde_json = "1.0" thiserror = "1.0.63" log = "0.4" +# for text component +fastnbt = { git = "https://github.com/owengage/fastnbt.git" } + # encryption aes = "0.8.4" cfb8 = "0.8.1" \ No newline at end of file diff --git a/pumpkin-protocol/src/client/play/mod.rs b/pumpkin-protocol/src/client/play/mod.rs index 2ed0b4c79..10a3c01ad 100644 --- a/pumpkin-protocol/src/client/play/mod.rs +++ b/pumpkin-protocol/src/client/play/mod.rs @@ -1,4 +1,4 @@ -use crate::{bytebuf::ByteBuffer, BitSet, ClientPacket, VarInt}; +use crate::{bytebuf::ByteBuffer, text::Text, BitSet, ClientPacket, VarInt}; pub struct SetHeldItem { slot: i8, @@ -44,6 +44,24 @@ impl ClientPacket for CPlayerAbilities { } } +pub struct CPlayDisconnect { + reason: Text, +} + +impl CPlayDisconnect { + pub fn new(reason: Text) -> Self { + Self { reason } + } +} + +impl<'a> ClientPacket for CPlayDisconnect { + const PACKET_ID: VarInt = 0x1D; + + fn write(&self, bytebuf: &mut ByteBuffer) { + bytebuf.put_slice(&self.reason.encode()); + } +} + pub struct CChangeDifficulty { difficulty: u8, locked: bool, diff --git a/pumpkin/src/client/mod.rs b/pumpkin/src/client/mod.rs index e77c98ca8..01cad190d 100644 --- a/pumpkin/src/client/mod.rs +++ b/pumpkin/src/client/mod.rs @@ -12,7 +12,11 @@ use crate::{ use mio::{event::Event, net::TcpStream, Token}; use player_packet::PlayerPacketProcessor; use pumpkin_protocol::{ - client::{config::CConfigDisconnect, login::CLoginDisconnect}, + client::{ + config::CConfigDisconnect, + login::CLoginDisconnect, + play::{CPlayDisconnect, CSyncPlayerPostion}, + }, packet_decoder::PacketDecoder, packet_encoder::PacketEncoder, server::{ @@ -22,6 +26,7 @@ use pumpkin_protocol::{ play::SConfirmTeleport, status::{SPingRequest, SStatusRequest}, }, + text::{Text}, ClientPacket, ConnectionState, PacketError, RawPacket, ServerPacket, }; @@ -118,6 +123,12 @@ impl Client { Ok(()) } + pub fn teleport(&mut self) { + assert!(self.player.is_some()); + self.send_packet(CSyncPlayerPostion::new(10.0, 10.0, 10.0, 10.0, 0.0, 0, 10)) + .unwrap_or_else(|e| self.kick(&e.to_string())); + } + pub fn process_packets(&mut self, server: &mut Server) { let mut i = 0; while i < self.client_packets_queue.len() { @@ -258,7 +269,7 @@ impl Client { /// Kicks the Client with a reason depending on the connection state pub fn kick(&mut self, reason: &str) { - // Todo + dbg!(reason); match self.connection_state { ConnectionState::Login => { self.send_packet(CLoginDisconnect::new(reason)) @@ -268,6 +279,12 @@ impl Client { self.send_packet(CConfigDisconnect::new(reason)) .unwrap_or_else(|_| self.close()); } + ConnectionState::Play => { + self.send_packet(CPlayDisconnect::new(Text { + text: reason.to_string(), + })) + .unwrap_or_else(|_| self.close()); + } _ => { log::warn!("Cant't kick in {:?} State", self.connection_state) } diff --git a/pumpkin/src/server.rs b/pumpkin/src/server.rs index f7809643a..8f7689516 100644 --- a/pumpkin/src/server.rs +++ b/pumpkin/src/server.rs @@ -134,7 +134,7 @@ impl Server { .send_packet(CGameEvent::new(13, 0.0)) .unwrap_or_else(|e| client.kick(&e.to_string())); - Server::spawn_test_chunk(client); + // Server::spawn_test_chunk(client); client.player = Some(player); }