From c5612f6ddb261c9d375393ee58d6f695c173b947 Mon Sep 17 00:00:00 2001 From: Snowiiii Date: Tue, 13 Aug 2024 13:05:04 +0200 Subject: [PATCH] Handle older/newer Clients --- pumpkin/src/client/client_packet.rs | 16 ++++++++++++---- pumpkin/src/client/mod.rs | 2 ++ pumpkin/src/commands/gamemode.rs | 23 +++++++++++++++-------- pumpkin/src/server.rs | 2 +- 4 files changed, 30 insertions(+), 13 deletions(-) diff --git a/pumpkin/src/client/client_packet.rs b/pumpkin/src/client/client_packet.rs index d9f9d5335..7fab59493 100644 --- a/pumpkin/src/client/client_packet.rs +++ b/pumpkin/src/client/client_packet.rs @@ -13,7 +13,7 @@ use pumpkin_protocol::{ login::{SEncryptionResponse, SLoginAcknowledged, SLoginPluginResponse, SLoginStart}, status::{SPingRequest, SStatusRequest}, }, - ConnectionState, KnownPack, + ConnectionState, KnownPack, CURRENT_MC_PROTOCOL, }; use pumpkin_text::TextComponent; use rsa::Pkcs1v15Encrypt; @@ -22,7 +22,7 @@ use sha1::{Digest, Sha1}; use crate::{ client::authentication::{self, GameProfile}, entity::player::{ChatMode, Hand}, - server::Server, + server::{Server, CURRENT_MC_VERSION}, }; use super::{ @@ -34,9 +34,17 @@ use super::{ /// Implements the `Client` Packets impl Client { pub fn handle_handshake(&mut self, _server: &mut Server, handshake: SHandShake) { - // TODO set protocol version and check protocol version + self.protocol_version = handshake.protocol_version.0; self.connection_state = handshake.next_state; - dbg!("handshake"); + if self.connection_state == ConnectionState::Login { + if self.protocol_version < CURRENT_MC_PROTOCOL as i32 { + self.kick(&format!("Client outdated, Server uses Minecraft {CURRENT_MC_VERSION}, Protocol {CURRENT_MC_PROTOCOL}")); + return; + } else if self.protocol_version > CURRENT_MC_PROTOCOL as i32 { + self.kick(&format!("Server outdated, Server uses Minecraft {CURRENT_MC_VERSION}, Protocol {CURRENT_MC_PROTOCOL}")); + return; + } + } } pub fn handle_status_request(&mut self, server: &mut Server, _status_request: SStatusRequest) { diff --git a/pumpkin/src/client/mod.rs b/pumpkin/src/client/mod.rs index 68404b971..caa70bb27 100644 --- a/pumpkin/src/client/mod.rs +++ b/pumpkin/src/client/mod.rs @@ -62,6 +62,7 @@ pub struct Client { pub config: Option, pub brand: Option, + pub protocol_version: i32, pub connection_state: ConnectionState, pub encrytion: bool, pub closed: bool, @@ -76,6 +77,7 @@ pub struct Client { impl Client { pub fn new(token: Rc, connection: TcpStream, address: SocketAddr) -> Self { Self { + protocol_version: 0, gameprofile: None, config: None, brand: None, diff --git a/pumpkin/src/commands/gamemode.rs b/pumpkin/src/commands/gamemode.rs index 42f86cbce..af0953913 100644 --- a/pumpkin/src/commands/gamemode.rs +++ b/pumpkin/src/commands/gamemode.rs @@ -17,7 +17,10 @@ impl<'a> Command<'a> for GamemodeCommand { let args: Vec<&str> = command.split_whitespace().collect(); if args.len() != 2 { - player.send_system_message(TextComponent::from("Usage: /gamemode ").color_named(pumpkin_text::color::NamedColor::Red)); + player.send_system_message( + TextComponent::from("Usage: /gamemode ") + .color_named(pumpkin_text::color::NamedColor::Red), + ); return; } @@ -33,16 +36,20 @@ impl<'a> Command<'a> for GamemodeCommand { Ok(i) => match GameMode::from_u8(i) { Some(mode) => { player.set_gamemode(mode); - player.send_system_message(format!("Set own game mode to {:?}", mode).into()); + player.send_system_message( + format!("Set own game mode to {:?}", mode).into(), + ); return; - }, - None => {}, + } + None => {} }, - Err(_) => {}, + Err(_) => {} } - - - player.send_system_message(TextComponent::from("Invalid gamemode").color_named(pumpkin_text::color::NamedColor::Red)); + + player.send_system_message( + TextComponent::from("Invalid gamemode") + .color_named(pumpkin_text::color::NamedColor::Red), + ); } } } diff --git a/pumpkin/src/server.rs b/pumpkin/src/server.rs index ee31453bb..e92f9bbde 100644 --- a/pumpkin/src/server.rs +++ b/pumpkin/src/server.rs @@ -37,7 +37,7 @@ use crate::{ entity::player::{GameMode, Player}, }; -pub const CURRENT_MC_VERSION: &str = "1.21"; +pub const CURRENT_MC_VERSION: &str = "1.21.1"; pub struct Server { pub compression_threshold: Option,