Added command javadoc

This commit is contained in:
Felix Cravic 2020-07-21 18:04:02 +02:00
parent 617d855c84
commit 287f7e64a3
4 changed files with 147 additions and 0 deletions

View File

@ -56,14 +56,31 @@ public class CommandManager {
running = false;
}
/**
* Register a command with all the auto-completion features
*
* @param command the command to register
*/
public void register(Command command) {
this.dispatcher.register(command);
}
/**
* Register a simple command without auto-completion
*
* @param commandProcessor the command to register
*/
public void register(CommandProcessor commandProcessor) {
this.commandProcessorMap.put(commandProcessor.getCommandName().toLowerCase(), commandProcessor);
}
/**
* Execute a command for a sender
*
* @param sender the sender of the command
* @param command the raw command string (without the command prefix)
* @return
*/
public boolean execute(CommandSender sender, String command) {
Check.notNull(sender, "Source cannot be null");
Check.notNull(command, "Command string cannot be null");
@ -81,15 +98,18 @@ public class CommandManager {
}
try {
// Check for rich-command
this.dispatcher.execute(sender, command);
return true;
} catch (NullPointerException e) {
// Check for legacy-command
String[] splitted = command.split(" ");
String commandName = splitted[0];
CommandProcessor commandProcessor = commandProcessorMap.get(commandName.toLowerCase());
if (commandProcessor == null)
return false;
// Execute the legacy-command
String[] args = command.substring(command.indexOf(" ") + 1).split(" ");
return commandProcessor.process(sender, commandName, args);
@ -97,18 +117,43 @@ public class CommandManager {
}
}
/**
* Get the current command prefix (what should always be before the command name, ie: '/')
*
* @return the command prefix
*/
public String getCommandPrefix() {
return commandPrefix;
}
/**
* Change the command prefix
* <p>
* This field can be changed half-way, but the client auto-completion still expect the '/' char
*
* @param commandPrefix the new command prefix
*/
public void setCommandPrefix(String commandPrefix) {
this.commandPrefix = commandPrefix;
}
/**
* Get the console sender (which is used as a {@link CommandSender})
*
* @return the console sender
*/
public ConsoleSender getConsoleSender() {
return consoleSender;
}
/**
* Get the declare commands packet for a specific player
* <p>
* Can be used to update the player auto-completion list
*
* @param player the player to get the commands packet
* @return the {@link DeclareCommandsPacket} for {@code player}
*/
public DeclareCommandsPacket createDeclareCommandsPacket(Player player) {
return buildPacket(player);
}

View File

@ -2,13 +2,45 @@ package net.minestom.server.command;
import net.minestom.server.entity.Player;
/**
* Represent a simple command which give you the whole string representation
*/
public interface CommandProcessor {
/**
* Get the main command's name
*
* @return the main command's name
*/
String getCommandName();
/**
* Get the command's aliases
* <p>
* Can be null or empty
*
* @return the command aliases
*/
String[] getAliases();
/**
* Called when the command is executed by a {@link CommandSender}
*
* @param sender the sender which executed the command
* @param command the command name used
* @param args an array containing all the args (split by space char)
* @return true when the command is successful, false otherwise
*/
boolean process(CommandSender sender, String command, String[] args);
/**
* Called to know if a player has access to the command
* <p>
* Right now it is only used to know if the player should see the command in auto-completion
* Conditions still need to be checked in {@link #process(CommandSender, String, String[])}
*
* @param player the player to check the access
* @return true if the player has access to the command, false otherwise
*/
boolean hasAccess(Player player);
}

View File

@ -2,6 +2,17 @@ package net.minestom.server.command.builder;
import net.minestom.server.command.CommandSender;
/**
* Callback executed when an error is found within the argument
*/
public interface ArgumentCallback {
/**
* Executed when an error is found
*
* @param source the sender which executed the command
* @param value the raw argument which gave the error
* @param error the error id (you can check its meaning in the specific argument class)
*/
void apply(CommandSender source, String value, int error);
}

View File

@ -7,6 +7,9 @@ import java.util.ArrayList;
import java.util.Collection;
import java.util.List;
/**
* Represent a command which have suggestion/auto-completion
*/
public class Command {
private String name;
@ -27,40 +30,96 @@ public class Command {
this(name, new String[0]);
}
/**
* 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
*
* @return the command condition
*/
public CommandCondition getCondition() {
return condition;
}
/**
* Set the command condition
*
* @param commandCondition the new command condition
*/
public void setCondition(CommandCondition commandCondition) {
this.condition = commandCondition;
}
/**
* Add an argument callback
* <p>
* The argument callback is called when there's an error in the argument
*
* @param callback the callback for the argument
* @param argument the argument which get the callback
*/
public void addCallback(ArgumentCallback callback, Argument argument) {
argument.setCallback(callback);
}
/**
* Add a new syntax in the command
* <p>
* A syntax is simply a list of arguments
*
* @param executor the executor to call when the syntax is successfully received
* @param args all the arguments of the syntax
*/
public void addSyntax(CommandExecutor executor, Argument... args) {
CommandSyntax syntax = new CommandSyntax(args);
syntax.setExecutor(executor);
this.syntaxes.add(syntax);
}
/**
* Get the main command's name
*
* @return the main command's name
*/
public String getName() {
return name;
}
/**
* Get the command's aliases
* <p>
* Can be null or empty
*
* @return the command aliases
*/
public String[] getAliases() {
return aliases;
}
/**
* Get the default executor (which is called when there is no argument)
*
* @return the default executor
*/
public CommandExecutor getDefaultExecutor() {
return defaultExecutor;
}
/**
* Set the default executor (which is called when there is no argument)
*
* @param executor the new default executor
*/
public void setDefaultExecutor(CommandExecutor executor) {
this.defaultExecutor = executor;
}
/**
* Get all the syntaxes of this command
*
* @return a collection containing all this command syntaxes
*/
public Collection<CommandSyntax> getSyntaxes() {
return syntaxes;
}