parse protocol version

one thing less to care about when porting
This commit is contained in:
Alexander Medvedev 2025-04-01 16:06:51 +02:00
parent bfdd57b88f
commit a2d9396bbd
8 changed files with 16 additions and 17 deletions

View File

@ -1,4 +1,5 @@
{ {
"version": 770,
"serverbound": { "serverbound": {
"handshake": [ "handshake": [
"intention" "intention"

View File

@ -6,6 +6,7 @@ use serde::Deserialize;
#[derive(Deserialize)] #[derive(Deserialize)]
pub struct Packets { pub struct Packets {
version: u32,
serverbound: HashMap<String, Vec<String>>, serverbound: HashMap<String, Vec<String>>,
clientbound: HashMap<String, Vec<String>>, clientbound: HashMap<String, Vec<String>>,
} }
@ -15,10 +16,14 @@ pub(crate) fn build() -> TokenStream {
let packets: Packets = serde_json::from_str(include_str!("../../assets/packets.json")) let packets: Packets = serde_json::from_str(include_str!("../../assets/packets.json"))
.expect("Failed to parse packets.json"); .expect("Failed to parse packets.json");
let version = packets.version;
let serverbound_consts = parse_packets(packets.serverbound); let serverbound_consts = parse_packets(packets.serverbound);
let clientbound_consts = parse_packets(packets.clientbound); let clientbound_consts = parse_packets(packets.clientbound);
quote!( 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 { pub mod serverbound {
#serverbound_consts #serverbound_consts
} }

View File

@ -1,7 +1,6 @@
use std::{ use std::{
io::{Read, Write}, io::{Read, Write},
marker::PhantomData, marker::PhantomData,
num::NonZeroU16,
}; };
use aes::cipher::{BlockDecryptMut, BlockEncryptMut, BlockSizeUser, generic_array::GenericArray}; use aes::cipher::{BlockDecryptMut, BlockEncryptMut, BlockSizeUser, generic_array::GenericArray};
@ -27,10 +26,6 @@ pub mod ser;
#[cfg(feature = "serverbound")] #[cfg(feature = "serverbound")]
pub mod server; 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_SIZE: u64 = 2097152;
pub const MAX_PACKET_DATA_SIZE: usize = 8388608; pub const MAX_PACKET_DATA_SIZE: usize = 8388608;

View File

@ -1,16 +1,16 @@
use async_trait::async_trait; 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::click::ClickEvent;
use pumpkin_util::text::hover::HoverEvent; use pumpkin_util::text::hover::HoverEvent;
use pumpkin_util::text::{TextComponent, color::NamedColor}; use pumpkin_util::text::{TextComponent, color::NamedColor};
use std::borrow::Cow; use std::borrow::Cow;
use crate::server::CURRENT_MC_VERSION;
use crate::{ use crate::{
GIT_VERSION, GIT_VERSION,
command::{ command::{
CommandError, CommandExecutor, CommandSender, args::ConsumedArgs, tree::CommandTree, CommandError, CommandExecutor, CommandSender, args::ConsumedArgs, tree::CommandTree,
}, },
server::CURRENT_MC_VERSION,
}; };
const NAMES: [&str; 2] = ["pumpkin", "version"]; const NAMES: [&str; 2] = ["pumpkin", "version"];

View File

@ -37,6 +37,7 @@
compile_error!("Compiling for WASI targets is not supported!"); compile_error!("Compiling for WASI targets is not supported!");
use plugin::PluginManager; use plugin::PluginManager;
use pumpkin_data::packet::CURRENT_MC_PROTOCOL;
use std::{ use std::{
io::{self}, io::{self},
sync::LazyLock, sync::LazyLock,
@ -49,7 +50,6 @@ use tokio::sync::Mutex;
use crate::server::CURRENT_MC_VERSION; use crate::server::CURRENT_MC_VERSION;
use pumpkin::{PumpkinServer, SHOULD_STOP, STOP_INTERRUPT, init_log, stop_server}; 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 pumpkin_util::text::{TextComponent, color::NamedColor};
use std::time::Instant; use std::time::Instant;
// Setup some tokens to allow us to identify which event is for which socket. // 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()) .thread_name(|_| "rayon-worker".to_string())
.build_global() .build_global()
.expect("Rayon thread pool can only be initialized once"); .expect("Rayon thread pool can only be initialized once");
log::info!( log::info!(
"Starting Pumpkin {CARGO_PKG_VERSION} ({GIT_VERSION}) for Minecraft {CURRENT_MC_VERSION} (Protocol {CURRENT_MC_PROTOCOL})", "Starting Pumpkin {CARGO_PKG_VERSION} ({GIT_VERSION}) for Minecraft {CURRENT_MC_VERSION} (Protocol {CURRENT_MC_PROTOCOL})",
); );

View File

@ -1,6 +1,5 @@
use std::num::NonZeroI32; use pumpkin_data::packet::CURRENT_MC_PROTOCOL;
use pumpkin_protocol::{ConnectionState, server::handshake::SHandShake};
use pumpkin_protocol::{CURRENT_MC_PROTOCOL, ConnectionState, server::handshake::SHandShake};
use pumpkin_util::text::TextComponent; use pumpkin_util::text::TextComponent;
use crate::{net::Client, server::CURRENT_MC_VERSION}; use crate::{net::Client, server::CURRENT_MC_VERSION};
@ -16,7 +15,7 @@ impl Client {
self.connection_state.store(handshake.next_state); self.connection_state.store(handshake.next_state);
if self.connection_state.load() != ConnectionState::Status { if self.connection_state.load() != ConnectionState::Status {
let protocol = version; let protocol = version;
match protocol.cmp(&NonZeroI32::from(CURRENT_MC_PROTOCOL).get()) { match protocol.cmp(&(CURRENT_MC_PROTOCOL as i32)) {
std::cmp::Ordering::Less => { std::cmp::Ordering::Less => {
self.kick(TextComponent::translate( self.kick(TextComponent::translate(
"multiplayer.disconnect.outdated_client", "multiplayer.disconnect.outdated_client",

View File

@ -2,14 +2,14 @@ use core::error;
use std::{ use std::{
fs::File, fs::File,
io::{Cursor, Read}, io::{Cursor, Read},
num::NonZeroU32,
path::Path, path::Path,
}; };
use base64::{Engine as _, engine::general_purpose}; use base64::{Engine as _, engine::general_purpose};
use pumpkin_config::{BASIC_CONFIG, BasicConfiguration}; use pumpkin_config::{BASIC_CONFIG, BasicConfiguration};
use pumpkin_data::packet::CURRENT_MC_PROTOCOL;
use pumpkin_protocol::{ use pumpkin_protocol::{
CURRENT_MC_PROTOCOL, Players, StatusResponse, Version, Players, StatusResponse, Version,
client::{config::CPluginMessage, status::CStatusResponse}, client::{config::CPluginMessage, status::CStatusResponse},
codec::{Codec, var_int::VarInt}, codec::{Codec, var_int::VarInt},
}; };
@ -144,7 +144,7 @@ impl CachedStatus {
StatusResponse { StatusResponse {
version: Some(Version { version: Some(Version {
name: CURRENT_MC_VERSION.into(), name: CURRENT_MC_VERSION.into(),
protocol: NonZeroU32::from(CURRENT_MC_PROTOCOL).get(), protocol: CURRENT_MC_PROTOCOL,
}), }),
players: Some(Players { players: Some(Players {
max: config.max_players, max: config.max_players,

View File

@ -12,7 +12,7 @@ pub fn is_april() -> bool {
#[must_use] #[must_use]
pub fn modify_chat_message(message: &str) -> Option<String> { pub fn modify_chat_message(message: &str) -> Option<String> {
if !advanced_config().fun.april_fools && is_april() { if !advanced_config().fun.april_fools || !is_april() {
return None; return None;
} }
let mut words: Vec<&str> = message.split_whitespace().collect(); let mut words: Vec<&str> = message.split_whitespace().collect();