Used Configuration more often

This commit is contained in:
zp3x 2024-07-30 22:08:12 +02:00
parent 37cac21899
commit 0632d4a60a
10 changed files with 35 additions and 47 deletions

View File

@ -1,7 +1,7 @@
server_address = "127.0.0.1" server_address = "127.0.0.1"
server_port = 25565 server_port = 25565
seed = "" seed = ""
max_plyers = -1 max_plyers = 32
view_distances = 10 view_distances = 10
simulation_distance = 10 simulation_distance = 10
resource_pack = "" resource_pack = ""

View File

@ -148,7 +148,7 @@ impl ClientPacketProcessor for Client {
fn handle_config_acknowledged( fn handle_config_acknowledged(
&mut self, &mut self,
server: &mut Server, server: &mut Server,
config_acknowledged: SAcknowledgeFinishConfig, _config_acknowledged: SAcknowledgeFinishConfig,
) { ) {
dbg!("config acknowledged"); dbg!("config acknowledged");
self.connection_state = ConnectionState::Play; self.connection_state = ConnectionState::Play;

View File

@ -21,7 +21,7 @@ use crate::{
use crate::protocol::ConnectionState; use crate::protocol::ConnectionState;
use anyhow::Context; use anyhow::Context;
use mio::{event::Event, net::TcpStream, Registry, Token}; use mio::{event::Event, net::TcpStream, Token};
use packet_decoder::PacketDecoder; use packet_decoder::PacketDecoder;
use packet_encoder::PacketEncoder; use packet_encoder::PacketEncoder;
use rsa::Pkcs1v15Encrypt; use rsa::Pkcs1v15Encrypt;

View File

@ -6,8 +6,8 @@ use crate::server::Difficulty;
#[derive(Deserialize, Serialize)] #[derive(Deserialize, Serialize)]
pub struct AdvancedConfiguration { pub struct AdvancedConfiguration {
liquid_physics: bool, pub liquid_physics: bool,
encryption: bool, pub encryption: bool,
} }
impl Default for AdvancedConfiguration { impl Default for AdvancedConfiguration {
@ -24,7 +24,7 @@ pub struct BasicConfiguration {
pub server_address: String, pub server_address: String,
pub server_port: u16, pub server_port: u16,
pub seed: String, pub seed: String,
pub max_plyers: i32, pub max_plyers: u32,
pub view_distances: u8, pub view_distances: u8,
pub simulation_distance: u8, pub simulation_distance: u8,
pub resource_pack: String, pub resource_pack: String,
@ -43,7 +43,7 @@ impl Default for BasicConfiguration {
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: -1, max_plyers: 0,
view_distances: 10, view_distances: 10,
simulation_distance: 10, simulation_distance: 10,
resource_pack: "".to_string(), resource_pack: "".to_string(),

View File

@ -1,9 +1,4 @@
use std::rc::Rc; use crate::protocol::VarInt;
use crate::{
client::Client,
protocol::{ClientPacket, RawPacket, VarInt},
};
use super::{Entity, EntityId}; use super::{Entity, EntityId};

View File

@ -2,8 +2,6 @@ use mio::net::TcpListener;
use mio::{Events, Interest, Poll, Token}; use mio::{Events, Interest, Poll, Token};
use std::io::{self}; use std::io::{self};
use std::sync::{Arc, Mutex};
use client::interrupted; use client::interrupted;
use configuration::BasicConfiguration; use configuration::BasicConfiguration;
use server::Server; use server::Server;
@ -38,7 +36,14 @@ fn main() -> io::Result<()> {
let mut events = Events::with_capacity(128); let mut events = Events::with_capacity(128);
// Setup the TCP server socket. // 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)?; let mut listener = TcpListener::bind(addr)?;
// Register the server with poll we can receive events for it. // 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"); log::info!("You now can connect to the server");
let mut server = Server::new(); let mut server = Server::new((basic_config, advanced_configuration));
loop { loop {
if let Err(err) = poll.poll(&mut events, None) { if let Err(err) = poll.poll(&mut events, None) {

View File

@ -18,7 +18,7 @@ impl ClientPacket for CRegistryData {
fn write(&self, bytebuf: &mut crate::protocol::bytebuf::buffer::ByteBuffer) { fn write(&self, bytebuf: &mut crate::protocol::bytebuf::buffer::ByteBuffer) {
bytebuf.write_string(&self.registry_id); bytebuf.write_string(&self.registry_id);
bytebuf.write_var_int(self.entry_count); bytebuf.write_var_int(self.entry_count);
// bytebuf.write_array(self.entries); // bytebuf.write_array(self.entries);
} }
} }

View File

@ -0,0 +1 @@

View File

@ -1,18 +1,16 @@
use std::{ use std::{
borrow::BorrowMut,
collections::HashMap,
io::Cursor, io::Cursor,
rc::Rc,
sync::atomic::{AtomicI32, Ordering}, sync::atomic::{AtomicI32, Ordering},
}; };
use base64::{engine::general_purpose, Engine}; 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 rsa::{rand_core::OsRng, traits::PublicKeyParts, RsaPrivateKey, RsaPublicKey};
use serde::{Deserialize, Serialize}; use serde::{Deserialize, Serialize};
use crate::{ use crate::{
client::Client, client::Client,
configuration::{AdvancedConfiguration, BasicConfiguration},
entity::{ entity::{
player::{GameMode, Player}, player::{GameMode, Player},
Entity, EntityId, Entity, EntityId,
@ -41,16 +39,11 @@ pub struct Server {
pub difficulty: Difficulty, pub difficulty: Difficulty,
} }
impl Default for Server {
fn default() -> Self {
Self::new()
}
}
impl Server { impl Server {
pub fn new() -> Self { pub fn new(config: (BasicConfiguration, AdvancedConfiguration)) -> Self {
let max_players = 20; 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 // todo, only create when needed
let (public_key, private_key) = Self::generate_keys(); let (public_key, private_key) = Self::generate_keys();
@ -65,15 +58,15 @@ impl Server {
// 0 is invalid // 0 is invalid
entity_id: 2.into(), entity_id: 2.into(),
world: World::new(), world: World::new(),
online_mode: true, online_mode: config.0.online_mode,
encryption: true, encryption: config.1.encryption,
compression_threshold: None, // 256 compression_threshold: None, // 256
public_key, public_key,
private_key, private_key,
max_players, max_players,
status_response, status_response,
public_key_der, public_key_der,
difficulty: Difficulty::Normal, difficulty: config.0.default_difficulty,
} }
} }
@ -84,10 +77,11 @@ impl Server {
poll: &Poll, poll: &Poll,
event: &Event, event: &Event,
) -> anyhow::Result<bool> { ) -> anyhow::Result<bool> {
let _ = poll;
// todo, Poll players in every world // todo, Poll players in every world
client.poll(self, event) client.poll(self, event)
} }
//CLIENT KOMMT in den Player
pub fn spawn_player(&mut self, client: &mut Client) { pub fn spawn_player(&mut self, client: &mut Client) {
let player = Player { let player = Player {
entity: Entity { entity: Entity {
@ -128,7 +122,9 @@ impl Server {
self.entity_id.fetch_add(1, Ordering::SeqCst) 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"); let path = concat!(env!("CARGO_MANIFEST_DIR"), "/icon.png");
StatusResponse { StatusResponse {
@ -137,14 +133,14 @@ impl Server {
protocol: 767, protocol: 767,
}, },
players: Players { players: Players {
max: max_players, max: config.0.max_plyers,
online: 0, online: 0,
sample: vec![Sample { sample: vec![Sample {
name: "".into(), name: "".into(),
id: "".into(), id: "".into(),
}], }],
}, },
description: "Pumpkin Server".into(), description: config.0.motd.clone(),
favicon: Self::load_icon(path), favicon: Self::load_icon(path),
} }
} }

View File

@ -1,13 +1,4 @@
use mio::Token; use crate::entity::player::Player;
use crate::{
entity::{
player::{GameMode, Player},
Entity,
},
protocol::{client::play::CLogin, VarInt},
server::Difficulty,
};
pub struct World { pub struct World {
pub players: Vec<Player>, pub players: Vec<Player>,