From b7c3d4b42e3b9da30e0a5290ffad896b135e52c7 Mon Sep 17 00:00:00 2001 From: Gabriele C Date: Thu, 9 Nov 2017 11:19:24 +0100 Subject: [PATCH] Implement AuthMeBungee autologin (#1402) * Implement AuthMeBungee autologin There is a failing test due to cyclic dependency injection, @ljacqu could you take a quick look at this? * Try to fix recursive dependency injection * Fix codestyle * Fix the subchannel name, again... * Split BungeeService into BungeeSender and BungeeReceiver --- src/main/java/fr/xephi/authme/AuthMe.java | 4 + .../authme/PurgeLastPositionCommand.java | 8 +- .../initialization/OnShutdownPlayerSaver.java | 6 +- .../xephi/authme/listener/PlayerListener.java | 8 +- .../changepassword/AsyncChangePassword.java | 10 +- .../authme/process/email/AsyncAddEmail.java | 6 +- .../process/email/AsyncChangeEmail.java | 6 +- .../process/login/AsynchronousLogin.java | 8 +- .../process/login/ProcessSyncPlayerLogin.java | 6 +- .../process/logout/AsynchronousLogout.java | 10 +- .../authme/process/quit/AsynchronousQuit.java | 6 +- .../process/register/AsyncRegister.java | 8 +- .../register/ProcessSyncPasswordRegister.java | 6 +- .../unregister/AsynchronousUnregister.java | 6 +- .../service/bungeecord/BungeeReceiver.java | 92 +++++++++++++ .../service/bungeecord/BungeeSender.java | 82 ++++++++++++ .../service/bungeecord/BungeeService.java | 121 ------------------ .../service/bungeecord/MessageType.java | 1 + .../authme/AuthMeInitializationTest.java | 6 +- .../authme/PurgeLastPositionCommandTest.java | 4 +- .../AsyncChangePasswordTest.java | 4 +- .../process/email/AsyncAddEmailTest.java | 4 +- .../process/email/AsyncChangeEmailTest.java | 4 +- .../AsynchronousUnregisterTest.java | 4 +- 24 files changed, 241 insertions(+), 179 deletions(-) create mode 100644 src/main/java/fr/xephi/authme/service/bungeecord/BungeeReceiver.java create mode 100644 src/main/java/fr/xephi/authme/service/bungeecord/BungeeSender.java delete mode 100644 src/main/java/fr/xephi/authme/service/bungeecord/BungeeService.java diff --git a/src/main/java/fr/xephi/authme/AuthMe.java b/src/main/java/fr/xephi/authme/AuthMe.java index 45cf9ebdd..9a569b1ab 100644 --- a/src/main/java/fr/xephi/authme/AuthMe.java +++ b/src/main/java/fr/xephi/authme/AuthMe.java @@ -27,6 +27,7 @@ import fr.xephi.authme.security.crypts.Sha256; import fr.xephi.authme.service.BackupService; import fr.xephi.authme.service.BukkitService; import fr.xephi.authme.service.MigrationService; +import fr.xephi.authme.service.bungeecord.BungeeReceiver; import fr.xephi.authme.settings.Settings; import fr.xephi.authme.settings.SettingsWarner; import fr.xephi.authme.settings.properties.SecuritySettings; @@ -247,6 +248,9 @@ public class AuthMe extends JavaPlugin { commandHandler = injector.getSingleton(CommandHandler.class); backupService = injector.getSingleton(BackupService.class); + // Trigger instantiation (class not used elsewhere) + injector.getSingleton(BungeeReceiver.class); + // Trigger construction of API classes; they will keep track of the singleton injector.getSingleton(fr.xephi.authme.api.v3.AuthMeApi.class); injector.getSingleton(NewAPI.class); diff --git a/src/main/java/fr/xephi/authme/command/executable/authme/PurgeLastPositionCommand.java b/src/main/java/fr/xephi/authme/command/executable/authme/PurgeLastPositionCommand.java index c18380f4b..39af8ef64 100644 --- a/src/main/java/fr/xephi/authme/command/executable/authme/PurgeLastPositionCommand.java +++ b/src/main/java/fr/xephi/authme/command/executable/authme/PurgeLastPositionCommand.java @@ -4,8 +4,8 @@ import fr.xephi.authme.command.ExecutableCommand; import fr.xephi.authme.data.auth.PlayerAuth; import fr.xephi.authme.datasource.DataSource; import fr.xephi.authme.message.MessageKey; -import fr.xephi.authme.service.bungeecord.BungeeService; import fr.xephi.authme.service.CommonService; +import fr.xephi.authme.service.bungeecord.BungeeSender; import fr.xephi.authme.service.bungeecord.MessageType; import org.bukkit.command.CommandSender; @@ -24,7 +24,7 @@ public class PurgeLastPositionCommand implements ExecutableCommand { private CommonService commonService; @Inject - private BungeeService bungeeService; + private BungeeSender bungeeSender; @Override public void executeCommand(final CommandSender sender, List arguments) { @@ -34,7 +34,7 @@ public class PurgeLastPositionCommand implements ExecutableCommand { for (PlayerAuth auth : dataSource.getAllAuths()) { resetLastPosition(auth); dataSource.updateQuitLoc(auth); - bungeeService.sendAuthMeBungeecordMessage(MessageType.REFRESH_QUITLOC, playerName); + bungeeSender.sendAuthMeBungeecordMessage(MessageType.REFRESH_QUITLOC, playerName); } sender.sendMessage("All players last position locations are now reset"); } else { @@ -47,7 +47,7 @@ public class PurgeLastPositionCommand implements ExecutableCommand { resetLastPosition(auth); dataSource.updateQuitLoc(auth); - bungeeService.sendAuthMeBungeecordMessage(MessageType.REFRESH_QUITLOC, playerName); + bungeeSender.sendAuthMeBungeecordMessage(MessageType.REFRESH_QUITLOC, playerName); sender.sendMessage(playerName + "'s last position location is now reset"); } } diff --git a/src/main/java/fr/xephi/authme/initialization/OnShutdownPlayerSaver.java b/src/main/java/fr/xephi/authme/initialization/OnShutdownPlayerSaver.java index 38ef73b2c..063948008 100644 --- a/src/main/java/fr/xephi/authme/initialization/OnShutdownPlayerSaver.java +++ b/src/main/java/fr/xephi/authme/initialization/OnShutdownPlayerSaver.java @@ -5,8 +5,8 @@ import fr.xephi.authme.data.auth.PlayerCache; import fr.xephi.authme.data.limbo.LimboService; import fr.xephi.authme.datasource.DataSource; import fr.xephi.authme.service.BukkitService; -import fr.xephi.authme.service.bungeecord.BungeeService; import fr.xephi.authme.service.ValidationService; +import fr.xephi.authme.service.bungeecord.BungeeSender; import fr.xephi.authme.service.bungeecord.MessageType; import fr.xephi.authme.settings.Settings; import fr.xephi.authme.settings.SpawnLoader; @@ -37,7 +37,7 @@ public class OnShutdownPlayerSaver { @Inject private LimboService limboService; @Inject - private BungeeService bungeeService; + private BungeeSender bungeeSender; OnShutdownPlayerSaver() { } @@ -72,7 +72,7 @@ public class OnShutdownPlayerSaver { .realName(player.getName()) .location(loc).build(); dataSource.updateQuitLoc(auth); - bungeeService.sendAuthMeBungeecordMessage(MessageType.REFRESH_QUITLOC, player.getName()); + bungeeSender.sendAuthMeBungeecordMessage(MessageType.REFRESH_QUITLOC, player.getName()); } } } diff --git a/src/main/java/fr/xephi/authme/listener/PlayerListener.java b/src/main/java/fr/xephi/authme/listener/PlayerListener.java index c00c8b67f..30332c15d 100644 --- a/src/main/java/fr/xephi/authme/listener/PlayerListener.java +++ b/src/main/java/fr/xephi/authme/listener/PlayerListener.java @@ -8,11 +8,11 @@ import fr.xephi.authme.permission.PermissionsManager; import fr.xephi.authme.process.Management; import fr.xephi.authme.service.AntiBotService; import fr.xephi.authme.service.BukkitService; -import fr.xephi.authme.service.bungeecord.MessageType; -import fr.xephi.authme.service.bungeecord.BungeeService; import fr.xephi.authme.service.JoinMessageService; import fr.xephi.authme.service.TeleportationService; import fr.xephi.authme.service.ValidationService; +import fr.xephi.authme.service.bungeecord.BungeeSender; +import fr.xephi.authme.service.bungeecord.MessageType; import fr.xephi.authme.settings.Settings; import fr.xephi.authme.settings.SpawnLoader; import fr.xephi.authme.settings.properties.HooksSettings; @@ -86,7 +86,7 @@ public class PlayerListener implements Listener { @Inject private PermissionsManager permissionsManager; @Inject - private BungeeService bungeeService; + private BungeeSender bungeeSender; private boolean isAsyncPlayerPreLoginEventCalled = false; @@ -430,7 +430,7 @@ public class PlayerListener implements Listener { .location(spawn) .build(); dataSource.updateQuitLoc(auth); - bungeeService.sendAuthMeBungeecordMessage(MessageType.REFRESH_QUITLOC, name); + bungeeSender.sendAuthMeBungeecordMessage(MessageType.REFRESH_QUITLOC, name); } if (spawn != null && spawn.getWorld() != null) { event.setRespawnLocation(spawn); diff --git a/src/main/java/fr/xephi/authme/process/changepassword/AsyncChangePassword.java b/src/main/java/fr/xephi/authme/process/changepassword/AsyncChangePassword.java index a285b7680..1caaa5c68 100644 --- a/src/main/java/fr/xephi/authme/process/changepassword/AsyncChangePassword.java +++ b/src/main/java/fr/xephi/authme/process/changepassword/AsyncChangePassword.java @@ -6,10 +6,10 @@ import fr.xephi.authme.data.auth.PlayerCache; import fr.xephi.authme.datasource.DataSource; import fr.xephi.authme.message.MessageKey; import fr.xephi.authme.process.AsynchronousProcess; -import fr.xephi.authme.service.bungeecord.BungeeService; -import fr.xephi.authme.service.CommonService; import fr.xephi.authme.security.PasswordSecurity; import fr.xephi.authme.security.crypts.HashedPassword; +import fr.xephi.authme.service.CommonService; +import fr.xephi.authme.service.bungeecord.BungeeSender; import fr.xephi.authme.service.bungeecord.MessageType; import org.bukkit.command.CommandSender; import org.bukkit.entity.Player; @@ -31,7 +31,7 @@ public class AsyncChangePassword implements AsynchronousProcess { private PlayerCache playerCache; @Inject - private BungeeService bungeeService; + private BungeeSender bungeeSender; AsyncChangePassword() { } @@ -54,7 +54,7 @@ public class AsyncChangePassword implements AsynchronousProcess { commonService.send(player, MessageKey.ERROR); return; } - bungeeService.sendAuthMeBungeecordMessage(MessageType.REFRESH_PASSWORD, name); + bungeeSender.sendAuthMeBungeecordMessage(MessageType.REFRESH_PASSWORD, name); playerCache.updatePlayer(auth); commonService.send(player, MessageKey.PASSWORD_CHANGED_SUCCESS); @@ -84,7 +84,7 @@ public class AsyncChangePassword implements AsynchronousProcess { HashedPassword hashedPassword = passwordSecurity.computeHash(newPassword, lowerCaseName); if (dataSource.updatePassword(lowerCaseName, hashedPassword)) { - bungeeService.sendAuthMeBungeecordMessage(MessageType.REFRESH_PASSWORD, lowerCaseName); + bungeeSender.sendAuthMeBungeecordMessage(MessageType.REFRESH_PASSWORD, lowerCaseName); if (sender != null) { commonService.send(sender, MessageKey.PASSWORD_CHANGED_SUCCESS); ConsoleLogger.info(sender.getName() + " changed password of " + lowerCaseName); diff --git a/src/main/java/fr/xephi/authme/process/email/AsyncAddEmail.java b/src/main/java/fr/xephi/authme/process/email/AsyncAddEmail.java index b5b74c436..016d6169d 100644 --- a/src/main/java/fr/xephi/authme/process/email/AsyncAddEmail.java +++ b/src/main/java/fr/xephi/authme/process/email/AsyncAddEmail.java @@ -6,9 +6,9 @@ import fr.xephi.authme.data.auth.PlayerCache; import fr.xephi.authme.datasource.DataSource; import fr.xephi.authme.message.MessageKey; import fr.xephi.authme.process.AsynchronousProcess; -import fr.xephi.authme.service.bungeecord.BungeeService; import fr.xephi.authme.service.CommonService; import fr.xephi.authme.service.ValidationService; +import fr.xephi.authme.service.bungeecord.BungeeSender; import fr.xephi.authme.service.bungeecord.MessageType; import fr.xephi.authme.util.Utils; import org.bukkit.entity.Player; @@ -33,7 +33,7 @@ public class AsyncAddEmail implements AsynchronousProcess { private ValidationService validationService; @Inject - private BungeeService bungeeService; + private BungeeSender bungeeSender; AsyncAddEmail() { } @@ -60,7 +60,7 @@ public class AsyncAddEmail implements AsynchronousProcess { auth.setEmail(email); if (dataSource.updateEmail(auth)) { playerCache.updatePlayer(auth); - bungeeService.sendAuthMeBungeecordMessage(MessageType.REFRESH_EMAIL, playerName); + bungeeSender.sendAuthMeBungeecordMessage(MessageType.REFRESH_EMAIL, playerName); service.send(player, MessageKey.EMAIL_ADDED_SUCCESS); } else { ConsoleLogger.warning("Could not save email for player '" + player + "'"); diff --git a/src/main/java/fr/xephi/authme/process/email/AsyncChangeEmail.java b/src/main/java/fr/xephi/authme/process/email/AsyncChangeEmail.java index 237246d71..8edd94961 100644 --- a/src/main/java/fr/xephi/authme/process/email/AsyncChangeEmail.java +++ b/src/main/java/fr/xephi/authme/process/email/AsyncChangeEmail.java @@ -5,9 +5,9 @@ import fr.xephi.authme.data.auth.PlayerCache; import fr.xephi.authme.datasource.DataSource; import fr.xephi.authme.message.MessageKey; import fr.xephi.authme.process.AsynchronousProcess; -import fr.xephi.authme.service.bungeecord.BungeeService; import fr.xephi.authme.service.CommonService; import fr.xephi.authme.service.ValidationService; +import fr.xephi.authme.service.bungeecord.BungeeSender; import fr.xephi.authme.service.bungeecord.MessageType; import org.bukkit.entity.Player; @@ -31,7 +31,7 @@ public class AsyncChangeEmail implements AsynchronousProcess { private ValidationService validationService; @Inject - private BungeeService bungeeService; + private BungeeSender bungeeSender; AsyncChangeEmail() { } @@ -68,7 +68,7 @@ public class AsyncChangeEmail implements AsynchronousProcess { auth.setEmail(newEmail); if (dataSource.updateEmail(auth)) { playerCache.updatePlayer(auth); - bungeeService.sendAuthMeBungeecordMessage(MessageType.REFRESH_EMAIL, player.getName()); + bungeeSender.sendAuthMeBungeecordMessage(MessageType.REFRESH_EMAIL, player.getName()); service.send(player, MessageKey.EMAIL_CHANGED_SUCCESS); } else { service.send(player, MessageKey.ERROR); diff --git a/src/main/java/fr/xephi/authme/process/login/AsynchronousLogin.java b/src/main/java/fr/xephi/authme/process/login/AsynchronousLogin.java index 03dc9f46e..e76fea783 100644 --- a/src/main/java/fr/xephi/authme/process/login/AsynchronousLogin.java +++ b/src/main/java/fr/xephi/authme/process/login/AsynchronousLogin.java @@ -19,9 +19,9 @@ import fr.xephi.authme.process.AsynchronousProcess; import fr.xephi.authme.process.SyncProcessManager; import fr.xephi.authme.security.PasswordSecurity; import fr.xephi.authme.service.BukkitService; -import fr.xephi.authme.service.bungeecord.BungeeService; import fr.xephi.authme.service.CommonService; import fr.xephi.authme.service.SessionService; +import fr.xephi.authme.service.bungeecord.BungeeSender; import fr.xephi.authme.service.bungeecord.MessageType; import fr.xephi.authme.settings.properties.DatabaseSettings; import fr.xephi.authme.settings.properties.EmailSettings; @@ -76,7 +76,7 @@ public class AsynchronousLogin implements AsynchronousProcess { private SessionService sessionService; @Inject - private BungeeService bungeeService; + private BungeeSender bungeeSender; AsynchronousLogin() { } @@ -226,7 +226,7 @@ public class AsynchronousLogin implements AsynchronousProcess { auth.setLastLogin(System.currentTimeMillis()); auth.setLastIp(ip); dataSource.updateSession(auth); - bungeeService.sendAuthMeBungeecordMessage(MessageType.REFRESH_SESSION, player.getName()); + bungeeSender.sendAuthMeBungeecordMessage(MessageType.REFRESH_SESSION, player.getName()); // Successful login, so reset the captcha & temp ban count final String name = player.getName(); @@ -252,7 +252,7 @@ public class AsynchronousLogin implements AsynchronousProcess { playerCache.updatePlayer(auth); dataSource.setLogged(name); sessionService.grantSession(name); - bungeeService.sendAuthMeBungeecordMessage(MessageType.LOGIN, name); + bungeeSender.sendAuthMeBungeecordMessage(MessageType.LOGIN, name); // As the scheduling executes the Task most likely after the current // task, we schedule it in the end 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 752b3748a..359aca033 100644 --- a/src/main/java/fr/xephi/authme/process/login/ProcessSyncPlayerLogin.java +++ b/src/main/java/fr/xephi/authme/process/login/ProcessSyncPlayerLogin.java @@ -8,10 +8,10 @@ import fr.xephi.authme.events.LoginEvent; import fr.xephi.authme.events.RestoreInventoryEvent; import fr.xephi.authme.process.SynchronousProcess; import fr.xephi.authme.service.BukkitService; -import fr.xephi.authme.service.bungeecord.BungeeService; import fr.xephi.authme.service.CommonService; import fr.xephi.authme.service.JoinMessageService; import fr.xephi.authme.service.TeleportationService; +import fr.xephi.authme.service.bungeecord.BungeeSender; import fr.xephi.authme.settings.WelcomeMessageConfiguration; import fr.xephi.authme.settings.commandconfig.CommandManager; import fr.xephi.authme.settings.properties.RegistrationSettings; @@ -25,7 +25,7 @@ import static fr.xephi.authme.settings.properties.RestrictionSettings.PROTECT_IN public class ProcessSyncPlayerLogin implements SynchronousProcess { @Inject - private BungeeService bungeeService; + private BungeeSender bungeeSender; @Inject private LimboService limboService; @@ -96,6 +96,6 @@ public class ProcessSyncPlayerLogin implements SynchronousProcess { commandManager.runCommandsOnLogin(player); // Send Bungee stuff. The service will check if it is enabled or not. - bungeeService.connectPlayerOnLogin(player); + bungeeSender.connectPlayerOnLogin(player); } } diff --git a/src/main/java/fr/xephi/authme/process/logout/AsynchronousLogout.java b/src/main/java/fr/xephi/authme/process/logout/AsynchronousLogout.java index a64b0e5a4..41cedf4bc 100644 --- a/src/main/java/fr/xephi/authme/process/logout/AsynchronousLogout.java +++ b/src/main/java/fr/xephi/authme/process/logout/AsynchronousLogout.java @@ -7,9 +7,9 @@ import fr.xephi.authme.datasource.DataSource; import fr.xephi.authme.message.MessageKey; import fr.xephi.authme.process.AsynchronousProcess; import fr.xephi.authme.process.SyncProcessManager; -import fr.xephi.authme.service.bungeecord.BungeeService; import fr.xephi.authme.service.CommonService; import fr.xephi.authme.service.SessionService; +import fr.xephi.authme.service.bungeecord.BungeeSender; import fr.xephi.authme.service.bungeecord.MessageType; import fr.xephi.authme.settings.properties.RestrictionSettings; import org.bukkit.entity.Player; @@ -40,7 +40,7 @@ public class AsynchronousLogout implements AsynchronousProcess { private SessionService sessionService; @Inject - private BungeeService bungeeService; + private BungeeSender bungeeSender; AsynchronousLogout() { } @@ -59,18 +59,18 @@ public class AsynchronousLogout implements AsynchronousProcess { PlayerAuth auth = playerCache.getAuth(name); database.updateSession(auth); - bungeeService.sendAuthMeBungeecordMessage(MessageType.REFRESH_SESSION, name); + bungeeSender.sendAuthMeBungeecordMessage(MessageType.REFRESH_SESSION, name); if (service.getProperty(RestrictionSettings.SAVE_QUIT_LOCATION)) { auth.setQuitLocation(player.getLocation()); database.updateQuitLoc(auth); - bungeeService.sendAuthMeBungeecordMessage(MessageType.REFRESH_QUITLOC, name); + bungeeSender.sendAuthMeBungeecordMessage(MessageType.REFRESH_QUITLOC, name); } playerCache.removePlayer(name); codeManager.unverify(name); database.setUnlogged(name); sessionService.revokeSession(name); - bungeeService.sendAuthMeBungeecordMessage(MessageType.LOGOUT, name); + bungeeSender.sendAuthMeBungeecordMessage(MessageType.LOGOUT, name); syncProcessManager.processSyncPlayerLogout(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 66d6938f5..b5643217f 100644 --- a/src/main/java/fr/xephi/authme/process/quit/AsynchronousQuit.java +++ b/src/main/java/fr/xephi/authme/process/quit/AsynchronousQuit.java @@ -10,7 +10,7 @@ import fr.xephi.authme.process.SyncProcessManager; import fr.xephi.authme.service.CommonService; import fr.xephi.authme.service.SessionService; import fr.xephi.authme.service.ValidationService; -import fr.xephi.authme.service.bungeecord.BungeeService; +import fr.xephi.authme.service.bungeecord.BungeeSender; import fr.xephi.authme.service.bungeecord.MessageType; import fr.xephi.authme.settings.SpawnLoader; import fr.xephi.authme.settings.properties.PluginSettings; @@ -54,7 +54,7 @@ public class AsynchronousQuit implements AsynchronousProcess { private SessionService sessionService; @Inject - private BungeeService bungeeService; + private BungeeSender bungeeSender; AsynchronousQuit() { } @@ -88,7 +88,7 @@ public class AsynchronousQuit implements AsynchronousProcess { .lastLogin(System.currentTimeMillis()) .build(); database.updateSession(auth); - bungeeService.sendAuthMeBungeecordMessage(MessageType.REFRESH_QUITLOC, name); + bungeeSender.sendAuthMeBungeecordMessage(MessageType.REFRESH_QUITLOC, name); } //always unauthenticate the player - use session only for auto logins on the same ip 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 a48942fd5..11f6354c9 100644 --- a/src/main/java/fr/xephi/authme/process/register/AsyncRegister.java +++ b/src/main/java/fr/xephi/authme/process/register/AsyncRegister.java @@ -8,10 +8,10 @@ import fr.xephi.authme.message.MessageKey; import fr.xephi.authme.permission.PermissionsManager; import fr.xephi.authme.process.AsynchronousProcess; import fr.xephi.authme.process.register.executors.RegistrationExecutor; -import fr.xephi.authme.process.register.executors.RegistrationParameters; import fr.xephi.authme.process.register.executors.RegistrationMethod; -import fr.xephi.authme.service.bungeecord.BungeeService; +import fr.xephi.authme.process.register.executors.RegistrationParameters; import fr.xephi.authme.service.CommonService; +import fr.xephi.authme.service.bungeecord.BungeeSender; import fr.xephi.authme.service.bungeecord.MessageType; import fr.xephi.authme.settings.properties.RegistrationSettings; import fr.xephi.authme.settings.properties.RestrictionSettings; @@ -39,7 +39,7 @@ public class AsyncRegister implements AsynchronousProcess { @Inject private SingletonStore registrationExecutorFactory; @Inject - private BungeeService bungeeService; + private BungeeSender bungeeSender; AsyncRegister() { } @@ -88,7 +88,7 @@ public class AsyncRegister implements AsynchronousProcess { PlayerAuth auth = executor.buildPlayerAuth(parameters); if (database.saveAuth(auth)) { executor.executePostPersistAction(parameters); - bungeeService.sendAuthMeBungeecordMessage(MessageType.REGISTER, parameters.getPlayerName()); + bungeeSender.sendAuthMeBungeecordMessage(MessageType.REGISTER, parameters.getPlayerName()); } else { service.send(parameters.getPlayer(), MessageKey.ERROR); } 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 a1ac8663b..589c88aa2 100644 --- a/src/main/java/fr/xephi/authme/process/register/ProcessSyncPasswordRegister.java +++ b/src/main/java/fr/xephi/authme/process/register/ProcessSyncPasswordRegister.java @@ -4,8 +4,8 @@ import fr.xephi.authme.ConsoleLogger; import fr.xephi.authme.data.limbo.LimboService; import fr.xephi.authme.message.MessageKey; import fr.xephi.authme.process.SynchronousProcess; -import fr.xephi.authme.service.bungeecord.BungeeService; import fr.xephi.authme.service.CommonService; +import fr.xephi.authme.service.bungeecord.BungeeSender; import fr.xephi.authme.settings.commandconfig.CommandManager; import fr.xephi.authme.settings.properties.EmailSettings; import fr.xephi.authme.settings.properties.RegistrationSettings; @@ -19,7 +19,7 @@ import javax.inject.Inject; public class ProcessSyncPasswordRegister implements SynchronousProcess { @Inject - private BungeeService bungeeService; + private BungeeSender bungeeSender; @Inject private CommonService service; @@ -72,6 +72,6 @@ public class ProcessSyncPasswordRegister implements SynchronousProcess { } // Send Bungee stuff. The service will check if it is enabled or not. - bungeeService.connectPlayerOnLogin(player); + bungeeSender.connectPlayerOnLogin(player); } } diff --git a/src/main/java/fr/xephi/authme/process/unregister/AsynchronousUnregister.java b/src/main/java/fr/xephi/authme/process/unregister/AsynchronousUnregister.java index d4845c937..70a3d1611 100644 --- a/src/main/java/fr/xephi/authme/process/unregister/AsynchronousUnregister.java +++ b/src/main/java/fr/xephi/authme/process/unregister/AsynchronousUnregister.java @@ -11,9 +11,9 @@ import fr.xephi.authme.message.MessageKey; import fr.xephi.authme.process.AsynchronousProcess; import fr.xephi.authme.security.PasswordSecurity; import fr.xephi.authme.service.BukkitService; -import fr.xephi.authme.service.bungeecord.BungeeService; import fr.xephi.authme.service.CommonService; import fr.xephi.authme.service.TeleportationService; +import fr.xephi.authme.service.bungeecord.BungeeSender; import fr.xephi.authme.service.bungeecord.MessageType; import fr.xephi.authme.settings.commandconfig.CommandManager; import fr.xephi.authme.settings.properties.RegistrationSettings; @@ -54,7 +54,7 @@ public class AsynchronousUnregister implements AsynchronousProcess { private CommandManager commandManager; @Inject - private BungeeService bungeeService; + private BungeeSender bungeeSender; AsynchronousUnregister() { } @@ -115,7 +115,7 @@ public class AsynchronousUnregister implements AsynchronousProcess { */ private void performPostUnregisterActions(String name, Player player) { playerCache.removePlayer(name); - bungeeService.sendAuthMeBungeecordMessage(MessageType.UNREGISTER, name); + bungeeSender.sendAuthMeBungeecordMessage(MessageType.UNREGISTER, name); if (player == null || !player.isOnline()) { return; diff --git a/src/main/java/fr/xephi/authme/service/bungeecord/BungeeReceiver.java b/src/main/java/fr/xephi/authme/service/bungeecord/BungeeReceiver.java new file mode 100644 index 000000000..0cc112aa9 --- /dev/null +++ b/src/main/java/fr/xephi/authme/service/bungeecord/BungeeReceiver.java @@ -0,0 +1,92 @@ +package fr.xephi.authme.service.bungeecord; + +import com.google.common.io.ByteArrayDataInput; +import com.google.common.io.ByteStreams; +import fr.xephi.authme.AuthMe; +import fr.xephi.authme.ConsoleLogger; +import fr.xephi.authme.datasource.DataSource; +import fr.xephi.authme.initialization.SettingsDependent; +import fr.xephi.authme.process.Management; +import fr.xephi.authme.service.BukkitService; +import fr.xephi.authme.settings.Settings; +import fr.xephi.authme.settings.properties.HooksSettings; +import org.bukkit.entity.Player; +import org.bukkit.plugin.messaging.Messenger; +import org.bukkit.plugin.messaging.PluginMessageListener; + +import javax.inject.Inject; + +public class BungeeReceiver implements PluginMessageListener, SettingsDependent { + + private final AuthMe plugin; + private final BukkitService bukkitService; + private final Management management; + private final DataSource dataSource; + + private boolean isEnabled; + + @Inject + BungeeReceiver(AuthMe plugin, BukkitService bukkitService, Management management, DataSource dataSource, + Settings settings) { + this.plugin = plugin; + this.bukkitService = bukkitService; + this.management = management; + this.dataSource = dataSource; + reload(settings); + } + + @Override + public void reload(Settings settings) { + this.isEnabled = settings.getProperty(HooksSettings.BUNGEECORD); + + if (this.isEnabled) { + Messenger messenger = plugin.getServer().getMessenger(); + if (!messenger.isIncomingChannelRegistered(plugin, "BungeeCord")) { + messenger.registerIncomingPluginChannel(plugin, "BungeeCord", this); + } + } + } + + @Override + public void onPluginMessageReceived(String channel, Player player, byte[] data) { + if (!isEnabled) { + return; + } + + ByteArrayDataInput in = ByteStreams.newDataInput(data); + String subchannel = in.readUTF(); + if (!"AuthMe".equals(subchannel)) { + return; + } + + String type = in.readUTF(); + String name = in.readUTF(); + switch (type) { + case MessageType.UNREGISTER: + dataSource.invalidateCache(name); + break; + case MessageType.REFRESH_PASSWORD: + case MessageType.REFRESH_QUITLOC: + case MessageType.REFRESH_EMAIL: + case MessageType.REFRESH: + dataSource.refreshCache(name); + break; + case MessageType.BUNGEE_LOGIN: + handleBungeeLogin(name); + break; + default: + ConsoleLogger.debug("Received unsupported bungeecord message type! ({0})", type); + } + } + + private void handleBungeeLogin(String name) { + Player player = bukkitService.getPlayerExact(name); + if (player != null && player.isOnline()) { + management.forceLogin(player); + ConsoleLogger.info("The user " + player.getName() + " has been automatically logged in, " + + "as requested by the AuthMeBungee integration."); + } + + } + +} diff --git a/src/main/java/fr/xephi/authme/service/bungeecord/BungeeSender.java b/src/main/java/fr/xephi/authme/service/bungeecord/BungeeSender.java new file mode 100644 index 000000000..0a8e53ecf --- /dev/null +++ b/src/main/java/fr/xephi/authme/service/bungeecord/BungeeSender.java @@ -0,0 +1,82 @@ +package fr.xephi.authme.service.bungeecord; + +import com.google.common.io.ByteArrayDataOutput; +import com.google.common.io.ByteStreams; +import fr.xephi.authme.AuthMe; +import fr.xephi.authme.initialization.SettingsDependent; +import fr.xephi.authme.service.BukkitService; +import fr.xephi.authme.settings.Settings; +import fr.xephi.authme.settings.properties.HooksSettings; +import org.bukkit.entity.Player; +import org.bukkit.plugin.messaging.Messenger; + +import javax.inject.Inject; + +public class BungeeSender implements SettingsDependent { + + private final AuthMe plugin; + private final BukkitService bukkitService; + + private boolean isEnabled; + private String destinationServerOnLogin; + + /* + * Constructor. + */ + @Inject + BungeeSender(AuthMe plugin, BukkitService bukkitService, Settings settings) { + this.plugin = plugin; + this.bukkitService = bukkitService; + reload(settings); + } + + @Override + public void reload(Settings settings) { + this.isEnabled = settings.getProperty(HooksSettings.BUNGEECORD); + this.destinationServerOnLogin = settings.getProperty(HooksSettings.BUNGEECORD_SERVER); + + if (this.isEnabled) { + Messenger messenger = plugin.getServer().getMessenger(); + if (!messenger.isOutgoingChannelRegistered(plugin, "BungeeCord")) { + messenger.registerOutgoingPluginChannel(plugin, "BungeeCord"); + } + } + } + + public boolean isEnabled() { + return isEnabled; + } + + private void sendBungeecordMessage(String... data) { + ByteArrayDataOutput out = ByteStreams.newDataOutput(); + for (String element : data) { + out.writeUTF(element); + } + bukkitService.sendPluginMessage("BungeeCord", out.toByteArray()); + } + + /** + * Send a player to a specified server. If no server is configured, this will + * do nothing. + * + * @param player The player to send. + */ + public void connectPlayerOnLogin(Player player) { + if (isEnabled && !destinationServerOnLogin.isEmpty()) { + bukkitService.scheduleSyncDelayedTask(() -> + sendBungeecordMessage("Connect", player.getName(), destinationServerOnLogin), 20L); + } + } + + /** + * Sends a message to the AuthMe plugin messaging channel, if enabled. + * + * @param type The message type, See {@link MessageType} + * @param playerName the player related to the message + */ + public void sendAuthMeBungeecordMessage(String type, String playerName) { + if (isEnabled) { + sendBungeecordMessage("AuthMe", type, playerName.toLowerCase()); + } + } +} diff --git a/src/main/java/fr/xephi/authme/service/bungeecord/BungeeService.java b/src/main/java/fr/xephi/authme/service/bungeecord/BungeeService.java deleted file mode 100644 index 3ff3c16c4..000000000 --- a/src/main/java/fr/xephi/authme/service/bungeecord/BungeeService.java +++ /dev/null @@ -1,121 +0,0 @@ -package fr.xephi.authme.service.bungeecord; - -import com.google.common.io.ByteArrayDataInput; -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.datasource.DataSource; -import fr.xephi.authme.initialization.SettingsDependent; -import fr.xephi.authme.service.BukkitService; -import fr.xephi.authme.settings.Settings; -import fr.xephi.authme.settings.properties.HooksSettings; -import org.bukkit.entity.Player; -import org.bukkit.plugin.messaging.Messenger; -import org.bukkit.plugin.messaging.PluginMessageListener; - -import javax.inject.Inject; - -/** - * Class to manage all BungeeCord related processes. - */ -public class BungeeService implements SettingsDependent, PluginMessageListener { - - private final AuthMe plugin; - private final BukkitService service; - private final DataSource dataSource; - - private boolean isEnabled; - private String destinationServerOnLogin; - - - /* - * Constructor. - */ - @Inject - BungeeService(AuthMe plugin, BukkitService service, Settings settings, DataSource dataSource) { - this.plugin = plugin; - this.service = service; - this.dataSource = dataSource; - reload(settings); - } - - @Override - public void reload(Settings settings) { - this.isEnabled = settings.getProperty(HooksSettings.BUNGEECORD); - this.destinationServerOnLogin = settings.getProperty(HooksSettings.BUNGEECORD_SERVER); - Messenger messenger = plugin.getServer().getMessenger(); - if (!this.isEnabled) { - return; - } - if (!messenger.isOutgoingChannelRegistered(plugin, "BungeeCord")) { - messenger.registerOutgoingPluginChannel(plugin, "BungeeCord"); - } - if (!messenger.isIncomingChannelRegistered(plugin, "BungeeCord")) { - messenger.registerIncomingPluginChannel(plugin, "BungeeCord", this); - } - } - - private void sendBungeecordMessage(String... data) { - ByteArrayDataOutput out = ByteStreams.newDataOutput(); - for (String element : data) { - out.writeUTF(element); - } - service.sendPluginMessage("BungeeCord", out.toByteArray()); - } - - /** - * Send a player to a specified server. If no server is configured, this will - * do nothing. - * - * @param player The player to send. - */ - public void connectPlayerOnLogin(Player player) { - if (isEnabled && !destinationServerOnLogin.isEmpty()) { - service.scheduleSyncDelayedTask(() -> - sendBungeecordMessage("Connect", player.getName(), destinationServerOnLogin), 20L); - } - } - - /** - * Sends a message to the AuthMe plugin messaging channel, if enabled. - * - * @param type The message type, See {@link MessageType} - * @param playerName the player related to the message - */ - public void sendAuthMeBungeecordMessage(String type, String playerName) { - if (isEnabled) { - sendBungeecordMessage("AuthMe", type, playerName.toLowerCase()); - } - } - - @Override - public void onPluginMessageReceived(String channel, Player player, byte[] data) { - if (!isEnabled) { - return; - } - - ByteArrayDataInput in = ByteStreams.newDataInput(data); - String subchannel = in.readUTF(); - if (!"AuthMe".equals(subchannel)) { - return; - } - - String type = in.readUTF(); - String name = in.readUTF(); - switch (type) { - case MessageType.UNREGISTER: - dataSource.invalidateCache(name); - break; - case MessageType.REFRESH_PASSWORD: - case MessageType.REFRESH_QUITLOC: - case MessageType.REFRESH_EMAIL: - case MessageType.REFRESH: - dataSource.refreshCache(name); - break; - default: - ConsoleLogger.debug("Received unsupported bungeecord message type! ({0})", type); - } - } - -} diff --git a/src/main/java/fr/xephi/authme/service/bungeecord/MessageType.java b/src/main/java/fr/xephi/authme/service/bungeecord/MessageType.java index 1e692a934..c8543bea1 100644 --- a/src/main/java/fr/xephi/authme/service/bungeecord/MessageType.java +++ b/src/main/java/fr/xephi/authme/service/bungeecord/MessageType.java @@ -11,6 +11,7 @@ public final class MessageType { public static final String REFRESH_QUITLOC = "refresh.quitloc"; public static final String REFRESH_EMAIL = "refresh.email"; public static final String REFRESH = "refresh"; + public static final String BUNGEE_LOGIN = "bungeelogin"; private MessageType() { } diff --git a/src/test/java/fr/xephi/authme/AuthMeInitializationTest.java b/src/test/java/fr/xephi/authme/AuthMeInitializationTest.java index aa20e8570..d0f3c1b3e 100644 --- a/src/test/java/fr/xephi/authme/AuthMeInitializationTest.java +++ b/src/test/java/fr/xephi/authme/AuthMeInitializationTest.java @@ -16,6 +16,8 @@ import fr.xephi.authme.process.Management; import fr.xephi.authme.process.login.ProcessSyncPlayerLogin; import fr.xephi.authme.security.PasswordSecurity; import fr.xephi.authme.service.BukkitService; +import fr.xephi.authme.service.bungeecord.BungeeReceiver; +import fr.xephi.authme.service.bungeecord.BungeeSender; import fr.xephi.authme.settings.Settings; import fr.xephi.authme.task.purge.PurgeService; import org.bukkit.Bukkit; @@ -112,10 +114,12 @@ public class AuthMeInitializationTest { // then // Take a few samples and ensure that they are not null + assertThat(injector.getIfAvailable(AuthMeApi.class), not(nullValue())); assertThat(injector.getIfAvailable(BlockListener.class), not(nullValue())); + assertThat(injector.getIfAvailable(BungeeReceiver.class), not(nullValue())); + assertThat(injector.getIfAvailable(BungeeSender.class), not(nullValue())); assertThat(injector.getIfAvailable(CommandHandler.class), not(nullValue())); assertThat(injector.getIfAvailable(Management.class), not(nullValue())); - assertThat(injector.getIfAvailable(AuthMeApi.class), not(nullValue())); assertThat(injector.getIfAvailable(PasswordSecurity.class), not(nullValue())); assertThat(injector.getIfAvailable(PermissionsManager.class), not(nullValue())); assertThat(injector.getIfAvailable(ProcessSyncPlayerLogin.class), not(nullValue())); diff --git a/src/test/java/fr/xephi/authme/command/executable/authme/PurgeLastPositionCommandTest.java b/src/test/java/fr/xephi/authme/command/executable/authme/PurgeLastPositionCommandTest.java index 0f14a0dee..57bb21e26 100644 --- a/src/test/java/fr/xephi/authme/command/executable/authme/PurgeLastPositionCommandTest.java +++ b/src/test/java/fr/xephi/authme/command/executable/authme/PurgeLastPositionCommandTest.java @@ -3,8 +3,8 @@ package fr.xephi.authme.command.executable.authme; import fr.xephi.authme.data.auth.PlayerAuth; import fr.xephi.authme.datasource.DataSource; import fr.xephi.authme.message.MessageKey; -import fr.xephi.authme.service.bungeecord.BungeeService; import fr.xephi.authme.service.CommonService; +import fr.xephi.authme.service.bungeecord.BungeeSender; import org.bukkit.command.CommandSender; import org.junit.Test; import org.junit.runner.RunWith; @@ -37,7 +37,7 @@ public class PurgeLastPositionCommandTest { private CommonService service; @Mock - private BungeeService bungeeService; + private BungeeSender bungeeSender; @Test public void shouldPurgeLastPosOfUser() { diff --git a/src/test/java/fr/xephi/authme/process/changepassword/AsyncChangePasswordTest.java b/src/test/java/fr/xephi/authme/process/changepassword/AsyncChangePasswordTest.java index 1cb9390e4..b0dd97bbe 100644 --- a/src/test/java/fr/xephi/authme/process/changepassword/AsyncChangePasswordTest.java +++ b/src/test/java/fr/xephi/authme/process/changepassword/AsyncChangePasswordTest.java @@ -6,8 +6,8 @@ import fr.xephi.authme.datasource.DataSource; import fr.xephi.authme.message.MessageKey; import fr.xephi.authme.security.PasswordSecurity; import fr.xephi.authme.security.crypts.HashedPassword; -import fr.xephi.authme.service.bungeecord.BungeeService; import fr.xephi.authme.service.CommonService; +import fr.xephi.authme.service.bungeecord.BungeeSender; import org.bukkit.command.CommandSender; import org.junit.Before; import org.junit.Test; @@ -39,7 +39,7 @@ public class AsyncChangePasswordTest { @Mock private PasswordSecurity passwordSecurity; @Mock - private BungeeService bungeeService; + private BungeeSender bungeeSender; @Before public void setUpLogger() { diff --git a/src/test/java/fr/xephi/authme/process/email/AsyncAddEmailTest.java b/src/test/java/fr/xephi/authme/process/email/AsyncAddEmailTest.java index a982f815c..b945e9f93 100644 --- a/src/test/java/fr/xephi/authme/process/email/AsyncAddEmailTest.java +++ b/src/test/java/fr/xephi/authme/process/email/AsyncAddEmailTest.java @@ -5,9 +5,9 @@ import fr.xephi.authme.data.auth.PlayerAuth; import fr.xephi.authme.data.auth.PlayerCache; import fr.xephi.authme.datasource.DataSource; import fr.xephi.authme.message.MessageKey; -import fr.xephi.authme.service.bungeecord.BungeeService; import fr.xephi.authme.service.CommonService; import fr.xephi.authme.service.ValidationService; +import fr.xephi.authme.service.bungeecord.BungeeSender; import org.bukkit.entity.Player; import org.junit.BeforeClass; import org.junit.Test; @@ -47,7 +47,7 @@ public class AsyncAddEmailTest { private ValidationService validationService; @Mock - private BungeeService bungeeService; + private BungeeSender bungeeSender; @BeforeClass public static void setUp() { diff --git a/src/test/java/fr/xephi/authme/process/email/AsyncChangeEmailTest.java b/src/test/java/fr/xephi/authme/process/email/AsyncChangeEmailTest.java index 5f2a9c83b..4427c45d6 100644 --- a/src/test/java/fr/xephi/authme/process/email/AsyncChangeEmailTest.java +++ b/src/test/java/fr/xephi/authme/process/email/AsyncChangeEmailTest.java @@ -4,9 +4,9 @@ import fr.xephi.authme.data.auth.PlayerAuth; import fr.xephi.authme.data.auth.PlayerCache; import fr.xephi.authme.datasource.DataSource; import fr.xephi.authme.message.MessageKey; -import fr.xephi.authme.service.bungeecord.BungeeService; import fr.xephi.authme.service.CommonService; import fr.xephi.authme.service.ValidationService; +import fr.xephi.authme.service.bungeecord.BungeeSender; import org.bukkit.entity.Player; import org.junit.Test; import org.junit.runner.RunWith; @@ -46,7 +46,7 @@ public class AsyncChangeEmailTest { private ValidationService validationService; @Mock - private BungeeService bungeeService; + private BungeeSender bungeeSender; @Test public void shouldChangeEmail() { diff --git a/src/test/java/fr/xephi/authme/process/unregister/AsynchronousUnregisterTest.java b/src/test/java/fr/xephi/authme/process/unregister/AsynchronousUnregisterTest.java index 1cb836055..61bba4227 100644 --- a/src/test/java/fr/xephi/authme/process/unregister/AsynchronousUnregisterTest.java +++ b/src/test/java/fr/xephi/authme/process/unregister/AsynchronousUnregisterTest.java @@ -10,9 +10,9 @@ import fr.xephi.authme.message.MessageKey; import fr.xephi.authme.security.PasswordSecurity; import fr.xephi.authme.security.crypts.HashedPassword; import fr.xephi.authme.service.BukkitService; -import fr.xephi.authme.service.bungeecord.BungeeService; import fr.xephi.authme.service.CommonService; import fr.xephi.authme.service.TeleportationService; +import fr.xephi.authme.service.bungeecord.BungeeSender; import fr.xephi.authme.settings.commandconfig.CommandManager; import fr.xephi.authme.settings.properties.RegistrationSettings; import org.bukkit.command.CommandSender; @@ -63,7 +63,7 @@ public class AsynchronousUnregisterTest { @Mock private CommandManager commandManager; @Mock - private BungeeService bungeeService; + private BungeeSender bungeeSender; @BeforeClass public static void initLogger() {