Add Commands
This commit is contained in:
1 parent
5714c1b786
commit
c54ab006ae
14 files changed
+209
-18
No files matched your search
@@ -0,0 +1,38 @@
|
||||
use pumpkin::PumpkinCommand;
|
||||
use pumpkin_protocol::text::TextComponent;
|
||||
|
||||
use crate::{client::Client, server::Server};
|
||||
|
||||
mod pumpkin;
|
||||
|
||||
/// I think it would be great to split this up into a seperate crate, But idk how i should do that, Because we have to rely on Client and Server
|
||||
pub trait Command<'a> {
|
||||
// Name of the Plugin, Use lower case
|
||||
const NAME: &'a str;
|
||||
const DESCRIPTION: &'a str;
|
||||
|
||||
fn on_execute(sender: &mut CommandSender<'a>, command: String, server: &mut Server);
|
||||
}
|
||||
|
||||
pub enum CommandSender<'a> {
|
||||
Console,
|
||||
Player(&'a mut Client),
|
||||
}
|
||||
|
||||
impl<'a> CommandSender<'a> {
|
||||
pub fn send_message(&mut self, text: TextComponent) {
|
||||
match self {
|
||||
// todo: add color and stuff to console
|
||||
CommandSender::Console => log::info!("{}", text.text),
|
||||
CommandSender::Player(c) => c.send_message(text),
|
||||
}
|
||||
}
|
||||
}
|
||||
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)
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,29 @@
|
||||
use pumpkin_protocol::{text::TextComponent, CURRENT_MC_PROTOCOL};
|
||||
|
||||
use crate::server::CURRENT_MC_VERSION;
|
||||
|
||||
use super::Command;
|
||||
|
||||
pub struct PumpkinCommand {}
|
||||
|
||||
impl PumpkinCommand {
|
||||
pub fn new() -> Self {
|
||||
Self {}
|
||||
}
|
||||
}
|
||||
|
||||
impl<'a> Command<'a> for PumpkinCommand {
|
||||
const NAME: &'a str = "pumpkin";
|
||||
|
||||
const DESCRIPTION: &'a str = "Displays information about Pumpkin";
|
||||
|
||||
fn on_execute(
|
||||
sender: &mut super::CommandSender<'a>,
|
||||
_command: String,
|
||||
_server: &mut crate::server::Server,
|
||||
) {
|
||||
let version = env!("CARGO_PKG_VERSION");
|
||||
let description = env!("CARGO_PKG_DESCRIPTION");
|
||||
sender.send_message(TextComponent::from(format!("Pumpkin {version}, {description} (Minecraft {CURRENT_MC_VERSION}, Protocol {CURRENT_MC_PROTOCOL})")))
|
||||
}
|
||||
}
|
||||
Reference in new issue
Block a user