Skip to content

Commit

Permalink
Handle older/newer Clients
Browse files Browse the repository at this point in the history
  • Loading branch information
Snowiiii committed Aug 13, 2024
1 parent 32a172f commit c5612f6
Show file tree
Hide file tree
Showing 4 changed files with 30 additions and 13 deletions.
16 changes: 12 additions & 4 deletions pumpkin/src/client/client_packet.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -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::{
Expand All @@ -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) {
Expand Down
2 changes: 2 additions & 0 deletions pumpkin/src/client/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -62,6 +62,7 @@ pub struct Client {
pub config: Option<PlayerConfig>,
pub brand: Option<String>,

pub protocol_version: i32,
pub connection_state: ConnectionState,
pub encrytion: bool,
pub closed: bool,
Expand All @@ -76,6 +77,7 @@ pub struct Client {
impl Client {
pub fn new(token: Rc<Token>, connection: TcpStream, address: SocketAddr) -> Self {
Self {
protocol_version: 0,
gameprofile: None,
config: None,
brand: None,
Expand Down
23 changes: 15 additions & 8 deletions pumpkin/src/commands/gamemode.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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 <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;
}

Expand All @@ -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),
);
}
}
}
Expand Down
2 changes: 1 addition & 1 deletion pumpkin/src/server.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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<u8>,
Expand Down

0 comments on commit c5612f6

Please sign in to comment.