Added Command global listener

This commit is contained in:
themode 2020-10-08 01:21:15 +02:00
parent ec33478cc3
commit af369dedde
3 changed files with 23 additions and 6 deletions

View File

@ -1,5 +1,6 @@
package net.minestom.server.command.builder;
import net.minestom.server.command.CommandSender;
import net.minestom.server.command.builder.arguments.Argument;
import net.minestom.server.command.builder.arguments.ArgumentDynamicStringArray;
import net.minestom.server.command.builder.arguments.ArgumentDynamicWord;
@ -36,7 +37,7 @@ public class Command {
* Get the command condition
* <p>
* It is called no matter the syntax used and can be used to check permissions or
* the {@link net.minestom.server.command.CommandSender} type
* the {@link CommandSender} type
*
* @return the command condition
*/
@ -137,4 +138,18 @@ public class Command {
return null;
}
/**
* Called when a {@link CommandSender} executes this command.
* Executed before any syntax callback.
* <p>
* WARNING: the {@link CommandCondition} is not executed, and all the {@link CommandSyntax} are not checked,
* this is called every time a {@link CommandSender} send a command which start by {@link #getName()} or {@link #getAliases()}.
*
* @param sender the {@link CommandSender}
* @param arguments the UNCHECKED arguments of the command, some can be null even when unexpected
* @param command the raw UNCHECKED received command
*/
public void globalListener(CommandSender sender, Arguments arguments, String command) {
}
}

View File

@ -47,7 +47,7 @@ public class CommandDispatcher {
public void execute(CommandSender source, String commandString) {
CommandResult result = parse(commandString);
result.execute(source);
result.execute(source, commandString);
}
public Set<Command> getCommands() {
@ -235,7 +235,6 @@ public class CommandDispatcher {
}
private static class CommandResult {
// Command
private Command command;
@ -254,9 +253,12 @@ public class CommandDispatcher {
* The command will not be executed if the {@link CommandCondition} of the command
* is not validated
*
* @param source the command source
* @param source the command source
* @param commandString the command string
*/
public void execute(CommandSender source) {
public void execute(CommandSender source, String commandString) {
// Global listener
command.globalListener(source, arguments, commandString);
// Condition check
final CommandCondition condition = command.getCondition();
if (condition != null) {

View File

@ -3,7 +3,7 @@ package net.minestom.server.command.builder.condition;
import net.minestom.server.command.CommandSender;
/**
* Used to know if the command source is allowed to run the command
* Used to know if the {@link CommandSender} is allowed to run the command
*/
public interface CommandCondition {
boolean apply(CommandSender source);