From 5dfebfafbad6222ba70b9c6f3cc0b279164b3d4c Mon Sep 17 00:00:00 2001 From: Snowiiii Date: Sun, 4 Aug 2024 15:44:32 +0200 Subject: [PATCH] Improve config --- pumpkin/src/configuration.rs | 62 ++++++++++++++++++++++++++++++++---- pumpkin/src/server.rs | 29 +++++++++-------- 2 files changed, 72 insertions(+), 19 deletions(-) diff --git a/pumpkin/src/configuration.rs b/pumpkin/src/configuration.rs index 477040647..0cc0cd39c 100644 --- a/pumpkin/src/configuration.rs +++ b/pumpkin/src/configuration.rs @@ -4,46 +4,71 @@ use serde::{Deserialize, Serialize}; use crate::server::Difficulty; +/// Current Config version of the Base Config +const CURRENT_BASE_VERSION: &str = "1.0.0"; + #[derive(Deserialize, Serialize)] pub struct AdvancedConfiguration { pub liquid_physics: bool, - pub encryption: bool, } impl Default for AdvancedConfiguration { fn default() -> Self { Self { liquid_physics: true, - encryption: true, } } } #[derive(Deserialize, Serialize)] pub struct BasicConfiguration { + /// DO NOT CHANGE + /// Generated to see if config may have been updated with an new Update + pub config_version: String, + /// The Server Address to bind pub server_address: String, + /// The Server Port to bind + /// Minecraft java editon uses 25565 by default pub server_port: u16, + /// The World sent to the Client pub seed: String, - pub max_plyers: u32, + /// The Maximum amout of player which can join the Server + pub max_players: u32, + /// The Maximum view distance pub view_distances: u8, + /// The Maximum simulated view distance pub simulation_distance: u8, + /// Path for resource pack pub resource_pack: String, + /// Sha1 hash of resource pack, when present pub resource_pack_sha1: String, + /// default difficulty pub default_difficulty: Difficulty, + /// allow nether dimension pub allow_nether: bool, + /// is the server hardcore mode? pub hardcore: bool, + /// Online Mode, Require a valid Minecraft account to join the Server, Also adds support for Skins and Capes + /// IMPORTANT: Be carefull when turning this off, Everyone could join your server and use any Nickname or UUID they want pub online_mode: bool, - pub spawn_protection: u32, + /// Enable encryption for Packets send & received. + /// IMPORTANT: When Online mode is enabled, encryption MUST be enabled also + pub encryption: bool, + /// When enabled, Client can't use a diffrent ip to login as they use for the Authentication Server + /// Usally preventing proxy connections + pub prevent_proxy_connections: bool, + /// The description send when Client performs a Status request, (e.g Multiplayer Screen) pub motd: String, } impl Default for BasicConfiguration { fn default() -> Self { Self { + config_version: CURRENT_BASE_VERSION.to_string(), server_address: "127.0.0.1".to_string(), server_port: 25565, seed: "".to_string(), - max_plyers: 0, + max_players: 0, view_distances: 10, simulation_distance: 10, resource_pack: "".to_string(), @@ -52,7 +77,8 @@ impl Default for BasicConfiguration { allow_nether: true, hardcore: false, online_mode: true, - spawn_protection: 16, + encryption: true, + prevent_proxy_connections: true, motd: "A Blazing fast Pumpkin Server!".to_string(), } } @@ -81,7 +107,31 @@ impl BasicConfiguration { let config = BasicConfiguration::default(); let toml = toml::to_string(&config).expect("Couldn't create toml!"); std::fs::write(path, toml).expect("Couldn't save configuration"); + config.validate(); config } } + + pub fn validate(&self) { + assert_eq!( + self.config_version, CURRENT_BASE_VERSION, + "Config version does not match used Config version. Please update your config" + ); + assert!(self.view_distances >= 2, "View distance must be atleast 2"); + assert!( + self.view_distances <= 32, + "View distance must be less than 32" + ); + if self.online_mode { + assert!( + self.encryption, + "When Online Mode is enabled, Encryption must be enabled" + ) + } + assert_eq!( + !self.resource_pack.is_empty(), + !self.resource_pack_sha1.is_empty(), + "Resource Pack path or Sha1 hash is missing" + ); + } } diff --git a/pumpkin/src/server.rs b/pumpkin/src/server.rs index 44f369c68..8785ec9c0 100644 --- a/pumpkin/src/server.rs +++ b/pumpkin/src/server.rs @@ -28,15 +28,10 @@ use crate::{ pub struct Server { pub compression_threshold: Option, - pub online_mode: bool, - pub encryption: bool, // encryptiony is always required when online_mode is disabled pub public_key: RsaPublicKey, pub private_key: RsaPrivateKey, pub public_key_der: Box<[u8]>, - /// the maximum amount of players that can join the Server - pub max_players: u32, - // pub world: World, pub status_response: StatusResponse, // We cache the json response here so we don't parse it every time someone makes a Status request. @@ -48,7 +43,11 @@ pub struct Server { // todo replace with HashMap entity_id: AtomicI32, // todo: place this into every world - pub difficulty: Difficulty, + pub base_config: BasicConfiguration, + pub advanced_config: AdvancedConfiguration, + + /// Used for Authentication, None is Online mode is disabled + pub auth_client: Option, } impl Server { @@ -67,22 +66,26 @@ impl Server { &private_key.e().to_bytes_be(), ) .into_boxed_slice(); + let auth_client = if config.0.online_mode { + Some(reqwest::Client::new()) + } else { + None + }; Self { // 0 is invalid entity_id: 2.into(), // world: World::load(""), - online_mode: config.0.online_mode, - encryption: config.1.encryption, compression_threshold: None, // 256 public_key, cached_server_brand, private_key, - max_players: config.0.max_plyers, status_response, status_response_json, public_key_der, - difficulty: config.0.default_difficulty, + base_config: config.0, + auth_client, + advanced_config: config.1, } } @@ -102,9 +105,9 @@ impl Server { client .send_packet(CLogin::new( entity_id, - self.difficulty == Difficulty::Hard, + self.base_config.hardcore, vec!["minecraft:overworld".into()], - self.max_players as VarInt, + self.base_config.max_players as VarInt, 8, // view distance todo 8, // sim view dinstance todo false, @@ -184,7 +187,7 @@ impl Server { protocol: 767, }, players: Players { - max: config.max_plyers, + max: config.max_players, online: 0, sample: vec![Sample { name: "".into(),