From a2d9396bbdc2d04706a843766d1b2477bba8617c Mon Sep 17 00:00:00 2001 From: Alexander Medvedev Date: Tue, 1 Apr 2025 16:06:51 +0200 Subject: [PATCH] parse protocol version one thing less to care about when porting --- assets/packets.json | 1 + pumpkin-data/build/packet.rs | 5 +++++ pumpkin-protocol/src/lib.rs | 5 ----- pumpkin/src/command/commands/pumpkin.rs | 4 ++-- pumpkin/src/main.rs | 3 +-- pumpkin/src/net/packet/handshake.rs | 7 +++---- pumpkin/src/server/connection_cache.rs | 6 +++--- pumpkin/src/server/seasonal_events.rs | 2 +- 8 files changed, 16 insertions(+), 17 deletions(-) diff --git a/assets/packets.json b/assets/packets.json index 82b084d56..e3c2294d9 100644 --- a/assets/packets.json +++ b/assets/packets.json @@ -1,4 +1,5 @@ { + "version": 770, "serverbound": { "handshake": [ "intention" diff --git a/pumpkin-data/build/packet.rs b/pumpkin-data/build/packet.rs index f481bd412..bc5fdae33 100644 --- a/pumpkin-data/build/packet.rs +++ b/pumpkin-data/build/packet.rs @@ -6,6 +6,7 @@ use serde::Deserialize; #[derive(Deserialize)] pub struct Packets { + version: u32, serverbound: HashMap>, clientbound: HashMap>, } @@ -15,10 +16,14 @@ pub(crate) fn build() -> TokenStream { let packets: Packets = serde_json::from_str(include_str!("../../assets/packets.json")) .expect("Failed to parse packets.json"); + let version = packets.version; let serverbound_consts = parse_packets(packets.serverbound); let clientbound_consts = parse_packets(packets.clientbound); quote!( + /// The current Minecraft protocol version. This changes only when the protocol itself is modified. + pub const CURRENT_MC_PROTOCOL: u32 = #version; + pub mod serverbound { #serverbound_consts } diff --git a/pumpkin-protocol/src/lib.rs b/pumpkin-protocol/src/lib.rs index bf3c4de61..a7a35c17d 100644 --- a/pumpkin-protocol/src/lib.rs +++ b/pumpkin-protocol/src/lib.rs @@ -1,7 +1,6 @@ use std::{ io::{Read, Write}, marker::PhantomData, - num::NonZeroU16, }; use aes::cipher::{BlockDecryptMut, BlockEncryptMut, BlockSizeUser, generic_array::GenericArray}; @@ -27,10 +26,6 @@ pub mod ser; #[cfg(feature = "serverbound")] pub mod server; -/// The current Minecraft protocol number. -/// Don't forget to change this when porting. -pub const CURRENT_MC_PROTOCOL: NonZeroU16 = unsafe { NonZeroU16::new_unchecked(770) }; - pub const MAX_PACKET_SIZE: u64 = 2097152; pub const MAX_PACKET_DATA_SIZE: usize = 8388608; diff --git a/pumpkin/src/command/commands/pumpkin.rs b/pumpkin/src/command/commands/pumpkin.rs index 385bd685c..d85735eb8 100644 --- a/pumpkin/src/command/commands/pumpkin.rs +++ b/pumpkin/src/command/commands/pumpkin.rs @@ -1,16 +1,16 @@ use async_trait::async_trait; -use pumpkin_protocol::CURRENT_MC_PROTOCOL; +use pumpkin_data::packet::CURRENT_MC_PROTOCOL; use pumpkin_util::text::click::ClickEvent; use pumpkin_util::text::hover::HoverEvent; use pumpkin_util::text::{TextComponent, color::NamedColor}; use std::borrow::Cow; +use crate::server::CURRENT_MC_VERSION; use crate::{ GIT_VERSION, command::{ CommandError, CommandExecutor, CommandSender, args::ConsumedArgs, tree::CommandTree, }, - server::CURRENT_MC_VERSION, }; const NAMES: [&str; 2] = ["pumpkin", "version"]; diff --git a/pumpkin/src/main.rs b/pumpkin/src/main.rs index 21a81b9ab..126dca1ac 100644 --- a/pumpkin/src/main.rs +++ b/pumpkin/src/main.rs @@ -37,6 +37,7 @@ compile_error!("Compiling for WASI targets is not supported!"); use plugin::PluginManager; +use pumpkin_data::packet::CURRENT_MC_PROTOCOL; use std::{ io::{self}, sync::LazyLock, @@ -49,7 +50,6 @@ use tokio::sync::Mutex; use crate::server::CURRENT_MC_VERSION; use pumpkin::{PumpkinServer, SHOULD_STOP, STOP_INTERRUPT, init_log, stop_server}; -use pumpkin_protocol::CURRENT_MC_PROTOCOL; use pumpkin_util::text::{TextComponent, color::NamedColor}; use std::time::Instant; // Setup some tokens to allow us to identify which event is for which socket. @@ -92,7 +92,6 @@ async fn main() { .thread_name(|_| "rayon-worker".to_string()) .build_global() .expect("Rayon thread pool can only be initialized once"); - log::info!( "Starting Pumpkin {CARGO_PKG_VERSION} ({GIT_VERSION}) for Minecraft {CURRENT_MC_VERSION} (Protocol {CURRENT_MC_PROTOCOL})", ); diff --git a/pumpkin/src/net/packet/handshake.rs b/pumpkin/src/net/packet/handshake.rs index 46e66b494..bedf002bb 100644 --- a/pumpkin/src/net/packet/handshake.rs +++ b/pumpkin/src/net/packet/handshake.rs @@ -1,6 +1,5 @@ -use std::num::NonZeroI32; - -use pumpkin_protocol::{CURRENT_MC_PROTOCOL, ConnectionState, server::handshake::SHandShake}; +use pumpkin_data::packet::CURRENT_MC_PROTOCOL; +use pumpkin_protocol::{ConnectionState, server::handshake::SHandShake}; use pumpkin_util::text::TextComponent; use crate::{net::Client, server::CURRENT_MC_VERSION}; @@ -16,7 +15,7 @@ impl Client { self.connection_state.store(handshake.next_state); if self.connection_state.load() != ConnectionState::Status { let protocol = version; - match protocol.cmp(&NonZeroI32::from(CURRENT_MC_PROTOCOL).get()) { + match protocol.cmp(&(CURRENT_MC_PROTOCOL as i32)) { std::cmp::Ordering::Less => { self.kick(TextComponent::translate( "multiplayer.disconnect.outdated_client", diff --git a/pumpkin/src/server/connection_cache.rs b/pumpkin/src/server/connection_cache.rs index 036071c85..9d7905377 100644 --- a/pumpkin/src/server/connection_cache.rs +++ b/pumpkin/src/server/connection_cache.rs @@ -2,14 +2,14 @@ use core::error; use std::{ fs::File, io::{Cursor, Read}, - num::NonZeroU32, path::Path, }; use base64::{Engine as _, engine::general_purpose}; use pumpkin_config::{BASIC_CONFIG, BasicConfiguration}; +use pumpkin_data::packet::CURRENT_MC_PROTOCOL; use pumpkin_protocol::{ - CURRENT_MC_PROTOCOL, Players, StatusResponse, Version, + Players, StatusResponse, Version, client::{config::CPluginMessage, status::CStatusResponse}, codec::{Codec, var_int::VarInt}, }; @@ -144,7 +144,7 @@ impl CachedStatus { StatusResponse { version: Some(Version { name: CURRENT_MC_VERSION.into(), - protocol: NonZeroU32::from(CURRENT_MC_PROTOCOL).get(), + protocol: CURRENT_MC_PROTOCOL, }), players: Some(Players { max: config.max_players, diff --git a/pumpkin/src/server/seasonal_events.rs b/pumpkin/src/server/seasonal_events.rs index 870d0a653..1ce604d7a 100644 --- a/pumpkin/src/server/seasonal_events.rs +++ b/pumpkin/src/server/seasonal_events.rs @@ -12,7 +12,7 @@ pub fn is_april() -> bool { #[must_use] pub fn modify_chat_message(message: &str) -> Option { - if !advanced_config().fun.april_fools && is_april() { + if !advanced_config().fun.april_fools || !is_april() { return None; } let mut words: Vec<&str> = message.split_whitespace().collect();