#306 Add command service and set up constructor dependency injection

(work in progress)
- Pass all dependencies via constructor
- Encapsulate command handling more (e.g. split CommandHandler with new CommandMapper)
- Add help command to all base commands at one central point

See AccountsCommand or HelpCommand for an example of the advantages - all necessary functions come from CommandService; objects aren't retrieved through a singleton getInstance() method anymore
This commit is contained in:
ljacqu
2015-12-23 14:54:45 +01:00
parent f785d9d357
commit 8ef1b2ae3e
14 changed files with 517 additions and 471 deletions
@@ -1,13 +1,11 @@
package fr.xephi.authme.command.executable;
import fr.xephi.authme.command.CommandHandler;
import fr.xephi.authme.command.CommandService;
import fr.xephi.authme.command.CommandUtils;
import fr.xephi.authme.command.ExecutableCommand;
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;
@@ -16,41 +14,33 @@ import java.util.List;
import static fr.xephi.authme.command.FoundResultStatus.MISSING_BASE_COMMAND;
import static fr.xephi.authme.command.FoundResultStatus.UNKNOWN_LABEL;
public class HelpCommand extends ExecutableCommand {
public class HelpCommand implements 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 void executeCommand(CommandSender sender, List<String> arguments) {
// TODO #306 ljacqu 20151213: Get command handler from non-static context
CommandHandler commandHandler = Wrapper.getInstance().getAuthMe().getCommandHandler();
FoundCommandResult foundCommandResult = commandHandler.mapPartsToCommand(arguments);
public void executeCommand(CommandSender sender, List<String> arguments, CommandService commandService) {
FoundCommandResult result = commandService.mapPartsToCommand(sender, arguments);
// 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.
FoundResultStatus resultStatus = foundCommandResult.getResultStatus();
FoundResultStatus resultStatus = result.getResultStatus();
if (MISSING_BASE_COMMAND.equals(resultStatus)) {
sender.sendMessage(ChatColor.DARK_RED + "Could not get base command");
return;
} else if (UNKNOWN_LABEL.equals(resultStatus)) {
if (foundCommandResult.getCommandDescription() == null) {
if (result.getCommandDescription() == null) {
sender.sendMessage(ChatColor.DARK_RED + "Unknown command");
return;
} else {
sender.sendMessage(ChatColor.GOLD + "Assuming " + ChatColor.WHITE
+ CommandUtils.constructCommandPath(foundCommandResult.getCommandDescription()));
+ CommandUtils.constructCommandPath(result.getCommandDescription()));
}
}
PermissionsManager permissionsManager = Wrapper.getInstance().getAuthMe().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);
if (arguments.size() == 1) {
commandService.outputHelp(sender, result, HelpProvider.SHOW_CHILDREN);
} else {
commandService.outputHelp(sender, result, HelpProvider.ALL_OPTIONS);
}
}
}
@@ -1,88 +1,65 @@
package fr.xephi.authme.command.executable.authme;
import fr.xephi.authme.AuthMe;
import fr.xephi.authme.cache.auth.PlayerAuth;
import fr.xephi.authme.command.CommandService;
import fr.xephi.authme.command.ExecutableCommand;
import fr.xephi.authme.output.MessageKey;
import fr.xephi.authme.output.Messages;
import fr.xephi.authme.util.StringUtils;
import org.bukkit.command.CommandSender;
import java.util.List;
public class AccountsCommand extends ExecutableCommand {
/**
* Shows all accounts registered by the same IP address for the given player name or IP address.
*/
public class AccountsCommand implements ExecutableCommand {
@Override
public void executeCommand(final CommandSender sender, List<String> arguments) {
final AuthMe plugin = AuthMe.getInstance();
final Messages m = plugin.getMessages();
public void executeCommand(final CommandSender sender, List<String> arguments,
final CommandService commandService) {
final String playerName = arguments.isEmpty() ? sender.getName() : arguments.get(0);
// Command logic
// Assumption: a player name cannot contain '.'
if (!playerName.contains(".")) {
plugin.getServer().getScheduler().runTaskAsynchronously(plugin, new Runnable() {
commandService.runTaskAsynchronously(new Runnable() {
@Override
public void run() {
PlayerAuth auth = plugin.database.getAuth(playerName.toLowerCase());
PlayerAuth auth = commandService.getDataSource().getAuth(playerName.toLowerCase());
if (auth == null) {
m.send(sender, MessageKey.UNKNOWN_USER);
commandService.send(sender, MessageKey.UNKNOWN_USER);
return;
}
StringBuilder message = new StringBuilder("[AuthMe] ");
List<String> accountList = plugin.database.getAllAuthsByName(auth);
List<String> accountList = commandService.getDataSource().getAllAuthsByName(auth);
if (accountList.isEmpty()) {
m.send(sender, MessageKey.USER_NOT_REGISTERED);
return;
}
if (accountList.size() == 1) {
commandService.send(sender, MessageKey.USER_NOT_REGISTERED);
} else if (accountList.size() == 1) {
sender.sendMessage("[AuthMe] " + playerName + " is a single account player");
return;
} else {
outputAccountsList(sender, playerName, accountList);
}
int i = 0;
for (String account : accountList) {
i++;
message.append(account);
if (i != accountList.size()) {
message.append(", ");
} else {
message.append('.');
}
}
sender.sendMessage("[AuthMe] " + playerName + " has "
+ String.valueOf(accountList.size()) + " accounts.");
sender.sendMessage(message.toString());
}
});
return;
} else {
plugin.getServer().getScheduler().runTaskAsynchronously(plugin, new Runnable() {
commandService.runTaskAsynchronously(new Runnable() {
@Override
public void run() {
List<String> accountList = plugin.database.getAllAuthsByIp(playerName);
StringBuilder message = new StringBuilder("[AuthMe] ");
List<String> accountList = commandService.getDataSource().getAllAuthsByIp(playerName);
if (accountList.isEmpty()) {
sender.sendMessage("[AuthMe] This IP does not exist in the database.");
return;
}
if (accountList.size() == 1) {
} else if (accountList.size() == 1) {
sender.sendMessage("[AuthMe] " + playerName + " is a single account player");
return;
} else {
outputAccountsList(sender, playerName, accountList);
}
int i = 0;
for (String account : accountList) {
i++;
message.append(account);
if (i != accountList.size()) {
message.append(", ");
} else {
message.append('.');
}
}
sender.sendMessage("[AuthMe] " + playerName + " has "
+ String.valueOf(accountList.size()) + " accounts.");
sender.sendMessage(message.toString());
}
});
}
}
private static void outputAccountsList(CommandSender sender, String playerName, List<String> accountList) {
sender.sendMessage("[AuthMe] " + playerName + " has " + accountList.size() + " accounts.");
String message = "[AuthMe] " + StringUtils.join(", ", accountList) + ".";
sender.sendMessage(message);
}
}
@@ -1,21 +1,17 @@
package fr.xephi.authme.command.executable.authme;
import fr.xephi.authme.AntiBot;
import fr.xephi.authme.AuthMe;
import fr.xephi.authme.command.CommandHandler;
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;
public class SwitchAntiBotCommand extends ExecutableCommand {
public class SwitchAntiBotCommand implements ExecutableCommand {
@Override
public void executeCommand(final CommandSender sender, List<String> arguments) {
public void executeCommand(final CommandSender sender, List<String> arguments, CommandService commandService) {
if (arguments.isEmpty()) {
sender.sendMessage("[AuthMe] AntiBot status: " + AntiBot.getAntiBotStatus().name());
return;
@@ -32,12 +28,13 @@ public class SwitchAntiBotCommand extends ExecutableCommand {
sender.sendMessage("[AuthMe] AntiBotMod Manual Override: disabled!");
} else {
sender.sendMessage(ChatColor.DARK_RED + "Invalid AntiBot mode!");
// TODO ljacqu 20151213: Fix static retrieval of command handler
// 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");
sender.sendMessage(ChatColor.GOLD + "Detailed help: " + ChatColor.WHITE + "/authme help antibot");*/
}
}
}
@@ -0,0 +1,19 @@
package fr.xephi.authme.command.executable.email;
import fr.xephi.authme.command.CommandService;
import fr.xephi.authme.command.ExecutableCommand;
import org.bukkit.command.CommandSender;
import java.util.List;
/**
* Base command for /email, showing information about the child commands.
*/
public class EmailBaseCommand implements ExecutableCommand {
@Override
public void executeCommand(CommandSender sender, List<String> arguments, CommandService commandService) {
// FIXME #306 use getCommandService().getHelpProvider();
// FIXME #306 HelpProvider.printHelp()
}
}