Create builder for command description; refactor command classes

- Create builder class for CommandDescription
- Remove redundant methods in command registration classes (e.g. certain validation is superfluous because we only instantiate the classes internally)
- Replace multiple occurrences where a field is directly instantiated e.g. with a list and then its value is overwritten by a constructor = redundant instantiation of objects
This commit is contained in:
ljacqu 2015-11-28 23:59:04 +01:00
parent 364583e7db
commit faf9a2c8ac
6 changed files with 265 additions and 416 deletions

View File

@ -7,7 +7,7 @@ public class CommandArgumentDescription {
// TODO: Allow argument to consist of infinite parts. <label ...>
/**
* Argument label.
* Argument label (one-word description of the argument).
*/
private String label;
/**
@ -15,31 +15,21 @@ public class CommandArgumentDescription {
*/
private String description;
/**
* Defines whether the argument is optional.
* Defines whether the argument is isOptional.
*/
private boolean optional = false;
private boolean isOptional = false;
/**
* Constructor.
*
* @param label The argument label.
* @param description The argument description.
* @param isOptional True if the argument is isOptional, false otherwise.
*/
public CommandArgumentDescription(String label, String description) {
this(label, description, false);
}
/**
* Constructor.
*
* @param label The argument label.
* @param description The argument description.
* @param optional True if the argument is optional, false otherwise.
*/
public CommandArgumentDescription(String label, String description, boolean optional) {
setLabel(label);
setDescription(description);
setOptional(optional);
public CommandArgumentDescription(String label, String description, boolean isOptional) {
this.label = label;
this.description = description;
this.isOptional = isOptional;
}
/**
@ -51,15 +41,6 @@ public class CommandArgumentDescription {
return this.label;
}
/**
* Set the argument label.
*
* @param label Argument label.
*/
public void setLabel(String label) {
this.label = label;
}
/**
* Get the argument description.
*
@ -69,30 +50,13 @@ public class CommandArgumentDescription {
return description;
}
/**
* Set the argument description.
*
* @param description Argument description.
*/
public void setDescription(String description) {
this.description = description;
}
/**
* Check whether the argument is optional.
*
* @return True if the argument is optional, false otherwise.
*/
public boolean isOptional() {
return optional;
return isOptional;
}
/**
* Set whether the argument is optional.
*
* @param optional True if the argument is optional, false otherwise.
*/
public void setOptional(boolean optional) {
this.optional = optional;
}
}

View File

@ -4,10 +4,7 @@ import fr.xephi.authme.permission.PermissionNode;
import fr.xephi.authme.util.StringUtils;
import org.bukkit.command.CommandSender;
import java.util.ArrayList;
import java.util.Collections;
import java.util.Comparator;
import java.util.List;
import java.util.*;
/**
*/
@ -16,15 +13,15 @@ public class CommandDescription {
/**
* Defines the acceptable labels.
*/
private List<String> labels = new ArrayList<>();
private List<String> labels;
/**
* Command description.
*/
private String description = "";
private String description;
/**
* Detailed description.
*/
private String detailedDescription = "";
private String detailedDescription;
/**
* The executable command instance.
*/
@ -32,23 +29,24 @@ public class CommandDescription {
/**
* The parent command.
*/
private CommandDescription parent = null;
private CommandDescription parent;
/**
* The child labels.
*/
// TODO: Remove list instantiation once Builder is the only way to construct objects
private List<CommandDescription> children = new ArrayList<>();
/**
* The command arguments.
*/
private List<CommandArgumentDescription> arguments = new ArrayList<>();
private List<CommandArgumentDescription> arguments;
/**
* Defines whether there is an argument maximum or not.
*/
private boolean noArgumentMaximum = false;
private boolean noArgumentMaximum;
/**
* Defines the command permissions.
*/
private CommandPermissions permissions = new CommandPermissions();
private CommandPermissions permissions;
/**
* Constructor.
@ -88,9 +86,9 @@ public class CommandDescription {
*/
public CommandDescription(ExecutableCommand executableCommand, String label, String description, CommandDescription parent, String detailedDescription, List<CommandArgumentDescription> arguments) {
setExecutableCommand(executableCommand);
setLabel(label);
setDescription(description);
setDetailedDescription(detailedDescription);
this.labels = Collections.singletonList(label);
this.description = description;
this.detailedDescription = detailedDescription;
setParent(parent);
setArguments(arguments);
}
@ -107,38 +105,40 @@ public class CommandDescription {
*/
public CommandDescription(ExecutableCommand executableCommand, List<String> labels, String description, String detailedDescription, CommandDescription parent, List<CommandArgumentDescription> arguments) {
setExecutableCommand(executableCommand);
setLabels(labels);
setDescription(description);
setDetailedDescription(detailedDescription);
this.labels = labels;
this.description = description;
this.detailedDescription = detailedDescription;
setParent(parent);
setArguments(arguments);
}
/**
* Check whether a label is valid to use.
* Private constructor. Use {@link CommandDescription#builder()} to create instances of this class.
*
* @param label The label to test.
*
* @return True if the label is valid to use, false otherwise.
* @param executableCommand The executable command, or null.
* @param labels List of command labels.
* @param description Command description.
* @param detailedDescription Detailed comment description.
* @param parent Parent command.
* @param arguments Command arguments.
*/
public static boolean isValidLabel(String label) {
// Make sure the label isn't null
if (label == null)
return false;
// Trim the label
label = label.trim();
// Make sure the label is at least one character long
if (label.length() <= 0)
return false;
// Make sure the label doesn't contain any spaces, return the result
return !label.contains(" ");
private CommandDescription(List<String> labels, String description, String detailedDescription,
ExecutableCommand executableCommand, CommandDescription parent,
List<CommandDescription> children, List<CommandArgumentDescription> arguments,
boolean noArgumentMaximum, CommandPermissions permissions) {
this.labels = labels;
this.description = description;
this.detailedDescription = detailedDescription;
this.executableCommand = executableCommand;
this.parent = parent;
this.children = children;
this.arguments = arguments;
this.noArgumentMaximum = noArgumentMaximum;
this.permissions = permissions;
}
/**
* Check whether two labels equal to each other.
* Check whether two labels are equal to each other.
*
* @param commandLabel The first command label.
* @param otherCommandLabel The other command label.
@ -154,28 +154,7 @@ public class CommandDescription {
return (commandLabel.equalsIgnoreCase(otherCommandLabel));
}
/**
* Get the first relative command label.
*
* @return First relative command label.
*/
public String getLabel() {
// Ensure there's any item in the command list
if (this.labels.size() == 0)
return "";
// Return the first command on the list
return this.labels.get(0);
}
/**
* Set the command label, this will append the command label to already existing ones.
*
* @param commandLabel Command label to set or add.
*/
public void setLabel(String commandLabel) {
setLabel(commandLabel, false);
}
/**
* Get the label most similar to the reference. The first label will be returned if no reference was supplied.
@ -220,74 +199,6 @@ public class CommandDescription {
return this.labels;
}
/**
* Set the list of command labels.
*
* @param labels New list of command labels. Null to clear the list of labels.
*/
public void setLabels(List<String> labels) {
// Check whether the command label list should be cleared
if (labels == null) {
this.labels.clear();
} else {
this.labels = labels;
}
}
/**
* Set the command label.
*
* @param commandLabel Command label to set.
* @param overwrite True to replace all old command labels, false to append this command label to the currently
* existing labels.
*
* @return Trie if the command label is added, or if it was added already. False on failure.
*/
public boolean setLabel(String commandLabel, boolean overwrite) {
// Check whether this new command should overwrite the previous ones
if (!overwrite)
return addLabel(commandLabel);
// Replace all labels with this new one
this.labels.clear();
return this.labels.add(commandLabel);
}
/**
* Add a command label to the list.
*
* @param commandLabel Command label to add.
*
* @return True if the label was added, or if it was added already. False on error.
*/
public boolean addLabel(String commandLabel) {
// Verify the label
if (!isValidLabel(commandLabel))
return false;
// Ensure this command isn't a duplicate
if (hasLabel(commandLabel))
return true;
// Add the command to the list
return this.labels.add(commandLabel);
}
/**
* Add a list of command labels.
*
* @param commandLabels List of command labels to add.
*
* @return True if succeed, false on failure.
*/
public boolean addLabels(List<String> commandLabels) {
// Add each command label separately
for (String cmd : commandLabels)
if (!addLabel(cmd))
return false;
return true;
}
/**
* Check whether this command description has a specific command.
*
@ -305,23 +216,6 @@ public class CommandDescription {
return false;
}
/**
* Check whether this command description has a list of labels
*
* @param commandLabels List of labels
*
* @return True if all labels match, false otherwise
*/
public boolean hasLabels(List<String> commandLabels) {
// Check if there's a match for every command
for (String cmd : commandLabels)
if (!hasLabel(cmd))
return false;
// There seems to be a match for every command, return true
return true;
}
/**
* Check whether this command label is applicable with a command reference. This doesn't check if the parent
* are suitable too.
@ -567,8 +461,6 @@ public class CommandDescription {
// Make sure the description is valid
if (commandDescription == null)
return false;
if (!commandDescription.isValid())
return false;
// Make sure the child doesn't exist already
if (isChild(commandDescription))
@ -600,9 +492,7 @@ public class CommandDescription {
*/
public boolean isChild(CommandDescription commandDescription) {
// Make sure the description is valid
if (commandDescription == null)
return false;
if (!commandDescription.isValid())
if (commandDescription == null) // TODO: After builder, commandDescription == null -> never
return false;
// Check whether this child exists, return the result
@ -646,7 +536,10 @@ public class CommandDescription {
public void setArguments(List<CommandArgumentDescription> arguments) {
// Convert null into an empty argument list
if (arguments == null) {
this.arguments.clear();
// Note ljacqu 20151128: Temporary workaround to avoid null pointer exception. Soon we won't need setters
// on the main class (-> complete instantiation via Builder)
// TODO Remove this method once unused
this.arguments = new ArrayList<>();
} else {
this.arguments = arguments;
}
@ -669,7 +562,7 @@ public class CommandDescription {
* @return True if this command has any arguments.
*/
public boolean hasArguments() {
return (this.arguments.size() != 0);
return !arguments.isEmpty();
}
/**
@ -705,22 +598,13 @@ public class CommandDescription {
public int getMaximumArguments() {
// Check whether there is a maximum set
if (this.noArgumentMaximum)
// TODO ljacqu 20151128: Magic number
return -1;
// Return the maximum based on the registered arguments
return this.arguments.size();
}
/**
* Set whether there is an argument maximum.
*
* @param maximumArguments True if there is an argument maximum, based on the number of registered arguments.
*/
// TODO ljacqu 20151121: Rename the setter
public void setMaximumArguments(boolean maximumArguments) {
this.noArgumentMaximum = !maximumArguments;
}
/**
* Get the command description.
*
@ -730,26 +614,13 @@ public class CommandDescription {
return hasDescription() ? this.description : this.detailedDescription;
}
/**
* Set the command description.
*
* @param description New command description. Null to reset the description.
*/
public void setDescription(String description) {
if (description == null)
this.description = "";
else
this.description = description;
}
/**
* Check whether this command has any description.
*
* @return True if this command has any description.
*/
public boolean hasDescription() {
return (this.description.trim().length() != 0);
return !StringUtils.isEmpty(description);
}
/**
@ -758,29 +629,7 @@ public class CommandDescription {
* @return Command detailed description.
*/
public String getDetailedDescription() {
return hasDetailedDescription() ? this.detailedDescription : this.description;
}
/**
* Set the command detailed description.
*
* @param detailedDescription New command description. Null to reset the description.
*/
public void setDetailedDescription(String detailedDescription) {
if (detailedDescription == null)
this.detailedDescription = "";
else
this.detailedDescription = detailedDescription;
}
/**
* Check whether this command has any detailed description.
*
* @return True if this command has any detailed description.
*/
public boolean hasDetailedDescription() {
return (this.detailedDescription.trim().length() != 0);
return StringUtils.isEmpty(detailedDescription) ? this.detailedDescription : this.description;
}
/**
@ -907,15 +756,6 @@ public class CommandDescription {
return this.permissions;
}
/**
* Set the command permissions.
*
* @param commandPermissions The command permissions.
*/
public void setCommandPermissions(CommandPermissions commandPermissions) {
this.permissions = commandPermissions;
}
/**
* Set the command permissions.
*
@ -926,23 +766,112 @@ public class CommandDescription {
this.permissions = new CommandPermissions(permissionNode, defaultPermission);
}
/**
* Check whether the command description has been set up properly.
*
* @return True if the command description is valid, false otherwise.
*/
public boolean isValid() {
// Make sure any command label is set
if (getLabels().size() == 0) {
return false;
}
// Make sure the permissions are set up properly
if (this.permissions == null) {
return false;
}
// Everything seems to be correct, return the result
return true;
public static Builder builder() {
return new Builder();
}
public static final class Builder {
private List<String> labels;
private String description;
private String detailedDescription;
private ExecutableCommand executableCommand;
private CommandDescription parent;
private List<CommandDescription> children;
private List<CommandArgumentDescription> arguments;
private boolean noArgumentMaximum;
private CommandPermissions permissions;
/**
* Build a CommandDescription from the builder.
*
* @return The generated CommandDescription object
*/
public CommandDescription build() {
return new CommandDescription(
valueOrEmptyList(labels),
firstNonNull(description, ""),
firstNonNull(detailedDescription, ""),
firstNonNull(executableCommand, null), // TODO ljacqu 20151128: May `executableCommand` be null?
firstNonNull(parent, null),
valueOrEmptyList(children),
valueOrEmptyList(arguments),
noArgumentMaximum,
firstNonNull(permissions, null)
);
}
public Builder labels(List<String> labels) {
this.labels = labels;
return this;
}
public Builder labels(String... labels) {
return labels(asMutableList(labels));
}
public Builder description(String description) {
this.description = description;
return this;
}
public Builder detailedDescription(String detailedDescription) {
this.detailedDescription = detailedDescription;
return this;
}
public Builder executableCommand(ExecutableCommand executableCommand) {
this.executableCommand = executableCommand;
return this;
}
public Builder parent(CommandDescription parent) {
this.parent = parent;
return this;
}
public Builder children(List<CommandDescription> children) {
this.children = children;
return this;
}
public Builder children(CommandDescription... children) {
return children(asMutableList(children));
}
public Builder withArgument(String label, String description, boolean isOptional) {
if (arguments == null) {
arguments = new ArrayList<>();
}
arguments.add(new CommandArgumentDescription(label, description, isOptional));
return this;
}
public Builder noArgumentMaximum(boolean noArgumentMaximum) {
this.noArgumentMaximum = noArgumentMaximum;
return this;
}
public Builder permissions(CommandPermissions.DefaultPermission defaultPermission,
PermissionNode... permissionNodes) {
this.permissions = new CommandPermissions(asMutableList(permissionNodes), defaultPermission);
return this;
}
@SafeVarargs
private static <T> List<T> asMutableList(T... items) {
return new ArrayList<>(Arrays.asList(items));
}
private static <T> List<T> valueOrEmptyList(List<T> givenList) {
if (givenList != null) {
return givenList;
}
return new ArrayList<>();
}
private static <T> T firstNonNull(T first, T second) {
return first != null ? first : second;
}
}
}

View File

@ -13,8 +13,11 @@ import fr.xephi.authme.permission.AdminPermission;
import fr.xephi.authme.permission.UserPermission;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;
import static fr.xephi.authme.command.CommandPermissions.DefaultPermission.OP_ONLY;
/**
*/
public class CommandManager {
@ -38,51 +41,51 @@ public class CommandManager {
/**
* Register all commands.
*/
// TODO ljacqu 20151121: Create a builder class for CommandDescription
public void registerCommands() {
// Create a list of help command labels
final List<String> helpCommandLabels = new ArrayList<String>() {
{
add("help");
add("hlp");
add("h");
add("sos");
add("?");
}
};
final List<String> helpCommandLabels = Arrays.asList("help", "hlp", "h", "sos", "?");
// Register the base AuthMe Reloaded command
CommandDescription authMeBaseCommand = new CommandDescription(new AuthMeCommand(), new ArrayList<String>() {
{
add("authme");
}
}, "Main command", "The main AuthMeReloaded command. The root for all admin commands.", null);
CommandDescription authMeBaseCommand = CommandDescription.builder()
.executableCommand(new AuthMeCommand())
.labels("authme")
.description("Main command")
.detailedDescription("The main AuthMeReloaded command. The root for all admin commands.")
.parent(null)
.build();
// Register the help command
CommandDescription authMeHelpCommand = new CommandDescription(new HelpCommand(), helpCommandLabels,
"View help", "View detailed help pages about AuthMeReloaded commands.", authMeBaseCommand);
authMeHelpCommand.addArgument(new CommandArgumentDescription("query", "The command or query to view help for.", true));
authMeHelpCommand.setMaximumArguments(false);
CommandDescription authMeHelpCommand = CommandDescription.builder()
.executableCommand(new HelpCommand())
.labels(helpCommandLabels)
.description("View help")
.detailedDescription("View detailed help pages about AuthMeReloaded commands.")
.parent(authMeBaseCommand)
.withArgument("query", "The command or query to view help for.", true)
.build();
// Register the register command
CommandDescription registerCommand = new CommandDescription(new RegisterCommand(), new ArrayList<String>() {
{
add("register");
add("reg");
add("r");
}
}, "Register a player", "Register the specified player with the specified password.", authMeBaseCommand);
registerCommand.setCommandPermissions(UserPermission.REGISTER, CommandPermissions.DefaultPermission.OP_ONLY);
registerCommand.addArgument(new CommandArgumentDescription("player", "Player name", false));
registerCommand.addArgument(new CommandArgumentDescription("password", "Password", false));
CommandDescription registerCommand = CommandDescription.builder()
.executableCommand(new RegisterCommand())
.labels("register", "reg", "r")
.description("Register a player")
.detailedDescription("Register the specified player with the specified password.")
.parent(authMeBaseCommand)
.permissions(OP_ONLY, UserPermission.REGISTER)
.withArgument("player", "Player name", false)
.withArgument("password", "Password", false)
.build();
// Register the unregister command
CommandDescription unregisterCommand = new CommandDescription(new UnregisterCommand(), helpCommandLabels,
"Unregister a player", "Unregister the specified player.", authMeBaseCommand);
unregisterCommand.setCommandPermissions(UserPermission.UNREGISTER, CommandPermissions.DefaultPermission.OP_ONLY);
unregisterCommand.addArgument(new CommandArgumentDescription("player", "Player name", false));
CommandDescription unregisterCommand = CommandDescription.builder()
.executableCommand(new UnregisterCommand())
.labels("unregister", "unreg", "unr", "delete", "del")
.description("Unregister a player")
.detailedDescription("Unregister the specified player.")
.parent(authMeBaseCommand)
.permissions(OP_ONLY, UserPermission.UNREGISTER)
.withArgument("player", "Player name", false)
.build();
// Register the forcelogin command
CommandDescription forceLoginCommand = new CommandDescription(new ForceLoginCommand(), new ArrayList<String>() {
@ -92,7 +95,7 @@ public class CommandManager {
add("login");
}
}, "Enforce login player", "Enforce the specified player to login.", authMeBaseCommand);
forceLoginCommand.setCommandPermissions(UserPermission.CAN_LOGIN_BE_FORCED, CommandPermissions.DefaultPermission.OP_ONLY);
forceLoginCommand.setCommandPermissions(UserPermission.CAN_LOGIN_BE_FORCED, OP_ONLY);
forceLoginCommand.addArgument(new CommandArgumentDescription("player", "Online player name", true));
// Register the changepassword command
@ -105,7 +108,7 @@ public class CommandManager {
add("cp");
}
}, "Change player's password", "Change the password of a player.", authMeBaseCommand);
changePasswordCommand.setCommandPermissions(AdminPermission.CHANGE_PASSWORD, CommandPermissions.DefaultPermission.OP_ONLY);
changePasswordCommand.setCommandPermissions(AdminPermission.CHANGE_PASSWORD, OP_ONLY);
changePasswordCommand.addArgument(new CommandArgumentDescription("player", "Player name", false));
changePasswordCommand.addArgument(new CommandArgumentDescription("pwd", "New password", false));
@ -117,7 +120,7 @@ public class CommandManager {
add("ll");
}
}, "Player's last login", "View the date of the specified players last login", authMeBaseCommand);
lastLoginCommand.setCommandPermissions(AdminPermission.LAST_LOGIN, CommandPermissions.DefaultPermission.OP_ONLY);
lastLoginCommand.setCommandPermissions(AdminPermission.LAST_LOGIN, OP_ONLY);
lastLoginCommand.addArgument(new CommandArgumentDescription("player", "Player name", true));
// Register the accounts command
@ -128,7 +131,7 @@ public class CommandManager {
add("account");
}
}, "Display player accounts", "Display all accounts of a player by it's player name or IP.", authMeBaseCommand);
accountsCommand.setCommandPermissions(AdminPermission.ACCOUNTS, CommandPermissions.DefaultPermission.OP_ONLY);
accountsCommand.setCommandPermissions(AdminPermission.ACCOUNTS, OP_ONLY);
accountsCommand.addArgument(new CommandArgumentDescription("player", "Player name or IP", true));
// Register the getemail command
@ -141,7 +144,7 @@ public class CommandManager {
add("mail");
}
}, "Display player's email", "Display the email address of the specified player if set.", authMeBaseCommand);
getEmailCommand.setCommandPermissions(AdminPermission.GET_EMAIL, CommandPermissions.DefaultPermission.OP_ONLY);
getEmailCommand.setCommandPermissions(AdminPermission.GET_EMAIL, OP_ONLY);
getEmailCommand.addArgument(new CommandArgumentDescription("player", "Player name", true));
// Register the setemail command
@ -154,7 +157,7 @@ public class CommandManager {
add("setmail");
}
}, "Change player's email", "Change the email address of the specified player.", authMeBaseCommand);
setEmailCommand.setCommandPermissions(AdminPermission.CHANGE_EMAIL, CommandPermissions.DefaultPermission.OP_ONLY);
setEmailCommand.setCommandPermissions(AdminPermission.CHANGE_EMAIL, OP_ONLY);
setEmailCommand.addArgument(new CommandArgumentDescription("player", "Player name", false));
setEmailCommand.addArgument(new CommandArgumentDescription("email", "Player email", false));
@ -166,7 +169,7 @@ public class CommandManager {
add("ip");
}
}, "Get player's IP", "Get the IP address of the specified online player.", authMeBaseCommand);
getIpCommand.setCommandPermissions(AdminPermission.GET_IP, CommandPermissions.DefaultPermission.OP_ONLY);
getIpCommand.setCommandPermissions(AdminPermission.GET_IP, OP_ONLY);
getIpCommand.addArgument(new CommandArgumentDescription("player", "Online player name", true));
// Register the spawn command
@ -177,7 +180,7 @@ public class CommandManager {
add("home");
}
}, "Teleport to spawn", "Teleport to the spawn.", authMeBaseCommand);
spawnCommand.setCommandPermissions(AdminPermission.SPAWN, CommandPermissions.DefaultPermission.OP_ONLY);
spawnCommand.setCommandPermissions(AdminPermission.SPAWN, OP_ONLY);
// Register the setspawn command
CommandDescription setSpawnCommand = new CommandDescription(new SetSpawnCommand(), new ArrayList<String>() {
@ -187,7 +190,7 @@ public class CommandManager {
add("chgspawn");
}
}, "Change the spawn", "Change the player's spawn to your current position.", authMeBaseCommand);
setSpawnCommand.setCommandPermissions(AdminPermission.SET_SPAWN, CommandPermissions.DefaultPermission.OP_ONLY);
setSpawnCommand.setCommandPermissions(AdminPermission.SET_SPAWN, OP_ONLY);
// Register the firstspawn command
CommandDescription firstSpawnCommand = new CommandDescription(new FirstSpawnCommand(), new ArrayList<String>() {
@ -197,7 +200,7 @@ public class CommandManager {
add("firsthome");
}
}, "Teleport to first spawn", "Teleport to the first spawn.", authMeBaseCommand);
firstSpawnCommand.setCommandPermissions(AdminPermission.FIRST_SPAWN, CommandPermissions.DefaultPermission.OP_ONLY);
firstSpawnCommand.setCommandPermissions(AdminPermission.FIRST_SPAWN, OP_ONLY);
// Register the setfirstspawn command
CommandDescription setFirstSpawnCommand = new CommandDescription(new SetFirstSpawnCommand(), new ArrayList<String>() {
@ -207,7 +210,7 @@ public class CommandManager {
add("chgfirstspawn");
}
}, "Change the first spawn", "Change the first player's spawn to your current position.", authMeBaseCommand);
setFirstSpawnCommand.setCommandPermissions(AdminPermission.SET_FIRST_SPAWN, CommandPermissions.DefaultPermission.OP_ONLY);
setFirstSpawnCommand.setCommandPermissions(AdminPermission.SET_FIRST_SPAWN, OP_ONLY);
// Register the purge command
CommandDescription purgeCommand = new CommandDescription(new PurgeCommand(), new ArrayList<String>() {
@ -217,7 +220,7 @@ public class CommandManager {
add("delete");
}
}, "Purge old data", "Purge old AuthMeReloaded data longer than the specified amount of days ago.", authMeBaseCommand);
purgeCommand.setCommandPermissions(AdminPermission.PURGE, CommandPermissions.DefaultPermission.OP_ONLY);
purgeCommand.setCommandPermissions(AdminPermission.PURGE, OP_ONLY);
purgeCommand.addArgument(new CommandArgumentDescription("days", "Number of days", false));
// Register the purgelastposition command
@ -232,7 +235,7 @@ public class CommandManager {
add("resetlastpos");
}
}, "Purge player's last position", "Purge the last know position of the specified player.", authMeBaseCommand);
purgeLastPositionCommand.setCommandPermissions(AdminPermission.PURGE_LAST_POSITION, CommandPermissions.DefaultPermission.OP_ONLY);
purgeLastPositionCommand.setCommandPermissions(AdminPermission.PURGE_LAST_POSITION, OP_ONLY);
purgeLastPositionCommand.addArgument(new CommandArgumentDescription("player", "Player name", true));
// Register the purgebannedplayers command
@ -245,7 +248,7 @@ public class CommandManager {
add("deletebannedplayer");
}
}, "Purge banned palyers data", "Purge all AuthMeReloaded data for banned players.", authMeBaseCommand);
purgeBannedPlayersCommand.setCommandPermissions(AdminPermission.PURGE_BANNED_PLAYERS, CommandPermissions.DefaultPermission.OP_ONLY);
purgeBannedPlayersCommand.setCommandPermissions(AdminPermission.PURGE_BANNED_PLAYERS, OP_ONLY);
// Register the switchantibot command
CommandDescription switchAntiBotCommand = new CommandDescription(new SwitchAntiBotCommand(), new ArrayList<String>() {
@ -256,7 +259,7 @@ public class CommandManager {
add("antibot");
}
}, "Switch AntiBot mode", "Switch or toggle the AntiBot mode to the specified state.", authMeBaseCommand);
switchAntiBotCommand.setCommandPermissions(AdminPermission.SWITCH_ANTIBOT, CommandPermissions.DefaultPermission.OP_ONLY);
switchAntiBotCommand.setCommandPermissions(AdminPermission.SWITCH_ANTIBOT, OP_ONLY);
switchAntiBotCommand.addArgument(new CommandArgumentDescription("mode", "ON / OFF", true));
// // Register the resetname command
@ -280,12 +283,17 @@ public class CommandManager {
add("rld");
}
}, "Reload plugin", "Reload the AuthMeReloaded plugin.", authMeBaseCommand);
reloadCommand.setCommandPermissions(AdminPermission.RELOAD, CommandPermissions.DefaultPermission.OP_ONLY);
reloadCommand.setCommandPermissions(AdminPermission.RELOAD, OP_ONLY);
// Register the version command
/* TODO ljacqu 20151128: labels should not be helpCommandLabels! Previously it was:
add("version");
add("ver");
add("v");
add("about");
add("info");*/
CommandDescription versionCommand = new CommandDescription(new VersionCommand(), helpCommandLabels,
"Version info", "Show detailed information about the installed AuthMeReloaded version, and shows the developers, contributors, license and other information.", authMeBaseCommand);
versionCommand.setMaximumArguments(false);
// Register the base login command
CommandDescription loginBaseCommand = new CommandDescription(new LoginCommand(), new ArrayList<String>() {
@ -302,7 +310,6 @@ public class CommandManager {
CommandDescription loginHelpCommand = new CommandDescription(new HelpCommand(), helpCommandLabels,
"View help", "View detailed help pages about AuthMeReloaded login commands.", loginBaseCommand);
loginHelpCommand.addArgument(new CommandArgumentDescription("query", "The command or query to view help for.", true));
loginHelpCommand.setMaximumArguments(false);
// Register the base logout command
CommandDescription logoutBaseCommand = new CommandDescription(new LogoutCommand(), new ArrayList<String>() {
@ -317,7 +324,6 @@ public class CommandManager {
CommandDescription logoutHelpCommand = new CommandDescription(new HelpCommand(), helpCommandLabels,
"View help", "View detailed help pages about AuthMeReloaded logout commands.", logoutBaseCommand);
logoutHelpCommand.addArgument(new CommandArgumentDescription("query", "The command or query to view help for.", true));
logoutHelpCommand.setMaximumArguments(false);
// Register the base register command
CommandDescription registerBaseCommand = new CommandDescription(new fr.xephi.authme.command.executable.register.RegisterCommand(), new ArrayList<String>() {
@ -330,13 +336,11 @@ public class CommandManager {
registerBaseCommand.setCommandPermissions(UserPermission.REGISTER, CommandPermissions.DefaultPermission.ALLOWED);
registerBaseCommand.addArgument(new CommandArgumentDescription("password", "Password", false));
registerBaseCommand.addArgument(new CommandArgumentDescription("verifyPassword", "Verify password", false));
registerBaseCommand.setMaximumArguments(false);
// Register the help command
CommandDescription registerHelpCommand = new CommandDescription(new HelpCommand(), helpCommandLabels,
"View help", "View detailed help pages about AuthMeReloaded register commands.", registerBaseCommand);
registerHelpCommand.addArgument(new CommandArgumentDescription("query", "The command or query to view help for.", true));
registerHelpCommand.setMaximumArguments(false);
// Register the base unregister command
CommandDescription unregisterBaseCommand = new CommandDescription(new fr.xephi.authme.command.executable.unregister.UnregisterCommand(), new ArrayList<String>() {
@ -352,7 +356,6 @@ public class CommandManager {
// Register the help command
CommandDescription unregisterHelpCommand = new CommandDescription(new HelpCommand(), helpCommandLabels, "View help", "View detailed help pages about AuthMeReloaded unregister commands.", unregisterBaseCommand);
unregisterHelpCommand.addArgument(new CommandArgumentDescription("query", "The command or query to view help for.", true));
unregisterHelpCommand.setMaximumArguments(false);
// Register the base changepassword command
CommandDescription changePasswordBaseCommand = new CommandDescription(new fr.xephi.authme.command.executable.changepassword.ChangePasswordCommand(), new ArrayList<String>() {
@ -365,13 +368,11 @@ public class CommandManager {
changePasswordBaseCommand.setCommandPermissions(UserPermission.CHANGE_PASSWORD, CommandPermissions.DefaultPermission.ALLOWED);
changePasswordBaseCommand.addArgument(new CommandArgumentDescription("password", "Password", false));
changePasswordBaseCommand.addArgument(new CommandArgumentDescription("verifyPassword", "Verify password", false));
changePasswordBaseCommand.setMaximumArguments(false);
// Register the help command
CommandDescription changePasswordHelpCommand = new CommandDescription(new HelpCommand(), helpCommandLabels,
"View help", "View detailed help pages about AuthMeReloaded change password commands.", changePasswordBaseCommand);
changePasswordHelpCommand.addArgument(new CommandArgumentDescription("query", "The command or query to view help for.", true));
changePasswordHelpCommand.setMaximumArguments(false);
// Register the base Dungeon Maze command
CommandDescription emailBaseCommand = new CommandDescription(new HelpCommand(), new ArrayList<String>() {
@ -386,7 +387,6 @@ public class CommandManager {
CommandDescription emailHelpCommand = new CommandDescription(new HelpCommand(), helpCommandLabels,
"View help", "View detailed help pages about AuthMeReloaded help commands.", emailBaseCommand);
emailHelpCommand.addArgument(new CommandArgumentDescription("query", "The command or query to view help for.", true));
emailHelpCommand.setMaximumArguments(false);
// Register the add command
CommandDescription addEmailCommand = new CommandDescription(new AddEmailCommand(), new ArrayList<String>() {
@ -437,13 +437,11 @@ public class CommandManager {
}, "Captcha command", "Captcha command for AuthMeReloaded.", null);
captchaBaseCommand.setCommandPermissions(UserPermission.CAPTCHA, CommandPermissions.DefaultPermission.ALLOWED);
captchaBaseCommand.addArgument(new CommandArgumentDescription("captcha", "The captcha", false));
captchaBaseCommand.setMaximumArguments(false);
// Register the help command
CommandDescription captchaHelpCommand = new CommandDescription(new HelpCommand(), helpCommandLabels,
"View help", "View detailed help pages about AuthMeReloaded change captcha commands.", captchaBaseCommand);
captchaHelpCommand.addArgument(new CommandArgumentDescription("query", "The command or query to view help for.", true));
captchaHelpCommand.setMaximumArguments(false);
// Register the base converter command
CommandDescription converterBaseCommand = new CommandDescription(new ConverterCommand(), new ArrayList<String>() {
@ -454,15 +452,13 @@ public class CommandManager {
add("conv");
}
}, "Convert command", "Convert command for AuthMeReloaded.", null);
converterBaseCommand.setCommandPermissions(UserPermission.CONVERTER, CommandPermissions.DefaultPermission.OP_ONLY);
converterBaseCommand.setCommandPermissions(UserPermission.CONVERTER, OP_ONLY);
converterBaseCommand.addArgument(new CommandArgumentDescription("job", "Conversion job: flattosql / flattosqlite /| xauth / crazylogin / rakamak / royalauth / vauth / sqltoflat", false));
converterBaseCommand.setMaximumArguments(false);
// Register the help command
CommandDescription converterHelpCommand = new CommandDescription(new HelpCommand(), helpCommandLabels,
"View help", "View detailed help pages about AuthMeReloaded change captcha commands.", converterBaseCommand);
converterHelpCommand.addArgument(new CommandArgumentDescription("query", "The command or query to view help for.", true));
converterHelpCommand.setMaximumArguments(false);
// Add the base commands to the commands array
this.commandDescriptions.add(authMeBaseCommand);

View File

@ -9,9 +9,6 @@ import org.bukkit.entity.Player;
import java.util.ArrayList;
import java.util.List;
//import com.timvisee.dungeonmaze.Core;
//import com.timvisee.dungeonmaze.permission.PermissionsManager;
/**
*/
public class CommandPermissions {
@ -19,17 +16,11 @@ public class CommandPermissions {
/**
* Defines the permission nodes required to have permission to execute this command.
*/
private List<PermissionNode> permissionNodes = new ArrayList<>();
private List<PermissionNode> permissionNodes;
/**
* Defines the default permission if the permission nodes couldn't be used.
*/
private DefaultPermission defaultPermission = DefaultPermission.NOT_ALLOWED;
/**
* Constructor.
*/
public CommandPermissions() {
}
private DefaultPermission defaultPermission;
/**
* Constructor.
@ -38,6 +29,7 @@ public class CommandPermissions {
* @param defaultPermission The default permission if the permission nodes couldn't be used.
*/
public CommandPermissions(PermissionNode permissionNode, DefaultPermission defaultPermission) {
this.permissionNodes = new ArrayList<>();
this.permissionNodes.add(permissionNode);
this.defaultPermission = defaultPermission;
}
@ -49,34 +41,8 @@ public class CommandPermissions {
* @param defaultPermission The default permission if the permission nodes couldn't be used.
*/
public CommandPermissions(List<PermissionNode> permissionNodes, DefaultPermission defaultPermission) {
this.permissionNodes.addAll(permissionNodes);
}
/**
* Add a permission node required to execute this command.
*
* @param permissionNode The permission node to add.
*
* @return True on success, false on failure.
*/
public boolean addPermissionNode(PermissionNode permissionNode) {
// Make sure this permission node hasn't been added already
if (hasPermissionNode(permissionNode))
return true;
// Add the permission node, return the result
return this.permissionNodes.add(permissionNode);
}
/**
* Check whether this command requires a specified permission node to execute.
*
* @param permissionNode The permission node to check for.
*
* @return True if this permission node is required, false if not.
*/
public boolean hasPermissionNode(PermissionNode permissionNode) {
return this.permissionNodes.contains(permissionNode);
this.permissionNodes = permissionNodes;
this.defaultPermission = defaultPermission;
}
/**
@ -88,14 +54,6 @@ public class CommandPermissions {
return this.permissionNodes;
}
/**
* Set the permission nodes required to execute this command.
*
* @param permissionNodes The permission nodes required to execute this command.
*/
public void setPermissionNodes(List<PermissionNode> permissionNodes) {
this.permissionNodes = permissionNodes;
}
/**
* Get the number of permission nodes set.

View File

@ -44,7 +44,7 @@ public class HelpPrinter {
sender.sendMessage(ChatColor.GOLD + "Short Description: " + ChatColor.WHITE + command.getDescription());
// Print the detailed description, if available
if (command.hasDetailedDescription()) {
if (!StringUtils.isEmpty(command.getDetailedDescription())) {
sender.sendMessage(ChatColor.GOLD + "Detailed Description:");
sender.sendMessage(ChatColor.WHITE + " " + command.getDetailedDescription());
}

View File

@ -6,6 +6,7 @@ import fr.xephi.authme.command.CommandParts;
import fr.xephi.authme.command.executable.authme.AuthMeCommand;
import fr.xephi.authme.command.executable.authme.RegisterCommand;
import org.junit.Test;
import org.mockito.Mockito;
import java.util.ArrayList;
import java.util.Arrays;
@ -26,7 +27,9 @@ public class HelpSyntaxHelperTest {
@Test
public void shouldFormatSimpleCommand() {
// given
CommandDescription description = getDescription();
CommandDescription description = getDescriptionBuilder()
.withArgument("name", "The name", true)
.build();
// when
String result = HelpSyntaxHelper.getCommandSyntax(
@ -39,9 +42,9 @@ public class HelpSyntaxHelperTest {
@Test
public void shouldFormatSimpleCommandWithOptionalParam() {
// given
CommandDescription description = getDescription();
description.setArguments(singletonList(
new CommandArgumentDescription("test", "", false)));
CommandDescription description = getDescriptionBuilder()
.withArgument("test", "", false)
.build();
// when
String result = HelpSyntaxHelper.getCommandSyntax(
@ -54,10 +57,10 @@ public class HelpSyntaxHelperTest {
@Test
public void shouldFormatCommandWithMultipleParams() {
// given
CommandDescription description = getDescription();
description.setArguments(Arrays.asList(
new CommandArgumentDescription("name", "", true),
new CommandArgumentDescription("test", "", false)));
CommandDescription description = getDescriptionBuilder()
.withArgument("name", "", true)
.withArgument("test", "", false)
.build();
// when
String result = HelpSyntaxHelper.getCommandSyntax(
@ -70,10 +73,10 @@ public class HelpSyntaxHelperTest {
@Test
public void shouldHighlightCommandWithMultipleParams() {
// given
CommandDescription description = getDescription();
description.setArguments(Arrays.asList(
new CommandArgumentDescription("name", "", true),
new CommandArgumentDescription("test", "", false)));
CommandDescription description = getDescriptionBuilder()
.withArgument("name", "", true)
.withArgument("test", "", false)
.build();
// when
String result = HelpSyntaxHelper.getCommandSyntax(
@ -88,8 +91,7 @@ public class HelpSyntaxHelperTest {
@Test
public void shouldHighlightCommandWithNoParams() {
// given
CommandDescription description = getDescription();
description.setArguments(new ArrayList<CommandArgumentDescription>());
CommandDescription description = getDescriptionBuilder().build();
// when
String result = HelpSyntaxHelper.getCommandSyntax(
@ -102,7 +104,9 @@ public class HelpSyntaxHelperTest {
@Test
public void shouldFormatSimpleCommandWithAlternativeLabel() {
// given
CommandDescription description = getDescription();
CommandDescription description = getDescriptionBuilder()
.withArgument("name", "The name", true)
.build();
// when
String result = HelpSyntaxHelper.getCommandSyntax(
@ -115,11 +119,11 @@ public class HelpSyntaxHelperTest {
@Test
public void shouldHighlightCommandWithAltLabelAndUnlimitedArguments() {
// given
CommandDescription description = getDescription();
description.setArguments(Arrays.asList(
new CommandArgumentDescription("name", "", true),
new CommandArgumentDescription("test", "", false)));
description.setMaximumArguments(false);
CommandDescription description = getDescriptionBuilder()
.withArgument("name", "", true)
.withArgument("test", "", false)
.noArgumentMaximum(true)
.build();
// when
String result = HelpSyntaxHelper.getCommandSyntax(
@ -132,21 +136,19 @@ public class HelpSyntaxHelperTest {
}
private static CommandDescription getDescription() {
CommandDescription base = new CommandDescription(new AuthMeCommand(),
singletonList("authme"),
"Base command",
"AuthMe base command",
null);
CommandArgumentDescription userArg = new CommandArgumentDescription(
"name", "", true);
private static CommandDescription.Builder getDescriptionBuilder() {
CommandDescription base = CommandDescription.builder()
.labels("authme")
.description("Base command")
.detailedDescription("AuthMe base command")
.parent(null)
.build();
return new CommandDescription(
new RegisterCommand(),
Arrays.asList("register", "r"),
"Register a player",
"Register the specified player with the specified password.",
base,
singletonList(userArg));
return CommandDescription.builder()
.executableCommand(Mockito.mock(RegisterCommand.class))
.labels("register", "r")
.description("Register a player")
.detailedDescription("Register the specified player with the specified password.")
.parent(base);
}
}