Support Commands in Server Console

This commit is contained in:
Snowiiii 2024-08-05 14:00:40 +02:00
parent c54ab006ae
commit ebda64b2c7
4 changed files with 45 additions and 24 deletions

View File

@ -48,7 +48,7 @@ Pumpkin is currently under heavy development.
- [ ] Player Attack - [ ] Player Attack
- Server - Server
- [ ] Chat - [ ] Chat
- [ ] Commands - [x] Commands
Check out our [Github Project](https://github.com/users/Snowiiii/projects/12/views/1) to see current progress Check out our [Github Project](https://github.com/users/Snowiiii/projects/12/views/1) to see current progress

View File

@ -30,9 +30,10 @@ impl<'a> CommandSender<'a> {
} }
pub fn handle_command(sender: &mut CommandSender, command: String, server: &mut Server) { pub fn handle_command(sender: &mut CommandSender, command: String, server: &mut Server) {
let command = command.to_lowercase(); let command = command.to_lowercase();
dbg!("handling command");
dbg!(&command);
if command.starts_with(PumpkinCommand::NAME) { 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());
} }

View File

@ -18,9 +18,15 @@ pub mod util;
#[cfg(not(target_os = "wasi"))] #[cfg(not(target_os = "wasi"))]
fn main() -> io::Result<()> { 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 client::Client;
use commands::handle_command;
use configuration::AdvancedConfiguration; use configuration::AdvancedConfiguration;
let basic_config = BasicConfiguration::load("configuration.toml"); let basic_config = BasicConfiguration::load("configuration.toml");
@ -52,11 +58,30 @@ fn main() -> io::Result<()> {
// Unique token for each incoming connection. // Unique token for each incoming connection.
let mut unique_token = Token(SERVER.0 + 1); let mut unique_token = Token(SERVER.0 + 1);
let mut connections: HashMap<Token, Rc<RefCell<Client>>> = HashMap::new(); let mut connections: HashMap<Token, Client> = HashMap::new();
log::info!("You now can connect to the server"); 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 { loop {
if let Err(err) = poll.poll(&mut events, None) { if let Err(err) = poll.poll(&mut events, None) {
@ -96,29 +121,23 @@ fn main() -> io::Result<()> {
Interest::READABLE.add(Interest::WRITABLE), Interest::READABLE.add(Interest::WRITABLE),
)?; )?;
let rc_token = Rc::new(token); let rc_token = Rc::new(token);
let client = Rc::new(RefCell::new(Client::new( let client = Client::new(Rc::clone(&rc_token), connection, addr);
Rc::clone(&rc_token), // server.add_client(rc_token, Rc::clone(&client));
connection,
addr,
)));
server.add_client(rc_token, Rc::clone(&client));
connections.insert(token, client); connections.insert(token, client);
}, },
token => { token => {
// Maybe received an event for a TCP connection. // Maybe received an event for a TCP connection.
let done = if let Some(client) = connections.get_mut(&token) { let done = if let Some(client) = connections.get_mut(&token) {
let mut client = client.borrow_mut(); client.poll(&mut server.lock().unwrap(), event);
client.poll(&mut server, event);
client.closed client.closed
} else { } else {
// Sporadic events happen, we can safely ignore them. // Sporadic events happen, we can safely ignore them.
false false
}; };
if done { if done {
if let Some(client) = connections.remove(&token) { if let Some(mut client) = connections.remove(&token) {
let mut client = client.borrow_mut(); // server.remove_client(&token);
server.remove_client(&token);
poll.registry().deregister(&mut client.connection)?; poll.registry().deregister(&mut client.connection)?;
} }
} }

View File

@ -1,6 +1,5 @@
use std::{ use std::{
cell::RefCell, cell::RefCell,
collections::HashMap,
io::Cursor, io::Cursor,
rc::Rc, rc::Rc,
sync::atomic::{AtomicI32, Ordering}, 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 /// Cache the Server brand buffer so we don't have to rebuild them every time a player joins
pub cached_server_brand: Vec<u8>, pub cached_server_brand: Vec<u8>,
pub current_clients: HashMap<Rc<Token>, Rc<RefCell<Client>>>, // pub current_clients: HashMap<Rc<Token>, Rc<RefCell<Client>>>,
// 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
@ -91,7 +90,7 @@ impl Server {
status_response, status_response,
status_response_json, status_response_json,
public_key_der, public_key_der,
current_clients: HashMap::new(), // current_clients: HashMap::new(),
base_config: config.0, base_config: config.0,
auth_client, auth_client,
advanced_config: config.1, advanced_config: config.1,
@ -105,11 +104,11 @@ impl Server {
} }
pub fn add_client(&mut self, token: Rc<Token>, client: Rc<RefCell<Client>>) { pub fn add_client(&mut self, token: Rc<Token>, client: Rc<RefCell<Client>>) {
self.current_clients.insert(token, client); // self.current_clients.insert(token, client);
} }
pub fn remove_client(&mut self, token: &Token) { pub fn remove_client(&mut self, token: &Token) {
self.current_clients.remove(token); // self.current_clients.remove(token);
} }
// todo: do this in a world // todo: do this in a world
@ -182,13 +181,15 @@ impl Server {
P: ClientPacket, P: ClientPacket,
P: Clone, P: Clone,
{ {
for client in self.current_clients.values() { /* for client in self.current_clients.values() {
// Check if client is a player // Check if client is a player
if client.borrow().is_player() { if client.borrow().is_player() {
// we need to clone, Because we send a new packet to every client // we need to clone, Because we send a new packet to every client
client.borrow_mut().send_packet(packet.clone())?; client.borrow_mut().send_packet(packet.clone())?;
} }
} }
*/
Ok(()) Ok(())
} }