Split command management into initializer and handler

- Create Initializer class that only initializes AuthMe commands
- Move remaining method to CommandHandler
- Deprecate constructors on CommandDescription in favor of the builder
- Various cleanups and comments
This commit is contained in:
ljacqu 2015-11-29 12:51:11 +01:00
parent d6df921841
commit da0c5d1ea2
8 changed files with 170 additions and 229 deletions

View File

@ -418,8 +418,7 @@ public class AuthMe extends JavaPlugin {
* Set up the command handler. * Set up the command handler.
*/ */
private void setupCommandHandler() { private void setupCommandHandler() {
this.commandHandler = new CommandHandler(false); this.commandHandler = new CommandHandler();
this.commandHandler.init();
} }
/** /**

View File

@ -1,6 +1,7 @@
package fr.xephi.authme.command; package fr.xephi.authme.command;
/** /**
* Wrapper for the description of a command argument.
*/ */
public class CommandArgumentDescription { public class CommandArgumentDescription {
@ -9,15 +10,15 @@ public class CommandArgumentDescription {
/** /**
* Argument label (one-word description of the argument). * Argument label (one-word description of the argument).
*/ */
private String label; private final String label;
/** /**
* Argument description. * Argument description.
*/ */
private String description; private final String description;
/** /**
* Defines whether the argument is optional. * Defines whether the argument is optional.
*/ */
private boolean isOptional = false; private final boolean isOptional;
/** /**
* Constructor. * Constructor.
@ -51,7 +52,7 @@ public class CommandArgumentDescription {
} }
/** /**
* Check whether the argument is optional. * Return whether the argument is optional.
* *
* @return True if the argument is optional, false otherwise. * @return True if the argument is optional, false otherwise.
*/ */

View File

@ -15,9 +15,9 @@ import java.util.List;
* Command description - defines which labels ("names") will lead to a command and points to the * Command description - defines which labels ("names") will lead to a command and points to the
* {@link ExecutableCommand} implementation that executes the logic of the command. * {@link ExecutableCommand} implementation that executes the logic of the command.
* *
* CommandDescription is built hierarchically and have one parent or {@code null} for base commands (main commands * CommandDescription instances are built hierarchically and have one parent or {@code null} for base commands
* such as /authme) and may have multiple children extending the mapping of the parent: e.g. if /authme has a child * (main commands such as /authme) and may have multiple children extending the mapping of the parent: e.g. if
* whose label is "register", then "/authme register" is the command that the child defines. * /authme has a child whose label is "register", then "/authme register" is the command that the child defines.
*/ */
public class CommandDescription { public class CommandDescription {
@ -68,6 +68,7 @@ public class CommandDescription {
* @param detailedDescription Detailed comment description. * @param detailedDescription Detailed comment description.
* @param parent Parent command. * @param parent Parent command.
*/ */
@Deprecated
public CommandDescription(ExecutableCommand executableCommand, List<String> labels, String description, String detailedDescription, CommandDescription parent) { public CommandDescription(ExecutableCommand executableCommand, List<String> labels, String description, String detailedDescription, CommandDescription parent) {
this(executableCommand, labels, description, detailedDescription, parent, null); this(executableCommand, labels, description, detailedDescription, parent, null);
} }
@ -82,6 +83,7 @@ public class CommandDescription {
* @param parent Parent command. * @param parent Parent command.
* @param arguments Command arguments. * @param arguments Command arguments.
*/ */
@Deprecated
public CommandDescription(ExecutableCommand executableCommand, List<String> labels, String description, String detailedDescription, CommandDescription parent, List<CommandArgumentDescription> arguments) { public CommandDescription(ExecutableCommand executableCommand, List<String> labels, String description, String detailedDescription, CommandDescription parent, List<CommandArgumentDescription> arguments) {
setExecutableCommand(executableCommand); setExecutableCommand(executableCommand);
this.labels = labels; this.labels = labels;
@ -739,6 +741,9 @@ public class CommandDescription {
return new Builder(); return new Builder();
} }
/**
* Builder for initializing CommandDescription objects.
*/
public static final class Builder { public static final class Builder {
private List<String> labels; private List<String> labels;
private String description; private String description;
@ -750,7 +755,8 @@ public class CommandDescription {
private CommandPermissions permissions; private CommandPermissions permissions;
/** /**
* Build a CommandDescription from the builder. * Build a CommandDescription from the builder or throw an exception if mandatory
* fields have not been set.
* *
* @return The generated CommandDescription object * @return The generated CommandDescription object
*/ */
@ -796,6 +802,16 @@ public class CommandDescription {
return this; return this;
} }
/**
* Add an argument that the command description requires. This method can be called multiples times to add
* multiple arguments.
*
* @param label The label of the argument (single word name of the argument)
* @param description The description of the argument
* @param isOptional True if the argument is option, false if it is mandatory
*
* @return The builder
*/
public Builder withArgument(String label, String description, boolean isOptional) { public Builder withArgument(String label, String description, boolean isOptional) {
arguments.add(new CommandArgumentDescription(label, description, isOptional)); arguments.add(new CommandArgumentDescription(label, description, isOptional));
return this; return this;

View File

@ -10,78 +10,11 @@ import java.util.Arrays;
import java.util.List; import java.util.List;
/** /**
* The AuthMe command handler, responsible for mapping incoming commands to the correct {@link CommandDescription}
* or to display help messages for unknown invocations.
*/ */
public class CommandHandler { public class CommandHandler {
/**
* The command manager instance.
*/
private CommandManager commandManager;
/**
* Constructor.
*
* @param init True to immediately initialize.
*/
public CommandHandler(boolean init) {
// Initialize
if (init)
init();
}
/**
* Initialize the command handler.
*
* @return True if succeed, false on failure. True will also be returned if the command handler was already
* initialized.
*/
public boolean init() {
// Make sure the handler isn't initialized already
if (isInit())
return true;
// Initialize the command manager
this.commandManager = new CommandManager(false);
this.commandManager.registerCommands();
// Return the result
return true;
}
/**
* Check whether the command handler is initialized.
*
* @return True if the command handler is initialized.
*/
public boolean isInit() {
return this.commandManager != null;
}
/**
* Destroy the command handler.
*
* @return True if the command handler was destroyed successfully, false otherwise. True will also be returned if
* the command handler wasn't initialized.
*/
public boolean destroy() {
// Make sure the command handler is initialized
if (!isInit())
return true;
// Unset the command manager
this.commandManager = null;
return true;
}
/**
* Get the command manager.
*
* @return Command manager instance.
*/
public CommandManager getCommandManager() {
return this.commandManager;
}
/** /**
* Process a command. * Process a command.
* *
@ -92,6 +25,7 @@ public class CommandHandler {
* *
* @return True if the command was executed, false otherwise. * @return True if the command was executed, false otherwise.
*/ */
// TODO ljacqu 20151129: Rename onCommand() method to something not suggesting it is auto-invoked by an event
public boolean onCommand(CommandSender sender, org.bukkit.command.Command bukkitCommand, String bukkitCommandLabel, String[] bukkitArgs) { public boolean onCommand(CommandSender sender, org.bukkit.command.Command bukkitCommand, String bukkitCommandLabel, String[] bukkitArgs) {
// Process the arguments // Process the arguments
List<String> args = processArguments(bukkitArgs); List<String> args = processArguments(bukkitArgs);
@ -102,7 +36,7 @@ public class CommandHandler {
return false; return false;
// Get a suitable command for this reference, and make sure it isn't null // Get a suitable command for this reference, and make sure it isn't null
FoundCommandResult result = this.commandManager.findCommand(commandReference); FoundCommandResult result = findCommand(commandReference);
if (result == null) { if (result == null) {
sender.sendMessage(ChatColor.DARK_RED + "Failed to parse " + AuthMe.getPluginName() + " command!"); sender.sendMessage(ChatColor.DARK_RED + "Failed to parse " + AuthMe.getPluginName() + " command!");
return false; return false;
@ -207,4 +141,33 @@ public class CommandHandler {
// Return the argument // Return the argument
return arguments; return arguments;
} }
/**
* Find the best suitable command for the specified reference.
*
* @param queryReference The query reference to find a command for.
*
* @return The command found, or null.
*/
public FoundCommandResult findCommand(CommandParts queryReference) {
// Make sure the command reference is valid
if (queryReference.getCount() <= 0)
return null;
// TODO ljacqu 20151129: If base commands are only used in here (or in the future CommandHandler after changes),
// it might make sense to make the CommandInitializer package-private and to return its result into this class
// instead of regularly fetching the list of base commands from the other class.
for (CommandDescription commandDescription : CommandInitializer.getBaseCommands()) {
// Check whether there's a command description available for the
// current command
if (!commandDescription.isSuitableLabel(queryReference))
continue;
// Find the command reference, return the result
return commandDescription.findCommand(queryReference);
}
// No applicable command description found, return false
return null;
}
} }

View File

@ -1,7 +1,26 @@
package fr.xephi.authme.command; package fr.xephi.authme.command;
import fr.xephi.authme.command.executable.HelpCommand; import fr.xephi.authme.command.executable.HelpCommand;
import fr.xephi.authme.command.executable.authme.*; import fr.xephi.authme.command.executable.authme.AccountsCommand;
import fr.xephi.authme.command.executable.authme.AuthMeCommand;
import fr.xephi.authme.command.executable.authme.ChangePasswordCommand;
import fr.xephi.authme.command.executable.authme.FirstSpawnCommand;
import fr.xephi.authme.command.executable.authme.ForceLoginCommand;
import fr.xephi.authme.command.executable.authme.GetEmailCommand;
import fr.xephi.authme.command.executable.authme.GetIpCommand;
import fr.xephi.authme.command.executable.authme.LastLoginCommand;
import fr.xephi.authme.command.executable.authme.PurgeBannedPlayersCommand;
import fr.xephi.authme.command.executable.authme.PurgeCommand;
import fr.xephi.authme.command.executable.authme.PurgeLastPositionCommand;
import fr.xephi.authme.command.executable.authme.RegisterCommand;
import fr.xephi.authme.command.executable.authme.ReloadCommand;
import fr.xephi.authme.command.executable.authme.SetEmailCommand;
import fr.xephi.authme.command.executable.authme.SetFirstSpawnCommand;
import fr.xephi.authme.command.executable.authme.SetSpawnCommand;
import fr.xephi.authme.command.executable.authme.SpawnCommand;
import fr.xephi.authme.command.executable.authme.SwitchAntiBotCommand;
import fr.xephi.authme.command.executable.authme.UnregisterCommand;
import fr.xephi.authme.command.executable.authme.VersionCommand;
import fr.xephi.authme.command.executable.captcha.CaptchaCommand; import fr.xephi.authme.command.executable.captcha.CaptchaCommand;
import fr.xephi.authme.command.executable.converter.ConverterCommand; import fr.xephi.authme.command.executable.converter.ConverterCommand;
import fr.xephi.authme.command.executable.email.AddEmailCommand; import fr.xephi.authme.command.executable.email.AddEmailCommand;
@ -11,6 +30,7 @@ import fr.xephi.authme.command.executable.login.LoginCommand;
import fr.xephi.authme.command.executable.logout.LogoutCommand; import fr.xephi.authme.command.executable.logout.LogoutCommand;
import fr.xephi.authme.permission.AdminPermission; import fr.xephi.authme.permission.AdminPermission;
import fr.xephi.authme.permission.UserPermission; import fr.xephi.authme.permission.UserPermission;
import fr.xephi.authme.util.Wrapper;
import java.util.ArrayList; import java.util.ArrayList;
import java.util.Arrays; import java.util.Arrays;
@ -20,132 +40,125 @@ import static fr.xephi.authme.command.CommandPermissions.DefaultPermission.ALLOW
import static fr.xephi.authme.command.CommandPermissions.DefaultPermission.OP_ONLY; import static fr.xephi.authme.command.CommandPermissions.DefaultPermission.OP_ONLY;
/** /**
* Initializes all available AuthMe commands.
*/ */
public class CommandManager { public final class CommandInitializer {
/** private static List<CommandDescription> baseCommands;
* The list of commandDescriptions.
*/
private final List<CommandDescription> commandDescriptions = new ArrayList<>();
/** private CommandInitializer() {
* Constructor. // Helper class
*
* @param registerCommands True to register the commands, false otherwise.
*/
public CommandManager(boolean registerCommands) {
// Register the commands
if (registerCommands)
registerCommands();
} }
/** public static List<CommandDescription> getBaseCommands() {
* Register all commands. if (baseCommands == null) {
*/ Wrapper.getInstance().getLogger().info("Initializing AuthMe commands");
public void registerCommands() { initializeCommands();
}
return baseCommands;
}
private static void initializeCommands() {
// Create a list of help command labels // Create a list of help command labels
final List<String> helpCommandLabels = Arrays.asList("help", "hlp", "h", "sos", "?"); final List<String> helpCommandLabels = Arrays.asList("help", "hlp", "h", "sos", "?");
ExecutableCommand helpCommandExecutable = new HelpCommand(); final ExecutableCommand helpCommandExecutable = new HelpCommand();
// Register the base AuthMe Reloaded command // Register the base AuthMe Reloaded command
CommandDescription authMeBaseCommand = CommandDescription.builder() final CommandDescription AUTHME_BASE = CommandDescription.builder()
.executableCommand(new AuthMeCommand())
.labels("authme") .labels("authme")
.description("Main command") .description("Main command")
.detailedDescription("The main AuthMeReloaded command. The root for all admin commands.") .detailedDescription("The main AuthMeReloaded command. The root for all admin commands.")
.parent(null) .executableCommand(new AuthMeCommand())
.build(); .build();
// Register the help command // Register the help command
CommandDescription authMeHelpCommand = CommandDescription.builder() CommandDescription.builder()
.executableCommand(helpCommandExecutable) .parent(AUTHME_BASE)
.labels(helpCommandLabels) .labels(helpCommandLabels)
.description("View help") .description("View help")
.detailedDescription("View detailed help pages about AuthMeReloaded commands.") .detailedDescription("View detailed help pages about AuthMeReloaded commands.")
.parent(authMeBaseCommand)
.withArgument("query", "The command or query to view help for.", true) .withArgument("query", "The command or query to view help for.", true)
.executableCommand(helpCommandExecutable)
.build(); .build();
// Register the register command // Register the register command
CommandDescription registerCommand = CommandDescription.builder() CommandDescription.builder()
.executableCommand(new RegisterCommand()) .parent(AUTHME_BASE)
.labels("register", "reg", "r") .labels("register", "reg", "r")
.description("Register a player") .description("Register a player")
.detailedDescription("Register the specified player with the specified password.") .detailedDescription("Register the specified player with the specified password.")
.parent(authMeBaseCommand)
.permissions(OP_ONLY, UserPermission.REGISTER)
.withArgument("player", "Player name", false) .withArgument("player", "Player name", false)
.withArgument("password", "Password", false) .withArgument("password", "Password", false)
.permissions(OP_ONLY, UserPermission.REGISTER)
.executableCommand(new RegisterCommand())
.build(); .build();
// Register the unregister command // Register the unregister command
CommandDescription unregisterCommand = CommandDescription.builder() CommandDescription.builder()
.executableCommand(new UnregisterCommand()) .parent(AUTHME_BASE)
.labels("unregister", "unreg", "unr") .labels("unregister", "unreg", "unr")
.description("Unregister a player") .description("Unregister a player")
.detailedDescription("Unregister the specified player.") .detailedDescription("Unregister the specified player.")
.parent(authMeBaseCommand)
.permissions(OP_ONLY, UserPermission.UNREGISTER)
.withArgument("player", "Player name", false) .withArgument("player", "Player name", false)
.permissions(OP_ONLY, UserPermission.UNREGISTER)
.executableCommand(new UnregisterCommand())
.build(); .build();
// Register the forcelogin command // Register the forcelogin command
CommandDescription forceLoginCommand = CommandDescription.builder() CommandDescription.builder()
.executableCommand(new ForceLoginCommand()) .parent(AUTHME_BASE)
.labels("forcelogin", "login") .labels("forcelogin", "login")
.description("Enforce login player") .description("Enforce login player")
.detailedDescription("Enforce the specified player to login.") .detailedDescription("Enforce the specified player to login.")
.parent(authMeBaseCommand)
.permissions(OP_ONLY, UserPermission.CAN_LOGIN_BE_FORCED)
.withArgument("player", "Online player name", true) .withArgument("player", "Online player name", true)
.permissions(OP_ONLY, UserPermission.CAN_LOGIN_BE_FORCED)
.executableCommand(new ForceLoginCommand())
.build(); .build();
// Register the changepassword command // Register the changepassword command
CommandDescription changePasswordCommand = CommandDescription.builder() CommandDescription.builder()
.executableCommand(new ChangePasswordCommand()) .parent(AUTHME_BASE)
.labels("password", "changepassword", "changepass", "cp") .labels("password", "changepassword", "changepass", "cp")
.description("Change a player's password") .description("Change a player's password")
.detailedDescription("Change the password of a player.") .detailedDescription("Change the password of a player.")
.parent(authMeBaseCommand)
.permissions(OP_ONLY, UserPermission.CHANGE_PASSWORD)
.withArgument("player", "Player name", false) .withArgument("player", "Player name", false)
.withArgument("pwd", "New password", false) .withArgument("pwd", "New password", false)
.permissions(OP_ONLY, UserPermission.CHANGE_PASSWORD)
.executableCommand(new ChangePasswordCommand())
.build(); .build();
// Register the last login command // Register the last login command
CommandDescription lastLoginCommand = CommandDescription.builder() CommandDescription.builder()
.executableCommand(new LastLoginCommand()) .parent(AUTHME_BASE)
.labels("lastlogin", "ll") .labels("lastlogin", "ll")
.description("Player's last login") .description("Player's last login")
.detailedDescription("View the date of the specified players last login.") .detailedDescription("View the date of the specified players last login.")
.parent(authMeBaseCommand)
.permissions(OP_ONLY, AdminPermission.LAST_LOGIN)
.withArgument("player", "Player name", true) .withArgument("player", "Player name", true)
.permissions(OP_ONLY, AdminPermission.LAST_LOGIN)
.executableCommand(new LastLoginCommand())
.build(); .build();
// Register the accounts command // Register the accounts command
CommandDescription accountsCommand = CommandDescription.builder() CommandDescription.builder()
.executableCommand(new AccountsCommand()) .parent(AUTHME_BASE)
.labels("accounts", "account") .labels("accounts", "account")
.description("Display player accounts") .description("Display player accounts")
.detailedDescription("Display all accounts of a player by his player name or IP.") .detailedDescription("Display all accounts of a player by his player name or IP.")
.parent(authMeBaseCommand)
.permissions(OP_ONLY, AdminPermission.ACCOUNTS)
.withArgument("player", "Player name or IP", true) .withArgument("player", "Player name or IP", true)
.permissions(OP_ONLY, AdminPermission.ACCOUNTS)
.executableCommand(new AccountsCommand())
.build(); .build();
// Register the getemail command // Register the getemail command
CommandDescription getEmailCommand = new CommandDescription(new GetEmailCommand(), new ArrayList<String>() { CommandDescription.builder()
.parent(AUTHME_BASE)
{ .labels("getemail", "getmail", "email", "mail")
add("getemail"); .description("Display player's email")
add("getmail"); .detailedDescription("Display the email address of the specified player if set.")
add("email"); .withArgument("player", "Player name", true)
add("mail"); .permissions(OP_ONLY, AdminPermission.GET_EMAIL)
} .executableCommand(new GetEmailCommand())
}, "Display player's email", "Display the email address of the specified player if set.", authMeBaseCommand); .build();
getEmailCommand.setCommandPermissions(AdminPermission.GET_EMAIL, OP_ONLY);
getEmailCommand.addArgument(new CommandArgumentDescription("player", "Player name", true));
// Register the setemail command // Register the setemail command
CommandDescription setEmailCommand = new CommandDescription(new SetEmailCommand(), new ArrayList<String>() { CommandDescription setEmailCommand = new CommandDescription(new SetEmailCommand(), new ArrayList<String>() {
@ -156,7 +169,7 @@ public class CommandManager {
add("setemail"); add("setemail");
add("setmail"); add("setmail");
} }
}, "Change player's email", "Change the email address of the specified player.", authMeBaseCommand); }, "Change player's email", "Change the email address of the specified player.", AUTHME_BASE);
setEmailCommand.setCommandPermissions(AdminPermission.CHANGE_EMAIL, OP_ONLY); setEmailCommand.setCommandPermissions(AdminPermission.CHANGE_EMAIL, OP_ONLY);
setEmailCommand.addArgument(new CommandArgumentDescription("player", "Player name", false)); setEmailCommand.addArgument(new CommandArgumentDescription("player", "Player name", false));
setEmailCommand.addArgument(new CommandArgumentDescription("email", "Player email", false)); setEmailCommand.addArgument(new CommandArgumentDescription("email", "Player email", false));
@ -168,7 +181,7 @@ public class CommandManager {
add("getip"); add("getip");
add("ip"); add("ip");
} }
}, "Get player's IP", "Get the IP address of the specified online player.", authMeBaseCommand); }, "Get player's IP", "Get the IP address of the specified online player.", AUTHME_BASE);
getIpCommand.setCommandPermissions(AdminPermission.GET_IP, OP_ONLY); getIpCommand.setCommandPermissions(AdminPermission.GET_IP, OP_ONLY);
getIpCommand.addArgument(new CommandArgumentDescription("player", "Online player name", true)); getIpCommand.addArgument(new CommandArgumentDescription("player", "Online player name", true));
@ -179,7 +192,7 @@ public class CommandManager {
add("spawn"); add("spawn");
add("home"); add("home");
} }
}, "Teleport to spawn", "Teleport to the spawn.", authMeBaseCommand); }, "Teleport to spawn", "Teleport to the spawn.", AUTHME_BASE);
spawnCommand.setCommandPermissions(AdminPermission.SPAWN, OP_ONLY); spawnCommand.setCommandPermissions(AdminPermission.SPAWN, OP_ONLY);
// Register the setspawn command // Register the setspawn command
@ -189,7 +202,7 @@ public class CommandManager {
add("setspawn"); add("setspawn");
add("chgspawn"); add("chgspawn");
} }
}, "Change the spawn", "Change the player's spawn to your current position.", authMeBaseCommand); }, "Change the spawn", "Change the player's spawn to your current position.", AUTHME_BASE);
setSpawnCommand.setCommandPermissions(AdminPermission.SET_SPAWN, OP_ONLY); setSpawnCommand.setCommandPermissions(AdminPermission.SET_SPAWN, OP_ONLY);
// Register the firstspawn command // Register the firstspawn command
@ -199,7 +212,7 @@ public class CommandManager {
add("firstspawn"); add("firstspawn");
add("firsthome"); add("firsthome");
} }
}, "Teleport to first spawn", "Teleport to the first spawn.", authMeBaseCommand); }, "Teleport to first spawn", "Teleport to the first spawn.", AUTHME_BASE);
firstSpawnCommand.setCommandPermissions(AdminPermission.FIRST_SPAWN, OP_ONLY); firstSpawnCommand.setCommandPermissions(AdminPermission.FIRST_SPAWN, OP_ONLY);
// Register the setfirstspawn command // Register the setfirstspawn command
@ -209,7 +222,7 @@ public class CommandManager {
add("setfirstspawn"); add("setfirstspawn");
add("chgfirstspawn"); add("chgfirstspawn");
} }
}, "Change the first spawn", "Change the first player's spawn to your current position.", authMeBaseCommand); }, "Change the first spawn", "Change the first player's spawn to your current position.", AUTHME_BASE);
setFirstSpawnCommand.setCommandPermissions(AdminPermission.SET_FIRST_SPAWN, OP_ONLY); setFirstSpawnCommand.setCommandPermissions(AdminPermission.SET_FIRST_SPAWN, OP_ONLY);
// Register the purge command // Register the purge command
@ -219,7 +232,7 @@ public class CommandManager {
add("purge"); add("purge");
add("delete"); add("delete");
} }
}, "Purge old data", "Purge old AuthMeReloaded data longer than the specified amount of days ago.", authMeBaseCommand); }, "Purge old data", "Purge old AuthMeReloaded data longer than the specified amount of days ago.", AUTHME_BASE);
purgeCommand.setCommandPermissions(AdminPermission.PURGE, OP_ONLY); purgeCommand.setCommandPermissions(AdminPermission.PURGE, OP_ONLY);
purgeCommand.addArgument(new CommandArgumentDescription("days", "Number of days", false)); purgeCommand.addArgument(new CommandArgumentDescription("days", "Number of days", false));
@ -234,7 +247,7 @@ public class CommandManager {
add("resetlastposition"); add("resetlastposition");
add("resetlastpos"); add("resetlastpos");
} }
}, "Purge player's last position", "Purge the last know position of the specified player.", authMeBaseCommand); }, "Purge player's last position", "Purge the last know position of the specified player.", AUTHME_BASE);
purgeLastPositionCommand.setCommandPermissions(AdminPermission.PURGE_LAST_POSITION, OP_ONLY); purgeLastPositionCommand.setCommandPermissions(AdminPermission.PURGE_LAST_POSITION, OP_ONLY);
purgeLastPositionCommand.addArgument(new CommandArgumentDescription("player", "Player name", true)); purgeLastPositionCommand.addArgument(new CommandArgumentDescription("player", "Player name", true));
@ -247,7 +260,7 @@ public class CommandManager {
add("deletebannedplayers"); add("deletebannedplayers");
add("deletebannedplayer"); add("deletebannedplayer");
} }
}, "Purge banned palyers data", "Purge all AuthMeReloaded data for banned players.", authMeBaseCommand); }, "Purge banned palyers data", "Purge all AuthMeReloaded data for banned players.", AUTHME_BASE);
purgeBannedPlayersCommand.setCommandPermissions(AdminPermission.PURGE_BANNED_PLAYERS, OP_ONLY); purgeBannedPlayersCommand.setCommandPermissions(AdminPermission.PURGE_BANNED_PLAYERS, OP_ONLY);
// Register the switchantibot command // Register the switchantibot command
@ -258,7 +271,7 @@ public class CommandManager {
add("toggleantibot"); add("toggleantibot");
add("antibot"); add("antibot");
} }
}, "Switch AntiBot mode", "Switch or toggle the AntiBot mode to the specified state.", authMeBaseCommand); }, "Switch AntiBot mode", "Switch or toggle the AntiBot mode to the specified state.", AUTHME_BASE);
switchAntiBotCommand.setCommandPermissions(AdminPermission.SWITCH_ANTIBOT, OP_ONLY); switchAntiBotCommand.setCommandPermissions(AdminPermission.SWITCH_ANTIBOT, OP_ONLY);
switchAntiBotCommand.addArgument(new CommandArgumentDescription("mode", "ON / OFF", true)); switchAntiBotCommand.addArgument(new CommandArgumentDescription("mode", "ON / OFF", true));
@ -282,7 +295,7 @@ public class CommandManager {
add("reload"); add("reload");
add("rld"); add("rld");
} }
}, "Reload plugin", "Reload the AuthMeReloaded plugin.", authMeBaseCommand); }, "Reload plugin", "Reload the AuthMeReloaded plugin.", AUTHME_BASE);
reloadCommand.setCommandPermissions(AdminPermission.RELOAD, OP_ONLY); reloadCommand.setCommandPermissions(AdminPermission.RELOAD, OP_ONLY);
// Register the version command // Register the version command
@ -292,7 +305,7 @@ public class CommandManager {
.description("Version info") .description("Version info")
.detailedDescription("Show detailed information about the installed AuthMeReloaded version, and shows the " .detailedDescription("Show detailed information about the installed AuthMeReloaded version, and shows the "
+ "developers, contributors, license and other information.") + "developers, contributors, license and other information.")
.parent(authMeBaseCommand) .parent(AUTHME_BASE)
.build(); .build();
// Register the base login command // Register the base login command
@ -461,59 +474,15 @@ public class CommandManager {
converterHelpCommand.addArgument(new CommandArgumentDescription("query", "The command or query to view help for.", true)); converterHelpCommand.addArgument(new CommandArgumentDescription("query", "The command or query to view help for.", true));
// Add the base commands to the commands array // Add the base commands to the commands array
this.commandDescriptions.add(authMeBaseCommand); baseCommands = Arrays.asList(
this.commandDescriptions.add(loginBaseCommand); AUTHME_BASE,
this.commandDescriptions.add(logoutBaseCommand); loginBaseCommand,
this.commandDescriptions.add(registerBaseCommand); logoutBaseCommand,
this.commandDescriptions.add(unregisterBaseCommand); registerBaseCommand,
this.commandDescriptions.add(changePasswordBaseCommand); unregisterBaseCommand,
this.commandDescriptions.add(emailBaseCommand); changePasswordBaseCommand,
this.commandDescriptions.add(captchaBaseCommand); emailBaseCommand,
this.commandDescriptions.add(converterBaseCommand); captchaBaseCommand,
} converterBaseCommand);
/**
* Get the list of command descriptions
*
* @return List of command descriptions.
*/
public List<CommandDescription> getCommandDescriptions() {
return this.commandDescriptions;
}
/**
* Get the number of command description count.
*
* @return Command description count.
*/
public int getCommandDescriptionCount() {
return this.getCommandDescriptions().size();
}
/**
* Find the best suitable command for the specified reference.
*
* @param queryReference The query reference to find a command for.
*
* @return The command found, or null.
*/
public FoundCommandResult findCommand(CommandParts queryReference) {
// Make sure the command reference is valid
if (queryReference.getCount() <= 0)
return null;
// Get the base command description
for (CommandDescription commandDescription : this.commandDescriptions) {
// Check whether there's a command description available for the
// current command
if (!commandDescription.isSuitableLabel(queryReference))
continue;
// Find the command reference, return the result
return commandDescription.findCommand(queryReference);
}
// No applicable command description found, return false
return null;
} }
} }

View File

@ -37,9 +37,9 @@ public class HelpProvider {
*/ */
public static void showHelp(CommandSender sender, CommandParts reference, CommandParts helpQuery, boolean showCommand, boolean showDescription, boolean showArguments, boolean showPermissions, boolean showAlternatives, boolean showCommands) { public static void showHelp(CommandSender sender, CommandParts reference, CommandParts helpQuery, boolean showCommand, boolean showDescription, boolean showArguments, boolean showPermissions, boolean showAlternatives, boolean showCommands) {
// Find the command for this help query, one with and one without a prefixed base command // Find the command for this help query, one with and one without a prefixed base command
FoundCommandResult result = AuthMe.getInstance().getCommandHandler().getCommandManager().findCommand(new CommandParts(helpQuery.getList())); FoundCommandResult result = AuthMe.getInstance().getCommandHandler().findCommand(new CommandParts(helpQuery.getList()));
CommandParts commandReferenceOther = new CommandParts(reference.get(0), helpQuery.getList()); CommandParts commandReferenceOther = new CommandParts(reference.get(0), helpQuery.getList());
FoundCommandResult resultOther = AuthMe.getInstance().getCommandHandler().getCommandManager().findCommand(commandReferenceOther); FoundCommandResult resultOther = AuthMe.getInstance().getCommandHandler().findCommand(commandReferenceOther);
if (resultOther != null) { if (resultOther != null) {
if (result == null) if (result == null)
result = resultOther; result = resultOther;

View File

@ -260,6 +260,7 @@ public class PermissionsManager {
* *
* @param event Event instance. * @param event Event instance.
*/ */
// TODO ljacqu 20151129: Misleading name since onPluginEnable is a typical event-based method name
public void onPluginEnable(PluginEnableEvent event) { public void onPluginEnable(PluginEnableEvent event) {
// Get the plugin and it's name // Get the plugin and it's name
Plugin plugin = event.getPlugin(); Plugin plugin = event.getPlugin();

View File

@ -18,7 +18,7 @@ import static org.junit.Assert.assertThat;
import static org.junit.Assert.fail; import static org.junit.Assert.fail;
/** /**
* Test for {@link CommandManager}, especially to guarantee the integrity of the defined commands. * Test for {@link CommandInitializer} to guarantee the integrity of the defined commands.
*/ */
public class CommandManagerTest { public class CommandManagerTest {
@ -28,24 +28,20 @@ public class CommandManagerTest {
*/ */
private static int MAX_ALLOWED_DEPTH = 1; private static int MAX_ALLOWED_DEPTH = 1;
private static CommandManager manager; private static List<CommandDescription> commands;
@BeforeClass @BeforeClass
public static void initializeCommandManager() { public static void initializeCommandManager() {
manager = new CommandManager(true); commands = CommandInitializer.getBaseCommands();
} }
@Test @Test
public void shouldInitializeCommands() { public void shouldInitializeCommands() {
// given/when // given/when/then
int commandCount = manager.getCommandDescriptionCount();
List<CommandDescription> commands = manager.getCommandDescriptions();
// then
// It obviously doesn't make sense to test much of the concrete data // It obviously doesn't make sense to test much of the concrete data
// that is being initialized; we just want to guarantee with this test // that is being initialized; we just want to guarantee with this test
// that data is indeed being initialized and we take a few "probes" // that data is indeed being initialized and we take a few "probes"
assertThat(commandCount, equalTo(9)); assertThat(commands.size(), equalTo(9));
assertThat(commandsIncludeLabel(commands, "authme"), equalTo(true)); assertThat(commandsIncludeLabel(commands, "authme"), equalTo(true));
assertThat(commandsIncludeLabel(commands, "register"), equalTo(true)); assertThat(commandsIncludeLabel(commands, "register"), equalTo(true));
assertThat(commandsIncludeLabel(commands, "help"), equalTo(false)); assertThat(commandsIncludeLabel(commands, "help"), equalTo(false));
@ -62,7 +58,7 @@ public class CommandManagerTest {
}; };
// when/then // when/then
walkThroughCommands(manager.getCommandDescriptions(), descriptionTester); walkThroughCommands(commands, descriptionTester);
} }
/** Ensure that all children of a command stored the parent. */ /** Ensure that all children of a command stored the parent. */
@ -84,7 +80,7 @@ public class CommandManagerTest {
}; };
// when/then // when/then
walkThroughCommands(manager.getCommandDescriptions(), connectionTester); walkThroughCommands(commands, connectionTester);
} }
@Test @Test
@ -105,7 +101,7 @@ public class CommandManagerTest {
}; };
// when/then // when/then
walkThroughCommands(manager.getCommandDescriptions(), uniqueMappingTester); walkThroughCommands(commands, uniqueMappingTester);
} }
/** /**
@ -132,7 +128,7 @@ public class CommandManagerTest {
}; };
// when/then // when/then
walkThroughCommands(manager.getCommandDescriptions(), descriptionTester); walkThroughCommands(commands, descriptionTester);
} }
/** /**
@ -143,7 +139,6 @@ public class CommandManagerTest {
public void shouldNotHaveMultipleInstancesOfSameExecutableCommandSubType() { public void shouldNotHaveMultipleInstancesOfSameExecutableCommandSubType() {
// given // given
final Map<Class<? extends ExecutableCommand>, ExecutableCommand> implementations = new HashMap<>(); final Map<Class<? extends ExecutableCommand>, ExecutableCommand> implementations = new HashMap<>();
CommandManager manager = new CommandManager(true);
BiConsumer descriptionTester = new BiConsumer() { BiConsumer descriptionTester = new BiConsumer() {
@Override @Override
public void accept(CommandDescription command, int depth) { public void accept(CommandDescription command, int depth) {
@ -160,10 +155,7 @@ public class CommandManagerTest {
} }
}; };
// when // when/then
List<CommandDescription> commands = manager.getCommandDescriptions();
// then
walkThroughCommands(commands, descriptionTester); walkThroughCommands(commands, descriptionTester);
} }
@ -186,7 +178,7 @@ public class CommandManagerTest {
}; };
// when/then // when/then
walkThroughCommands(manager.getCommandDescriptions(), argumentOrderTester); walkThroughCommands(commands, argumentOrderTester);
} }
/** /**
@ -209,7 +201,7 @@ public class CommandManagerTest {
}; };
// when/then // when/then
walkThroughCommands(manager.getCommandDescriptions(), noArgumentForParentChecker); walkThroughCommands(commands, noArgumentForParentChecker);
} }