Get player via BukkitService; add unit tests for commands

This commit is contained in:
ljacqu
2016-04-08 19:56:44 +02:00
parent 0cda9a7698
commit e2b50b72a5
14 changed files with 525 additions and 44 deletions
@@ -14,8 +14,10 @@ import fr.xephi.authme.security.PasswordSecurity;
import fr.xephi.authme.settings.NewSetting;
import fr.xephi.authme.settings.SpawnLoader;
import fr.xephi.authme.settings.domain.Property;
import fr.xephi.authme.util.BukkitService;
import fr.xephi.authme.util.ValidationService;
import org.bukkit.command.CommandSender;
import org.bukkit.entity.Player;
import java.util.List;
@@ -36,13 +38,15 @@ public class CommandService {
private final SpawnLoader spawnLoader;
private final AntiBot antiBot;
private final ValidationService validationService;
private final BukkitService bukkitService;
/*
* Constructor.
*/
public CommandService(AuthMe authMe, CommandMapper commandMapper, HelpProvider helpProvider, Messages messages,
PasswordSecurity passwordSecurity, PermissionsManager permissionsManager, NewSetting settings,
PluginHooks pluginHooks, SpawnLoader spawnLoader, AntiBot antiBot, ValidationService validationService) {
PluginHooks pluginHooks, SpawnLoader spawnLoader, AntiBot antiBot,
ValidationService validationService, BukkitService bukkitService) {
this.authMe = authMe;
this.messages = messages;
this.helpProvider = helpProvider;
@@ -54,6 +58,7 @@ public class CommandService {
this.spawnLoader = spawnLoader;
this.antiBot = antiBot;
this.validationService = validationService;
this.bukkitService = bukkitService;
}
/**
@@ -222,4 +227,8 @@ public class CommandService {
return validationService.isEmailFreeForRegistration(email, sender);
}
public Player getPlayer(String name) {
return bukkitService.getPlayerExact(name);
}
}
@@ -2,13 +2,13 @@ package fr.xephi.authme.command.executable.authme;
import fr.xephi.authme.command.CommandService;
import fr.xephi.authme.command.ExecutableCommand;
import fr.xephi.authme.permission.PlayerPermission;
import fr.xephi.authme.util.Utils;
import org.bukkit.command.CommandSender;
import org.bukkit.entity.Player;
import java.util.List;
import static fr.xephi.authme.permission.PlayerPermission.CAN_LOGIN_BE_FORCED;
/**
* Forces the login of a player, i.e. logs the player in without the need of a (correct) password.
*/
@@ -19,15 +19,14 @@ public class ForceLoginCommand implements ExecutableCommand {
// Get the player query
String playerName = arguments.isEmpty() ? sender.getName() : arguments.get(0);
Player player = Utils.getPlayer(playerName);
Player player = commandService.getPlayer(playerName);
if (player == null || !player.isOnline()) {
sender.sendMessage("Player needs to be online!");
} else if (!commandService.getPermissionsManager()
.hasPermission(player, PlayerPermission.CAN_LOGIN_BE_FORCED)) {
sender.sendMessage("You cannot force login for the player " + playerName + "!");
} else if (!commandService.getPermissionsManager().hasPermission(player, CAN_LOGIN_BE_FORCED)) {
sender.sendMessage("You cannot force login the player " + playerName + "!");
} else {
commandService.getManagement().performLogin(player, "dontneed", true);
sender.sendMessage("Force Login for " + playerName + " performed!");
sender.sendMessage("Force login for " + playerName + " performed!");
}
}
}
@@ -2,8 +2,6 @@ package fr.xephi.authme.command.executable.authme;
import fr.xephi.authme.command.CommandService;
import fr.xephi.authme.command.ExecutableCommand;
import fr.xephi.authme.settings.properties.HooksSettings;
import fr.xephi.authme.util.Utils;
import org.bukkit.command.CommandSender;
import org.bukkit.entity.Player;
@@ -14,9 +12,9 @@ public class GetIpCommand implements ExecutableCommand {
@Override
public void executeCommand(CommandSender sender, List<String> arguments, CommandService commandService) {
// Get the player query
String playerName = (arguments.size() >= 1) ? arguments.get(0) : sender.getName();
String playerName = arguments.get(0);
Player player = Utils.getPlayer(playerName);
Player player = commandService.getPlayer(playerName);
if (player == null) {
sender.sendMessage("The player is not online");
return;
@@ -24,6 +22,5 @@ public class GetIpCommand implements ExecutableCommand {
sender.sendMessage(player.getName() + "'s IP is: " + player.getAddress().getAddress().getHostAddress()
+ ":" + player.getAddress().getPort());
}
}
@@ -6,8 +6,8 @@ import fr.xephi.authme.command.CommandService;
import fr.xephi.authme.command.ExecutableCommand;
import fr.xephi.authme.output.MessageKey;
import fr.xephi.authme.security.crypts.HashedPassword;
import org.bukkit.Bukkit;
import org.bukkit.command.CommandSender;
import org.bukkit.entity.Player;
import java.util.List;
@@ -52,11 +52,12 @@ public class RegisterAdminCommand implements ExecutableCommand {
return;
}
commandService.getDataSource().setUnlogged(playerNameLowerCase);
if (Bukkit.getPlayerExact(playerName) != null) {
Bukkit.getPlayerExact(playerName).kickPlayer("An admin just registered you, please log again");
} else {
commandService.send(sender, MessageKey.REGISTER_SUCCESS);
ConsoleLogger.info(playerName + " registered");
commandService.send(sender, MessageKey.REGISTER_SUCCESS);
ConsoleLogger.info(sender.getName() + " registered " + playerName);
Player player = commandService.getPlayer(playerName);
if (player != null) {
player.kickPlayer("An admin just registered you, please log in again");
}
}
});
@@ -49,7 +49,7 @@ public class UnregisterAdminCommand implements ExecutableCommand {
}
// Unregister the player
Player target = Utils.getPlayer(playerNameLowerCase);
Player target = commandService.getPlayer(playerNameLowerCase);
PlayerCache.getInstance().removePlayer(playerNameLowerCase);
Utils.setGroup(target, Utils.GroupType.UNREGISTERED);
if (target != null && target.isOnline()) {