From cb2ffca6d35cd56f283e64fe34020a209741f264 Mon Sep 17 00:00:00 2001 From: ljacqu Date: Wed, 23 Dec 2015 23:51:39 +0100 Subject: [PATCH] #306 Change help provider to non-static / add calls to help functionality --- src/main/java/fr/xephi/authme/AuthMe.java | 16 +++---- .../xephi/authme/command/CommandMapper.java | 6 ++- .../xephi/authme/command/CommandService.java | 45 +++++++++++++++++++ .../authme/SwitchAntiBotCommand.java | 18 ++++---- .../executable/email/EmailBaseCommand.java | 7 ++- .../authme/command/help/HelpProvider.java | 18 ++------ .../command/CommandInitializerTest.java | 1 - .../authme/command/CommandMapperTest.java | 3 +- 8 files changed, 77 insertions(+), 37 deletions(-) diff --git a/src/main/java/fr/xephi/authme/AuthMe.java b/src/main/java/fr/xephi/authme/AuthMe.java index a4d8ddb77..443788e89 100644 --- a/src/main/java/fr/xephi/authme/AuthMe.java +++ b/src/main/java/fr/xephi/authme/AuthMe.java @@ -199,6 +199,13 @@ public class AuthMe extends JavaPlugin { setPluginInfos(); + // Load settings and custom configurations, if it fails, stop the server due to security reasons. + if (loadSettings()) { + server.shutdown(); + setEnabled(false); + return; + } + // Set up messages messages = Messages.getInstance(); @@ -209,13 +216,6 @@ public class AuthMe extends JavaPlugin { // Set up the module manager setupModuleManager(); - // Load settings and custom configurations, if it fails, stop the server due to security reasons. - if (loadSettings()) { - server.shutdown(); - setEnabled(false); - return; - } - // Setup otherAccounts file this.otherAccounts = OtherAccounts.getInstance(); @@ -412,7 +412,7 @@ public class AuthMe extends JavaPlugin { private CommandHandler initializeCommandHandler(PermissionsManager permissionsManager, Messages messages) { HelpProvider helpProvider = new HelpProvider(permissionsManager); Set baseCommands = CommandInitializer.buildCommands(); - CommandMapper mapper = new CommandMapper(baseCommands, messages, permissionsManager); + CommandMapper mapper = new CommandMapper(baseCommands, messages, permissionsManager, helpProvider); CommandService commandService = new CommandService(this, mapper, helpProvider, messages); return new CommandHandler(commandService); } diff --git a/src/main/java/fr/xephi/authme/command/CommandMapper.java b/src/main/java/fr/xephi/authme/command/CommandMapper.java index ccf96dc6c..69d31fe5f 100644 --- a/src/main/java/fr/xephi/authme/command/CommandMapper.java +++ b/src/main/java/fr/xephi/authme/command/CommandMapper.java @@ -38,12 +38,14 @@ public class CommandMapper { private final Set baseCommands; private final Messages messages; private final PermissionsManager permissionsManager; + private final HelpProvider helpProvider; public CommandMapper(Set baseCommands, Messages messages, - PermissionsManager permissionsManager) { + PermissionsManager permissionsManager, HelpProvider helpProvider) { this.baseCommands = baseCommands; this.messages = messages; this.permissionsManager = permissionsManager; + this.helpProvider = helpProvider; } public void outputStandardError(CommandSender sender, FoundCommandResult result) { @@ -97,7 +99,7 @@ public class CommandMapper { // Show the command argument help sender.sendMessage(ChatColor.DARK_RED + "Incorrect command arguments!"); - List lines = HelpProvider.printHelp(result, HelpProvider.SHOW_ARGUMENTS); + List lines = helpProvider.printHelp(sender, result, HelpProvider.SHOW_ARGUMENTS); for (String line : lines) { sender.sendMessage(line); } diff --git a/src/main/java/fr/xephi/authme/command/CommandService.java b/src/main/java/fr/xephi/authme/command/CommandService.java index 58d50f035..3d0c80d47 100644 --- a/src/main/java/fr/xephi/authme/command/CommandService.java +++ b/src/main/java/fr/xephi/authme/command/CommandService.java @@ -20,6 +20,14 @@ public class CommandService { private final HelpProvider helpProvider; private final CommandMapper commandMapper; + /** + * Constructor. + * + * @param authMe The plugin instance + * @param commandMapper Command mapper + * @param helpProvider Help provider + * @param messages Messages instance + */ public CommandService(AuthMe authMe, CommandMapper commandMapper, HelpProvider helpProvider, Messages messages) { this.authMe = authMe; this.messages = messages; @@ -27,27 +35,64 @@ public class CommandService { this.commandMapper = commandMapper; } + /** + * Send a message to a player. + * + * @param sender The command sender to send the message to + * @param messageKey The message key to send + */ public void send(CommandSender sender, MessageKey messageKey) { messages.send(sender, messageKey); } + /** + * Map command parts to a command description. + * + * @param sender The command sender issuing the request (for permission check), or null to skip permissions + * @param commandParts The received command parts to map to a command + * @return The computed mapping result + */ public FoundCommandResult mapPartsToCommand(CommandSender sender, List commandParts) { return commandMapper.mapPartsToCommand(sender, commandParts); } + /** + * Output the standard error message for the status in the provided {@link FoundCommandResult} object. + * Does not output anything for successful mappings. + * + * @param sender The sender to output the error to + * @param result The mapping result to process + */ public void outputMappingError(CommandSender sender, FoundCommandResult result) { commandMapper.outputStandardError(sender, result); } + /** + * Run the given task asynchronously with the Bukkit scheduler. + * + * @param task The task to run + */ public void runTaskAsynchronously(Runnable task) { authMe.getServer().getScheduler().runTaskAsynchronously(authMe, task); } + /** + * Return the AuthMe data source. + * + * @return The used data source + */ public DataSource getDataSource() { // TODO ljacqu 20151222: Add getter for .database and rename the field to dataSource return authMe.database; } + /** + * Output the help for a given command. + * + * @param sender The sender to output the help to + * @param result The result to output information about + * @param options Output options, see {@link HelpProvider} + */ public void outputHelp(CommandSender sender, FoundCommandResult result, int options) { List lines = helpProvider.printHelp(sender, result, options); for (String line : lines) { diff --git a/src/main/java/fr/xephi/authme/command/executable/authme/SwitchAntiBotCommand.java b/src/main/java/fr/xephi/authme/command/executable/authme/SwitchAntiBotCommand.java index b95e4cf26..e11645a94 100644 --- a/src/main/java/fr/xephi/authme/command/executable/authme/SwitchAntiBotCommand.java +++ b/src/main/java/fr/xephi/authme/command/executable/authme/SwitchAntiBotCommand.java @@ -3,11 +3,17 @@ package fr.xephi.authme.command.executable.authme; import fr.xephi.authme.AntiBot; import fr.xephi.authme.command.CommandService; import fr.xephi.authme.command.ExecutableCommand; +import fr.xephi.authme.command.FoundCommandResult; +import fr.xephi.authme.command.help.HelpProvider; import org.bukkit.ChatColor; import org.bukkit.command.CommandSender; +import java.util.Arrays; import java.util.List; +/** + * Display or change the status of the antibot mod. + */ public class SwitchAntiBotCommand implements ExecutableCommand { @Override @@ -25,16 +31,12 @@ public class SwitchAntiBotCommand implements ExecutableCommand { sender.sendMessage("[AuthMe] AntiBot Manual Override: enabled!"); } else if ("OFF".equalsIgnoreCase(newState)) { AntiBot.overrideAntiBotStatus(false); - sender.sendMessage("[AuthMe] AntiBotMod Manual Override: disabled!"); + sender.sendMessage("[AuthMe] AntiBot Manual Override: disabled!"); } else { sender.sendMessage(ChatColor.DARK_RED + "Invalid AntiBot mode!"); - // FIXME #306: Restore help showing logic - /* - CommandHandler commandHandler = AuthMe.getInstance().getCommandHandler(); - FoundCommandResult foundCommandResult = - commandHandler.mapPartsToCommand(Arrays.asList("authme", "antibot")); - HelpProvider.printHelp(foundCommandResult, HelpProvider.SHOW_ARGUMENTS); - sender.sendMessage(ChatColor.GOLD + "Detailed help: " + ChatColor.WHITE + "/authme help antibot");*/ + FoundCommandResult result = commandService.mapPartsToCommand(sender, Arrays.asList("authme", "antibot")); + commandService.outputHelp(sender, result, HelpProvider.SHOW_ARGUMENTS); + sender.sendMessage(ChatColor.GOLD + "Detailed help: " + ChatColor.WHITE + "/authme help antibot"); } } } diff --git a/src/main/java/fr/xephi/authme/command/executable/email/EmailBaseCommand.java b/src/main/java/fr/xephi/authme/command/executable/email/EmailBaseCommand.java index 0bf8c0c87..3a79843e8 100644 --- a/src/main/java/fr/xephi/authme/command/executable/email/EmailBaseCommand.java +++ b/src/main/java/fr/xephi/authme/command/executable/email/EmailBaseCommand.java @@ -2,8 +2,11 @@ package fr.xephi.authme.command.executable.email; import fr.xephi.authme.command.CommandService; import fr.xephi.authme.command.ExecutableCommand; +import fr.xephi.authme.command.FoundCommandResult; +import fr.xephi.authme.command.help.HelpProvider; import org.bukkit.command.CommandSender; +import java.util.Collections; import java.util.List; /** @@ -13,7 +16,7 @@ public class EmailBaseCommand implements ExecutableCommand { @Override public void executeCommand(CommandSender sender, List arguments, CommandService commandService) { - // FIXME #306 use getCommandService().getHelpProvider(); - // FIXME #306 HelpProvider.printHelp() + FoundCommandResult result = commandService.mapPartsToCommand(sender, Collections.singletonList("email")); + commandService.outputHelp(sender, result, HelpProvider.SHOW_CHILDREN); } } diff --git a/src/main/java/fr/xephi/authme/command/help/HelpProvider.java b/src/main/java/fr/xephi/authme/command/help/HelpProvider.java index 3eb4045c6..11235de51 100644 --- a/src/main/java/fr/xephi/authme/command/help/HelpProvider.java +++ b/src/main/java/fr/xephi/authme/command/help/HelpProvider.java @@ -49,28 +49,16 @@ public class HelpProvider { this.permissionsManager = permissionsManager; } - public static List printHelp(FoundCommandResult foundCommand, int options) { - return printHelp(foundCommand, null, null, options); - } - public List printHelp(CommandSender sender, FoundCommandResult result, int options) { - // FIXME don't overload and pass to the static method - // FIXME remove the static methods altogether - return printHelp(result, sender, permissionsManager, options); - } - - // sender and permissions manager may be null if SHOW_PERMISSIONS is not set - public static List printHelp(FoundCommandResult foundCommand, CommandSender sender, - PermissionsManager permissionsManager, int options) { - if (foundCommand.getCommandDescription() == null) { + if (result.getCommandDescription() == null) { return singletonList(ChatColor.DARK_RED + "Failed to retrieve any help information!"); } List lines = new ArrayList<>(); lines.add(ChatColor.GOLD + "==========[ " + Settings.helpHeader + " HELP ]=========="); - CommandDescription command = foundCommand.getCommandDescription(); - List labels = ImmutableList.copyOf(foundCommand.getLabels()); + CommandDescription command = result.getCommandDescription(); + List labels = ImmutableList.copyOf(result.getLabels()); List correctLabels = ImmutableList.copyOf(filterCorrectLabels(command, labels)); if (!hasFlag(HIDE_COMMAND, options)) { diff --git a/src/test/java/fr/xephi/authme/command/CommandInitializerTest.java b/src/test/java/fr/xephi/authme/command/CommandInitializerTest.java index cef723320..5a8d5d853 100644 --- a/src/test/java/fr/xephi/authme/command/CommandInitializerTest.java +++ b/src/test/java/fr/xephi/authme/command/CommandInitializerTest.java @@ -275,7 +275,6 @@ public class CommandInitializerTest { * count of arguments. */ @Test - @Ignore // TODO #306 ljacqu 20151214: Un-ignore this test and fix the offending command public void shouldPointToSameExecutableCommandWithConsistentArgumentCount() { // given final Map, Integer> mandatoryArguments = new HashMap<>(); diff --git a/src/test/java/fr/xephi/authme/command/CommandMapperTest.java b/src/test/java/fr/xephi/authme/command/CommandMapperTest.java index a2197c4b3..e65c08c68 100644 --- a/src/test/java/fr/xephi/authme/command/CommandMapperTest.java +++ b/src/test/java/fr/xephi/authme/command/CommandMapperTest.java @@ -1,5 +1,6 @@ package fr.xephi.authme.command; +import fr.xephi.authme.command.help.HelpProvider; import fr.xephi.authme.output.Messages; import fr.xephi.authme.permission.PermissionsManager; import org.bukkit.command.CommandSender; @@ -42,7 +43,7 @@ public class CommandMapperTest { @Before public void setUpMocks() { permissionsManagerMock = mock(PermissionsManager.class); - mapper = new CommandMapper(commands, mock(Messages.class), permissionsManagerMock); + mapper = new CommandMapper(commands, mock(Messages.class), permissionsManagerMock, mock(HelpProvider.class)); } // -----------