#723 Create provider for TimeoutTask and MessageTask, remove LimboCache#getInstance

- Create class to handle the creation of "LimboPlayer tasks" (adds encapsulation, reduces duplication)
- Move group setting into its own class because (mutual dependency between LimboCache and PermissionsManager otherwise)
This commit is contained in:
ljacqu
2016-06-12 21:59:26 +02:00
parent 67c72dc46d
commit 89bbfc48ee
16 changed files with 508 additions and 243 deletions
@@ -2,6 +2,7 @@ package fr.xephi.authme.process;
import fr.xephi.authme.output.MessageKey;
import fr.xephi.authme.output.Messages;
import fr.xephi.authme.permission.AuthGroupHandler;
import fr.xephi.authme.permission.AuthGroupType;
import fr.xephi.authme.permission.PermissionNode;
import fr.xephi.authme.permission.PermissionsManager;
@@ -30,6 +31,9 @@ public class ProcessService {
@Inject
private PermissionsManager permissionsManager;
@Inject
private AuthGroupHandler authGroupHandler;
/**
* Retrieve a property's value.
*
@@ -104,7 +108,7 @@ public class ProcessService {
}
public boolean setGroup(Player player, AuthGroupType group) {
return permissionsManager.setGroup(player, group);
return authGroupHandler.setGroup(player, group);
}
}
@@ -5,7 +5,6 @@ import fr.xephi.authme.ConsoleLogger;
import fr.xephi.authme.cache.auth.PlayerAuth;
import fr.xephi.authme.cache.auth.PlayerCache;
import fr.xephi.authme.cache.limbo.LimboCache;
import fr.xephi.authme.cache.limbo.LimboPlayer;
import fr.xephi.authme.datasource.DataSource;
import fr.xephi.authme.events.ProtectInventoryEvent;
import fr.xephi.authme.hooks.PluginHooks;
@@ -19,8 +18,7 @@ import fr.xephi.authme.settings.properties.PluginSettings;
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.task.TimeoutTask;
import fr.xephi.authme.task.LimboPlayerTaskManager;
import fr.xephi.authme.util.BukkitService;
import fr.xephi.authme.util.TeleportationService;
import fr.xephi.authme.util.Utils;
@@ -30,7 +28,6 @@ import org.bukkit.entity.LivingEntity;
import org.bukkit.entity.Player;
import org.bukkit.potion.PotionEffect;
import org.bukkit.potion.PotionEffectType;
import org.bukkit.scheduler.BukkitTask;
import javax.inject.Inject;
@@ -67,6 +64,9 @@ public class AsynchronousJoin implements AsynchronousProcess {
@Inject
private BukkitService bukkitService;
@Inject
private LimboPlayerTaskManager limboPlayerTaskManager;
AsynchronousJoin() { }
@@ -193,17 +193,9 @@ public class AsynchronousJoin implements AsynchronousProcess {
});
// Timeout task
if (registrationTimeout > 0) {
BukkitTask id = bukkitService.runTaskLater(new TimeoutTask(plugin, name, player), registrationTimeout);
LimboPlayer limboPlayer = limboCache.getLimboPlayer(name);
if (limboPlayer != null) {
limboPlayer.setTimeoutTask(id);
}
}
// Timeout and message task
limboPlayerTaskManager.registerTimeoutTask(player);
// Message task
int msgInterval = service.getProperty(RegistrationSettings.MESSAGE_INTERVAL);
MessageKey msg;
if (isAuthAvailable) {
msg = MessageKey.LOGIN_MESSAGE;
@@ -212,14 +204,7 @@ public class AsynchronousJoin implements AsynchronousProcess {
? MessageKey.REGISTER_EMAIL_MESSAGE
: MessageKey.REGISTER_MESSAGE;
}
if (msgInterval > 0 && limboCache.getLimboPlayer(name) != null) {
BukkitTask msgTask = bukkitService.runTaskLater(new MessageTask(bukkitService, plugin.getMessages(),
name, msg, msgInterval), 20L);
LimboPlayer limboPlayer = limboCache.getLimboPlayer(name);
if (limboPlayer != null) {
limboPlayer.setMessageTask(msgTask);
}
}
limboPlayerTaskManager.registerMessageTask(name, msg);
}
private boolean isPlayerUnrestricted(String name) {
@@ -1,6 +1,5 @@
package fr.xephi.authme.process.login;
import fr.xephi.authme.AuthMe;
import fr.xephi.authme.ConsoleLogger;
import fr.xephi.authme.cache.CaptchaManager;
import fr.xephi.authme.cache.TempbanManager;
@@ -25,13 +24,12 @@ import fr.xephi.authme.settings.properties.EmailSettings;
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.task.LimboPlayerTaskManager;
import fr.xephi.authme.util.BukkitService;
import fr.xephi.authme.util.StringUtils;
import fr.xephi.authme.util.Utils;
import org.bukkit.Bukkit;
import org.bukkit.entity.Player;
import org.bukkit.scheduler.BukkitTask;
import javax.inject.Inject;
import java.util.List;
@@ -40,9 +38,6 @@ import java.util.List;
*/
public class AsynchronousLogin implements AsynchronousProcess {
@Inject
private AuthMe plugin;
@Inject
private DataSource database;
@@ -73,8 +68,12 @@ public class AsynchronousLogin implements AsynchronousProcess {
@Inject
private TempbanManager tempbanManager;
@Inject
private LimboPlayerTaskManager limboPlayerTaskManager;
AsynchronousLogin() { }
/**
* Queries the {@link fr.xephi.authme.cache.CaptchaManager} to
* see if a captcha needs to be entered in order to log in.
@@ -118,16 +117,11 @@ public class AsynchronousLogin implements AsynchronousProcess {
if (pAuth == null) {
service.send(player, MessageKey.USER_NOT_REGISTERED);
LimboPlayer limboPlayer = limboCache.getLimboPlayer(name);
if (limboPlayer != null) {
limboPlayer.getMessageTask().cancel();
String[] msg = service.getProperty(RegistrationSettings.USE_EMAIL_REGISTRATION)
? service.retrieveMessage(MessageKey.REGISTER_EMAIL_MESSAGE)
: service.retrieveMessage(MessageKey.REGISTER_MESSAGE);
BukkitTask messageTask = bukkitService.runTask(new MessageTask(bukkitService,
name, msg, service.getProperty(RegistrationSettings.MESSAGE_INTERVAL)));
limboPlayer.setMessageTask(messageTask);
}
// TODO ljacqu 20160612: Why is the message task being canceled and added again here?
MessageKey key = service.getProperty(RegistrationSettings.USE_EMAIL_REGISTRATION)
? MessageKey.REGISTER_EMAIL_MESSAGE
: MessageKey.REGISTER_MESSAGE;
limboPlayerTaskManager.registerMessageTask(name, key);
return null;
}
@@ -4,22 +4,18 @@ import com.google.common.io.ByteArrayDataOutput;
import com.google.common.io.ByteStreams;
import fr.xephi.authme.AuthMe;
import fr.xephi.authme.ConsoleLogger;
import fr.xephi.authme.cache.limbo.LimboCache;
import fr.xephi.authme.events.LogoutEvent;
import fr.xephi.authme.output.MessageKey;
import fr.xephi.authme.process.ProcessService;
import fr.xephi.authme.process.SynchronousProcess;
import fr.xephi.authme.settings.Settings;
import fr.xephi.authme.settings.properties.HooksSettings;
import fr.xephi.authme.settings.properties.RegistrationSettings;
import fr.xephi.authme.settings.properties.RestrictionSettings;
import fr.xephi.authme.task.MessageTask;
import fr.xephi.authme.task.TimeoutTask;
import fr.xephi.authme.task.LimboPlayerTaskManager;
import fr.xephi.authme.util.BukkitService;
import org.bukkit.entity.Player;
import org.bukkit.potion.PotionEffect;
import org.bukkit.potion.PotionEffectType;
import org.bukkit.scheduler.BukkitTask;
import javax.inject.Inject;
@@ -34,14 +30,15 @@ public class ProcessSynchronousPlayerLogout implements SynchronousProcess {
@Inject
private ProcessService service;
@Inject
private LimboCache limboCache;
@Inject
private BukkitService bukkitService;
@Inject
private LimboPlayerTaskManager limboPlayerTaskManager;
ProcessSynchronousPlayerLogout() { }
private void sendBungeeMessage(Player player) {
ByteArrayDataOutput out = ByteStreams.newDataOutput();
out.writeUTF("Forward");
@@ -64,23 +61,19 @@ public class ProcessSynchronousPlayerLogout implements SynchronousProcess {
plugin.sessions.get(name).cancel();
plugin.sessions.remove(name);
}
if (Settings.protectInventoryBeforeLogInEnabled) {
if (service.getProperty(RestrictionSettings.PROTECT_INVENTORY_BEFORE_LOGIN)) {
plugin.inventoryProtector.sendBlankInventoryPacket(player);
}
int timeOut = service.getProperty(RestrictionSettings.TIMEOUT) * TICKS_PER_SECOND;
int interval = service.getProperty(RegistrationSettings.MESSAGE_INTERVAL);
if (timeOut != 0) {
BukkitTask id = bukkitService.runTaskLater(new TimeoutTask(plugin, name, player), timeOut);
limboCache.getLimboPlayer(name).setTimeoutTask(id);
}
BukkitTask msgT = bukkitService.runTask(new MessageTask(bukkitService, plugin.getMessages(),
name, MessageKey.LOGIN_MESSAGE, interval));
limboCache.getLimboPlayer(name).setMessageTask(msgT);
limboPlayerTaskManager.registerTimeoutTask(player);
limboPlayerTaskManager.registerMessageTask(name, MessageKey.LOGIN_MESSAGE);
if (player.isInsideVehicle() && player.getVehicle() != null) {
player.getVehicle().eject();
}
final int timeout = service.getProperty(RestrictionSettings.TIMEOUT) * TICKS_PER_SECOND;
if (service.getProperty(RegistrationSettings.APPLY_BLIND_EFFECT)) {
player.addPotionEffect(new PotionEffect(PotionEffectType.BLINDNESS, timeOut, 2));
player.addPotionEffect(new PotionEffect(PotionEffectType.BLINDNESS, timeout, 2));
}
player.setOp(false);
restoreSpeedEffect(player);
@@ -1,64 +1,43 @@
package fr.xephi.authme.process.register;
import fr.xephi.authme.AuthMe;
import fr.xephi.authme.ConsoleLogger;
import fr.xephi.authme.cache.limbo.LimboCache;
import fr.xephi.authme.cache.limbo.LimboPlayer;
import fr.xephi.authme.output.MessageKey;
import fr.xephi.authme.permission.AuthGroupType;
import fr.xephi.authme.process.ProcessService;
import fr.xephi.authme.process.SynchronousProcess;
import fr.xephi.authme.settings.Settings;
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.task.TimeoutTask;
import fr.xephi.authme.task.LimboPlayerTaskManager;
import fr.xephi.authme.util.BukkitService;
import fr.xephi.authme.util.Utils;
import org.bukkit.entity.Player;
import org.bukkit.scheduler.BukkitTask;
import javax.inject.Inject;
import static fr.xephi.authme.util.BukkitService.TICKS_PER_SECOND;
public class ProcessSyncEmailRegister implements SynchronousProcess {
@Inject
private ProcessService service;
@Inject
private LimboCache limboCache;
@Inject
private BukkitService bukkitService;
@Inject
private AuthMe authMe;
private LimboPlayerTaskManager limboPlayerTaskManager;
ProcessSyncEmailRegister() { }
public void processEmailRegister(Player player) {
final String name = player.getName().toLowerCase();
LimboPlayer limbo = limboCache.getLimboPlayer(name);
if (!Settings.getRegisteredGroup.isEmpty()) {
service.setGroup(player, AuthGroupType.REGISTERED);
}
service.send(player, MessageKey.ACCOUNT_NOT_ACTIVATED);
int time = service.getProperty(RestrictionSettings.TIMEOUT) * TICKS_PER_SECOND;
int msgInterval = service.getProperty(RegistrationSettings.MESSAGE_INTERVAL);
if (limbo != null) {
if (time != 0) {
BukkitTask id = bukkitService.runTaskLater(new TimeoutTask(authMe, name, player), time);
limbo.setTimeoutTask(id);
}
BukkitTask messageTask = bukkitService.runTask(new MessageTask(
bukkitService, name, service.retrieveMessage(MessageKey.LOGIN_MESSAGE), msgInterval));
limbo.setMessageTask(messageTask);
}
limboPlayerTaskManager.registerTimeoutTask(player);
limboPlayerTaskManager.registerMessageTask(name, MessageKey.LOGIN_MESSAGE);
player.saveData();
if (!service.getProperty(SecuritySettings.REMOVE_SPAM_FROM_CONSOLE)) {
@@ -18,19 +18,16 @@ import fr.xephi.authme.settings.properties.HooksSettings;
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.task.TimeoutTask;
import fr.xephi.authme.task.LimboPlayerTaskManager;
import fr.xephi.authme.util.BukkitService;
import fr.xephi.authme.util.Utils;
import org.bukkit.Bukkit;
import org.bukkit.entity.Player;
import org.bukkit.potion.PotionEffectType;
import org.bukkit.scheduler.BukkitTask;
import javax.inject.Inject;
import static fr.xephi.authme.settings.properties.RestrictionSettings.HIDE_TABLIST_BEFORE_LOGIN;
import static fr.xephi.authme.util.BukkitService.TICKS_PER_SECOND;
/**
*/
@@ -48,8 +45,12 @@ public class ProcessSyncPasswordRegister implements SynchronousProcess {
@Inject
private LimboCache limboCache;
@Inject
private LimboPlayerTaskManager limboPlayerTaskManager;
ProcessSyncPasswordRegister() { }
private void sendBungeeMessage(Player player) {
ByteArrayDataOutput out = ByteStreams.newDataOutput();
out.writeUTF("Forward");
@@ -77,17 +78,11 @@ public class ProcessSyncPasswordRegister implements SynchronousProcess {
private void requestLogin(Player player) {
final String name = player.getName().toLowerCase();
Utils.teleportToSpawn(player);
limboCache.updateLimboPlayer(player);
int delay = service.getProperty(RestrictionSettings.TIMEOUT) * TICKS_PER_SECOND;
int interval = service.getProperty(RegistrationSettings.MESSAGE_INTERVAL);
BukkitTask task;
if (delay != 0) {
task = bukkitService.runTaskLater(new TimeoutTask(plugin, name, player), delay);
limboCache.getLimboPlayer(name).setTimeoutTask(task);
}
task = bukkitService.runTask(new MessageTask(bukkitService, plugin.getMessages(),
name, MessageKey.LOGIN_MESSAGE, interval));
limboCache.getLimboPlayer(name).setMessageTask(task);
limboPlayerTaskManager.registerTimeoutTask(player);
limboPlayerTaskManager.registerMessageTask(name, MessageKey.LOGIN_MESSAGE);
if (player.isInsideVehicle() && player.getVehicle() != null) {
player.getVehicle().eject();
}
@@ -5,7 +5,6 @@ import fr.xephi.authme.ConsoleLogger;
import fr.xephi.authme.cache.auth.PlayerAuth;
import fr.xephi.authme.cache.auth.PlayerCache;
import fr.xephi.authme.cache.limbo.LimboCache;
import fr.xephi.authme.cache.limbo.LimboPlayer;
import fr.xephi.authme.datasource.DataSource;
import fr.xephi.authme.output.MessageKey;
import fr.xephi.authme.permission.AuthGroupType;
@@ -15,14 +14,12 @@ import fr.xephi.authme.security.PasswordSecurity;
import fr.xephi.authme.settings.Settings;
import fr.xephi.authme.settings.properties.RegistrationSettings;
import fr.xephi.authme.settings.properties.RestrictionSettings;
import fr.xephi.authme.task.MessageTask;
import fr.xephi.authme.task.TimeoutTask;
import fr.xephi.authme.task.LimboPlayerTaskManager;
import fr.xephi.authme.util.BukkitService;
import fr.xephi.authme.util.Utils;
import org.bukkit.entity.Player;
import org.bukkit.potion.PotionEffect;
import org.bukkit.potion.PotionEffectType;
import org.bukkit.scheduler.BukkitTask;
import javax.inject.Inject;
@@ -51,8 +48,12 @@ public class AsynchronousUnregister implements AsynchronousProcess {
@Inject
private BukkitService bukkitService;
@Inject
private LimboPlayerTaskManager limboPlayerTaskManager;
AsynchronousUnregister() { }
public void unregister(Player player, String password, boolean force) {
final String name = player.getName().toLowerCase();
PlayerAuth cachedAuth = playerCache.getAuth(name);
@@ -61,7 +62,7 @@ public class AsynchronousUnregister implements AsynchronousProcess {
service.send(player, MessageKey.ERROR);
return;
}
int timeOut = service.getProperty(RestrictionSettings.TIMEOUT) * TICKS_PER_SECOND;
if (service.getProperty(RegistrationSettings.FORCE)) {
Utils.teleportToSpawn(player);
player.saveData();
@@ -70,17 +71,12 @@ public class AsynchronousUnregister implements AsynchronousProcess {
service.setGroup(player, AuthGroupType.UNREGISTERED);
}
limboCache.addLimboPlayer(player);
LimboPlayer limboPlayer = limboCache.getLimboPlayer(name);
int interval = service.getProperty(RegistrationSettings.MESSAGE_INTERVAL);
if (timeOut != 0) {
BukkitTask id = bukkitService.runTaskLater(new TimeoutTask(plugin, name, player), timeOut);
limboPlayer.setTimeoutTask(id);
}
limboPlayer.setMessageTask(bukkitService.runTask(new MessageTask(bukkitService,
plugin.getMessages(), name, MessageKey.REGISTER_MESSAGE, interval)));
limboPlayerTaskManager.registerTimeoutTask(player);
limboPlayerTaskManager.registerMessageTask(name, MessageKey.REGISTER_MESSAGE);
service.send(player, MessageKey.UNREGISTERED_SUCCESS);
ConsoleLogger.info(player.getDisplayName() + " unregistered himself");
return;
return; // TODO ljacqu 20160612: Why return here? No blind effect? Player not removed from PlayerCache?
}
if (!Settings.unRegisteredGroup.isEmpty()) {
service.setGroup(player, AuthGroupType.UNREGISTERED);
@@ -88,8 +84,9 @@ public class AsynchronousUnregister implements AsynchronousProcess {
playerCache.removePlayer(name);
// Apply blind effect
int timeout = service.getProperty(RestrictionSettings.TIMEOUT) * TICKS_PER_SECOND;
if (service.getProperty(RegistrationSettings.APPLY_BLIND_EFFECT)) {
player.addPotionEffect(new PotionEffect(PotionEffectType.BLINDNESS, timeOut, 2));
player.addPotionEffect(new PotionEffect(PotionEffectType.BLINDNESS, timeout, 2));
}
service.send(player, MessageKey.UNREGISTERED_SUCCESS);
ConsoleLogger.info(player.getDisplayName() + " unregistered himself");