mirror of
https://github.com/AuthMe/AuthMeReloaded.git
synced 2024-12-19 15:17:56 +01:00
Change to new HelpProvider, adjust classes to new methods
This commit is contained in:
parent
01cba2a508
commit
c48c56f08c
@ -178,7 +178,8 @@ public class CommandHandler {
|
||||
closestCommand = child;
|
||||
}
|
||||
}
|
||||
// TODO: Return the full list of labels and arguments
|
||||
// FIXME: Return the full list of labels and arguments
|
||||
// FIXME: Return INVALID_ARGUMENTS instead of UNKNOWN_LABEL when more accurate
|
||||
return new FoundCommandResult(
|
||||
closestCommand, null, null, minDifference, FoundCommandResult.ResultStatus.UNKNOWN_LABEL);
|
||||
}
|
||||
|
@ -3,6 +3,18 @@ package fr.xephi.authme.command;
|
||||
import java.util.List;
|
||||
|
||||
/**
|
||||
* Result of a command mapping by {@link CommandHandler}. An object of this class represents a successful mapping
|
||||
* as well as erroneous ones, as communicated with {@link ResultStatus}.
|
||||
* <p />
|
||||
* Fields other than {@link ResultStatus} are available depending, among other factors, on the status:
|
||||
* <ul>
|
||||
* <li>{@link ResultStatus#SUCCESS} entails that mapping the input to a command was successful. Therefore,
|
||||
* the command description, labels and arguments are set. The difference is 0.0.</li>
|
||||
* <li>{@link ResultStatus#INCORRECT_ARGUMENTS}</li>
|
||||
* <li>{@link ResultStatus#UNKNOWN_LABEL}</li>
|
||||
* <li>{@link ResultStatus#MISSING_BASE_COMMAND} should never occur. Any other fields may not be present and any
|
||||
* processing of the object should be aborted.</li>
|
||||
* </ul>
|
||||
*/
|
||||
public class FoundCommandResult {
|
||||
|
||||
|
@ -1,28 +1,54 @@
|
||||
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;
|
||||
import fr.xephi.authme.command.FoundCommandResult;
|
||||
import fr.xephi.authme.command.help.HelpProvider;
|
||||
import fr.xephi.authme.permission.PermissionsManager;
|
||||
import org.bukkit.ChatColor;
|
||||
import org.bukkit.command.CommandSender;
|
||||
|
||||
import java.util.List;
|
||||
|
||||
/**
|
||||
*/
|
||||
import static fr.xephi.authme.command.FoundCommandResult.ResultStatus.*;
|
||||
|
||||
public class HelpCommand extends ExecutableCommand {
|
||||
|
||||
// Convention: arguments is not the actual invoked arguments but the command that was invoked,
|
||||
// e.g. "/authme help register" would typically be arguments = [register], but here we pass [authme, register]
|
||||
@Override
|
||||
public boolean executeCommand(CommandSender sender, CommandParts commandReference, CommandParts commandArguments) {
|
||||
// Check whether quick help should be shown
|
||||
List<String> arguments = commandArguments.getList();
|
||||
public void executeCommand(CommandSender sender, List<String> arguments) {
|
||||
// TODO #306 ljacqu 20151213: Get command handler from non-static context
|
||||
CommandHandler commandHandler = AuthMe.getInstance().getCommandHandler();
|
||||
FoundCommandResult foundCommandResult = commandHandler.mapPartsToCommand(arguments);
|
||||
|
||||
// Set the proper command arguments for the quick help and show it
|
||||
if (arguments.isEmpty()) {
|
||||
commandArguments = new CommandParts(commandReference.get(0));
|
||||
HelpProvider.showHelp(sender, commandReference, commandArguments, false, false, false, false, false, true);
|
||||
// TODO ljacqu 20151213: This is essentially the same logic as in CommandHandler and we'd like to have the same
|
||||
// messages. Maybe we can have another method in CommandHandler where the end command isn't executed upon
|
||||
// success.
|
||||
FoundCommandResult.ResultStatus resultStatus = foundCommandResult.getResultStatus();
|
||||
if (MISSING_BASE_COMMAND.equals(resultStatus)) {
|
||||
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) {
|
||||
sender.sendMessage(ChatColor.DARK_RED + "Unknown command");
|
||||
return;
|
||||
} else {
|
||||
HelpProvider.showHelp(sender, commandReference, commandArguments);
|
||||
sender.sendMessage(ChatColor.GOLD + "Assuming " + ChatColor.WHITE + "/"
|
||||
+ CommandUtils.labelsToString(foundCommandResult.getCommandDescription().getLabels()));
|
||||
}
|
||||
}
|
||||
|
||||
return true;
|
||||
PermissionsManager permissionsManager = AuthMe.getInstance().getPermissionsManager();
|
||||
List<String> lines = arguments.size() == 1
|
||||
? HelpProvider.printHelp(foundCommandResult, HelpProvider.SHOW_CHILDREN)
|
||||
: HelpProvider.printHelp(foundCommandResult, sender, permissionsManager, HelpProvider.ALL_OPTIONS);
|
||||
for (String line : lines) {
|
||||
sender.sendMessage(line);
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
}
|
||||
|
@ -16,19 +16,14 @@ public class AccountsCommand extends ExecutableCommand {
|
||||
final AuthMe plugin = AuthMe.getInstance();
|
||||
final Messages m = plugin.getMessages();
|
||||
|
||||
// Get the player query
|
||||
String playerQuery = sender.getName();
|
||||
if (commandArguments.getCount() >= 1) {
|
||||
playerQuery = commandArguments.get(0);
|
||||
}
|
||||
final String playerQueryFinal = playerQuery;
|
||||
final String playerName = arguments.isEmpty() ? sender.getName() : arguments.get(0);
|
||||
|
||||
// Command logic
|
||||
if (!playerQueryFinal.contains(".")) {
|
||||
if (!playerName.contains(".")) {
|
||||
plugin.getServer().getScheduler().runTaskAsynchronously(plugin, new Runnable() {
|
||||
@Override
|
||||
public void run() {
|
||||
PlayerAuth auth = plugin.database.getAuth(playerQueryFinal.toLowerCase());
|
||||
PlayerAuth auth = plugin.database.getAuth(playerName.toLowerCase());
|
||||
if (auth == null) {
|
||||
m.send(sender, MessageKey.UNKNOWN_USER);
|
||||
return;
|
||||
@ -40,7 +35,7 @@ public class AccountsCommand extends ExecutableCommand {
|
||||
return;
|
||||
}
|
||||
if (accountList.size() == 1) {
|
||||
sender.sendMessage("[AuthMe] " + playerQueryFinal + " is a single account player");
|
||||
sender.sendMessage("[AuthMe] " + playerName + " is a single account player");
|
||||
return;
|
||||
}
|
||||
int i = 0;
|
||||
@ -53,7 +48,7 @@ public class AccountsCommand extends ExecutableCommand {
|
||||
message.append('.');
|
||||
}
|
||||
}
|
||||
sender.sendMessage("[AuthMe] " + playerQueryFinal + " has "
|
||||
sender.sendMessage("[AuthMe] " + playerName + " has "
|
||||
+ String.valueOf(accountList.size()) + " accounts.");
|
||||
sender.sendMessage(message.toString());
|
||||
}
|
||||
@ -63,14 +58,14 @@ public class AccountsCommand extends ExecutableCommand {
|
||||
plugin.getServer().getScheduler().runTaskAsynchronously(plugin, new Runnable() {
|
||||
@Override
|
||||
public void run() {
|
||||
List<String> accountList = plugin.database.getAllAuthsByIp(playerQueryFinal);
|
||||
List<String> accountList = plugin.database.getAllAuthsByIp(playerName);
|
||||
StringBuilder message = new StringBuilder("[AuthMe] ");
|
||||
if (accountList.isEmpty()) {
|
||||
sender.sendMessage("[AuthMe] This IP does not exist in the database.");
|
||||
return;
|
||||
}
|
||||
if (accountList.size() == 1) {
|
||||
sender.sendMessage("[AuthMe] " + playerQueryFinal + " is a single account player");
|
||||
sender.sendMessage("[AuthMe] " + playerName + " is a single account player");
|
||||
return;
|
||||
}
|
||||
int i = 0;
|
||||
@ -83,7 +78,7 @@ public class AccountsCommand extends ExecutableCommand {
|
||||
message.append('.');
|
||||
}
|
||||
}
|
||||
sender.sendMessage("[AuthMe] " + playerQueryFinal + " has "
|
||||
sender.sendMessage("[AuthMe] " + playerName + " has "
|
||||
+ String.valueOf(accountList.size()) + " accounts.");
|
||||
sender.sendMessage(message.toString());
|
||||
}
|
||||
|
@ -14,7 +14,7 @@ import java.util.List;
|
||||
public class SetEmailCommand extends ExecutableCommand {
|
||||
|
||||
@Override
|
||||
public void executeCommand(CommandSender sender, List<String> arguments) {
|
||||
public void executeCommand(final CommandSender sender, List<String> arguments) {
|
||||
// AuthMe plugin instance
|
||||
final AuthMe plugin = AuthMe.getInstance();
|
||||
|
||||
@ -59,6 +59,5 @@ public class SetEmailCommand extends ExecutableCommand {
|
||||
|
||||
}
|
||||
});
|
||||
return true;
|
||||
}
|
||||
}
|
||||
|
@ -1,53 +1,43 @@
|
||||
package fr.xephi.authme.command.executable.authme;
|
||||
|
||||
import fr.xephi.authme.AntiBot;
|
||||
import fr.xephi.authme.command.CommandUtils;
|
||||
import fr.xephi.authme.AuthMe;
|
||||
import fr.xephi.authme.command.CommandHandler;
|
||||
import fr.xephi.authme.command.ExecutableCommand;
|
||||
import fr.xephi.authme.command.FoundCommandResult;
|
||||
import fr.xephi.authme.command.help.HelpProvider;
|
||||
import fr.xephi.authme.util.CollectionUtils;
|
||||
import org.bukkit.ChatColor;
|
||||
import org.bukkit.command.CommandSender;
|
||||
|
||||
import java.util.Arrays;
|
||||
import java.util.List;
|
||||
|
||||
public class SwitchAntiBotCommand extends ExecutableCommand {
|
||||
|
||||
@Override
|
||||
public void executeCommand(final CommandSender sender, List<String> arguments) {
|
||||
// Get the new state
|
||||
String newState = null;
|
||||
|
||||
if (arguments.size() == 1) {
|
||||
newState = arguments.get(0);
|
||||
} else if (arguments.size() == 0) {
|
||||
if (arguments.isEmpty()) {
|
||||
sender.sendMessage("[AuthMe] AntiBot status: " + AntiBot.getAntiBotStatus().name());
|
||||
return;
|
||||
}
|
||||
|
||||
// Enable the mod
|
||||
String newState = arguments.get(0);
|
||||
|
||||
// Enable or disable the mod
|
||||
if ("ON".equalsIgnoreCase(newState)) {
|
||||
AntiBot.overrideAntiBotStatus(true);
|
||||
sender.sendMessage("[AuthMe] AntiBot Manual Override: enabled!");
|
||||
return;
|
||||
}
|
||||
|
||||
// Disable the mod
|
||||
if ("OFF".equalsIgnoreCase(newState)) {
|
||||
} else if ("OFF".equalsIgnoreCase(newState)) {
|
||||
AntiBot.overrideAntiBotStatus(false);
|
||||
sender.sendMessage("[AuthMe] AntiBotMod Manual Override: disabled!");
|
||||
return;
|
||||
}
|
||||
|
||||
// Show the invalid arguments warning
|
||||
} else {
|
||||
sender.sendMessage(ChatColor.DARK_RED + "Invalid AntiBot mode!");
|
||||
|
||||
// Show the command argument help
|
||||
// FIXME fix help reference
|
||||
HelpProvider.showHelp(sender, commandReference, commandReference, true, false, true, false, false, false);
|
||||
|
||||
// Show the command to use for detailed help
|
||||
List<String> helpCommandReference = CollectionUtils.getRange(commandReference.getList(), 1);
|
||||
sender.sendMessage(ChatColor.GOLD + "Detailed help: " + ChatColor.WHITE + "/"
|
||||
+ commandReference.get(0) + " help " + CommandUtils.labelsToString(helpCommandReference));
|
||||
// TODO ljacqu 20151213: Fix static retrieval of command handler
|
||||
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");
|
||||
}
|
||||
}
|
||||
}
|
||||
|
@ -1,193 +0,0 @@
|
||||
package fr.xephi.authme.command.help;
|
||||
|
||||
import java.util.ArrayList;
|
||||
import java.util.Collections;
|
||||
import java.util.Comparator;
|
||||
import java.util.List;
|
||||
|
||||
import org.bukkit.ChatColor;
|
||||
import org.bukkit.command.CommandSender;
|
||||
import org.bukkit.entity.Player;
|
||||
|
||||
import fr.xephi.authme.AuthMe;
|
||||
import fr.xephi.authme.command.CommandArgumentDescription;
|
||||
import fr.xephi.authme.command.CommandDescription;
|
||||
import fr.xephi.authme.command.CommandPermissions;
|
||||
import fr.xephi.authme.permission.PermissionNode;
|
||||
import fr.xephi.authme.util.CollectionUtils;
|
||||
import fr.xephi.authme.util.StringUtils;
|
||||
|
||||
/**
|
||||
*/
|
||||
public class HelpPrinter {
|
||||
|
||||
/**
|
||||
* Print the command help information.
|
||||
*
|
||||
* @param sender The command sender to print the help to.
|
||||
* @param command The command to print.
|
||||
* @param commandReference The command reference used.
|
||||
*/
|
||||
public static void printCommand(CommandSender sender, CommandDescription command, CommandParts commandReference) {
|
||||
// Print the proper command syntax
|
||||
sender.sendMessage(ChatColor.GOLD + "Command: " + HelpSyntaxHelper.getCommandSyntax(command, commandReference, null, true));
|
||||
}
|
||||
|
||||
/**
|
||||
* Print the command help description information. This will print both the short, as the detailed description if available.
|
||||
*
|
||||
* @param sender The command sender to print the help to.
|
||||
* @param command The command to print the description help for.
|
||||
*/
|
||||
public static void printCommandDescription(CommandSender sender, CommandDescription command) {
|
||||
sender.sendMessage(ChatColor.GOLD + "Short Description: " + ChatColor.WHITE + command.getDescription());
|
||||
|
||||
// Print the detailed description, if available
|
||||
if (!StringUtils.isEmpty(command.getDetailedDescription())) {
|
||||
sender.sendMessage(ChatColor.GOLD + "Detailed Description:");
|
||||
sender.sendMessage(ChatColor.WHITE + " " + command.getDetailedDescription());
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Print the command help arguments information if available.
|
||||
*
|
||||
* @param sender The command sender to print the help to.
|
||||
* @param command The command to print the argument help for.
|
||||
*/
|
||||
@SuppressWarnings("StringConcatenationInsideStringBufferAppend")
|
||||
public static void printArguments(CommandSender sender, CommandDescription command) {
|
||||
// Make sure there are any commands to print
|
||||
if (!command.hasArguments())
|
||||
return;
|
||||
|
||||
// Print the header
|
||||
sender.sendMessage(ChatColor.GOLD + "Arguments:");
|
||||
|
||||
// Print each argument
|
||||
for (CommandArgumentDescription arg : command.getArguments()) {
|
||||
// Create a string builder to build the syntax in
|
||||
StringBuilder argString = new StringBuilder();
|
||||
argString.append(" " + ChatColor.YELLOW + ChatColor.ITALIC + arg.getName() + " : " + ChatColor.WHITE + arg.getDescription());
|
||||
|
||||
// Suffix a note if the command is optional
|
||||
if (arg.isOptional())
|
||||
argString.append(ChatColor.GRAY + "" + ChatColor.ITALIC + " (Optional)");
|
||||
|
||||
// Print the syntax
|
||||
sender.sendMessage(argString.toString());
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Print the command help permissions information if available.
|
||||
*
|
||||
* @param sender The command sender to print the help to.
|
||||
* @param command The command to print the permissions help for.
|
||||
*/
|
||||
public static void printPermissions(CommandSender sender, CommandDescription command) {
|
||||
// Get the permissions and make sure they aren't missing
|
||||
CommandPermissions permissions = command.getCommandPermissions();
|
||||
if (permissions == null || CollectionUtils.isEmpty(permissions.getPermissionNodes())) {
|
||||
return;
|
||||
}
|
||||
|
||||
// Print the header
|
||||
sender.sendMessage(ChatColor.GOLD + "Permissions:");
|
||||
|
||||
// Print each node
|
||||
for (PermissionNode node : permissions.getPermissionNodes()) {
|
||||
boolean nodePermission = true;
|
||||
if (sender instanceof Player)
|
||||
nodePermission = AuthMe.getInstance().getPermissionsManager().hasPermission((Player) sender, node);
|
||||
final String nodePermsString = ChatColor.GRAY + (nodePermission ? ChatColor.ITALIC + " (Permission!)" : ChatColor.ITALIC + " (No Permission!)");
|
||||
sender.sendMessage(" " + ChatColor.YELLOW + ChatColor.ITALIC + node.getNode() + nodePermsString);
|
||||
}
|
||||
|
||||
// Print the default permission
|
||||
// TODO ljacqu 20151205: This is duplicating the logic in PermissionsManager#evaluateDefaultPermission
|
||||
// Either use the command manager here, or if that's too heavy, look into moving certain permissions logic
|
||||
// into a Utils class
|
||||
switch (permissions.getDefaultPermission()) {
|
||||
case ALLOWED:
|
||||
sender.sendMessage(ChatColor.GOLD + " Default: " + ChatColor.GRAY + ChatColor.ITALIC + "Permission!");
|
||||
break;
|
||||
|
||||
case OP_ONLY:
|
||||
final String defaultPermsString = ChatColor.GRAY + (sender.isOp() ? ChatColor.ITALIC + " (Permission!)" : ChatColor.ITALIC + " (No Permission!)");
|
||||
sender.sendMessage(ChatColor.GOLD + " Default: " + ChatColor.YELLOW + ChatColor.ITALIC + "OP's Only!" + defaultPermsString);
|
||||
break;
|
||||
|
||||
case NOT_ALLOWED:
|
||||
default:
|
||||
sender.sendMessage(ChatColor.GOLD + " Default: " + ChatColor.GRAY + ChatColor.ITALIC + "No Permission!");
|
||||
break;
|
||||
}
|
||||
|
||||
// Print the permission result
|
||||
if (permissions.hasPermission(sender))
|
||||
sender.sendMessage(ChatColor.GOLD + " Result: " + ChatColor.GREEN + ChatColor.ITALIC + "Permission!");
|
||||
else
|
||||
sender.sendMessage(ChatColor.GOLD + " Result: " + ChatColor.DARK_RED + ChatColor.ITALIC + "No Permission!");
|
||||
}
|
||||
|
||||
/**
|
||||
* Print the command help alternatives information if available.
|
||||
*
|
||||
* @param sender The command sender to print the help to.
|
||||
* @param command The command used.
|
||||
* @param commandReference The original command reference used for this command.
|
||||
*/
|
||||
public static void printAlternatives(CommandSender sender, CommandDescription command, CommandParts commandReference) {
|
||||
// Make sure there are any alternatives
|
||||
if (command.getLabels().size() <= 1)
|
||||
return;
|
||||
|
||||
// Print the header
|
||||
sender.sendMessage(ChatColor.GOLD + "Alternatives:");
|
||||
|
||||
// Get the label used
|
||||
final String usedLabel = commandReference.get(command.getParentCount());
|
||||
|
||||
// Create a list of alternatives
|
||||
List<String> alternatives = new ArrayList<>();
|
||||
for (String entry : command.getLabels()) {
|
||||
// Exclude the proper argument
|
||||
if (entry.equalsIgnoreCase(usedLabel))
|
||||
continue;
|
||||
alternatives.add(entry);
|
||||
}
|
||||
|
||||
// Sort the alternatives
|
||||
Collections.sort(alternatives, new Comparator<String>() {
|
||||
@Override
|
||||
public int compare(String o1, String o2) {
|
||||
return Double.compare(StringUtils.getDifference(usedLabel, o1), StringUtils.getDifference(usedLabel, o2));
|
||||
}
|
||||
});
|
||||
|
||||
// Print each alternative with proper syntax
|
||||
for (String alternative : alternatives)
|
||||
sender.sendMessage(" " + HelpSyntaxHelper.getCommandSyntax(command, commandReference, alternative, true));
|
||||
}
|
||||
|
||||
/**
|
||||
* Print the command help child's information if available.
|
||||
*
|
||||
* @param sender The command sender to print the help to.
|
||||
* @param command The command to print the help for.
|
||||
* @param commandReference The original command reference used for this command.
|
||||
*/
|
||||
public static void printChildren(CommandSender sender, CommandDescription command, CommandParts commandReference) {
|
||||
// Make sure there are child's
|
||||
if (command.getChildren().size() <= 0)
|
||||
return;
|
||||
|
||||
// Print the header
|
||||
sender.sendMessage(ChatColor.GOLD + "Commands:");
|
||||
|
||||
// Loop through each child
|
||||
for (CommandDescription child : command.getChildren())
|
||||
sender.sendMessage(" " + HelpSyntaxHelper.getCommandSyntax(child, commandReference, null, false) + ChatColor.GRAY + ChatColor.ITALIC + " : " + child.getDescription());
|
||||
}
|
||||
}
|
@ -1,79 +0,0 @@
|
||||
package fr.xephi.authme.command.help;
|
||||
|
||||
import fr.xephi.authme.command.CommandArgumentDescription;
|
||||
import fr.xephi.authme.command.CommandDescription;
|
||||
import fr.xephi.authme.command.CommandUtils;
|
||||
import fr.xephi.authme.util.CollectionUtils;
|
||||
import fr.xephi.authme.util.StringUtils;
|
||||
import org.bukkit.ChatColor;
|
||||
|
||||
import java.util.List;
|
||||
|
||||
/**
|
||||
* Helper class for formatting a command's structure (name and arguments)
|
||||
* for a Minecraft user.
|
||||
*/
|
||||
public final class HelpSyntaxHelper {
|
||||
|
||||
private HelpSyntaxHelper() {
|
||||
// Helper class
|
||||
}
|
||||
|
||||
/**
|
||||
* Get the formatted syntax for a command.
|
||||
*
|
||||
* @param commandDescription The command to build the syntax for.
|
||||
* @param commandReference The reference of the command.
|
||||
* @param alternativeLabel The alternative label to use for this command syntax.
|
||||
* @param highlight True to highlight the important parts of this command.
|
||||
*
|
||||
* @return The command with proper syntax.
|
||||
*/
|
||||
public static String getCommandSyntax(CommandDescription commandDescription, CommandParts commandReference,
|
||||
String alternativeLabel, boolean highlight) {
|
||||
// Create a string builder with white color and prefixed slash
|
||||
StringBuilder sb = new StringBuilder()
|
||||
.append(ChatColor.WHITE)
|
||||
.append("/");
|
||||
|
||||
// Get the help command reference, and the command label
|
||||
CommandParts helpCommandReference = commandDescription.getCommandReference(commandReference);
|
||||
List<String> helpLabels = helpCommandReference.getList();
|
||||
|
||||
final String parentCommand = CommandUtils.labelsToString(
|
||||
CollectionUtils.getRange(helpCommandReference.getList(), 0, helpLabels.size() - 1));
|
||||
|
||||
// Check whether the alternative label should be used
|
||||
String commandLabel;
|
||||
if (StringUtils.isEmpty(alternativeLabel)) {
|
||||
commandLabel = helpLabels.get(helpLabels.size() - 1);
|
||||
} else {
|
||||
commandLabel = alternativeLabel;
|
||||
}
|
||||
|
||||
// Show the important bit of the command, highlight this part if required
|
||||
sb.append(parentCommand)
|
||||
.append(" ")
|
||||
.append(highlight ? ChatColor.YELLOW.toString() + ChatColor.BOLD : "")
|
||||
.append(commandLabel);
|
||||
|
||||
if (highlight) {
|
||||
sb.append(ChatColor.YELLOW);
|
||||
}
|
||||
|
||||
// Add each command argument
|
||||
for (CommandArgumentDescription arg : commandDescription.getArguments()) {
|
||||
sb.append(ChatColor.ITALIC).append(formatArgument(arg));
|
||||
}
|
||||
|
||||
// Return the build command syntax
|
||||
return sb.toString();
|
||||
}
|
||||
|
||||
private static String formatArgument(CommandArgumentDescription argument) {
|
||||
if (argument.isOptional()) {
|
||||
return " [" + argument.getName() + "]";
|
||||
}
|
||||
return " <" + argument.getName() + ">";
|
||||
}
|
||||
}
|
@ -76,7 +76,7 @@ public class CommandInitializerTest {
|
||||
BiConsumer connectionTester = new BiConsumer() {
|
||||
@Override
|
||||
public void accept(CommandDescription command, int depth) {
|
||||
if (command.hasChildren()) {
|
||||
if (!command.getChildren().isEmpty()) {
|
||||
for (CommandDescription child : command.getChildren()) {
|
||||
assertThat(command.equals(child.getParent()), equalTo(true));
|
||||
}
|
||||
@ -222,7 +222,7 @@ public class CommandInitializerTest {
|
||||
public void accept(CommandDescription command, int depth) {
|
||||
// Fail if the command has children and has arguments at the same time
|
||||
// Exception: If the parent only has one child defining the help label, it is acceptable
|
||||
if (command.hasChildren() && command.hasArguments()
|
||||
if (!command.getChildren().isEmpty() && !command.getArguments().isEmpty()
|
||||
&& (command.getChildren().size() != 1 || !command.getChildren().get(0).hasLabel("help"))) {
|
||||
fail("Parent command (labels='" + command.getLabels() + "') should not have any arguments");
|
||||
}
|
||||
@ -280,7 +280,7 @@ public class CommandInitializerTest {
|
||||
private static void walkThroughCommands(Collection<CommandDescription> commands, BiConsumer consumer, int depth) {
|
||||
for (CommandDescription command : commands) {
|
||||
consumer.accept(command, depth);
|
||||
if (command.hasChildren()) {
|
||||
if (!command.getChildren().isEmpty()) {
|
||||
walkThroughCommands(command.getChildren(), consumer, depth + 1);
|
||||
}
|
||||
}
|
||||
|
@ -14,7 +14,7 @@ import org.junit.Ignore;
|
||||
import org.junit.Test;
|
||||
import org.mockito.Mockito;
|
||||
|
||||
import java.util.Collections;
|
||||
import java.util.ArrayList;
|
||||
|
||||
import static org.hamcrest.Matchers.equalTo;
|
||||
import static org.junit.Assert.assertThat;
|
||||
@ -41,10 +41,9 @@ public class CaptchaCommandTest {
|
||||
ExecutableCommand command = new CaptchaCommand();
|
||||
|
||||
// when
|
||||
boolean result = command.executeCommand(sender, new CommandParts(Collections.EMPTY_LIST), new CommandParts(Collections.EMPTY_LIST));
|
||||
command.executeCommand(sender, new ArrayList<String>());
|
||||
|
||||
// then
|
||||
assertThat(result, equalTo(true));
|
||||
assertThat(wrapperMock.wasMockCalled(AuthMe.class), equalTo(false));
|
||||
assertThat(wrapperMock.wasMockCalled(Messages.class), equalTo(false));
|
||||
}
|
||||
@ -57,10 +56,9 @@ public class CaptchaCommandTest {
|
||||
ExecutableCommand command = new CaptchaCommand();
|
||||
|
||||
// when
|
||||
boolean result = command.executeCommand(player, new CommandParts(Collections.EMPTY_LIST), new CommandParts(Collections.EMPTY_LIST));
|
||||
command.executeCommand(player, new ArrayList<String>());
|
||||
|
||||
// then
|
||||
assertThat(result, equalTo(true));
|
||||
verify(wrapperMock.getMessages()).send(player, MessageKey.USAGE_LOGIN);
|
||||
}
|
||||
|
||||
|
@ -16,14 +16,19 @@ import org.junit.Before;
|
||||
import org.junit.Test;
|
||||
import org.mockito.ArgumentCaptor;
|
||||
|
||||
import java.util.ArrayList;
|
||||
import java.util.Arrays;
|
||||
import java.util.Collections;
|
||||
|
||||
import static java.util.Arrays.asList;
|
||||
import static org.hamcrest.MatcherAssert.assertThat;
|
||||
import static org.hamcrest.Matchers.equalTo;
|
||||
import static org.mockito.Matchers.anyInt;
|
||||
import static org.mockito.Mockito.*;
|
||||
import static org.mockito.Mockito.any;
|
||||
import static org.mockito.Mockito.eq;
|
||||
import static org.mockito.Mockito.mock;
|
||||
import static org.mockito.Mockito.never;
|
||||
import static org.mockito.Mockito.verify;
|
||||
import static org.mockito.Mockito.when;
|
||||
|
||||
/**
|
||||
* Test for {@link ChangePasswordCommand}.
|
||||
@ -52,13 +57,11 @@ public class ChangePasswordCommandTest {
|
||||
// given
|
||||
CommandSender sender = mock(BlockCommandSender.class);
|
||||
ChangePasswordCommand command = new ChangePasswordCommand();
|
||||
CommandParts arguments = mock(CommandParts.class);
|
||||
|
||||
// when
|
||||
command.executeCommand(sender, newParts(), arguments);
|
||||
command.executeCommand(sender, new ArrayList<String>());
|
||||
|
||||
// then
|
||||
verify(arguments, never()).get(anyInt());
|
||||
assertThat(wrapperMock.wasMockCalled(Server.class), equalTo(false));
|
||||
}
|
||||
|
||||
@ -69,7 +72,7 @@ public class ChangePasswordCommandTest {
|
||||
ChangePasswordCommand command = new ChangePasswordCommand();
|
||||
|
||||
// when
|
||||
command.executeCommand(sender, newParts(), new CommandParts("pass"));
|
||||
command.executeCommand(sender, Collections.singletonList("pass"));
|
||||
|
||||
// then
|
||||
verify(messagesMock).send(sender, MessageKey.NOT_LOGGED_IN);
|
||||
@ -83,7 +86,7 @@ public class ChangePasswordCommandTest {
|
||||
ChangePasswordCommand command = new ChangePasswordCommand();
|
||||
|
||||
// when
|
||||
command.executeCommand(sender, newParts(), newParts("old123", "!pass"));
|
||||
command.executeCommand(sender, Arrays.asList("old123", "!pass"));
|
||||
|
||||
// then
|
||||
verify(messagesMock).send(sender, MessageKey.PASSWORD_MATCH_ERROR);
|
||||
@ -98,7 +101,7 @@ public class ChangePasswordCommandTest {
|
||||
ChangePasswordCommand command = new ChangePasswordCommand();
|
||||
|
||||
// when
|
||||
command.executeCommand(sender, newParts(), newParts("old_", "Tester"));
|
||||
command.executeCommand(sender, Arrays.asList("old_", "Tester"));
|
||||
|
||||
// then
|
||||
verify(messagesMock).send(sender, MessageKey.PASSWORD_IS_USERNAME_ERROR);
|
||||
@ -113,7 +116,7 @@ public class ChangePasswordCommandTest {
|
||||
Settings.passwordMaxLength = 3;
|
||||
|
||||
// when
|
||||
command.executeCommand(sender, newParts(), newParts("12", "test"));
|
||||
command.executeCommand(sender, Arrays.asList("12", "test"));
|
||||
|
||||
// then
|
||||
verify(messagesMock).send(sender, MessageKey.INVALID_PASSWORD_LENGTH);
|
||||
@ -128,7 +131,7 @@ public class ChangePasswordCommandTest {
|
||||
Settings.getPasswordMinLen = 7;
|
||||
|
||||
// when
|
||||
command.executeCommand(sender, newParts(), newParts("oldverylongpassword", "tester"));
|
||||
command.executeCommand(sender, Arrays.asList("oldverylongpassword", "tester"));
|
||||
|
||||
// then
|
||||
verify(messagesMock).send(sender, MessageKey.INVALID_PASSWORD_LENGTH);
|
||||
@ -143,7 +146,7 @@ public class ChangePasswordCommandTest {
|
||||
Settings.unsafePasswords = asList("test", "abc123");
|
||||
|
||||
// when
|
||||
command.executeCommand(sender, newParts(), newParts("oldpw", "abc123"));
|
||||
command.executeCommand(sender, Arrays.asList("oldpw", "abc123"));
|
||||
|
||||
// then
|
||||
verify(messagesMock).send(sender, MessageKey.PASSWORD_UNSAFE_ERROR);
|
||||
@ -157,7 +160,7 @@ public class ChangePasswordCommandTest {
|
||||
ChangePasswordCommand command = new ChangePasswordCommand();
|
||||
|
||||
// when
|
||||
command.executeCommand(sender, newParts(), newParts("abc123", "abc123"));
|
||||
command.executeCommand(sender, Arrays.asList("abc123", "abc123"));
|
||||
|
||||
// then
|
||||
verify(messagesMock, never()).send(eq(sender), any(MessageKey.class));
|
||||
@ -175,8 +178,4 @@ public class ChangePasswordCommandTest {
|
||||
return player;
|
||||
}
|
||||
|
||||
private static CommandParts newParts(String... parts) {
|
||||
return new CommandParts(Arrays.asList(parts));
|
||||
}
|
||||
|
||||
}
|
||||
|
@ -11,7 +11,6 @@ import org.junit.Test;
|
||||
import org.mockito.Mockito;
|
||||
|
||||
import java.util.ArrayList;
|
||||
import java.util.Arrays;
|
||||
|
||||
import static org.mockito.Mockito.never;
|
||||
import static org.mockito.Mockito.verify;
|
||||
@ -40,7 +39,7 @@ public class AddEmailCommandTest {
|
||||
AddEmailCommand command = new AddEmailCommand();
|
||||
|
||||
// when
|
||||
command.executeCommand(sender, newParts(), newParts());
|
||||
command.executeCommand(sender, new ArrayList<String>());
|
||||
|
||||
// then
|
||||
verify(authMeMock, never()).getManagement();
|
||||
@ -53,15 +52,11 @@ public class AddEmailCommandTest {
|
||||
AddEmailCommand command = new AddEmailCommand();
|
||||
|
||||
// when
|
||||
command.executeCommand(sender, newParts(),
|
||||
new CommandParts(Arrays.asList("mail@example", "other_example")));
|
||||
command.executeCommand(sender, new ArrayList<String>());
|
||||
|
||||
// then
|
||||
verify(authMeMock).getManagement();
|
||||
verify(managementMock).performAddEmail(sender, "mail@example", "other_example");
|
||||
}
|
||||
|
||||
private static CommandParts newParts() {
|
||||
return new CommandParts(new ArrayList<String>());
|
||||
}
|
||||
}
|
||||
|
@ -40,7 +40,7 @@ public class ChangeEmailCommandTest {
|
||||
ChangeEmailCommand command = new ChangeEmailCommand();
|
||||
|
||||
// when
|
||||
command.executeCommand(sender, newParts(), newParts());
|
||||
command.executeCommand(sender, new ArrayList<String>());
|
||||
|
||||
// then
|
||||
verify(authMeMock, never()).getManagement();
|
||||
@ -53,15 +53,11 @@ public class ChangeEmailCommandTest {
|
||||
ChangeEmailCommand command = new ChangeEmailCommand();
|
||||
|
||||
// when
|
||||
command.executeCommand(sender, newParts(),
|
||||
new CommandParts(Arrays.asList("new.mail@example.org", "old_mail@example.org")));
|
||||
command.executeCommand(sender, Arrays.asList("new.mail@example.org", "old_mail@example.org"));
|
||||
|
||||
// then
|
||||
verify(authMeMock).getManagement();
|
||||
verify(managementMock).performChangeEmail(sender, "new.mail@example.org", "old_mail@example.org");
|
||||
}
|
||||
|
||||
private static CommandParts newParts() {
|
||||
return new CommandParts(new ArrayList<String>());
|
||||
}
|
||||
}
|
||||
|
@ -8,7 +8,7 @@ import org.junit.Ignore;
|
||||
import org.junit.Test;
|
||||
import org.mockito.Mockito;
|
||||
|
||||
import java.util.Collections;
|
||||
import java.util.ArrayList;
|
||||
|
||||
/**
|
||||
* Test for {@link RecoverEmailCommand}.
|
||||
@ -28,7 +28,7 @@ public class RecoverEmailCommandTest {
|
||||
RecoverEmailCommand command = new RecoverEmailCommand();
|
||||
|
||||
// when
|
||||
command.executeCommand(sender, new CommandParts(Collections.EMPTY_LIST), new CommandParts(Collections.EMPTY_LIST));
|
||||
command.executeCommand(sender, new ArrayList<String>());
|
||||
|
||||
// then
|
||||
}
|
||||
|
@ -11,6 +11,7 @@ import org.junit.Test;
|
||||
import org.mockito.Mockito;
|
||||
|
||||
import java.util.ArrayList;
|
||||
import java.util.Collections;
|
||||
|
||||
import static org.mockito.Matchers.*;
|
||||
import static org.mockito.Mockito.mock;
|
||||
@ -39,7 +40,7 @@ public class LoginCommandTest {
|
||||
LoginCommand command = new LoginCommand();
|
||||
|
||||
// when
|
||||
command.executeCommand(sender, newParts(), newParts());
|
||||
command.executeCommand(sender, new ArrayList<String>());
|
||||
|
||||
// then
|
||||
Mockito.verify(managementMock, never()).performLogin(any(Player.class), anyString(), anyBoolean());
|
||||
@ -52,7 +53,7 @@ public class LoginCommandTest {
|
||||
LoginCommand command = new LoginCommand();
|
||||
|
||||
// when
|
||||
command.executeCommand(sender, newParts(), new CommandParts("password"));
|
||||
command.executeCommand(sender, Collections.singletonList("password"));
|
||||
|
||||
// then
|
||||
Mockito.verify(managementMock).performLogin(eq(sender), eq("password"), eq(false));
|
||||
@ -65,7 +66,7 @@ public class LoginCommandTest {
|
||||
LoginCommand command = new LoginCommand();
|
||||
|
||||
// when
|
||||
command.executeCommand(sender, newParts(), newParts());
|
||||
command.executeCommand(sender, new ArrayList<String>());
|
||||
|
||||
// then
|
||||
// TODO ljacqu 20151121: May make sense to handle null password in LoginCommand instead of forwarding the call
|
||||
@ -73,7 +74,4 @@ public class LoginCommandTest {
|
||||
Mockito.verify(managementMock).performLogin(eq(sender), eq(password), eq(false));
|
||||
}
|
||||
|
||||
private static CommandParts newParts() {
|
||||
return new CommandParts(new ArrayList<String>());
|
||||
}
|
||||
}
|
||||
|
@ -12,6 +12,7 @@ import org.junit.Test;
|
||||
import org.mockito.Mockito;
|
||||
|
||||
import java.util.ArrayList;
|
||||
import java.util.Collections;
|
||||
|
||||
import static org.mockito.Matchers.any;
|
||||
import static org.mockito.Mockito.mock;
|
||||
@ -41,7 +42,7 @@ public class LogoutCommandTest {
|
||||
LogoutCommand command = new LogoutCommand();
|
||||
|
||||
// when
|
||||
command.executeCommand(sender, new CommandParts(new ArrayList<String>()), new CommandParts(new ArrayList<String>()));
|
||||
command.executeCommand(sender, new ArrayList<String>());
|
||||
|
||||
// then
|
||||
Mockito.verify(managementMock, never()).performLogout(any(Player.class));
|
||||
@ -54,7 +55,7 @@ public class LogoutCommandTest {
|
||||
LogoutCommand command = new LogoutCommand();
|
||||
|
||||
// when
|
||||
command.executeCommand(sender, new CommandParts(new ArrayList<String>()), new CommandParts("password"));
|
||||
command.executeCommand(sender, Collections.singletonList("password"));
|
||||
|
||||
// then
|
||||
Mockito.verify(managementMock).performLogout(sender);
|
||||
|
@ -14,6 +14,7 @@ import org.mockito.ArgumentCaptor;
|
||||
import org.mockito.Mockito;
|
||||
|
||||
import java.util.ArrayList;
|
||||
import java.util.Collections;
|
||||
|
||||
import static org.hamcrest.Matchers.equalTo;
|
||||
import static org.junit.Assert.assertThat;
|
||||
@ -49,7 +50,7 @@ public class RegisterCommandTest {
|
||||
ArgumentCaptor<String> messageCaptor = ArgumentCaptor.forClass(String.class);
|
||||
|
||||
// when
|
||||
command.executeCommand(sender, newParts(), newParts());
|
||||
command.executeCommand(sender, new ArrayList<String>());
|
||||
|
||||
// then
|
||||
verify(sender).sendMessage(messageCaptor.capture());
|
||||
@ -64,7 +65,7 @@ public class RegisterCommandTest {
|
||||
RegisterCommand command = new RegisterCommand();
|
||||
|
||||
// when
|
||||
command.executeCommand(sender, newParts(), newParts());
|
||||
command.executeCommand(sender, new ArrayList<String>());
|
||||
|
||||
// then
|
||||
verify(messagesMock).send(sender, MessageKey.USAGE_REGISTER);
|
||||
@ -78,13 +79,10 @@ public class RegisterCommandTest {
|
||||
RegisterCommand command = new RegisterCommand();
|
||||
|
||||
// when
|
||||
command.executeCommand(sender, newParts(), new CommandParts("password"));
|
||||
command.executeCommand(sender, Collections.singletonList("password"));
|
||||
|
||||
// then
|
||||
verify(managementMock).performRegister(sender, "password", "");
|
||||
}
|
||||
|
||||
private static CommandParts newParts() {
|
||||
return new CommandParts(new ArrayList<String>());
|
||||
}
|
||||
}
|
||||
|
@ -1,131 +0,0 @@
|
||||
package fr.xephi.authme.command.help;
|
||||
|
||||
import fr.xephi.authme.command.CommandDescription;
|
||||
import fr.xephi.authme.command.ExecutableCommand;
|
||||
import fr.xephi.authme.command.executable.authme.RegisterAdminCommand;
|
||||
import org.junit.Test;
|
||||
import org.mockito.Mockito;
|
||||
|
||||
import java.util.ArrayList;
|
||||
|
||||
import static org.bukkit.ChatColor.BOLD;
|
||||
import static org.bukkit.ChatColor.ITALIC;
|
||||
import static org.bukkit.ChatColor.WHITE;
|
||||
import static org.bukkit.ChatColor.YELLOW;
|
||||
import static org.hamcrest.Matchers.equalTo;
|
||||
import static org.junit.Assert.assertThat;
|
||||
|
||||
/**
|
||||
* Test for {@link HelpSyntaxHelper}.
|
||||
*/
|
||||
public class HelpSyntaxHelperTest {
|
||||
|
||||
@Test
|
||||
public void shouldFormatSimpleCommand() {
|
||||
// given
|
||||
CommandDescription description = getDescriptionBuilder()
|
||||
.withArgument("name", "The name", true)
|
||||
.build();
|
||||
|
||||
// when
|
||||
String result = HelpSyntaxHelper.getCommandSyntax(description, newParts(), "", false);
|
||||
|
||||
// then
|
||||
assertThat(result, equalTo(WHITE + "/authme register" + ITALIC + " [name]"));
|
||||
}
|
||||
|
||||
@Test
|
||||
public void shouldFormatSimpleCommandWithOptionalParam() {
|
||||
// given
|
||||
CommandDescription description = getDescriptionBuilder()
|
||||
.withArgument("test", "", false)
|
||||
.build();
|
||||
|
||||
// when
|
||||
String result = HelpSyntaxHelper.getCommandSyntax(description, newParts(), null, false);
|
||||
|
||||
// then
|
||||
assertThat(result, equalTo(WHITE + "/authme register" + ITALIC + " <test>"));
|
||||
}
|
||||
|
||||
@Test
|
||||
public void shouldFormatCommandWithMultipleParams() {
|
||||
// given
|
||||
CommandDescription description = getDescriptionBuilder()
|
||||
.withArgument("name", "", true)
|
||||
.withArgument("test", "", false)
|
||||
.build();
|
||||
|
||||
// when
|
||||
String result = HelpSyntaxHelper.getCommandSyntax(description, newParts(), "", false);
|
||||
|
||||
// then
|
||||
assertThat(result, equalTo(WHITE + "/authme register" + ITALIC + " [name]" + ITALIC + " <test>"));
|
||||
}
|
||||
|
||||
@Test
|
||||
public void shouldHighlightCommandWithMultipleParams() {
|
||||
// given
|
||||
CommandDescription description = getDescriptionBuilder()
|
||||
.withArgument("name", "", true)
|
||||
.withArgument("test", "", false)
|
||||
.build();
|
||||
|
||||
// when
|
||||
String result = HelpSyntaxHelper.getCommandSyntax(description, newParts(), "", true);
|
||||
|
||||
// then
|
||||
assertThat(result, equalTo(WHITE + "/authme "
|
||||
+ YELLOW + BOLD + "register"
|
||||
+ YELLOW + ITALIC + " [name]" + ITALIC + " <test>"));
|
||||
}
|
||||
|
||||
@Test
|
||||
public void shouldHighlightCommandWithNoParams() {
|
||||
// given
|
||||
CommandDescription description = getDescriptionBuilder().build();
|
||||
|
||||
// when
|
||||
String result = HelpSyntaxHelper.getCommandSyntax(description, newParts(), null, true);
|
||||
|
||||
// then
|
||||
assertThat(result, equalTo(WHITE + "/authme " + YELLOW + BOLD + "register" + YELLOW));
|
||||
}
|
||||
|
||||
@Test
|
||||
public void shouldFormatSimpleCommandWithAlternativeLabel() {
|
||||
// given
|
||||
CommandDescription description = getDescriptionBuilder()
|
||||
.withArgument("name", "The name", true)
|
||||
.build();
|
||||
|
||||
// when
|
||||
String result = HelpSyntaxHelper.getCommandSyntax(description, newParts(), "alt", false);
|
||||
|
||||
// then
|
||||
assertThat(result, equalTo(WHITE + "/authme alt" + ITALIC + " [name]"));
|
||||
}
|
||||
|
||||
private static CommandParts newParts() {
|
||||
// TODO ljacqu 20151204: Remove this method once CommandParts has been removed
|
||||
return new CommandParts(new ArrayList<String>());
|
||||
}
|
||||
|
||||
private static CommandDescription.CommandBuilder getDescriptionBuilder() {
|
||||
CommandDescription base = CommandDescription.builder()
|
||||
.labels("authme")
|
||||
.description("Base command")
|
||||
.detailedDescription("AuthMe base command")
|
||||
.parent(null)
|
||||
.executableCommand(Mockito.mock(ExecutableCommand.class))
|
||||
.build();
|
||||
|
||||
return CommandDescription.builder()
|
||||
.executableCommand(Mockito.mock(RegisterAdminCommand.class))
|
||||
.labels("register", "r")
|
||||
.description("Register a player")
|
||||
.detailedDescription("Register the specified player with the specified password.")
|
||||
.parent(base)
|
||||
.executableCommand(Mockito.mock(ExecutableCommand.class));
|
||||
}
|
||||
}
|
Loading…
Reference in New Issue
Block a user