From 351431d1d89d4537ae5d866059f4d7e8a98d6729 Mon Sep 17 00:00:00 2001 From: Gnat008 Date: Mon, 11 Jul 2016 15:05:38 -0400 Subject: [PATCH] Move Bungee interactions to new service class --- .../changepassword/AsyncChangePassword.java | 25 ++-- .../process/login/ProcessSyncPlayerLogin.java | 39 ++----- .../ProcessSynchronousPlayerLogout.java | 23 ++-- .../register/ProcessSyncPasswordRegister.java | 34 ++---- .../xephi/authme/service/BungeeService.java | 107 ++++++++++++++++++ 5 files changed, 135 insertions(+), 93 deletions(-) create mode 100644 src/main/java/fr/xephi/authme/service/BungeeService.java 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 4389ac351..380714bc0 100644 --- a/src/main/java/fr/xephi/authme/process/changepassword/AsyncChangePassword.java +++ b/src/main/java/fr/xephi/authme/process/changepassword/AsyncChangePassword.java @@ -1,7 +1,5 @@ package fr.xephi.authme.process.changepassword; -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.auth.PlayerAuth; @@ -12,7 +10,7 @@ import fr.xephi.authme.process.AsynchronousProcess; import fr.xephi.authme.process.ProcessService; import fr.xephi.authme.security.PasswordSecurity; import fr.xephi.authme.security.crypts.HashedPassword; -import fr.xephi.authme.settings.properties.HooksSettings; +import fr.xephi.authme.service.BungeeService; import fr.xephi.authme.util.BukkitService; import org.bukkit.entity.Player; @@ -23,6 +21,9 @@ public class AsyncChangePassword implements AsynchronousProcess { @Inject private AuthMe plugin; + @Inject + private BungeeService bungeeService; + @Inject private DataSource dataSource; @@ -56,21 +57,9 @@ public class AsyncChangePassword implements AsynchronousProcess { playerCache.updatePlayer(auth); processService.send(player, MessageKey.PASSWORD_CHANGED_SUCCESS); ConsoleLogger.info(player.getName() + " changed his password"); - if (processService.getProperty(HooksSettings.BUNGEECORD)) { - final String hash = hashedPassword.getHash(); - final String salt = hashedPassword.getSalt(); - bukkitService.scheduleSyncDelayedTask(new Runnable() { - @Override - public void run() { - ByteArrayDataOutput out = ByteStreams.newDataOutput(); - out.writeUTF("Forward"); - out.writeUTF("ALL"); - out.writeUTF("AuthMe"); - out.writeUTF("changepassword;" + name + ";" + hash + ";" + salt); - player.sendPluginMessage(plugin, "BungeeCord", out.toByteArray()); - } - }); - } + + // Send a Bungee message for the password change + bungeeService.sendPasswordChanged(player, hashedPassword); } else { processService.send(player, MessageKey.WRONG_PASSWORD); } 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 1032bde93..9de55223b 100644 --- a/src/main/java/fr/xephi/authme/process/login/ProcessSyncPlayerLogin.java +++ b/src/main/java/fr/xephi/authme/process/login/ProcessSyncPlayerLogin.java @@ -1,7 +1,5 @@ package fr.xephi.authme.process.login; -import com.google.common.io.ByteArrayDataOutput; -import com.google.common.io.ByteStreams; import fr.xephi.authme.AuthMe; import fr.xephi.authme.cache.auth.PlayerAuth; import fr.xephi.authme.cache.limbo.LimboCache; @@ -12,7 +10,7 @@ import fr.xephi.authme.events.RestoreInventoryEvent; import fr.xephi.authme.listener.AuthMePlayerListener; import fr.xephi.authme.process.ProcessService; import fr.xephi.authme.process.SynchronousProcess; -import fr.xephi.authme.settings.properties.HooksSettings; +import fr.xephi.authme.service.BungeeService; import fr.xephi.authme.settings.properties.RegistrationSettings; import fr.xephi.authme.util.BukkitService; import fr.xephi.authme.util.TeleportationService; @@ -36,6 +34,9 @@ public class ProcessSyncPlayerLogin implements SynchronousProcess { @Inject private AuthMe plugin; + @Inject + private BungeeService bungeeService; + @Inject private ProcessService service; @@ -118,7 +119,7 @@ public class ProcessSyncPlayerLogin implements SynchronousProcess { // The Login event now fires (as intended) after everything is processed bukkitService.callEvent(new LoginEvent(player)); player.saveData(); - sendBungeeMessage(player); + bungeeService.sendBungeeMessage(player, "login"); // Login is done, display welcome message if (service.getProperty(RegistrationSettings.USE_WELCOME_MESSAGE)) { @@ -136,33 +137,7 @@ public class ProcessSyncPlayerLogin implements SynchronousProcess { // Login is now finished; we can force all commands forceCommands(player); - sendTo(player); - } - - private void sendTo(Player player) { - if (!service.getProperty(HooksSettings.BUNGEECORD)) { - return; - } - if (service.getProperty(HooksSettings.BUNGEECORD_SERVER).isEmpty()) { - return; - } - - ByteArrayDataOutput out = ByteStreams.newDataOutput(); - out.writeUTF("Connect"); - out.writeUTF(service.getProperty(HooksSettings.BUNGEECORD_SERVER)); - player.sendPluginMessage(plugin, "BungeeCord", out.toByteArray()); - } - - private void sendBungeeMessage(Player player) { - if (!service.getProperty(HooksSettings.BUNGEECORD)) { - return; - } - - ByteArrayDataOutput out = ByteStreams.newDataOutput(); - out.writeUTF("Forward"); - out.writeUTF("ALL"); - out.writeUTF("AuthMe"); - out.writeUTF("login;" + player.getName()); - player.sendPluginMessage(plugin, "BungeeCord", out.toByteArray()); + // Send Bungee stuff. The service will check if it is enabled or not. + bungeeService.connectPlayer(player); } } 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 9069577f1..5af96750a 100644 --- a/src/main/java/fr/xephi/authme/process/logout/ProcessSynchronousPlayerLogout.java +++ b/src/main/java/fr/xephi/authme/process/logout/ProcessSynchronousPlayerLogout.java @@ -1,7 +1,5 @@ package fr.xephi.authme.process.logout; -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.SessionManager; @@ -11,7 +9,7 @@ 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.properties.HooksSettings; +import fr.xephi.authme.service.BungeeService; import fr.xephi.authme.settings.properties.RegistrationSettings; import fr.xephi.authme.settings.properties.RestrictionSettings; import fr.xephi.authme.task.PlayerDataTaskManager; @@ -31,6 +29,9 @@ public class ProcessSynchronousPlayerLogout implements SynchronousProcess { @Inject private AuthMe plugin; + @Inject + private BungeeService bungeeService; + @Inject private ProcessService service; @@ -52,16 +53,6 @@ public class ProcessSynchronousPlayerLogout implements SynchronousProcess { ProcessSynchronousPlayerLogout() { } - - private void sendBungeeMessage(Player player) { - ByteArrayDataOutput out = ByteStreams.newDataOutput(); - out.writeUTF("Forward"); - out.writeUTF("ALL"); - out.writeUTF("AuthMe"); - out.writeUTF("logout;" + player.getName()); - player.sendPluginMessage(plugin, "BungeeCord", out.toByteArray()); - } - public void processSyncLogout(Player player) { final String name = player.getName().toLowerCase(); if (sessionManager.hasSession(name)) { @@ -78,9 +69,9 @@ public class ProcessSynchronousPlayerLogout implements SynchronousProcess { // Player is now logout... Time to fire event ! bukkitService.callEvent(new LogoutEvent(player)); - if (service.getProperty(HooksSettings.BUNGEECORD)) { - sendBungeeMessage(player); - } + // Send Bungee stuff. The service will check if it is enabled or not. + bungeeService.sendBungeeMessage(player, "logout"); + service.send(player, MessageKey.LOGOUT_SUCCESS); ConsoleLogger.info(player.getName() + " logged out"); } 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 555843518..04e4a21b7 100644 --- a/src/main/java/fr/xephi/authme/process/register/ProcessSyncPasswordRegister.java +++ b/src/main/java/fr/xephi/authme/process/register/ProcessSyncPasswordRegister.java @@ -1,7 +1,5 @@ package fr.xephi.authme.process.register; -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; @@ -10,9 +8,9 @@ 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.service.BungeeService; import fr.xephi.authme.settings.Settings; import fr.xephi.authme.settings.properties.EmailSettings; -import fr.xephi.authme.settings.properties.HooksSettings; import fr.xephi.authme.settings.properties.RegistrationSettings; import fr.xephi.authme.settings.properties.SecuritySettings; import fr.xephi.authme.task.PlayerDataTaskManager; @@ -32,6 +30,9 @@ public class ProcessSyncPasswordRegister implements SynchronousProcess { @Inject private AuthMe plugin; + @Inject + private BungeeService bungeeService; + @Inject private ProcessService service; @@ -47,16 +48,6 @@ public class ProcessSyncPasswordRegister implements SynchronousProcess { ProcessSyncPasswordRegister() { } - - private void sendBungeeMessage(Player player) { - ByteArrayDataOutput out = ByteStreams.newDataOutput(); - out.writeUTF("Forward"); - out.writeUTF("ALL"); - out.writeUTF("AuthMe"); - out.writeUTF("register;" + player.getName()); - player.sendPluginMessage(plugin, "BungeeCord", out.toByteArray()); - } - private void forceCommands(Player player) { for (String command : service.getProperty(RegistrationSettings.FORCE_REGISTER_COMMANDS)) { player.performCommand(command.replace("%p", player.getName())); @@ -127,19 +118,8 @@ public class ProcessSyncPasswordRegister implements SynchronousProcess { return; } - if (service.getProperty(HooksSettings.BUNGEECORD)) { - sendBungeeMessage(player); - } - - sendTo(player); - } - - private void sendTo(Player player) { - if (!service.getProperty(HooksSettings.BUNGEECORD_SERVER).isEmpty()) { - ByteArrayDataOutput out = ByteStreams.newDataOutput(); - out.writeUTF("Connect"); - out.writeUTF(service.getProperty(HooksSettings.BUNGEECORD_SERVER)); - player.sendPluginMessage(plugin, "BungeeCord", out.toByteArray()); - } + // Send Bungee stuff. The service will check if it is enabled or not. + bungeeService.sendBungeeMessage(player, "register"); + bungeeService.connectPlayer(player); } } diff --git a/src/main/java/fr/xephi/authme/service/BungeeService.java b/src/main/java/fr/xephi/authme/service/BungeeService.java new file mode 100644 index 000000000..c765c12a7 --- /dev/null +++ b/src/main/java/fr/xephi/authme/service/BungeeService.java @@ -0,0 +1,107 @@ +package fr.xephi.authme.service; + +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.security.crypts.HashedPassword; +import fr.xephi.authme.settings.NewSetting; +import fr.xephi.authme.settings.properties.HooksSettings; +import fr.xephi.authme.util.BukkitService; +import org.bukkit.entity.Player; + +import javax.inject.Inject; + +/** + * Class to manage all BungeeCord related processes. + */ +public class BungeeService implements SettingsDependent { + + private AuthMe plugin; + private BukkitService bukkitService; + + private boolean isEnabled; + private String bungeeServer; + + /** + * Constructor. + * + * @param plugin AuthMe plugin. + * @param settings AuthMe settings. + */ + @Inject + BungeeService(AuthMe plugin, BukkitService bukkitService, NewSetting settings) { + this.plugin = plugin; + this.bukkitService = bukkitService; + reload(settings); + } + + /** + * Sends a Bungee message to a player, e.g. login. + * + * @param player The player to send the message to. + * @param action The action to send, e.g. login. + */ + public void sendBungeeMessage(Player player, String action) { + if (!isEnabled) { + return; + } + + ByteArrayDataOutput out = ByteStreams.newDataOutput(); + out.writeUTF("Forward"); + out.writeUTF("ALL"); + out.writeUTF("AuthMe"); + out.writeUTF(action + ";" + player.getName()); + player.sendPluginMessage(plugin, "BungeeCord", out.toByteArray()); + } + + /** + * Send a Bungee message for a password change. + * + * @param player The player who's password is changed. + * @param password The new password. + */ + public void sendPasswordChanged(final Player player, HashedPassword password) { + if (!isEnabled) { + return; + } + + final String hash = password.getHash(); + final String salt = password.getSalt(); + + bukkitService.scheduleSyncDelayedTask(new Runnable() { + @Override + public void run() { + ByteArrayDataOutput out = ByteStreams.newDataOutput(); + out.writeUTF("Forward"); + out.writeUTF("ALL"); + out.writeUTF("AuthMe"); + out.writeUTF("changepassword;" + player.getName() + ";" + hash + ";" + salt); + player.sendPluginMessage(plugin, "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 connectPlayer(Player player) { + if (!isEnabled || bungeeServer.isEmpty()) { + return; + } + + ByteArrayDataOutput out = ByteStreams.newDataOutput(); + out.writeUTF("Connect"); + out.writeUTF(bungeeServer); + player.sendPluginMessage(plugin, "BungeeCord", out.toByteArray()); + } + + @Override + public void reload(NewSetting settings) { + this.isEnabled = settings.getProperty(HooksSettings.BUNGEECORD); + this.bungeeServer = settings.getProperty(HooksSettings.BUNGEECORD_SERVER); + } +}