Fix label handling issues after reformatting

- Pass the correct arguments to HelpCommand
- Fix minor formatting / conditional check issues
This commit is contained in:
ljacqu 2015-12-19 10:35:02 +01:00
parent 01a294a334
commit bf9724dd34
4 changed files with 41 additions and 24 deletions

View File

@ -1,6 +1,7 @@
package fr.xephi.authme.command; package fr.xephi.authme.command;
import fr.xephi.authme.AuthMe; import fr.xephi.authme.AuthMe;
import fr.xephi.authme.command.executable.HelpCommand;
import fr.xephi.authme.command.help.HelpProvider; import fr.xephi.authme.command.help.HelpProvider;
import fr.xephi.authme.permission.PermissionsManager; import fr.xephi.authme.permission.PermissionsManager;
import fr.xephi.authme.util.CollectionUtils; 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 double SUGGEST_COMMAND_THRESHOLD = 0.75;
private static final Class<? extends ExecutableCommand> HELP_COMMAND_CLASS = HelpCommand.class;
private final Set<CommandDescription> baseCommands; private final Set<CommandDescription> baseCommands;
private final PermissionsManager permissionsManager; private final PermissionsManager permissionsManager;
@ -174,7 +177,10 @@ public class CommandHandler {
List<String> remainingParts = parts.subList(1, parts.size()); List<String> remainingParts = parts.subList(1, parts.size());
CommandDescription childCommand = getSuitableChild(base, remainingParts); CommandDescription childCommand = getSuitableChild(base, remainingParts);
if (childCommand != null) { 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())) { } else if (hasSuitableArgumentCount(base, remainingParts.size())) {
return new FoundCommandResult(base, parts.subList(0, 1), parts.subList(1, parts.size())); return new FoundCommandResult(base, parts.subList(0, 1), parts.subList(1, parts.size()));
} }
@ -248,6 +254,17 @@ public class CommandHandler {
return null; 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) { private static boolean hasSuitableArgumentCount(CommandDescription command, int argumentCount) {
int minArgs = CommandUtils.getMinNumberOfArguments(command); int minArgs = CommandUtils.getMinNumberOfArguments(command);
int maxArgs = CommandUtils.getMaxNumberOfArguments(command); int maxArgs = CommandUtils.getMaxNumberOfArguments(command);

View File

@ -35,12 +35,22 @@ public final class CommandUtils {
} }
public static String constructCommandPath(CommandDescription command) { public static String constructCommandPath(CommandDescription command) {
List<String> 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<CommandDescription> constructParentList(CommandDescription command) {
List<CommandDescription> commands = new ArrayList<>();
CommandDescription currentCommand = command; CommandDescription currentCommand = command;
while (currentCommand != null) { while (currentCommand != null) {
labels.add(currentCommand.getLabels().get(0)); commands.add(currentCommand);
currentCommand = currentCommand.getParent(); currentCommand = currentCommand.getParent();
} }
return "/" + labelsToString(Lists.reverse(labels)); return Lists.reverse(commands);
} }
} }

View File

@ -1,6 +1,5 @@
package fr.xephi.authme.command.executable; package fr.xephi.authme.command.executable;
import fr.xephi.authme.AuthMe;
import fr.xephi.authme.command.CommandHandler; import fr.xephi.authme.command.CommandHandler;
import fr.xephi.authme.command.CommandUtils; import fr.xephi.authme.command.CommandUtils;
import fr.xephi.authme.command.ExecutableCommand; 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.FoundResultStatus;
import fr.xephi.authme.command.help.HelpProvider; import fr.xephi.authme.command.help.HelpProvider;
import fr.xephi.authme.permission.PermissionsManager; import fr.xephi.authme.permission.PermissionsManager;
import fr.xephi.authme.util.Wrapper;
import org.bukkit.ChatColor; import org.bukkit.ChatColor;
import org.bukkit.command.CommandSender; import org.bukkit.command.CommandSender;
import java.util.List; 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.MISSING_BASE_COMMAND;
import static fr.xephi.authme.command.FoundResultStatus.UNKNOWN_LABEL; import static fr.xephi.authme.command.FoundResultStatus.UNKNOWN_LABEL;
@ -24,7 +23,7 @@ public class HelpCommand extends ExecutableCommand {
@Override @Override
public void executeCommand(CommandSender sender, List<String> arguments) { public void executeCommand(CommandSender sender, List<String> arguments) {
// TODO #306 ljacqu 20151213: Get command handler from non-static context // 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); 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 // 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. // success.
FoundResultStatus resultStatus = foundCommandResult.getResultStatus(); FoundResultStatus resultStatus = foundCommandResult.getResultStatus();
if (MISSING_BASE_COMMAND.equals(resultStatus)) { if (MISSING_BASE_COMMAND.equals(resultStatus)) {
// FIXME something wrong - this error appears
sender.sendMessage(ChatColor.DARK_RED + "Could not get base command"); sender.sendMessage(ChatColor.DARK_RED + "Could not get base command");
return; return;
} else if (INCORRECT_ARGUMENTS.equals(resultStatus) || UNKNOWN_LABEL.equals(resultStatus)) { } else if (UNKNOWN_LABEL.equals(resultStatus)) {
if (foundCommandResult.getCommandDescription() != null) { if (foundCommandResult.getCommandDescription() == null) {
sender.sendMessage(ChatColor.DARK_RED + "Unknown command"); sender.sendMessage(ChatColor.DARK_RED + "Unknown command");
return; return;
} else { } else {
sender.sendMessage(ChatColor.GOLD + "Assuming " + ChatColor.WHITE + "/" sender.sendMessage(ChatColor.GOLD + "Assuming " + ChatColor.WHITE
+ CommandUtils.labelsToString(foundCommandResult.getCommandDescription().getLabels())); + CommandUtils.constructCommandPath(foundCommandResult.getCommandDescription()));
} }
} }
PermissionsManager permissionsManager = AuthMe.getInstance().getPermissionsManager(); PermissionsManager permissionsManager = Wrapper.getInstance().getAuthMe().getPermissionsManager();
List<String> lines = arguments.size() == 1 List<String> lines = arguments.size() == 1
? HelpProvider.printHelp(foundCommandResult, HelpProvider.SHOW_CHILDREN) ? HelpProvider.printHelp(foundCommandResult, HelpProvider.SHOW_CHILDREN)
: HelpProvider.printHelp(foundCommandResult, sender, permissionsManager, HelpProvider.ALL_OPTIONS); : HelpProvider.printHelp(foundCommandResult, sender, permissionsManager, HelpProvider.ALL_OPTIONS);

View File

@ -2,7 +2,6 @@ package fr.xephi.authme.command.help;
import com.google.common.annotations.VisibleForTesting; import com.google.common.annotations.VisibleForTesting;
import com.google.common.collect.ImmutableList; import com.google.common.collect.ImmutableList;
import com.google.common.collect.Lists;
import fr.xephi.authme.command.CommandArgumentDescription; import fr.xephi.authme.command.CommandArgumentDescription;
import fr.xephi.authme.command.CommandDescription; import fr.xephi.authme.command.CommandDescription;
import fr.xephi.authme.command.CommandPermissions; import fr.xephi.authme.command.CommandPermissions;
@ -208,9 +207,9 @@ public final class HelpProvider {
/** Format a command argument with the proper type of brackets. */ /** Format a command argument with the proper type of brackets. */
private static String formatArgument(CommandArgumentDescription argument) { private static String formatArgument(CommandArgumentDescription argument) {
if (argument.isOptional()) { if (argument.isOptional()) {
return " [" + argument.getName() + "]"; return "[" + argument.getName() + "]";
} }
return " <" + argument.getName() + ">"; return "<" + argument.getName() + ">";
} }
private static boolean hasFlag(int flag, int options) { private static boolean hasFlag(int flag, int options) {
@ -219,14 +218,7 @@ public final class HelpProvider {
@VisibleForTesting @VisibleForTesting
protected static List<String> filterCorrectLabels(CommandDescription command, List<String> labels) { protected static List<String> filterCorrectLabels(CommandDescription command, List<String> labels) {
List<CommandDescription> commands = new ArrayList<>(command.getParentCount() + 1); List<CommandDescription> commands = CommandUtils.constructParentList(command);
CommandDescription currentCommand = command;
while (currentCommand != null) {
commands.add(currentCommand);
currentCommand = currentCommand.getParent();
}
commands = Lists.reverse(commands);
List<String> correctLabels = new ArrayList<>(); List<String> correctLabels = new ArrayList<>();
boolean foundIncorrectLabel = false; boolean foundIncorrectLabel = false;
for (int i = 0; i < commands.size(); ++i) { for (int i = 0; i < commands.size(); ++i) {