Minor - rename LimboPlayer methods + code householding

- Rename *taskId methods to *task to reflect what they handle
- Remove usages of Wrapper where applicable
- Replace some uses of legacy Settings with NewSetting calls
This commit is contained in:
ljacqu 2016-03-06 15:38:08 +01:00
parent 3f4681c5ed
commit 654cebd5a7
21 changed files with 99 additions and 109 deletions

View File

@ -815,8 +815,8 @@ public class AuthMe extends JavaPlugin {
}
Utils.addNormal(player, limbo.getGroup());
player.setOp(limbo.getOperator());
limbo.getTimeoutTaskId().cancel();
player.setOp(limbo.isOperator());
limbo.getTimeoutTask().cancel();
LimboCache.getInstance().deleteLimboPlayer(name);
if (this.playerBackup.doesCacheExist(player)) {
this.playerBackup.removeCache(player);

View File

@ -16,8 +16,8 @@ import static com.google.common.base.Preconditions.checkNotNull;
public class LimboCache {
private volatile static LimboCache singleton;
public final ConcurrentHashMap<String, LimboPlayer> cache;
public final AuthMe plugin;
private final ConcurrentHashMap<String, LimboPlayer> cache;
private final AuthMe plugin;
private final JsonCache jsonCache;
/**
@ -84,7 +84,7 @@ public class LimboCache {
checkNotNull(name);
name = name.toLowerCase();
if (cache.containsKey(name)) {
cache.get(name).clearTask();
cache.get(name).clearTasks();
cache.remove(name);
}
}

View File

@ -4,14 +4,16 @@ import org.bukkit.Location;
import org.bukkit.scheduler.BukkitTask;
/**
* Represents a player which is not logged in and keeps track of certain states (like OP status, flying)
* which may be revoked from the player until he has logged in or registered.
*/
public class LimboPlayer {
private final String name;
private final boolean fly;
private Location loc = null;
private BukkitTask timeoutTaskId = null;
private BukkitTask messageTaskId = null;
private BukkitTask timeoutTask = null;
private BukkitTask messageTask = null;
private boolean operator = false;
private String group;
@ -25,36 +27,36 @@ public class LimboPlayer {
}
/**
* Method getName.
* Return the name of the player.
*
* @return String
* @return The player's name
*/
public String getName() {
return name;
}
/**
* Method getLoc.
* Return the player's original location.
*
* @return Location
* @return The player's location
*/
public Location getLoc() {
return loc;
}
/**
* Method getOperator.
* Return whether the player is an operator or not (i.e. whether he is an OP).
*
* @return boolean
* @return True if the player has OP status, false otherwise
*/
public boolean getOperator() {
public boolean isOperator() {
return operator;
}
/**
* Method getGroup.
* Return the player's permissions group.
*
* @return String
* @return The permissions group the player belongs to
*/
public String getGroup() {
return group;
@ -64,54 +66,61 @@ public class LimboPlayer {
return fly;
}
public BukkitTask getTimeoutTaskId() {
return timeoutTaskId;
}
/**
* Method setTimeoutTaskId.
* Return the timeout task, which kicks the player if he hasn't registered or logged in
* after a configurable amount of time.
*
* @param i BukkitTask
* @return The timeout task associated to the player
*/
public void setTimeoutTaskId(BukkitTask i) {
if (this.timeoutTaskId != null) {
this.timeoutTaskId.cancel();
}
this.timeoutTaskId = i;
public BukkitTask getTimeoutTask() {
return timeoutTask;
}
/**
* Method getMessageTaskId.
* Set the timeout task of the player. The timeout task kicks the player after a configurable
* amount of time if he hasn't logged in or registered.
*
* @return BukkitTask
* @param timeoutTask The task to set
*/
public BukkitTask getMessageTaskId() {
return messageTaskId;
public void setTimeoutTask(BukkitTask timeoutTask) {
if (this.timeoutTask != null) {
this.timeoutTask.cancel();
}
this.timeoutTask = timeoutTask;
}
/**
* Method setMessageTaskId.
* Return the message task reminding the player to log in or register.
*
* @param messageTaskId BukkitTask
* @return The task responsible for sending the message regularly
*/
public void setMessageTaskId(BukkitTask messageTaskId) {
if (this.messageTaskId != null) {
this.messageTaskId.cancel();
}
this.messageTaskId = messageTaskId;
public BukkitTask getMessageTask() {
return messageTask;
}
/**
* Method clearTask.
* Set the messages task responsible for telling the player to log in or register.
*
* @param messageTask The message task to set
*/
public void clearTask() {
if (messageTaskId != null) {
messageTaskId.cancel();
public void setMessageTask(BukkitTask messageTask) {
if (this.messageTask != null) {
this.messageTask.cancel();
}
messageTaskId = null;
if (timeoutTaskId != null) {
timeoutTaskId.cancel();
this.messageTask = messageTask;
}
/**
* Clears all tasks associated to the player.
*/
public void clearTasks() {
if (messageTask != null) {
messageTask.cancel();
}
timeoutTaskId = null;
messageTask = null;
if (timeoutTask != null) {
timeoutTask.cancel();
}
timeoutTask = null;
}
}

View File

@ -1,6 +1,7 @@
package fr.xephi.authme.command;
import fr.xephi.authme.AuthMe;
import fr.xephi.authme.cache.auth.PlayerCache;
import fr.xephi.authme.command.help.HelpProvider;
import fr.xephi.authme.datasource.DataSource;
import fr.xephi.authme.cache.IpAddressManager;
@ -190,4 +191,8 @@ public class CommandService {
return ipAddressManager;
}
public PlayerCache getPlayerCache() {
return PlayerCache.getInstance();
}
}

View File

@ -60,9 +60,9 @@ public class UnregisterAdminCommand implements ExecutableCommand {
BukkitScheduler scheduler = sender.getServer().getScheduler();
if (timeOut != 0) {
BukkitTask id = scheduler.runTaskLater(plugin, new TimeoutTask(plugin, playerNameLowerCase, target), timeOut);
LimboCache.getInstance().getLimboPlayer(playerNameLowerCase).setTimeoutTaskId(id);
LimboCache.getInstance().getLimboPlayer(playerNameLowerCase).setTimeoutTask(id);
}
LimboCache.getInstance().getLimboPlayer(playerNameLowerCase).setMessageTaskId(
LimboCache.getInstance().getLimboPlayer(playerNameLowerCase).setMessageTask(
scheduler.runTask(
plugin, new MessageTask(plugin, playerNameLowerCase, MessageKey.REGISTER_MESSAGE, interval)
)

View File

@ -7,7 +7,6 @@ import fr.xephi.authme.command.PlayerCommand;
import fr.xephi.authme.output.MessageKey;
import fr.xephi.authme.security.RandomString;
import fr.xephi.authme.settings.properties.SecuritySettings;
import fr.xephi.authme.util.Wrapper;
import org.bukkit.entity.Player;
import java.util.List;
@ -18,9 +17,8 @@ public class CaptchaCommand extends PlayerCommand {
public void runCommand(Player player, List<String> arguments, CommandService commandService) {
final String playerNameLowerCase = player.getName().toLowerCase();
final String captcha = arguments.get(0);
Wrapper wrapper = Wrapper.getInstance();
final AuthMe plugin = wrapper.getAuthMe();
PlayerCache playerCache = wrapper.getPlayerCache();
final AuthMe plugin = commandService.getAuthMe();
PlayerCache playerCache = PlayerCache.getInstance();
// Command logic
if (playerCache.isAuthenticated(playerNameLowerCase)) {

View File

@ -8,7 +8,6 @@ import fr.xephi.authme.output.MessageKey;
import fr.xephi.authme.settings.properties.RestrictionSettings;
import fr.xephi.authme.settings.properties.SecuritySettings;
import fr.xephi.authme.task.ChangePasswordTask;
import fr.xephi.authme.util.Wrapper;
import org.bukkit.entity.Player;
import java.util.List;
@ -24,8 +23,7 @@ public class ChangePasswordCommand extends PlayerCommand {
String newPassword = arguments.get(1);
String name = player.getName().toLowerCase();
Wrapper wrapper = Wrapper.getInstance();
final PlayerCache playerCache = wrapper.getPlayerCache();
final PlayerCache playerCache = commandService.getPlayerCache();
if (!playerCache.isAuthenticated(name)) {
commandService.send(player, MessageKey.NOT_LOGGED_IN);
return;

View File

@ -64,8 +64,6 @@ public class AsynchronousJoin implements Process {
}
final String ip = service.getIpAddressManager().getPlayerIp(player);
if (isNameRestricted(name, ip, player.getAddress().getHostName(), service.getSettings())) {
service.scheduleSyncDelayedTask(new Runnable() {
@Override
@ -196,7 +194,7 @@ public class AsynchronousJoin implements Process {
int msgInterval = service.getProperty(RegistrationSettings.MESSAGE_INTERVAL);
if (registrationTimeout > 0) {
BukkitTask id = service.runTaskLater(new TimeoutTask(plugin, name, player), registrationTimeout);
LimboCache.getInstance().getLimboPlayer(name).setTimeoutTaskId(id);
LimboCache.getInstance().getLimboPlayer(name).setTimeoutTask(id);
}
MessageKey msg;
@ -209,7 +207,7 @@ public class AsynchronousJoin implements Process {
}
if (msgInterval > 0 && LimboCache.getInstance().getLimboPlayer(name) != null) {
BukkitTask msgTask = service.runTask(new MessageTask(plugin, name, msg, msgInterval));
LimboCache.getInstance().getLimboPlayer(name).setMessageTaskId(msgTask);
LimboCache.getInstance().getLimboPlayer(name).setMessageTask(msgTask);
}
}

View File

@ -17,6 +17,7 @@ import fr.xephi.authme.security.RandomString;
import fr.xephi.authme.settings.Settings;
import fr.xephi.authme.settings.properties.DatabaseSettings;
import fr.xephi.authme.settings.properties.RegistrationSettings;
import fr.xephi.authme.settings.properties.RestrictionSettings;
import fr.xephi.authme.settings.properties.SecuritySettings;
import fr.xephi.authme.task.MessageTask;
import fr.xephi.authme.util.StringUtils;
@ -88,13 +89,13 @@ public class AsynchronousLogin implements Process {
if (pAuth == null) {
service.send(player, MessageKey.USER_NOT_REGISTERED);
if (LimboCache.getInstance().hasLimboPlayer(name)) {
LimboCache.getInstance().getLimboPlayer(name).getMessageTaskId().cancel();
LimboCache.getInstance().getLimboPlayer(name).getMessageTask().cancel();
String[] msg = service.getProperty(RegistrationSettings.USE_EMAIL_REGISTRATION)
? service.retrieveMessage(MessageKey.REGISTER_EMAIL_MESSAGE)
: service.retrieveMessage(MessageKey.REGISTER_MESSAGE);
BukkitTask messageTask = service.runTask(
new MessageTask(plugin, name, msg, service.getProperty(RegistrationSettings.MESSAGE_INTERVAL)));
LimboCache.getInstance().getLimboPlayer(name).setMessageTaskId(messageTask);
LimboCache.getInstance().getLimboPlayer(name).setMessageTask(messageTask);
}
return null;
}
@ -128,7 +129,7 @@ public class AsynchronousLogin implements Process {
return;
}
if (pAuth.getIp().equals("127.0.0.1") && !pAuth.getIp().equals(ip)) {
if ("127.0.0.1".equals(pAuth.getIp()) && !pAuth.getIp().equals(ip)) {
pAuth.setIp(ip);
database.updateIp(pAuth.getNickname(), ip);
}
@ -181,19 +182,20 @@ public class AsynchronousLogin implements Process {
ProcessSyncPlayerLogin syncPlayerLogin = new ProcessSyncPlayerLogin(
player, plugin, database, service.getSettings());
if (syncPlayerLogin.getLimbo() != null) {
if (syncPlayerLogin.getLimbo().getTimeoutTaskId() != null) {
syncPlayerLogin.getLimbo().getTimeoutTaskId().cancel();
if (syncPlayerLogin.getLimbo().getTimeoutTask() != null) {
syncPlayerLogin.getLimbo().getTimeoutTask().cancel();
}
if (syncPlayerLogin.getLimbo().getMessageTaskId() != null) {
syncPlayerLogin.getLimbo().getMessageTaskId().cancel();
if (syncPlayerLogin.getLimbo().getMessageTask() != null) {
syncPlayerLogin.getLimbo().getMessageTask().cancel();
}
}
Bukkit.getScheduler().scheduleSyncDelayedTask(plugin, syncPlayerLogin);
} else if (player.isOnline()) {
if (!Settings.noConsoleSpam)
if (!service.getProperty(SecuritySettings.REMOVE_SPAM_FROM_CONSOLE)) {
ConsoleLogger.info(realName + " used the wrong password");
if (Settings.isKickOnWrongPasswordEnabled) {
Bukkit.getScheduler().scheduleSyncDelayedTask(plugin, new Runnable() {
}
if (service.getProperty(RestrictionSettings.KICK_ON_WRONG_PASSWORD)) {
service.scheduleSyncDelayedTask(new Runnable() {
@Override
public void run() {
player.kickPlayer(service.retrieveSingleMessage(MessageKey.WRONG_PASSWORD));

View File

@ -67,7 +67,7 @@ public class ProcessSyncPlayerLogin implements Runnable {
}
private void restoreOpState() {
player.setOp(limbo.getOperator());
player.setOp(limbo.isOperator());
}
private void packQuitLocation() {

View File

@ -72,10 +72,10 @@ public class ProcessSynchronousPlayerLogout implements Process {
int interval = service.getProperty(RegistrationSettings.MESSAGE_INTERVAL);
if (timeOut != 0) {
BukkitTask id = service.runTaskLater(new TimeoutTask(plugin, name, player), timeOut);
LimboCache.getInstance().getLimboPlayer(name).setTimeoutTaskId(id);
LimboCache.getInstance().getLimboPlayer(name).setTimeoutTask(id);
}
BukkitTask msgT = service.runTask(new MessageTask(plugin, name, MessageKey.LOGIN_MESSAGE, interval));
LimboCache.getInstance().getLimboPlayer(name).setMessageTaskId(msgT);
LimboCache.getInstance().getLimboPlayer(name).setMessageTask(msgT);
if (player.isInsideVehicle() && player.getVehicle() != null) {
player.getVehicle().eject();
}

View File

@ -68,7 +68,7 @@ public class AsynchronousQuit implements Process {
Utils.addNormal(player, limbo.getGroup());
}
needToChange = true;
isOp = limbo.getOperator();
isOp = limbo.isOperator();
LimboCache.getInstance().deleteLimboPlayer(name);
}
if (Settings.isSessionsEnabled && !isKick) {

View File

@ -49,11 +49,11 @@ public class ProcessSyncEmailRegister implements Process {
if (limbo != null) {
if (time != 0) {
BukkitTask id = service.runTaskLater(new TimeoutTask(service.getAuthMe(), name, player), time);
limbo.setTimeoutTaskId(id);
limbo.setTimeoutTask(id);
}
BukkitTask messageTask = service.runTask(new MessageTask(
service.getAuthMe(), name, service.retrieveMessage(MessageKey.LOGIN_MESSAGE), msgInterval));
limbo.setMessageTaskId(messageTask);
limbo.setMessageTask(messageTask);
}
player.saveData();

View File

@ -67,10 +67,10 @@ public class ProcessSyncPasswordRegister implements Process {
BukkitTask task;
if (delay != 0) {
task = service.runTaskLater(new TimeoutTask(service.getAuthMe(), name, player), delay);
cache.getLimboPlayer(name).setTimeoutTaskId(task);
cache.getLimboPlayer(name).setTimeoutTask(task);
}
task = service.runTask(new MessageTask(plugin, name, MessageKey.LOGIN_MESSAGE, interval));
cache.getLimboPlayer(name).setMessageTaskId(task);
cache.getLimboPlayer(name).setMessageTask(task);
if (player.isInsideVehicle() && player.getVehicle() != null) {
player.getVehicle().eject();
}

View File

@ -75,9 +75,9 @@ public class AsynchronousUnregister implements Process {
BukkitScheduler scheduler = plugin.getServer().getScheduler();
if (timeOut != 0) {
BukkitTask id = scheduler.runTaskLater(plugin, new TimeoutTask(plugin, name, player), timeOut);
limboPlayer.setTimeoutTaskId(id);
limboPlayer.setTimeoutTask(id);
}
limboPlayer.setMessageTaskId(scheduler.runTask(plugin,
limboPlayer.setMessageTask(scheduler.runTask(plugin,
new MessageTask(plugin, name, MessageKey.REGISTER_MESSAGE, interval)));
service.send(player, MessageKey.UNREGISTERED_SUCCESS);
ConsoleLogger.info(player.getDisplayName() + " unregistered himself");

View File

@ -49,7 +49,7 @@ public class MessageTask implements Runnable {
}
BukkitTask nextTask = plugin.getServer().getScheduler().runTaskLater(plugin, this, interval * 20);
if (LimboCache.getInstance().hasLimboPlayer(name)) {
LimboCache.getInstance().getLimboPlayer(name).setMessageTaskId(nextTask);
LimboCache.getInstance().getLimboPlayer(name).setMessageTask(nextTask);
}
return;
}

View File

@ -8,7 +8,6 @@ import org.bukkit.Server;
import org.bukkit.scheduler.BukkitScheduler;
import java.io.File;
import java.util.logging.Logger;
/**
* Wrapper for the retrieval of common singletons used throughout the application.
@ -48,10 +47,6 @@ public class Wrapper {
return getAuthMe().getServer();
}
public Logger getLogger() {
return getAuthMe().getLogger();
}
public Messages getMessages() {
return getAuthMe().getMessages();
}

View File

@ -1,6 +1,5 @@
package fr.xephi.authme.manager;
package fr.xephi.authme.cache;
import fr.xephi.authme.cache.CaptchaManager;
import fr.xephi.authme.settings.NewSetting;
import fr.xephi.authme.settings.properties.SecuritySettings;
import org.junit.Test;

View File

@ -1,6 +1,5 @@
package fr.xephi.authme.manager;
package fr.xephi.authme.cache;
import fr.xephi.authme.cache.IpAddressManager;
import fr.xephi.authme.settings.NewSetting;
import fr.xephi.authme.settings.properties.HooksSettings;
import org.bukkit.entity.Player;

View File

@ -7,8 +7,6 @@ import fr.xephi.authme.output.MessageKey;
import fr.xephi.authme.settings.properties.RestrictionSettings;
import fr.xephi.authme.settings.properties.SecuritySettings;
import fr.xephi.authme.task.ChangePasswordTask;
import fr.xephi.authme.util.WrapperMock;
import org.bukkit.Server;
import org.bukkit.command.BlockCommandSender;
import org.bukkit.command.CommandSender;
import org.bukkit.entity.Player;
@ -21,6 +19,7 @@ import java.util.Arrays;
import java.util.Collections;
import static org.hamcrest.MatcherAssert.assertThat;
import static org.hamcrest.Matchers.containsString;
import static org.hamcrest.Matchers.equalTo;
import static org.mockito.BDDMockito.given;
import static org.mockito.Mockito.any;
@ -35,14 +34,10 @@ import static org.mockito.Mockito.when;
*/
public class ChangePasswordCommandTest {
private WrapperMock wrapperMock;
private PlayerCache cacheMock;
private CommandService commandService;
@Before
public void setUpMocks() {
wrapperMock = WrapperMock.createInstance();
cacheMock = wrapperMock.getPlayerCache();
commandService = mock(CommandService.class);
when(commandService.getProperty(SecuritySettings.MIN_PASSWORD_LENGTH)).thenReturn(2);
@ -62,7 +57,9 @@ public class ChangePasswordCommandTest {
command.executeCommand(sender, new ArrayList<String>(), commandService);
// then
assertThat(wrapperMock.wasMockCalled(Server.class), equalTo(false));
ArgumentCaptor<String> captor = ArgumentCaptor.forClass(String.class);
verify(sender).sendMessage(captor.capture());
assertThat(captor.getValue(), containsString("only for players"));
}
@Test
@ -76,7 +73,6 @@ public class ChangePasswordCommandTest {
// then
verify(commandService).send(sender, MessageKey.NOT_LOGGED_IN);
assertThat(wrapperMock.wasMockCalled(Server.class), equalTo(false));
}
@Test
@ -90,7 +86,6 @@ public class ChangePasswordCommandTest {
// then
verify(commandService).send(sender, MessageKey.PASSWORD_MATCH_ERROR);
assertThat(wrapperMock.wasMockCalled(Server.class), equalTo(false));
}
@ -105,7 +100,6 @@ public class ChangePasswordCommandTest {
// then
verify(commandService).send(sender, MessageKey.PASSWORD_IS_USERNAME_ERROR);
assertThat(wrapperMock.wasMockCalled(Server.class), equalTo(false));
}
@Test
@ -120,7 +114,6 @@ public class ChangePasswordCommandTest {
// then
verify(commandService).send(sender, MessageKey.INVALID_PASSWORD_LENGTH);
assertThat(wrapperMock.wasMockCalled(Server.class), equalTo(false));
}
@Test
@ -135,7 +128,6 @@ public class ChangePasswordCommandTest {
// then
verify(commandService).send(sender, MessageKey.INVALID_PASSWORD_LENGTH);
assertThat(wrapperMock.wasMockCalled(Server.class), equalTo(false));
}
@Test
@ -151,7 +143,6 @@ public class ChangePasswordCommandTest {
// then
verify(commandService).send(sender, MessageKey.PASSWORD_UNSAFE_ERROR);
assertThat(wrapperMock.wasMockCalled(Server.class), equalTo(false));
}
@Test
@ -175,7 +166,9 @@ public class ChangePasswordCommandTest {
private Player initPlayerWithName(String name, boolean loggedIn) {
Player player = mock(Player.class);
when(player.getName()).thenReturn(name);
when(cacheMock.isAuthenticated(name)).thenReturn(loggedIn);
PlayerCache playerCache = mock(PlayerCache.class);
when(playerCache.isAuthenticated(name)).thenReturn(loggedIn);
when(commandService.getPlayerCache()).thenReturn(playerCache);
return player;
}

View File

@ -10,7 +10,6 @@ import org.mockito.Mockito;
import java.io.File;
import java.util.HashMap;
import java.util.Map;
import java.util.logging.Logger;
/**
* Class returning mocks for all calls in {@link Wrapper}.
@ -36,11 +35,6 @@ public class WrapperMock extends Wrapper {
return instance;
}
@Override
public Logger getLogger() {
return getMock(Logger.class);
}
@Override
public Server getServer() {
return getMock(Server.class);