From d68a25b169786dcc703e5885598eda3a4aee51ba Mon Sep 17 00:00:00 2001 From: StripedMonkey Date: Thu, 12 Sep 2024 18:50:26 -0400 Subject: [PATCH] adopt proposed alternative name for cached data --- ..._server_listing.rs => connection_cache.rs} | 42 +++++++++++-------- pumpkin/src/server/mod.rs | 11 +++-- 2 files changed, 32 insertions(+), 21 deletions(-) rename pumpkin/src/server/{bikeshed_server_listing.rs => connection_cache.rs} (94%) diff --git a/pumpkin/src/server/bikeshed_server_listing.rs b/pumpkin/src/server/connection_cache.rs similarity index 94% rename from pumpkin/src/server/bikeshed_server_listing.rs rename to pumpkin/src/server/connection_cache.rs index 57ca00fc7..a8268ba3a 100644 --- a/pumpkin/src/server/bikeshed_server_listing.rs +++ b/pumpkin/src/server/connection_cache.rs @@ -10,33 +10,49 @@ use pumpkin_protocol::{ use super::CURRENT_MC_VERSION; -pub struct BikeShedServerListing { +pub struct CachedStatus { _status_response: StatusResponse, // We cache the json response here so we don't parse it every time someone makes a Status request. // Keep in mind that we must parse this again, when the StatusResponse changes which usually happen when a player joins or leaves status_response_json: String, +} + +pub struct CachedBranding { /// Cached Server brand buffer so we don't have to rebuild them every time a player joins cached_server_brand: Vec, } -impl BikeShedServerListing { +impl CachedBranding { + pub fn new() -> CachedBranding { + let cached_server_brand = Self::build_brand(); + CachedBranding { + cached_server_brand, + } + } + pub fn get_branding(&self) -> CPluginMessage { + CPluginMessage::new("minecraft:brand", &self.cached_server_brand) + } + fn build_brand() -> Vec { + let brand = "Pumpkin"; + let mut buf = vec![]; + let _ = VarInt(brand.len() as i32).encode(&mut buf); + buf.extend_from_slice(brand.as_bytes()); + buf + } +} + +impl CachedStatus { pub fn new() -> Self { let status_response = Self::build_response(&BASIC_CONFIG); let status_response_json = serde_json::to_string(&status_response) .expect("Failed to parse Status response into JSON"); - let cached_server_brand = Self::build_brand(); - BikeShedServerListing { + CachedStatus { _status_response: status_response, status_response_json, - cached_server_brand, } } - pub fn get_branding(&self) -> CPluginMessage { - CPluginMessage::new("minecraft:brand", &self.cached_server_brand) - } - pub fn get_status(&self) -> CStatusResponse<'_> { CStatusResponse::new(&self.status_response_json) } @@ -83,12 +99,4 @@ impl BikeShedServerListing { general_purpose::STANDARD.encode_string(image, &mut result); result } - - fn build_brand() -> Vec { - let brand = "Pumpkin"; - let mut buf = vec![]; - let _ = VarInt(brand.len() as i32).encode(&mut buf); - buf.extend_from_slice(brand.as_bytes()); - buf - } } diff --git a/pumpkin/src/server/mod.rs b/pumpkin/src/server/mod.rs index 364101500..68dcd9501 100644 --- a/pumpkin/src/server/mod.rs +++ b/pumpkin/src/server/mod.rs @@ -1,5 +1,5 @@ +use connection_cache::{CachedBranding, CachedStatus}; use key_store::KeyStore; -use bikeshed_server_listing::BikeShedServerListing; use mio::Token; use parking_lot::{Mutex, RwLock}; use pumpkin_config::BASIC_CONFIG; @@ -31,11 +31,14 @@ use crate::{ world::World, }; +mod connection_cache; mod key_store; pub const CURRENT_MC_VERSION: &str = "1.21.1"; pub struct Server { key_store: KeyStore, + server_listing: CachedStatus, + server_branding: CachedBranding, pub plugin_loader: PluginLoader, pub command_dispatcher: Arc>, @@ -57,7 +60,6 @@ impl Server { pub fn new() -> Self { // TODO: only create when needed - let server_listing = BikeShedServerListing::new(); let auth_client = if BASIC_CONFIG.online_mode { Some( reqwest::Client::builder() @@ -89,7 +91,8 @@ impl Server { command_dispatcher: Arc::new(command_dispatcher), auth_client, key_store: KeyStore::new(), - server_listing, + server_listing: CachedStatus::new(), + server_branding: CachedBranding::new(), } } @@ -147,7 +150,7 @@ impl Server { } pub fn get_branding(&self) -> CPluginMessage<'_> { - self.server_listing.get_branding() + self.server_branding.get_branding() } pub fn get_status(&self) -> CStatusResponse<'_> {