From bf9724dd34aa0702baa24b1809dcbd59be91e65a Mon Sep 17 00:00:00 2001 From: ljacqu Date: Sat, 19 Dec 2015 10:35:02 +0100 Subject: [PATCH] Fix label handling issues after reformatting - Pass the correct arguments to HelpCommand - Fix minor formatting / conditional check issues --- .../xephi/authme/command/CommandHandler.java | 19 ++++++++++++++++++- .../fr/xephi/authme/command/CommandUtils.java | 16 +++++++++++++--- .../command/executable/HelpCommand.java | 16 +++++++--------- .../authme/command/help/HelpProvider.java | 14 +++----------- 4 files changed, 41 insertions(+), 24 deletions(-) diff --git a/src/main/java/fr/xephi/authme/command/CommandHandler.java b/src/main/java/fr/xephi/authme/command/CommandHandler.java index a53a32e1b..09ccbd3cf 100644 --- a/src/main/java/fr/xephi/authme/command/CommandHandler.java +++ b/src/main/java/fr/xephi/authme/command/CommandHandler.java @@ -1,6 +1,7 @@ package fr.xephi.authme.command; import fr.xephi.authme.AuthMe; +import fr.xephi.authme.command.executable.HelpCommand; import fr.xephi.authme.command.help.HelpProvider; import fr.xephi.authme.permission.PermissionsManager; import fr.xephi.authme.util.CollectionUtils; @@ -28,6 +29,8 @@ public class CommandHandler { */ private static final double SUGGEST_COMMAND_THRESHOLD = 0.75; + private static final Class HELP_COMMAND_CLASS = HelpCommand.class; + private final Set baseCommands; private final PermissionsManager permissionsManager; @@ -174,7 +177,10 @@ public class CommandHandler { List remainingParts = parts.subList(1, parts.size()); CommandDescription childCommand = getSuitableChild(base, remainingParts); if (childCommand != null) { - return new FoundCommandResult(childCommand, parts.subList(0, 2), parts.subList(2, parts.size())); + FoundCommandResult result = new FoundCommandResult( + childCommand, parts.subList(0, 2), parts.subList(2, parts.size())); + transformResultForHelp(result); + return result; } else if (hasSuitableArgumentCount(base, remainingParts.size())) { return new FoundCommandResult(base, parts.subList(0, 1), parts.subList(1, parts.size())); } @@ -248,6 +254,17 @@ public class CommandHandler { return null; } + private static void transformResultForHelp(FoundCommandResult result) { + if (result.getCommandDescription() != null + && HELP_COMMAND_CLASS.equals(result.getCommandDescription().getExecutableCommand().getClass())) { + // For "/authme help register" we have labels = [authme, help] and arguments = [register] + // But for the help command we want labels = [authme, help] and arguments = [authme, register], + // so we can use the arguments as the labels to the command to show help for + final String baseLabel = result.getLabels().get(0); + result.getArguments().add(0, baseLabel); + } + } + private static boolean hasSuitableArgumentCount(CommandDescription command, int argumentCount) { int minArgs = CommandUtils.getMinNumberOfArguments(command); int maxArgs = CommandUtils.getMaxNumberOfArguments(command); diff --git a/src/main/java/fr/xephi/authme/command/CommandUtils.java b/src/main/java/fr/xephi/authme/command/CommandUtils.java index 2e4d02a28..905e8922e 100644 --- a/src/main/java/fr/xephi/authme/command/CommandUtils.java +++ b/src/main/java/fr/xephi/authme/command/CommandUtils.java @@ -35,12 +35,22 @@ public final class CommandUtils { } public static String constructCommandPath(CommandDescription command) { - List labels = new ArrayList<>(); + StringBuilder sb = new StringBuilder(); + String prefix = "/"; + for (CommandDescription ancestor : constructParentList(command)) { + sb.append(prefix).append(ancestor.getLabels().get(0)); + prefix = " "; + } + return sb.toString(); + } + + public static List constructParentList(CommandDescription command) { + List commands = new ArrayList<>(); CommandDescription currentCommand = command; while (currentCommand != null) { - labels.add(currentCommand.getLabels().get(0)); + commands.add(currentCommand); currentCommand = currentCommand.getParent(); } - return "/" + labelsToString(Lists.reverse(labels)); + return Lists.reverse(commands); } } diff --git a/src/main/java/fr/xephi/authme/command/executable/HelpCommand.java b/src/main/java/fr/xephi/authme/command/executable/HelpCommand.java index 95dea98a1..793803e59 100644 --- a/src/main/java/fr/xephi/authme/command/executable/HelpCommand.java +++ b/src/main/java/fr/xephi/authme/command/executable/HelpCommand.java @@ -1,6 +1,5 @@ package fr.xephi.authme.command.executable; -import fr.xephi.authme.AuthMe; import fr.xephi.authme.command.CommandHandler; import fr.xephi.authme.command.CommandUtils; import fr.xephi.authme.command.ExecutableCommand; @@ -8,12 +7,12 @@ import fr.xephi.authme.command.FoundCommandResult; import fr.xephi.authme.command.FoundResultStatus; import fr.xephi.authme.command.help.HelpProvider; import fr.xephi.authme.permission.PermissionsManager; +import fr.xephi.authme.util.Wrapper; import org.bukkit.ChatColor; import org.bukkit.command.CommandSender; import java.util.List; -import static fr.xephi.authme.command.FoundResultStatus.INCORRECT_ARGUMENTS; import static fr.xephi.authme.command.FoundResultStatus.MISSING_BASE_COMMAND; import static fr.xephi.authme.command.FoundResultStatus.UNKNOWN_LABEL; @@ -24,7 +23,7 @@ public class HelpCommand extends ExecutableCommand { @Override public void executeCommand(CommandSender sender, List arguments) { // TODO #306 ljacqu 20151213: Get command handler from non-static context - CommandHandler commandHandler = AuthMe.getInstance().getCommandHandler(); + CommandHandler commandHandler = Wrapper.getInstance().getAuthMe().getCommandHandler(); FoundCommandResult foundCommandResult = commandHandler.mapPartsToCommand(arguments); // TODO ljacqu 20151213: This is essentially the same logic as in CommandHandler and we'd like to have the same @@ -32,20 +31,19 @@ public class HelpCommand extends ExecutableCommand { // success. FoundResultStatus resultStatus = foundCommandResult.getResultStatus(); if (MISSING_BASE_COMMAND.equals(resultStatus)) { - // FIXME something wrong - this error appears sender.sendMessage(ChatColor.DARK_RED + "Could not get base command"); return; - } else if (INCORRECT_ARGUMENTS.equals(resultStatus) || UNKNOWN_LABEL.equals(resultStatus)) { - if (foundCommandResult.getCommandDescription() != null) { + } else if (UNKNOWN_LABEL.equals(resultStatus)) { + if (foundCommandResult.getCommandDescription() == null) { sender.sendMessage(ChatColor.DARK_RED + "Unknown command"); return; } else { - sender.sendMessage(ChatColor.GOLD + "Assuming " + ChatColor.WHITE + "/" - + CommandUtils.labelsToString(foundCommandResult.getCommandDescription().getLabels())); + sender.sendMessage(ChatColor.GOLD + "Assuming " + ChatColor.WHITE + + CommandUtils.constructCommandPath(foundCommandResult.getCommandDescription())); } } - PermissionsManager permissionsManager = AuthMe.getInstance().getPermissionsManager(); + PermissionsManager permissionsManager = Wrapper.getInstance().getAuthMe().getPermissionsManager(); List lines = arguments.size() == 1 ? HelpProvider.printHelp(foundCommandResult, HelpProvider.SHOW_CHILDREN) : HelpProvider.printHelp(foundCommandResult, sender, permissionsManager, HelpProvider.ALL_OPTIONS); 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 a60937cd6..73fd957ac 100644 --- a/src/main/java/fr/xephi/authme/command/help/HelpProvider.java +++ b/src/main/java/fr/xephi/authme/command/help/HelpProvider.java @@ -2,7 +2,6 @@ package fr.xephi.authme.command.help; import com.google.common.annotations.VisibleForTesting; import com.google.common.collect.ImmutableList; -import com.google.common.collect.Lists; import fr.xephi.authme.command.CommandArgumentDescription; import fr.xephi.authme.command.CommandDescription; import fr.xephi.authme.command.CommandPermissions; @@ -208,9 +207,9 @@ public final class HelpProvider { /** Format a command argument with the proper type of brackets. */ private static String formatArgument(CommandArgumentDescription argument) { if (argument.isOptional()) { - return " [" + argument.getName() + "]"; + return "[" + argument.getName() + "]"; } - return " <" + argument.getName() + ">"; + return "<" + argument.getName() + ">"; } private static boolean hasFlag(int flag, int options) { @@ -219,14 +218,7 @@ public final class HelpProvider { @VisibleForTesting protected static List filterCorrectLabels(CommandDescription command, List labels) { - List commands = new ArrayList<>(command.getParentCount() + 1); - CommandDescription currentCommand = command; - while (currentCommand != null) { - commands.add(currentCommand); - currentCommand = currentCommand.getParent(); - } - commands = Lists.reverse(commands); - + List commands = CommandUtils.constructParentList(command); List correctLabels = new ArrayList<>(); boolean foundIncorrectLabel = false; for (int i = 0; i < commands.size(); ++i) {