Handle older/newer Clients

This commit is contained in:
Snowiiii 2024-08-13 13:05:04 +02:00
parent 32a172f3c0
commit c5612f6ddb
4 changed files with 30 additions and 13 deletions

View File

@ -13,7 +13,7 @@ use pumpkin_protocol::{
login::{SEncryptionResponse, SLoginAcknowledged, SLoginPluginResponse, SLoginStart}, login::{SEncryptionResponse, SLoginAcknowledged, SLoginPluginResponse, SLoginStart},
status::{SPingRequest, SStatusRequest}, status::{SPingRequest, SStatusRequest},
}, },
ConnectionState, KnownPack, ConnectionState, KnownPack, CURRENT_MC_PROTOCOL,
}; };
use pumpkin_text::TextComponent; use pumpkin_text::TextComponent;
use rsa::Pkcs1v15Encrypt; use rsa::Pkcs1v15Encrypt;
@ -22,7 +22,7 @@ use sha1::{Digest, Sha1};
use crate::{ use crate::{
client::authentication::{self, GameProfile}, client::authentication::{self, GameProfile},
entity::player::{ChatMode, Hand}, entity::player::{ChatMode, Hand},
server::Server, server::{Server, CURRENT_MC_VERSION},
}; };
use super::{ use super::{
@ -34,9 +34,17 @@ use super::{
/// Implements the `Client` Packets /// Implements the `Client` Packets
impl Client { impl Client {
pub fn handle_handshake(&mut self, _server: &mut Server, handshake: SHandShake) { 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; 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) { pub fn handle_status_request(&mut self, server: &mut Server, _status_request: SStatusRequest) {

View File

@ -62,6 +62,7 @@ pub struct Client {
pub config: Option<PlayerConfig>, pub config: Option<PlayerConfig>,
pub brand: Option<String>, pub brand: Option<String>,
pub protocol_version: i32,
pub connection_state: ConnectionState, pub connection_state: ConnectionState,
pub encrytion: bool, pub encrytion: bool,
pub closed: bool, pub closed: bool,
@ -76,6 +77,7 @@ pub struct Client {
impl Client { impl Client {
pub fn new(token: Rc<Token>, connection: TcpStream, address: SocketAddr) -> Self { pub fn new(token: Rc<Token>, connection: TcpStream, address: SocketAddr) -> Self {
Self { Self {
protocol_version: 0,
gameprofile: None, gameprofile: None,
config: None, config: None,
brand: None, brand: None,

View File

@ -17,7 +17,10 @@ impl<'a> Command<'a> for GamemodeCommand {
let args: Vec<&str> = command.split_whitespace().collect(); let args: Vec<&str> = command.split_whitespace().collect();
if args.len() != 2 { if args.len() != 2 {
player.send_system_message(TextComponent::from("Usage: /gamemode <mode>").color_named(pumpkin_text::color::NamedColor::Red)); player.send_system_message(
TextComponent::from("Usage: /gamemode <mode>")
.color_named(pumpkin_text::color::NamedColor::Red),
);
return; return;
} }
@ -33,16 +36,20 @@ impl<'a> Command<'a> for GamemodeCommand {
Ok(i) => match GameMode::from_u8(i) { Ok(i) => match GameMode::from_u8(i) {
Some(mode) => { Some(mode) => {
player.set_gamemode(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; return;
}, }
None => {}, None => {}
}, },
Err(_) => {}, Err(_) => {}
} }
player.send_system_message(
player.send_system_message(TextComponent::from("Invalid gamemode").color_named(pumpkin_text::color::NamedColor::Red)); TextComponent::from("Invalid gamemode")
.color_named(pumpkin_text::color::NamedColor::Red),
);
} }
} }
} }

View File

@ -37,7 +37,7 @@ use crate::{
entity::player::{GameMode, Player}, 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 struct Server {
pub compression_threshold: Option<u8>, pub compression_threshold: Option<u8>,