#306 Create PlayerCommand abstract type

- Create PlayerCommand to handle player-only command checks centrally and consistently
- Adjust tests
This commit is contained in:
ljacqu
2015-12-26 19:47:47 +01:00
parent 74ceb66430
commit 27fee3ca64
25 changed files with 250 additions and 393 deletions
@@ -3,54 +3,36 @@ package fr.xephi.authme.command.executable.captcha;
import fr.xephi.authme.AuthMe;
import fr.xephi.authme.cache.auth.PlayerCache;
import fr.xephi.authme.command.CommandService;
import fr.xephi.authme.command.ExecutableCommand;
import fr.xephi.authme.command.PlayerCommand;
import fr.xephi.authme.output.MessageKey;
import fr.xephi.authme.output.Messages;
import fr.xephi.authme.security.RandomString;
import fr.xephi.authme.settings.Settings;
import fr.xephi.authme.util.Wrapper;
import org.bukkit.command.CommandSender;
import org.bukkit.entity.Player;
import java.util.List;
public class CaptchaCommand implements ExecutableCommand {
public class CaptchaCommand extends PlayerCommand {
@Override
public void executeCommand(CommandSender sender, List<String> arguments, CommandService commandService) {
// Make sure the current command executor is a player
if (!(sender instanceof Player)) {
return;
}
// Get the player instance and name
final Player player = (Player) sender;
public void runCommand(Player player, List<String> arguments, CommandService commandService) {
final String playerNameLowerCase = player.getName().toLowerCase();
// Get the parameter values
String captcha = arguments.get(0);
// AuthMe plugin instance
final Wrapper wrapper = Wrapper.getInstance();
final AuthMe plugin = wrapper.getAuthMe();
// Messages instance
final Messages m = wrapper.getMessages();
final String captcha = arguments.get(0);
final AuthMe plugin = AuthMe.getInstance();
// Command logic
if (PlayerCache.getInstance().isAuthenticated(playerNameLowerCase)) {
m.send(player, MessageKey.ALREADY_LOGGED_IN_ERROR);
commandService.send(player, MessageKey.ALREADY_LOGGED_IN_ERROR);
return;
}
if (!Settings.useCaptcha) {
m.send(player, MessageKey.USAGE_LOGIN);
commandService.send(player, MessageKey.USAGE_LOGIN);
return;
}
if (!plugin.cap.containsKey(playerNameLowerCase)) {
m.send(player, MessageKey.USAGE_LOGIN);
commandService.send(player, MessageKey.USAGE_LOGIN);
return;
}
@@ -58,7 +40,7 @@ public class CaptchaCommand implements ExecutableCommand {
plugin.cap.remove(playerNameLowerCase);
String randStr = new RandomString(Settings.captchaLength).nextString();
plugin.cap.put(playerNameLowerCase, randStr);
m.send(player, MessageKey.CAPTCHA_WRONG_ERROR, plugin.cap.get(playerNameLowerCase));
commandService.send(player, MessageKey.CAPTCHA_WRONG_ERROR, plugin.cap.get(playerNameLowerCase));
return;
}
@@ -66,7 +48,7 @@ public class CaptchaCommand implements ExecutableCommand {
plugin.cap.remove(playerNameLowerCase);
// Show a status message
m.send(player, MessageKey.CAPTCHA_SUCCESS);
m.send(player, MessageKey.LOGIN_MESSAGE);
commandService.send(player, MessageKey.CAPTCHA_SUCCESS);
commandService.send(player, MessageKey.LOGIN_MESSAGE);
}
}