From 0632d4a60a6fd19afb4d829e834529c11a39db51 Mon Sep 17 00:00:00 2001 From: zp3x <90056490+zp3x@users.noreply.github.com> Date: Tue, 30 Jul 2024 22:08:12 +0200 Subject: [PATCH] Used Configuration more often --- configuration.toml | 2 +- pumpkin/src/client/client_packet.rs | 2 +- pumpkin/src/client/mod.rs | 2 +- pumpkin/src/configuration.rs | 8 +++--- pumpkin/src/entity/player.rs | 7 +---- pumpkin/src/main.rs | 13 ++++++--- pumpkin/src/protocol/client/config/mod.rs | 2 +- pumpkin/src/protocol/registry/mod.rs | 1 + pumpkin/src/server.rs | 34 ++++++++++------------- pumpkin/src/world.rs | 11 +------- 10 files changed, 35 insertions(+), 47 deletions(-) diff --git a/configuration.toml b/configuration.toml index 0cf04ee10..aa0b0edc3 100644 --- a/configuration.toml +++ b/configuration.toml @@ -1,7 +1,7 @@ server_address = "127.0.0.1" server_port = 25565 seed = "" -max_plyers = -1 +max_plyers = 32 view_distances = 10 simulation_distance = 10 resource_pack = "" diff --git a/pumpkin/src/client/client_packet.rs b/pumpkin/src/client/client_packet.rs index 2ed1d8de3..3928713a5 100644 --- a/pumpkin/src/client/client_packet.rs +++ b/pumpkin/src/client/client_packet.rs @@ -148,7 +148,7 @@ impl ClientPacketProcessor for Client { fn handle_config_acknowledged( &mut self, server: &mut Server, - config_acknowledged: SAcknowledgeFinishConfig, + _config_acknowledged: SAcknowledgeFinishConfig, ) { dbg!("config acknowledged"); self.connection_state = ConnectionState::Play; diff --git a/pumpkin/src/client/mod.rs b/pumpkin/src/client/mod.rs index 9cd2f82c5..21f1b72d3 100644 --- a/pumpkin/src/client/mod.rs +++ b/pumpkin/src/client/mod.rs @@ -21,7 +21,7 @@ use crate::{ use crate::protocol::ConnectionState; use anyhow::Context; -use mio::{event::Event, net::TcpStream, Registry, Token}; +use mio::{event::Event, net::TcpStream, Token}; use packet_decoder::PacketDecoder; use packet_encoder::PacketEncoder; use rsa::Pkcs1v15Encrypt; diff --git a/pumpkin/src/configuration.rs b/pumpkin/src/configuration.rs index 2faa9020e..a72c6acec 100644 --- a/pumpkin/src/configuration.rs +++ b/pumpkin/src/configuration.rs @@ -6,8 +6,8 @@ use crate::server::Difficulty; #[derive(Deserialize, Serialize)] pub struct AdvancedConfiguration { - liquid_physics: bool, - encryption: bool, + pub liquid_physics: bool, + pub encryption: bool, } impl Default for AdvancedConfiguration { @@ -24,7 +24,7 @@ pub struct BasicConfiguration { pub server_address: String, pub server_port: u16, pub seed: String, - pub max_plyers: i32, + pub max_plyers: u32, pub view_distances: u8, pub simulation_distance: u8, pub resource_pack: String, @@ -43,7 +43,7 @@ impl Default for BasicConfiguration { server_address: "127.0.0.1".to_string(), server_port: 25565, seed: "".to_string(), - max_plyers: -1, + max_plyers: 0, view_distances: 10, simulation_distance: 10, resource_pack: "".to_string(), diff --git a/pumpkin/src/entity/player.rs b/pumpkin/src/entity/player.rs index 31f98d1f0..2c3486df3 100644 --- a/pumpkin/src/entity/player.rs +++ b/pumpkin/src/entity/player.rs @@ -1,9 +1,4 @@ -use std::rc::Rc; - -use crate::{ - client::Client, - protocol::{ClientPacket, RawPacket, VarInt}, -}; +use crate::protocol::VarInt; use super::{Entity, EntityId}; diff --git a/pumpkin/src/main.rs b/pumpkin/src/main.rs index 94aeaf8e4..03f1e02fb 100644 --- a/pumpkin/src/main.rs +++ b/pumpkin/src/main.rs @@ -2,8 +2,6 @@ use mio::net::TcpListener; use mio::{Events, Interest, Poll, Token}; use std::io::{self}; -use std::sync::{Arc, Mutex}; - use client::interrupted; use configuration::BasicConfiguration; use server::Server; @@ -38,7 +36,14 @@ fn main() -> io::Result<()> { let mut events = Events::with_capacity(128); // Setup the TCP server socket. - let addr = "127.0.0.1:25565".parse().unwrap(); + + let addr = format!( + "{}:{}", + basic_config.server_address, basic_config.server_port + ) + .parse() + .unwrap(); + let mut listener = TcpListener::bind(addr)?; // Register the server with poll we can receive events for it. @@ -52,7 +57,7 @@ fn main() -> io::Result<()> { log::info!("You now can connect to the server"); - let mut server = Server::new(); + let mut server = Server::new((basic_config, advanced_configuration)); loop { if let Err(err) = poll.poll(&mut events, None) { diff --git a/pumpkin/src/protocol/client/config/mod.rs b/pumpkin/src/protocol/client/config/mod.rs index 23d847f94..24689f9e3 100644 --- a/pumpkin/src/protocol/client/config/mod.rs +++ b/pumpkin/src/protocol/client/config/mod.rs @@ -18,7 +18,7 @@ impl ClientPacket for CRegistryData { fn write(&self, bytebuf: &mut crate::protocol::bytebuf::buffer::ByteBuffer) { bytebuf.write_string(&self.registry_id); bytebuf.write_var_int(self.entry_count); - // bytebuf.write_array(self.entries); + // bytebuf.write_array(self.entries); } } diff --git a/pumpkin/src/protocol/registry/mod.rs b/pumpkin/src/protocol/registry/mod.rs index e69de29bb..8b1378917 100644 --- a/pumpkin/src/protocol/registry/mod.rs +++ b/pumpkin/src/protocol/registry/mod.rs @@ -0,0 +1 @@ + diff --git a/pumpkin/src/server.rs b/pumpkin/src/server.rs index 4f4c43ef1..b721ca5ec 100644 --- a/pumpkin/src/server.rs +++ b/pumpkin/src/server.rs @@ -1,18 +1,16 @@ use std::{ - borrow::BorrowMut, - collections::HashMap, io::Cursor, - rc::Rc, sync::atomic::{AtomicI32, Ordering}, }; use base64::{engine::general_purpose, Engine}; -use mio::{event::Event, net::TcpStream, Poll, Token}; +use mio::{event::Event, Poll}; use rsa::{rand_core::OsRng, traits::PublicKeyParts, RsaPrivateKey, RsaPublicKey}; use serde::{Deserialize, Serialize}; use crate::{ client::Client, + configuration::{AdvancedConfiguration, BasicConfiguration}, entity::{ player::{GameMode, Player}, Entity, EntityId, @@ -41,16 +39,11 @@ pub struct Server { pub difficulty: Difficulty, } -impl Default for Server { - fn default() -> Self { - Self::new() - } -} - impl Server { - pub fn new() -> Self { + pub fn new(config: (BasicConfiguration, AdvancedConfiguration)) -> Self { let max_players = 20; - let status_response = Self::default_response(max_players); + let config_clone = &config; + let status_response = Self::default_response(config_clone); // todo, only create when needed let (public_key, private_key) = Self::generate_keys(); @@ -65,15 +58,15 @@ impl Server { // 0 is invalid entity_id: 2.into(), world: World::new(), - online_mode: true, - encryption: true, + online_mode: config.0.online_mode, + encryption: config.1.encryption, compression_threshold: None, // 256 public_key, private_key, max_players, status_response, public_key_der, - difficulty: Difficulty::Normal, + difficulty: config.0.default_difficulty, } } @@ -84,10 +77,11 @@ impl Server { poll: &Poll, event: &Event, ) -> anyhow::Result { + let _ = poll; // todo, Poll players in every world client.poll(self, event) } - //CLIENT KOMMT in den Player + pub fn spawn_player(&mut self, client: &mut Client) { let player = Player { entity: Entity { @@ -128,7 +122,9 @@ impl Server { self.entity_id.fetch_add(1, Ordering::SeqCst) } - pub fn default_response(max_players: u32) -> StatusResponse { + pub fn default_response( + config: &(BasicConfiguration, AdvancedConfiguration), + ) -> StatusResponse { let path = concat!(env!("CARGO_MANIFEST_DIR"), "/icon.png"); StatusResponse { @@ -137,14 +133,14 @@ impl Server { protocol: 767, }, players: Players { - max: max_players, + max: config.0.max_plyers, online: 0, sample: vec![Sample { name: "".into(), id: "".into(), }], }, - description: "Pumpkin Server".into(), + description: config.0.motd.clone(), favicon: Self::load_icon(path), } } diff --git a/pumpkin/src/world.rs b/pumpkin/src/world.rs index 7fb7085ba..2f8927440 100644 --- a/pumpkin/src/world.rs +++ b/pumpkin/src/world.rs @@ -1,13 +1,4 @@ -use mio::Token; - -use crate::{ - entity::{ - player::{GameMode, Player}, - Entity, - }, - protocol::{client::play::CLogin, VarInt}, - server::Difficulty, -}; +use crate::entity::player::Player; pub struct World { pub players: Vec,