From ebda64b2c76cff667b32c6343552d359c4b96fdd Mon Sep 17 00:00:00 2001 From: Snowiiii Date: Mon, 5 Aug 2024 14:00:40 +0200 Subject: [PATCH] Support Commands in Server Console --- README.md | 2 +- pumpkin/src/commands/mod.rs | 7 +++--- pumpkin/src/main.rs | 47 ++++++++++++++++++++++++++----------- pumpkin/src/server.rs | 13 +++++----- 4 files changed, 45 insertions(+), 24 deletions(-) diff --git a/README.md b/README.md index c1f080518..a993bb9d8 100644 --- a/README.md +++ b/README.md @@ -48,7 +48,7 @@ Pumpkin is currently under heavy development. - [ ] Player Attack - Server - [ ] Chat - - [ ] Commands + - [x] Commands Check out our [Github Project](https://github.com/users/Snowiiii/projects/12/views/1) to see current progress diff --git a/pumpkin/src/commands/mod.rs b/pumpkin/src/commands/mod.rs index 5578a3d17..796f858b1 100644 --- a/pumpkin/src/commands/mod.rs +++ b/pumpkin/src/commands/mod.rs @@ -30,9 +30,10 @@ impl<'a> CommandSender<'a> { } pub fn handle_command(sender: &mut CommandSender, command: String, server: &mut Server) { let command = command.to_lowercase(); - dbg!("handling command"); - dbg!(&command); if command.starts_with(PumpkinCommand::NAME) { - PumpkinCommand::on_execute(sender, command, server) + PumpkinCommand::on_execute(sender, command, server); + return; } + // todo: red color + sender.send_message("Command not Found".into()); } diff --git a/pumpkin/src/main.rs b/pumpkin/src/main.rs index d1ad1f961..9fd50e8ba 100644 --- a/pumpkin/src/main.rs +++ b/pumpkin/src/main.rs @@ -18,9 +18,15 @@ pub mod util; #[cfg(not(target_os = "wasi"))] fn main() -> io::Result<()> { - use std::{cell::RefCell, collections::HashMap, rc::Rc}; + use std::{ + collections::HashMap, + rc::Rc, + sync::{Arc, Mutex}, + thread, + }; use client::Client; + use commands::handle_command; use configuration::AdvancedConfiguration; let basic_config = BasicConfiguration::load("configuration.toml"); @@ -52,11 +58,30 @@ fn main() -> io::Result<()> { // Unique token for each incoming connection. let mut unique_token = Token(SERVER.0 + 1); - let mut connections: HashMap>> = HashMap::new(); + let mut connections: HashMap = HashMap::new(); log::info!("You now can connect to the server"); - let mut server = Server::new((basic_config, advanced_configuration)); + let server = Arc::new(Mutex::new(Server::new(( + basic_config, + advanced_configuration, + )))); + let server_clone = server.clone(); + + thread::spawn(move || { + let stdin = std::io::stdin(); + loop { + let mut out = String::new(); + stdin + .read_line(&mut out) + .expect("Failed to read console line"); + handle_command( + &mut commands::CommandSender::Console, + out, + &mut server_clone.lock().unwrap(), + ); + } + }); loop { if let Err(err) = poll.poll(&mut events, None) { @@ -96,29 +121,23 @@ fn main() -> io::Result<()> { Interest::READABLE.add(Interest::WRITABLE), )?; let rc_token = Rc::new(token); - let client = Rc::new(RefCell::new(Client::new( - Rc::clone(&rc_token), - connection, - addr, - ))); - server.add_client(rc_token, Rc::clone(&client)); + let client = Client::new(Rc::clone(&rc_token), connection, addr); + // server.add_client(rc_token, Rc::clone(&client)); connections.insert(token, client); }, token => { // Maybe received an event for a TCP connection. let done = if let Some(client) = connections.get_mut(&token) { - let mut client = client.borrow_mut(); - client.poll(&mut server, event); + client.poll(&mut server.lock().unwrap(), event); client.closed } else { // Sporadic events happen, we can safely ignore them. false }; if done { - if let Some(client) = connections.remove(&token) { - let mut client = client.borrow_mut(); - server.remove_client(&token); + if let Some(mut client) = connections.remove(&token) { + // server.remove_client(&token); poll.registry().deregister(&mut client.connection)?; } } diff --git a/pumpkin/src/server.rs b/pumpkin/src/server.rs index d32c3a69c..85f23dbae 100644 --- a/pumpkin/src/server.rs +++ b/pumpkin/src/server.rs @@ -1,6 +1,5 @@ use std::{ cell::RefCell, - collections::HashMap, io::Cursor, rc::Rc, sync::atomic::{AtomicI32, Ordering}, @@ -47,7 +46,7 @@ pub struct Server { /// Cache the Server brand buffer so we don't have to rebuild them every time a player joins pub cached_server_brand: Vec, - pub current_clients: HashMap, Rc>>, + // pub current_clients: HashMap, Rc>>, // todo replace with HashMap entity_id: AtomicI32, // todo: place this into every world @@ -91,7 +90,7 @@ impl Server { status_response, status_response_json, public_key_der, - current_clients: HashMap::new(), + // current_clients: HashMap::new(), base_config: config.0, auth_client, advanced_config: config.1, @@ -105,11 +104,11 @@ impl Server { } pub fn add_client(&mut self, token: Rc, client: Rc>) { - self.current_clients.insert(token, client); + // self.current_clients.insert(token, client); } pub fn remove_client(&mut self, token: &Token) { - self.current_clients.remove(token); + // self.current_clients.remove(token); } // todo: do this in a world @@ -182,13 +181,15 @@ impl Server { P: ClientPacket, P: Clone, { - for client in self.current_clients.values() { + /* for client in self.current_clients.values() { // Check if client is a player if client.borrow().is_player() { // we need to clone, Because we send a new packet to every client client.borrow_mut().send_packet(packet.clone())?; } } + + */ Ok(()) }