Add Game mode Command
This commit is contained in:
1 parent
cb3cb760ac
commit
1e114c008e
3 files changed
+112
-12
No files matched your search
@@ -0,0 +1,47 @@
|
||||
use super::Command;
|
||||
|
||||
pub struct GamemodeCommand {}
|
||||
|
||||
impl GamemodeCommand {
|
||||
pub fn new() -> Self {
|
||||
Self {}
|
||||
}
|
||||
}
|
||||
|
||||
impl<'a> Command<'a> for GamemodeCommand {
|
||||
const NAME: &'a str = "gamemode";
|
||||
|
||||
const DESCRIPTION: &'a str = "Changes the gamemode for a Player";
|
||||
|
||||
fn on_execute(
|
||||
sender: &mut super::CommandSender<'a>,
|
||||
command: String,
|
||||
_server: &mut crate::server::Server,
|
||||
) {
|
||||
let player = sender.as_mut_player().unwrap();
|
||||
let args: Vec<&str> = command.split_whitespace().collect();
|
||||
|
||||
if args.len() != 2 {
|
||||
// todo red
|
||||
player.send_message("Usage: /gamemode <mode>".into());
|
||||
return;
|
||||
}
|
||||
|
||||
let mode_str = args[1].to_lowercase();
|
||||
match mode_str.parse() {
|
||||
Ok(mode) => {
|
||||
player.set_gamemode(mode);
|
||||
player.send_message(format!("Set own game mode to {mode_str}").into());
|
||||
}
|
||||
Err(_) => {
|
||||
// todo red
|
||||
player.send_message("Invalid gamemode".into());
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
// todo: support console, (name required)
|
||||
fn player_required() -> bool {
|
||||
true
|
||||
}
|
||||
}
|
||||
@@ -1,8 +1,10 @@
|
||||
use gamemode::GamemodeCommand;
|
||||
use pumpkin::PumpkinCommand;
|
||||
use pumpkin_protocol::text::TextComponent;
|
||||
|
||||
use crate::{client::Client, server::Server};
|
||||
|
||||
mod gamemode;
|
||||
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
|
||||
@@ -12,6 +14,11 @@ pub trait Command<'a> {
|
||||
const DESCRIPTION: &'a str;
|
||||
|
||||
fn on_execute(sender: &mut CommandSender<'a>, command: String, server: &mut Server);
|
||||
|
||||
/// Specifies wether the Command Sender has to be a Player
|
||||
fn player_required() -> bool {
|
||||
false
|
||||
}
|
||||
}
|
||||
|
||||
pub enum CommandSender<'a> {
|
||||
@@ -27,13 +34,38 @@ impl<'a> CommandSender<'a> {
|
||||
CommandSender::Player(c) => c.send_message(text),
|
||||
}
|
||||
}
|
||||
|
||||
pub fn is_player(&mut self) -> bool {
|
||||
match self {
|
||||
CommandSender::Console => false,
|
||||
CommandSender::Player(_) => true,
|
||||
}
|
||||
}
|
||||
|
||||
pub fn is_console(&mut self) -> bool {
|
||||
match self {
|
||||
CommandSender::Console => true,
|
||||
CommandSender::Player(_) => false,
|
||||
}
|
||||
}
|
||||
pub fn as_mut_player(&mut self) -> Option<&mut Client> {
|
||||
match self {
|
||||
CommandSender::Player(client) => Some(client),
|
||||
CommandSender::Console => None,
|
||||
}
|
||||
}
|
||||
}
|
||||
pub fn handle_command(sender: &mut CommandSender, command: String, server: &mut Server) {
|
||||
let command = command.to_lowercase();
|
||||
// an ugly mess i know
|
||||
if command.starts_with(PumpkinCommand::NAME) {
|
||||
PumpkinCommand::on_execute(sender, command, server);
|
||||
return;
|
||||
}
|
||||
if command.starts_with(GamemodeCommand::NAME) {
|
||||
GamemodeCommand::on_execute(sender, command, server);
|
||||
return;
|
||||
}
|
||||
// todo: red color
|
||||
sender.send_message("Command not Found".into());
|
||||
}
|
||||
Reference in new issue
Block a user