Move Bungee interactions to new service class

This commit is contained in:
Gnat008 2016-07-11 15:05:38 -04:00
parent 4f68589b76
commit 351431d1d8
5 changed files with 135 additions and 93 deletions

View File

@ -1,7 +1,5 @@
package fr.xephi.authme.process.changepassword; 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.AuthMe;
import fr.xephi.authme.ConsoleLogger; import fr.xephi.authme.ConsoleLogger;
import fr.xephi.authme.cache.auth.PlayerAuth; 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.process.ProcessService;
import fr.xephi.authme.security.PasswordSecurity; import fr.xephi.authme.security.PasswordSecurity;
import fr.xephi.authme.security.crypts.HashedPassword; 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 fr.xephi.authme.util.BukkitService;
import org.bukkit.entity.Player; import org.bukkit.entity.Player;
@ -23,6 +21,9 @@ public class AsyncChangePassword implements AsynchronousProcess {
@Inject @Inject
private AuthMe plugin; private AuthMe plugin;
@Inject
private BungeeService bungeeService;
@Inject @Inject
private DataSource dataSource; private DataSource dataSource;
@ -56,21 +57,9 @@ public class AsyncChangePassword implements AsynchronousProcess {
playerCache.updatePlayer(auth); playerCache.updatePlayer(auth);
processService.send(player, MessageKey.PASSWORD_CHANGED_SUCCESS); processService.send(player, MessageKey.PASSWORD_CHANGED_SUCCESS);
ConsoleLogger.info(player.getName() + " changed his password"); ConsoleLogger.info(player.getName() + " changed his password");
if (processService.getProperty(HooksSettings.BUNGEECORD)) {
final String hash = hashedPassword.getHash(); // Send a Bungee message for the password change
final String salt = hashedPassword.getSalt(); bungeeService.sendPasswordChanged(player, hashedPassword);
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());
}
});
}
} else { } else {
processService.send(player, MessageKey.WRONG_PASSWORD); processService.send(player, MessageKey.WRONG_PASSWORD);
} }

View File

@ -1,7 +1,5 @@
package fr.xephi.authme.process.login; 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.AuthMe;
import fr.xephi.authme.cache.auth.PlayerAuth; import fr.xephi.authme.cache.auth.PlayerAuth;
import fr.xephi.authme.cache.limbo.LimboCache; 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.listener.AuthMePlayerListener;
import fr.xephi.authme.process.ProcessService; import fr.xephi.authme.process.ProcessService;
import fr.xephi.authme.process.SynchronousProcess; 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.RegistrationSettings;
import fr.xephi.authme.util.BukkitService; import fr.xephi.authme.util.BukkitService;
import fr.xephi.authme.util.TeleportationService; import fr.xephi.authme.util.TeleportationService;
@ -36,6 +34,9 @@ public class ProcessSyncPlayerLogin implements SynchronousProcess {
@Inject @Inject
private AuthMe plugin; private AuthMe plugin;
@Inject
private BungeeService bungeeService;
@Inject @Inject
private ProcessService service; private ProcessService service;
@ -118,7 +119,7 @@ public class ProcessSyncPlayerLogin implements SynchronousProcess {
// The Login event now fires (as intended) after everything is processed // The Login event now fires (as intended) after everything is processed
bukkitService.callEvent(new LoginEvent(player)); bukkitService.callEvent(new LoginEvent(player));
player.saveData(); player.saveData();
sendBungeeMessage(player); bungeeService.sendBungeeMessage(player, "login");
// Login is done, display welcome message // Login is done, display welcome message
if (service.getProperty(RegistrationSettings.USE_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 // Login is now finished; we can force all commands
forceCommands(player); forceCommands(player);
sendTo(player); // Send Bungee stuff. The service will check if it is enabled or not.
} bungeeService.connectPlayer(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());
} }
} }

View File

@ -1,7 +1,5 @@
package fr.xephi.authme.process.logout; 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.AuthMe;
import fr.xephi.authme.ConsoleLogger; import fr.xephi.authme.ConsoleLogger;
import fr.xephi.authme.cache.SessionManager; 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.permission.AuthGroupType;
import fr.xephi.authme.process.ProcessService; import fr.xephi.authme.process.ProcessService;
import fr.xephi.authme.process.SynchronousProcess; 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.RegistrationSettings;
import fr.xephi.authme.settings.properties.RestrictionSettings; import fr.xephi.authme.settings.properties.RestrictionSettings;
import fr.xephi.authme.task.PlayerDataTaskManager; import fr.xephi.authme.task.PlayerDataTaskManager;
@ -31,6 +29,9 @@ public class ProcessSynchronousPlayerLogout implements SynchronousProcess {
@Inject @Inject
private AuthMe plugin; private AuthMe plugin;
@Inject
private BungeeService bungeeService;
@Inject @Inject
private ProcessService service; private ProcessService service;
@ -52,16 +53,6 @@ public class ProcessSynchronousPlayerLogout implements SynchronousProcess {
ProcessSynchronousPlayerLogout() { 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) { public void processSyncLogout(Player player) {
final String name = player.getName().toLowerCase(); final String name = player.getName().toLowerCase();
if (sessionManager.hasSession(name)) { if (sessionManager.hasSession(name)) {
@ -78,9 +69,9 @@ public class ProcessSynchronousPlayerLogout implements SynchronousProcess {
// Player is now logout... Time to fire event ! // Player is now logout... Time to fire event !
bukkitService.callEvent(new LogoutEvent(player)); bukkitService.callEvent(new LogoutEvent(player));
if (service.getProperty(HooksSettings.BUNGEECORD)) { // Send Bungee stuff. The service will check if it is enabled or not.
sendBungeeMessage(player); bungeeService.sendBungeeMessage(player, "logout");
}
service.send(player, MessageKey.LOGOUT_SUCCESS); service.send(player, MessageKey.LOGOUT_SUCCESS);
ConsoleLogger.info(player.getName() + " logged out"); ConsoleLogger.info(player.getName() + " logged out");
} }

View File

@ -1,7 +1,5 @@
package fr.xephi.authme.process.register; 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.AuthMe;
import fr.xephi.authme.ConsoleLogger; import fr.xephi.authme.ConsoleLogger;
import fr.xephi.authme.cache.limbo.LimboCache; 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.permission.AuthGroupType;
import fr.xephi.authme.process.ProcessService; import fr.xephi.authme.process.ProcessService;
import fr.xephi.authme.process.SynchronousProcess; import fr.xephi.authme.process.SynchronousProcess;
import fr.xephi.authme.service.BungeeService;
import fr.xephi.authme.settings.Settings; import fr.xephi.authme.settings.Settings;
import fr.xephi.authme.settings.properties.EmailSettings; 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.RegistrationSettings;
import fr.xephi.authme.settings.properties.SecuritySettings; import fr.xephi.authme.settings.properties.SecuritySettings;
import fr.xephi.authme.task.PlayerDataTaskManager; import fr.xephi.authme.task.PlayerDataTaskManager;
@ -32,6 +30,9 @@ public class ProcessSyncPasswordRegister implements SynchronousProcess {
@Inject @Inject
private AuthMe plugin; private AuthMe plugin;
@Inject
private BungeeService bungeeService;
@Inject @Inject
private ProcessService service; private ProcessService service;
@ -47,16 +48,6 @@ public class ProcessSyncPasswordRegister implements SynchronousProcess {
ProcessSyncPasswordRegister() { 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) { private void forceCommands(Player player) {
for (String command : service.getProperty(RegistrationSettings.FORCE_REGISTER_COMMANDS)) { for (String command : service.getProperty(RegistrationSettings.FORCE_REGISTER_COMMANDS)) {
player.performCommand(command.replace("%p", player.getName())); player.performCommand(command.replace("%p", player.getName()));
@ -127,19 +118,8 @@ public class ProcessSyncPasswordRegister implements SynchronousProcess {
return; return;
} }
if (service.getProperty(HooksSettings.BUNGEECORD)) { // Send Bungee stuff. The service will check if it is enabled or not.
sendBungeeMessage(player); bungeeService.sendBungeeMessage(player, "register");
} bungeeService.connectPlayer(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());
}
} }
} }

View File

@ -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);
}
}