From 0c96a3113bf6e1aa3207641408fa2cb8c741054f Mon Sep 17 00:00:00 2001 From: Gnat008 Date: Mon, 27 Jun 2016 11:25:12 -0400 Subject: [PATCH] Encapsulate fields in main class; see #762 #655 #604 --- src/main/java/fr/xephi/authme/AuthMe.java | 71 ++++++++++++++++--- .../executable/email/RecoverEmailCommand.java | 4 +- .../xephi/authme/hooks/BungeeCordMessage.java | 6 +- .../authme/listener/AuthMeServerListener.java | 4 +- .../authme/process/join/AsynchronousJoin.java | 10 +-- .../process/login/ProcessSyncPlayerLogin.java | 4 +- .../ProcessSynchronousPlayerLogout.java | 8 +-- .../authme/process/quit/AsynchronousQuit.java | 4 +- .../process/register/AsyncRegister.java | 2 +- .../register/ProcessSyncPasswordRegister.java | 8 +-- 10 files changed, 84 insertions(+), 37 deletions(-) diff --git a/src/main/java/fr/xephi/authme/AuthMe.java b/src/main/java/fr/xephi/authme/AuthMe.java index 8156d2c10..b7474222e 100644 --- a/src/main/java/fr/xephi/authme/AuthMe.java +++ b/src/main/java/fr/xephi/authme/AuthMe.java @@ -5,7 +5,6 @@ import fr.xephi.authme.api.API; import fr.xephi.authme.api.NewAPI; import fr.xephi.authme.cache.auth.PlayerAuth; import fr.xephi.authme.cache.auth.PlayerCache; -import fr.xephi.authme.cache.backup.JsonCache; import fr.xephi.authme.cache.limbo.LimboCache; import fr.xephi.authme.cache.limbo.LimboPlayer; import fr.xephi.authme.command.CommandHandler; @@ -119,17 +118,13 @@ public class AuthMe extends JavaPlugin { private AuthMeServiceInitializer initializer; /* - * Public instances + * Private instances (sessions, mail, and ProtocolLib) */ - - // TODO: Encapsulate session management - public final ConcurrentHashMap sessions = new ConcurrentHashMap<>(); - // TODO #655: Encapsulate mail - public SendMailSSL mail; - // TODO #604: Encapsulate ProtocolLib members - public AuthMeInventoryPacketAdapter inventoryProtector; - public AuthMeTabCompletePacketAdapter tabComplete; - public AuthMeTablistPacketAdapter tablistHider; + private final ConcurrentHashMap sessions = new ConcurrentHashMap<>(); + private SendMailSSL mail; + private AuthMeInventoryPacketAdapter inventoryProtector; + private AuthMeTabCompletePacketAdapter tabComplete; + private AuthMeTablistPacketAdapter tablistHider; /** * Constructor. @@ -713,6 +708,60 @@ public class AuthMe extends JavaPlugin { return commandHandler.processCommand(sender, commandLabel, args); } + /** + * Get all current player sessions. + * + * @return A concurrent hashmap containing the sessions. + */ + public ConcurrentHashMap getSessions() { + return this.sessions; + } + + /** + * Get the mailing instance. + * + * @return The send mail instance. + */ + public SendMailSSL getMail() { + return this.mail; + } + + /** + * Get the ProtocolLib inventory packet adapter. + * + * @return The inventory packet adapter. + */ + public AuthMeInventoryPacketAdapter getInventoryProtector() { + return inventoryProtector; + } + + /** + * Get the ProtocolLib tab complete packet adapter. + * + * @return The tab complete packet adapter. + */ + public AuthMeTabCompletePacketAdapter getTabComplete() { + return tabComplete; + } + + /** + * Get the ProtocolLib tab list packet adapter. + * + * @return The tab list packet adapter. + */ + public AuthMeTablistPacketAdapter getTablistHider() { + return tablistHider; + } + + /** + * Disables instances should the ProtocolLib plugin be disabled on the server. + */ + public void disableProtocolLib() { + this.inventoryProtector = null; + this.tablistHider = null; + this.tabComplete = null; + } + // ------------- // Service getters (deprecated) // Use @Inject fields instead diff --git a/src/main/java/fr/xephi/authme/command/executable/email/RecoverEmailCommand.java b/src/main/java/fr/xephi/authme/command/executable/email/RecoverEmailCommand.java index fac517ea9..636027bbb 100644 --- a/src/main/java/fr/xephi/authme/command/executable/email/RecoverEmailCommand.java +++ b/src/main/java/fr/xephi/authme/command/executable/email/RecoverEmailCommand.java @@ -41,7 +41,7 @@ public class RecoverEmailCommand extends PlayerCommand { final String playerMail = arguments.get(0); final String playerName = player.getName(); - if (plugin.mail == null) { + if (plugin.getMail() == null) { ConsoleLogger.showError("Mail API is not set"); commandService.send(player, MessageKey.ERROR); return; @@ -76,7 +76,7 @@ public class RecoverEmailCommand extends PlayerCommand { } auth.setPassword(hashNew); dataSource.updatePassword(auth); - plugin.mail.main(auth, thePass); + plugin.getMail().main(auth, thePass); commandService.send(player, MessageKey.RECOVERY_EMAIL_SENT_MESSAGE); } else { commandService.send(player, MessageKey.REGISTER_EMAIL_MESSAGE); diff --git a/src/main/java/fr/xephi/authme/hooks/BungeeCordMessage.java b/src/main/java/fr/xephi/authme/hooks/BungeeCordMessage.java index 5c145bbfb..ff67b4ec8 100644 --- a/src/main/java/fr/xephi/authme/hooks/BungeeCordMessage.java +++ b/src/main/java/fr/xephi/authme/hooks/BungeeCordMessage.java @@ -61,9 +61,9 @@ public class BungeeCordMessage implements PluginMessageListener { playerCache.updatePlayer(auth); dataSource.setLogged(name); //START 03062016 sgdc3: should fix #731 but we need to recode this mess - if (plugin.sessions.containsKey(name)) { - plugin.sessions.get(name).cancel(); - plugin.sessions.remove(name); + if (plugin.getSessions().containsKey(name)) { + plugin.getSessions().get(name).cancel(); + plugin.getSessions().remove(name); } //END diff --git a/src/main/java/fr/xephi/authme/listener/AuthMeServerListener.java b/src/main/java/fr/xephi/authme/listener/AuthMeServerListener.java index b627ed6d6..d545ef103 100644 --- a/src/main/java/fr/xephi/authme/listener/AuthMeServerListener.java +++ b/src/main/java/fr/xephi/authme/listener/AuthMeServerListener.java @@ -75,9 +75,7 @@ public class AuthMeServerListener implements Listener { } if (pluginName.equalsIgnoreCase("ProtocolLib")) { - plugin.inventoryProtector = null; - plugin.tablistHider = null; - plugin.tabComplete = null; + plugin.disableProtocolLib(); ConsoleLogger.showError("ProtocolLib has been disabled, unhook packet inventory protection!"); } } diff --git a/src/main/java/fr/xephi/authme/process/join/AsynchronousJoin.java b/src/main/java/fr/xephi/authme/process/join/AsynchronousJoin.java index cf1741c1b..0eedd8022 100644 --- a/src/main/java/fr/xephi/authme/process/join/AsynchronousJoin.java +++ b/src/main/java/fr/xephi/authme/process/join/AsynchronousJoin.java @@ -122,11 +122,11 @@ public class AsynchronousJoin implements AsynchronousProcess { limboCache.updateLimboPlayer(player); // Protect inventory - if (service.getProperty(PROTECT_INVENTORY_BEFORE_LOGIN) && plugin.inventoryProtector != null) { + if (service.getProperty(PROTECT_INVENTORY_BEFORE_LOGIN) && plugin.getInventoryProtector() != null) { ProtectInventoryEvent ev = new ProtectInventoryEvent(player); bukkitService.callEvent(ev); if (ev.isCancelled()) { - plugin.inventoryProtector.sendInventoryPacket(player); + plugin.getInventoryProtector().sendInventoryPacket(player); if (!service.getProperty(SecuritySettings.REMOVE_SPAM_FROM_CONSOLE)) { ConsoleLogger.info("ProtectInventoryEvent has been cancelled for " + player.getName() + "..."); } @@ -135,9 +135,9 @@ public class AsynchronousJoin implements AsynchronousProcess { // Session logic if (service.getProperty(PluginSettings.SESSIONS_ENABLED) && (playerCache.isAuthenticated(name) || database.isLogged(name))) { - if (plugin.sessions.containsKey(name)) { - plugin.sessions.get(name).cancel(); - plugin.sessions.remove(name); + if (plugin.getSessions().containsKey(name)) { + plugin.getSessions().get(name).cancel(); + plugin.getSessions().remove(name); } PlayerAuth auth = database.getAuth(name); database.setUnlogged(name); diff --git a/src/main/java/fr/xephi/authme/process/login/ProcessSyncPlayerLogin.java b/src/main/java/fr/xephi/authme/process/login/ProcessSyncPlayerLogin.java index 521928332..e4ded1de4 100644 --- a/src/main/java/fr/xephi/authme/process/login/ProcessSyncPlayerLogin.java +++ b/src/main/java/fr/xephi/authme/process/login/ProcessSyncPlayerLogin.java @@ -106,8 +106,8 @@ public class ProcessSyncPlayerLogin implements SynchronousProcess { restoreInventory(player); } - if (service.getProperty(RestrictionSettings.HIDE_TABLIST_BEFORE_LOGIN) && plugin.tablistHider != null) { - plugin.tablistHider.sendTablist(player); + if (service.getProperty(RestrictionSettings.HIDE_TABLIST_BEFORE_LOGIN) && plugin.getTablistHider() != null) { + plugin.getTablistHider().sendTablist(player); } // Clean up no longer used temporary data diff --git a/src/main/java/fr/xephi/authme/process/logout/ProcessSynchronousPlayerLogout.java b/src/main/java/fr/xephi/authme/process/logout/ProcessSynchronousPlayerLogout.java index 88a13e306..6fb337f97 100644 --- a/src/main/java/fr/xephi/authme/process/logout/ProcessSynchronousPlayerLogout.java +++ b/src/main/java/fr/xephi/authme/process/logout/ProcessSynchronousPlayerLogout.java @@ -58,12 +58,12 @@ public class ProcessSynchronousPlayerLogout implements SynchronousProcess { public void processSyncLogout(Player player) { final String name = player.getName().toLowerCase(); - if (plugin.sessions.containsKey(name)) { - plugin.sessions.get(name).cancel(); - plugin.sessions.remove(name); + if (plugin.getSessions().containsKey(name)) { + plugin.getSessions().get(name).cancel(); + plugin.getSessions().remove(name); } if (service.getProperty(RestrictionSettings.PROTECT_INVENTORY_BEFORE_LOGIN)) { - plugin.inventoryProtector.sendBlankInventoryPacket(player); + plugin.getInventoryProtector().sendBlankInventoryPacket(player); } limboPlayerTaskManager.registerTimeoutTask(player); diff --git a/src/main/java/fr/xephi/authme/process/quit/AsynchronousQuit.java b/src/main/java/fr/xephi/authme/process/quit/AsynchronousQuit.java index d31f305c9..675a40ff1 100644 --- a/src/main/java/fr/xephi/authme/process/quit/AsynchronousQuit.java +++ b/src/main/java/fr/xephi/authme/process/quit/AsynchronousQuit.java @@ -94,7 +94,7 @@ public class AsynchronousQuit implements AsynchronousProcess { }, Settings.getSessionTimeout * TICKS_PER_MINUTE); - plugin.sessions.put(name, task); + plugin.getSessions().put(name, task); } else { //plugin is disabled; we cannot schedule more tasks so run it directly here postLogout(name); @@ -117,6 +117,6 @@ public class AsynchronousQuit implements AsynchronousProcess { private void postLogout(String name) { PlayerCache.getInstance().removePlayer(name); database.setUnlogged(name); - plugin.sessions.remove(name); + plugin.getSessions().remove(name); } } diff --git a/src/main/java/fr/xephi/authme/process/register/AsyncRegister.java b/src/main/java/fr/xephi/authme/process/register/AsyncRegister.java index 9c792fe66..4ddaff5e5 100644 --- a/src/main/java/fr/xephi/authme/process/register/AsyncRegister.java +++ b/src/main/java/fr/xephi/authme/process/register/AsyncRegister.java @@ -137,7 +137,7 @@ public class AsyncRegister implements AsynchronousProcess { } database.updateEmail(auth); database.updateSession(auth); - plugin.mail.main(auth, password); + plugin.getMail().main(auth, password); syncProcessManager.processSyncEmailRegister(player); } diff --git a/src/main/java/fr/xephi/authme/process/register/ProcessSyncPasswordRegister.java b/src/main/java/fr/xephi/authme/process/register/ProcessSyncPasswordRegister.java index 3076e7f1a..2ac8d027d 100644 --- a/src/main/java/fr/xephi/authme/process/register/ProcessSyncPasswordRegister.java +++ b/src/main/java/fr/xephi/authme/process/register/ProcessSyncPasswordRegister.java @@ -92,17 +92,17 @@ public class ProcessSyncPasswordRegister implements SynchronousProcess { final String name = player.getName().toLowerCase(); LimboPlayer limbo = limboCache.getLimboPlayer(name); if (limbo != null) { - if (service.getProperty(RestrictionSettings.HIDE_TABLIST_BEFORE_LOGIN) && plugin.tablistHider != null) { - plugin.tablistHider.sendTablist(player); + if (service.getProperty(RestrictionSettings.HIDE_TABLIST_BEFORE_LOGIN) && plugin.getTablistHider() != null) { + plugin.getTablistHider().sendTablist(player); } Utils.teleportToSpawn(player); - if (service.getProperty(PROTECT_INVENTORY_BEFORE_LOGIN) && plugin.inventoryProtector != null) { + if (service.getProperty(PROTECT_INVENTORY_BEFORE_LOGIN) && plugin.getInventoryProtector() != null) { RestoreInventoryEvent event = new RestoreInventoryEvent(player); bukkitService.callEvent(event); if (!event.isCancelled()) { - plugin.inventoryProtector.sendInventoryPacket(player); + plugin.getInventoryProtector().sendInventoryPacket(player); } }