add help command
This commit is contained in:
parent
40b2fdc45f
commit
bfb3840b94
@ -14,7 +14,7 @@ use crate::entity::player::GameMode;
|
||||
|
||||
pub(crate) const NAME: &str = "gamemode";
|
||||
|
||||
const DESCRIPTION: &str = "Changes the gamemode for a Player";
|
||||
const DESCRIPTION: &str = "Change a player's gamemode.";
|
||||
|
||||
const ARG_GAMEMODE: &str = "gamemode";
|
||||
const ARG_TARGET: &str = "target";
|
||||
|
||||
61
pumpkin/src/commands/cmd_help.rs
Normal file
61
pumpkin/src/commands/cmd_help.rs
Normal file
@ -0,0 +1,61 @@
|
||||
use crate::commands::{CommandSender, DISPATCHER, dispatcher_init};
|
||||
use crate::commands::dispatcher::{CommandDispatcher, InvalidTreeError};
|
||||
use crate::commands::dispatcher::InvalidTreeError::InvalidConsumptionError;
|
||||
use crate::commands::tree::{CommandTree, ConsumedArgs, RawArgs};
|
||||
use crate::commands::tree_builder::argument;
|
||||
|
||||
pub(crate) const NAME: &str = "help";
|
||||
pub(crate) const ALIAS: &str = "?";
|
||||
|
||||
const DESCRIPTION: &str = "Print a help message.";
|
||||
|
||||
const ARG_COMMAND: &str = "command";
|
||||
|
||||
fn consume_arg_command(_src: &CommandSender, args: &mut RawArgs) -> Option<String> {
|
||||
let s = args.pop()?;
|
||||
|
||||
let dispatcher = DISPATCHER;
|
||||
let dispatcher = dispatcher.get_or_init(dispatcher_init);
|
||||
|
||||
if dispatcher.commands.contains_key(s) { Some(s.into()) }
|
||||
else { None }
|
||||
}
|
||||
|
||||
fn parse_arg_command<'a>(consumed_args: &'a ConsumedArgs, dispatcher: &'a CommandDispatcher) -> Result<(&'a str, &'a CommandTree<'a>), InvalidTreeError> {
|
||||
let command_name = consumed_args.get(ARG_COMMAND)
|
||||
.ok_or(InvalidConsumptionError(None))?;
|
||||
|
||||
if let Some(tree) = dispatcher.commands.get::<&str>(&command_name.as_str()) {
|
||||
Ok((command_name, tree))
|
||||
} else {
|
||||
Err(InvalidConsumptionError(Some(command_name.into())))
|
||||
}
|
||||
}
|
||||
|
||||
pub(crate) fn init_command_tree<'a>() -> CommandTree<'a> {
|
||||
CommandTree::new(DESCRIPTION).with_child(
|
||||
argument(ARG_COMMAND, consume_arg_command).execute(&|sender, args| {
|
||||
let dispatcher = DISPATCHER;
|
||||
let dispatcher = dispatcher.get_or_init(dispatcher_init);
|
||||
|
||||
let (name, tree) = parse_arg_command(args, dispatcher)?;
|
||||
|
||||
sender.send_message(
|
||||
format!("{} - {} Usage:{}", name, tree.description, tree.paths_formatted(name)).into()
|
||||
);
|
||||
|
||||
Ok(())
|
||||
})
|
||||
).execute(&|sender, _args| {
|
||||
let dispatcher = DISPATCHER;
|
||||
let dispatcher = dispatcher.get_or_init(dispatcher_init);
|
||||
|
||||
for (name, tree) in &dispatcher.commands {
|
||||
sender.send_message(
|
||||
format!("{} - {} Usage:{}", name, tree.description, tree.paths_formatted(name)).into()
|
||||
);
|
||||
};
|
||||
|
||||
Ok(())
|
||||
})
|
||||
}
|
||||
@ -6,7 +6,7 @@ use crate::commands::tree::CommandTree;
|
||||
|
||||
pub(crate) const NAME: &str = "pumpkin";
|
||||
|
||||
const DESCRIPTION: &str = "Displays information about Pumpkin";
|
||||
const DESCRIPTION: &str = "Display information about Pumpkin.";
|
||||
|
||||
pub(crate) fn init_command_tree<'a>() -> CommandTree<'a> {
|
||||
CommandTree::new(DESCRIPTION).execute(&|sender, _| {
|
||||
|
||||
@ -3,7 +3,7 @@ use crate::commands::tree_builder::require;
|
||||
|
||||
pub(crate) const NAME: &str = "stop";
|
||||
|
||||
const DESCRIPTION: &str = "Stops the server";
|
||||
const DESCRIPTION: &str = "Stop the server.";
|
||||
|
||||
pub(crate) fn init_command_tree<'a>() -> CommandTree<'a> {
|
||||
CommandTree::new(DESCRIPTION).with_child(
|
||||
|
||||
@ -4,8 +4,6 @@ use pumpkin_text::TextComponent;
|
||||
|
||||
use crate::client::Client;
|
||||
use crate::commands::dispatcher::CommandDispatcher;
|
||||
use crate::server::Server;
|
||||
|
||||
mod cmd_gamemode;
|
||||
mod cmd_pumpkin;
|
||||
mod cmd_stop;
|
||||
@ -14,6 +12,7 @@ mod tree_builder;
|
||||
mod dispatcher;
|
||||
mod arg_player;
|
||||
mod cmd_teleport;
|
||||
mod cmd_help;
|
||||
|
||||
pub enum CommandSender<'a> {
|
||||
Rcon(&'a mut Vec<String>),
|
||||
@ -73,6 +72,8 @@ fn dispatcher_init<'a>() -> CommandDispatcher<'a> {
|
||||
map.insert(cmd_pumpkin::NAME, cmd_pumpkin::init_command_tree());
|
||||
map.insert(cmd_gamemode::NAME, cmd_gamemode::init_command_tree());
|
||||
map.insert(cmd_stop::NAME, cmd_stop::init_command_tree());
|
||||
map.insert(cmd_help::NAME, cmd_help::init_command_tree());
|
||||
map.insert(cmd_help::ALIAS, cmd_help::init_command_tree());
|
||||
|
||||
CommandDispatcher {
|
||||
commands: map,
|
||||
|
||||
@ -70,8 +70,8 @@ impl <'a> CommandTree<'a> {
|
||||
|
||||
let mut s = String::with_capacity(len);
|
||||
|
||||
for path in paths {
|
||||
s.push('\n');
|
||||
for path in paths.iter() {
|
||||
s.push(if paths.len() > 1 { '\n' } else { ' ' });
|
||||
s.push('/');
|
||||
s.push_str(name);
|
||||
for node in path {
|
||||
|
||||
Loading…
Reference in New Issue
Block a user