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
- Server
- [ ] Chat
- [ ] Commands
- [x] Commands
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) {
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());
}

View File

@ -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<Token, Rc<RefCell<Client>>> = HashMap::new();
let mut connections: HashMap<Token, Client> = 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)?;
}
}

View File

@ -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<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>
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<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) {
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(())
}