Skip to content

Commit

Permalink
remove repeating command aliases in /help
Browse files Browse the repository at this point in the history
  • Loading branch information
user622628252416 committed Aug 21, 2024
1 parent 5041b44 commit ab8956a
Show file tree
Hide file tree
Showing 3 changed files with 44 additions and 27 deletions.
38 changes: 19 additions & 19 deletions pumpkin/src/commands/cmd_help.rs
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
use crate::commands::dispatcher::InvalidTreeError::InvalidConsumptionError;
use crate::commands::dispatcher::{CommandDispatcher, InvalidTreeError};
use crate::commands::tree::{CommandTree, ConsumedArgs, RawArgs};
use crate::commands::tree::{Command, CommandTree, ConsumedArgs, RawArgs};
use crate::commands::tree_builder::argument;
use crate::commands::{dispatcher_init, CommandSender, DISPATCHER};
use pumpkin_text::TextComponent;
Expand All @@ -16,26 +16,20 @@ fn consume_arg_command(_src: &CommandSender, args: &mut RawArgs) -> Option<Strin

let dispatcher = DISPATCHER.get_or_init(dispatcher_init);

if dispatcher.commands.contains_key(s) {
Some(s.into())
} else {
None
}
dispatcher.get_tree(s).ok().map(|tree| tree.names[0].into())
}

fn parse_arg_command<'a>(
consumed_args: &'a ConsumedArgs,
dispatcher: &'a CommandDispatcher,
) -> Result<(&'a str, &'a CommandTree<'a>), InvalidTreeError> {
) -> Result<&'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())))
}
dispatcher
.get_tree(command_name)
.map_err(|_| InvalidConsumptionError(Some(command_name.into())))
}

pub(crate) fn init_command_tree<'a>() -> CommandTree<'a> {
Expand All @@ -44,11 +38,13 @@ pub(crate) fn init_command_tree<'a>() -> CommandTree<'a> {
argument(ARG_COMMAND, consume_arg_command).execute(&|sender, args| {
let dispatcher = DISPATCHER.get_or_init(dispatcher_init);

let (name, tree) = parse_arg_command(args, dispatcher)?;
let tree = parse_arg_command(args, dispatcher)?;

sender.send_message(TextComponent::text(&format!(
"{} - {} Usage: {}",
name, tree.description, tree
tree.names.join("/"),
tree.description,
tree
)));

Ok(())
Expand All @@ -57,15 +53,19 @@ pub(crate) fn init_command_tree<'a>() -> CommandTree<'a> {
.execute(&|sender, _args| {
let dispatcher = DISPATCHER.get_or_init(dispatcher_init);

let mut names: Vec<&str> = dispatcher.commands.keys().copied().collect();
names.sort();
let mut keys: Vec<&str> = dispatcher.commands.keys().copied().collect();
keys.sort();

for name in names {
let tree = &dispatcher.commands[name];
for key in keys {
let Command::Tree(tree) = &dispatcher.commands[key] else {
continue;
};

sender.send_message(TextComponent::text(&format!(
"{} - {} Usage: {}",
name, tree.description, tree
tree.names.join("/"),
tree.description,
tree
)));
}

Expand Down
25 changes: 20 additions & 5 deletions pumpkin/src/commands/dispatcher.rs
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
use crate::commands::dispatcher::InvalidTreeError::{
InvalidConsumptionError, InvalidRequirementError,
};
use crate::commands::tree::{CommandTree, ConsumedArgs, NodeType, RawArgs};
use crate::commands::tree::{Command, CommandTree, ConsumedArgs, NodeType, RawArgs};
use crate::commands::CommandSender;
use std::collections::HashMap;

Expand All @@ -17,7 +17,7 @@ pub(crate) enum InvalidTreeError {
}

pub(crate) struct CommandDispatcher<'a> {
pub(crate) commands: HashMap<&'a str, CommandTree<'a>>,
pub(crate) commands: HashMap<&'a str, Command<'a>>,
}

/// Stores registered [CommandTree]s and dispatches commands to them.
Expand All @@ -28,7 +28,7 @@ impl<'a> CommandDispatcher<'a> {
let key = parts.next().ok_or("Empty Command")?;
let raw_args: Vec<&str> = parts.rev().collect();

let tree = self.commands.get(key).ok_or("Command not found")?;
let tree = self.get_tree(key)?;

// try paths until fitting path is found
for path in tree.iter_paths() {
Expand All @@ -52,6 +52,21 @@ impl<'a> CommandDispatcher<'a> {
Err(format!("Invalid Syntax. Usage: {}", tree))
}

pub(crate) fn get_tree(&'a self, key: &str) -> Result<&'a CommandTree<'a>, String> {
let command = self.commands.get(key).ok_or("Command not found")?;

match command {
Command::Tree(tree) => Ok(tree),
Command::Alias(target) => {
let Some(Command::Tree(tree)) = &self.commands.get(target) else {
println!("Error while parsing command alias \"{key}\": pointing to \"{target}\" which is not a valid tree");
return Err("Internal Error (See logs for details)".into());
};
Ok(tree)
}
}
}

fn try_is_fitting_path(
src: &mut CommandSender,
path: Vec<usize>,
Expand Down Expand Up @@ -104,9 +119,9 @@ impl<'a> CommandDispatcher<'a> {
let primary_name = names.next().expect("at least one name must be provided");

for &name in names {
self.commands.insert(name, tree.clone());
self.commands.insert(name, Command::Alias(primary_name));
}

self.commands.insert(primary_name, tree);
self.commands.insert(primary_name, Command::Tree(tree));
}
}
8 changes: 5 additions & 3 deletions pumpkin/src/commands/tree.rs
Original file line number Diff line number Diff line change
Expand Up @@ -12,13 +12,11 @@ pub(crate) type ConsumedArgs<'a> = HashMap<&'a str, String>;
/// see [crate::commands::tree_builder::argument]
pub(crate) type ArgumentConsumer<'a> = fn(&CommandSender, &mut RawArgs) -> Option<String>;

#[derive(Clone)]
pub(crate) struct Node<'a> {
pub(crate) children: Vec<usize>,
pub(crate) node_type: NodeType<'a>,
}

#[derive(Clone)]
pub(crate) enum NodeType<'a> {
ExecuteLeaf {
run: &'a (dyn Fn(&mut CommandSender, &ConsumedArgs) -> Result<(), InvalidTreeError> + Sync),
Expand All @@ -34,7 +32,11 @@ pub(crate) enum NodeType<'a> {
},
}

#[derive(Clone)]
pub(crate) enum Command<'a> {
Tree(CommandTree<'a>),
Alias(&'a str),
}

pub(crate) struct CommandTree<'a> {
pub(crate) nodes: Vec<Node<'a>>,
pub(crate) children: Vec<usize>,
Expand Down

0 comments on commit ab8956a

Please sign in to comment.