Improve config

This commit is contained in:
Snowiiii 2024-08-04 15:44:32 +02:00
parent 920f51f782
commit 5dfebfafba
2 changed files with 72 additions and 19 deletions

View File

@ -4,46 +4,71 @@ use serde::{Deserialize, Serialize};
use crate::server::Difficulty; use crate::server::Difficulty;
/// Current Config version of the Base Config
const CURRENT_BASE_VERSION: &str = "1.0.0";
#[derive(Deserialize, Serialize)] #[derive(Deserialize, Serialize)]
pub struct AdvancedConfiguration { pub struct AdvancedConfiguration {
pub liquid_physics: bool, pub liquid_physics: bool,
pub encryption: bool,
} }
impl Default for AdvancedConfiguration { impl Default for AdvancedConfiguration {
fn default() -> Self { fn default() -> Self {
Self { Self {
liquid_physics: true, liquid_physics: true,
encryption: true,
} }
} }
} }
#[derive(Deserialize, Serialize)] #[derive(Deserialize, Serialize)]
pub struct BasicConfiguration { 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, pub server_address: String,
/// The Server Port to bind
/// Minecraft java editon uses 25565 by default
pub server_port: u16, pub server_port: u16,
/// The World sent to the Client
pub seed: String, 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, pub view_distances: u8,
/// The Maximum simulated view distance
pub simulation_distance: u8, pub simulation_distance: u8,
/// Path for resource pack
pub resource_pack: String, pub resource_pack: String,
/// Sha1 hash of resource pack, when present
pub resource_pack_sha1: String, pub resource_pack_sha1: String,
/// default difficulty
pub default_difficulty: Difficulty, pub default_difficulty: Difficulty,
/// allow nether dimension
pub allow_nether: bool, pub allow_nether: bool,
/// is the server hardcore mode?
pub hardcore: bool, 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 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, pub motd: String,
} }
impl Default for BasicConfiguration { impl Default for BasicConfiguration {
fn default() -> Self { fn default() -> Self {
Self { Self {
config_version: CURRENT_BASE_VERSION.to_string(),
server_address: "127.0.0.1".to_string(), server_address: "127.0.0.1".to_string(),
server_port: 25565, server_port: 25565,
seed: "".to_string(), seed: "".to_string(),
max_plyers: 0, max_players: 0,
view_distances: 10, view_distances: 10,
simulation_distance: 10, simulation_distance: 10,
resource_pack: "".to_string(), resource_pack: "".to_string(),
@ -52,7 +77,8 @@ impl Default for BasicConfiguration {
allow_nether: true, allow_nether: true,
hardcore: false, hardcore: false,
online_mode: true, online_mode: true,
spawn_protection: 16, encryption: true,
prevent_proxy_connections: true,
motd: "A Blazing fast Pumpkin Server!".to_string(), motd: "A Blazing fast Pumpkin Server!".to_string(),
} }
} }
@ -81,7 +107,31 @@ impl BasicConfiguration {
let config = BasicConfiguration::default(); let config = BasicConfiguration::default();
let toml = toml::to_string(&config).expect("Couldn't create toml!"); let toml = toml::to_string(&config).expect("Couldn't create toml!");
std::fs::write(path, toml).expect("Couldn't save configuration"); std::fs::write(path, toml).expect("Couldn't save configuration");
config.validate();
config 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"
);
}
} }

View File

@ -28,15 +28,10 @@ use crate::{
pub struct Server { pub struct Server {
pub compression_threshold: Option<u8>, pub compression_threshold: Option<u8>,
pub online_mode: bool,
pub encryption: bool, // encryptiony is always required when online_mode is disabled
pub public_key: RsaPublicKey, pub public_key: RsaPublicKey,
pub private_key: RsaPrivateKey, pub private_key: RsaPrivateKey,
pub public_key_der: Box<[u8]>, pub public_key_der: Box<[u8]>,
/// the maximum amount of players that can join the Server
pub max_players: u32,
// pub world: World, // pub world: World,
pub status_response: StatusResponse, pub status_response: StatusResponse,
// We cache the json response here so we don't parse it every time someone makes a Status request. // 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 <World, Player> // todo replace with HashMap <World, Player>
entity_id: AtomicI32, // todo: place this into every world 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<reqwest::Client>,
} }
impl Server { impl Server {
@ -67,22 +66,26 @@ impl Server {
&private_key.e().to_bytes_be(), &private_key.e().to_bytes_be(),
) )
.into_boxed_slice(); .into_boxed_slice();
let auth_client = if config.0.online_mode {
Some(reqwest::Client::new())
} else {
None
};
Self { Self {
// 0 is invalid // 0 is invalid
entity_id: 2.into(), entity_id: 2.into(),
// world: World::load(""), // world: World::load(""),
online_mode: config.0.online_mode,
encryption: config.1.encryption,
compression_threshold: None, // 256 compression_threshold: None, // 256
public_key, public_key,
cached_server_brand, cached_server_brand,
private_key, private_key,
max_players: config.0.max_plyers,
status_response, status_response,
status_response_json, status_response_json,
public_key_der, 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 client
.send_packet(CLogin::new( .send_packet(CLogin::new(
entity_id, entity_id,
self.difficulty == Difficulty::Hard, self.base_config.hardcore,
vec!["minecraft:overworld".into()], vec!["minecraft:overworld".into()],
self.max_players as VarInt, self.base_config.max_players as VarInt,
8, // view distance todo 8, // view distance todo
8, // sim view dinstance todo 8, // sim view dinstance todo
false, false,
@ -184,7 +187,7 @@ impl Server {
protocol: 767, protocol: 767,
}, },
players: Players { players: Players {
max: config.max_plyers, max: config.max_players,
online: 0, online: 0,
sample: vec![Sample { sample: vec![Sample {
name: "".into(), name: "".into(),