Encapsulate fields in main class; see #762 #655 #604

This commit is contained in:
Gnat008 2016-06-27 11:25:12 -04:00
parent 99704e7c29
commit 0c96a3113b
10 changed files with 84 additions and 37 deletions

View File

@ -5,7 +5,6 @@ import fr.xephi.authme.api.API;
import fr.xephi.authme.api.NewAPI; import fr.xephi.authme.api.NewAPI;
import fr.xephi.authme.cache.auth.PlayerAuth; import fr.xephi.authme.cache.auth.PlayerAuth;
import fr.xephi.authme.cache.auth.PlayerCache; 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.LimboCache;
import fr.xephi.authme.cache.limbo.LimboPlayer; import fr.xephi.authme.cache.limbo.LimboPlayer;
import fr.xephi.authme.command.CommandHandler; import fr.xephi.authme.command.CommandHandler;
@ -119,17 +118,13 @@ public class AuthMe extends JavaPlugin {
private AuthMeServiceInitializer initializer; private AuthMeServiceInitializer initializer;
/* /*
* Public instances * Private instances (sessions, mail, and ProtocolLib)
*/ */
private final ConcurrentHashMap<String, BukkitTask> sessions = new ConcurrentHashMap<>();
// TODO: Encapsulate session management private SendMailSSL mail;
public final ConcurrentHashMap<String, BukkitTask> sessions = new ConcurrentHashMap<>(); private AuthMeInventoryPacketAdapter inventoryProtector;
// TODO #655: Encapsulate mail private AuthMeTabCompletePacketAdapter tabComplete;
public SendMailSSL mail; private AuthMeTablistPacketAdapter tablistHider;
// TODO #604: Encapsulate ProtocolLib members
public AuthMeInventoryPacketAdapter inventoryProtector;
public AuthMeTabCompletePacketAdapter tabComplete;
public AuthMeTablistPacketAdapter tablistHider;
/** /**
* Constructor. * Constructor.
@ -713,6 +708,60 @@ public class AuthMe extends JavaPlugin {
return commandHandler.processCommand(sender, commandLabel, args); return commandHandler.processCommand(sender, commandLabel, args);
} }
/**
* Get all current player sessions.
*
* @return A concurrent hashmap containing the sessions.
*/
public ConcurrentHashMap<String, BukkitTask> 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) // Service getters (deprecated)
// Use @Inject fields instead // Use @Inject fields instead

View File

@ -41,7 +41,7 @@ public class RecoverEmailCommand extends PlayerCommand {
final String playerMail = arguments.get(0); final String playerMail = arguments.get(0);
final String playerName = player.getName(); final String playerName = player.getName();
if (plugin.mail == null) { if (plugin.getMail() == null) {
ConsoleLogger.showError("Mail API is not set"); ConsoleLogger.showError("Mail API is not set");
commandService.send(player, MessageKey.ERROR); commandService.send(player, MessageKey.ERROR);
return; return;
@ -76,7 +76,7 @@ public class RecoverEmailCommand extends PlayerCommand {
} }
auth.setPassword(hashNew); auth.setPassword(hashNew);
dataSource.updatePassword(auth); dataSource.updatePassword(auth);
plugin.mail.main(auth, thePass); plugin.getMail().main(auth, thePass);
commandService.send(player, MessageKey.RECOVERY_EMAIL_SENT_MESSAGE); commandService.send(player, MessageKey.RECOVERY_EMAIL_SENT_MESSAGE);
} else { } else {
commandService.send(player, MessageKey.REGISTER_EMAIL_MESSAGE); commandService.send(player, MessageKey.REGISTER_EMAIL_MESSAGE);

View File

@ -61,9 +61,9 @@ public class BungeeCordMessage implements PluginMessageListener {
playerCache.updatePlayer(auth); playerCache.updatePlayer(auth);
dataSource.setLogged(name); dataSource.setLogged(name);
//START 03062016 sgdc3: should fix #731 but we need to recode this mess //START 03062016 sgdc3: should fix #731 but we need to recode this mess
if (plugin.sessions.containsKey(name)) { if (plugin.getSessions().containsKey(name)) {
plugin.sessions.get(name).cancel(); plugin.getSessions().get(name).cancel();
plugin.sessions.remove(name); plugin.getSessions().remove(name);
} }
//END //END

View File

@ -75,9 +75,7 @@ public class AuthMeServerListener implements Listener {
} }
if (pluginName.equalsIgnoreCase("ProtocolLib")) { if (pluginName.equalsIgnoreCase("ProtocolLib")) {
plugin.inventoryProtector = null; plugin.disableProtocolLib();
plugin.tablistHider = null;
plugin.tabComplete = null;
ConsoleLogger.showError("ProtocolLib has been disabled, unhook packet inventory protection!"); ConsoleLogger.showError("ProtocolLib has been disabled, unhook packet inventory protection!");
} }
} }

View File

@ -122,11 +122,11 @@ public class AsynchronousJoin implements AsynchronousProcess {
limboCache.updateLimboPlayer(player); limboCache.updateLimboPlayer(player);
// Protect inventory // 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); ProtectInventoryEvent ev = new ProtectInventoryEvent(player);
bukkitService.callEvent(ev); bukkitService.callEvent(ev);
if (ev.isCancelled()) { if (ev.isCancelled()) {
plugin.inventoryProtector.sendInventoryPacket(player); plugin.getInventoryProtector().sendInventoryPacket(player);
if (!service.getProperty(SecuritySettings.REMOVE_SPAM_FROM_CONSOLE)) { if (!service.getProperty(SecuritySettings.REMOVE_SPAM_FROM_CONSOLE)) {
ConsoleLogger.info("ProtectInventoryEvent has been cancelled for " + player.getName() + "..."); ConsoleLogger.info("ProtectInventoryEvent has been cancelled for " + player.getName() + "...");
} }
@ -135,9 +135,9 @@ public class AsynchronousJoin implements AsynchronousProcess {
// Session logic // Session logic
if (service.getProperty(PluginSettings.SESSIONS_ENABLED) && (playerCache.isAuthenticated(name) || database.isLogged(name))) { if (service.getProperty(PluginSettings.SESSIONS_ENABLED) && (playerCache.isAuthenticated(name) || database.isLogged(name))) {
if (plugin.sessions.containsKey(name)) { if (plugin.getSessions().containsKey(name)) {
plugin.sessions.get(name).cancel(); plugin.getSessions().get(name).cancel();
plugin.sessions.remove(name); plugin.getSessions().remove(name);
} }
PlayerAuth auth = database.getAuth(name); PlayerAuth auth = database.getAuth(name);
database.setUnlogged(name); database.setUnlogged(name);

View File

@ -106,8 +106,8 @@ public class ProcessSyncPlayerLogin implements SynchronousProcess {
restoreInventory(player); restoreInventory(player);
} }
if (service.getProperty(RestrictionSettings.HIDE_TABLIST_BEFORE_LOGIN) && plugin.tablistHider != null) { if (service.getProperty(RestrictionSettings.HIDE_TABLIST_BEFORE_LOGIN) && plugin.getTablistHider() != null) {
plugin.tablistHider.sendTablist(player); plugin.getTablistHider().sendTablist(player);
} }
// Clean up no longer used temporary data // Clean up no longer used temporary data

View File

@ -58,12 +58,12 @@ public class ProcessSynchronousPlayerLogout implements SynchronousProcess {
public void processSyncLogout(Player player) { public void processSyncLogout(Player player) {
final String name = player.getName().toLowerCase(); final String name = player.getName().toLowerCase();
if (plugin.sessions.containsKey(name)) { if (plugin.getSessions().containsKey(name)) {
plugin.sessions.get(name).cancel(); plugin.getSessions().get(name).cancel();
plugin.sessions.remove(name); plugin.getSessions().remove(name);
} }
if (service.getProperty(RestrictionSettings.PROTECT_INVENTORY_BEFORE_LOGIN)) { if (service.getProperty(RestrictionSettings.PROTECT_INVENTORY_BEFORE_LOGIN)) {
plugin.inventoryProtector.sendBlankInventoryPacket(player); plugin.getInventoryProtector().sendBlankInventoryPacket(player);
} }
limboPlayerTaskManager.registerTimeoutTask(player); limboPlayerTaskManager.registerTimeoutTask(player);

View File

@ -94,7 +94,7 @@ public class AsynchronousQuit implements AsynchronousProcess {
}, Settings.getSessionTimeout * TICKS_PER_MINUTE); }, Settings.getSessionTimeout * TICKS_PER_MINUTE);
plugin.sessions.put(name, task); plugin.getSessions().put(name, task);
} else { } else {
//plugin is disabled; we cannot schedule more tasks so run it directly here //plugin is disabled; we cannot schedule more tasks so run it directly here
postLogout(name); postLogout(name);
@ -117,6 +117,6 @@ public class AsynchronousQuit implements AsynchronousProcess {
private void postLogout(String name) { private void postLogout(String name) {
PlayerCache.getInstance().removePlayer(name); PlayerCache.getInstance().removePlayer(name);
database.setUnlogged(name); database.setUnlogged(name);
plugin.sessions.remove(name); plugin.getSessions().remove(name);
} }
} }

View File

@ -137,7 +137,7 @@ public class AsyncRegister implements AsynchronousProcess {
} }
database.updateEmail(auth); database.updateEmail(auth);
database.updateSession(auth); database.updateSession(auth);
plugin.mail.main(auth, password); plugin.getMail().main(auth, password);
syncProcessManager.processSyncEmailRegister(player); syncProcessManager.processSyncEmailRegister(player);
} }

View File

@ -92,17 +92,17 @@ public class ProcessSyncPasswordRegister implements SynchronousProcess {
final String name = player.getName().toLowerCase(); final String name = player.getName().toLowerCase();
LimboPlayer limbo = limboCache.getLimboPlayer(name); LimboPlayer limbo = limboCache.getLimboPlayer(name);
if (limbo != null) { if (limbo != null) {
if (service.getProperty(RestrictionSettings.HIDE_TABLIST_BEFORE_LOGIN) && plugin.tablistHider != null) { if (service.getProperty(RestrictionSettings.HIDE_TABLIST_BEFORE_LOGIN) && plugin.getTablistHider() != null) {
plugin.tablistHider.sendTablist(player); plugin.getTablistHider().sendTablist(player);
} }
Utils.teleportToSpawn(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); RestoreInventoryEvent event = new RestoreInventoryEvent(player);
bukkitService.callEvent(event); bukkitService.callEvent(event);
if (!event.isCancelled()) { if (!event.isCancelled()) {
plugin.inventoryProtector.sendInventoryPacket(player); plugin.getInventoryProtector().sendInventoryPacket(player);
} }
} }