Skip to content

Commit

Permalink
Move Bungee interactions to new service class
Browse files Browse the repository at this point in the history
  • Loading branch information
EbonJaeger committed Jul 11, 2016
1 parent 4f68589 commit 351431d
Show file tree
Hide file tree
Showing 5 changed files with 135 additions and 93 deletions.
Original file line number Diff line number Diff line change
@@ -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;
Expand All @@ -12,7 +10,7 @@
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;

Expand All @@ -23,6 +21,9 @@ public class AsyncChangePassword implements AsynchronousProcess {
@Inject
private AuthMe plugin;

@Inject
private BungeeService bungeeService;

@Inject
private DataSource dataSource;

Expand Down Expand Up @@ -56,21 +57,9 @@ public void changePassword(final Player player, String oldPassword, String newPa
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);
}
Expand Down
Original file line number Diff line number Diff line change
@@ -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;
Expand All @@ -12,7 +10,7 @@
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;
Expand All @@ -36,6 +34,9 @@ public class ProcessSyncPlayerLogin implements SynchronousProcess {
@Inject
private AuthMe plugin;

@Inject
private BungeeService bungeeService;

@Inject
private ProcessService service;

Expand Down Expand Up @@ -118,7 +119,7 @@ public void processPlayerLogin(Player player) {
// 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)) {
Expand All @@ -136,33 +137,7 @@ public void processPlayerLogin(Player player) {
// 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);
}
}
Original file line number Diff line number Diff line change
@@ -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;
Expand All @@ -11,7 +9,7 @@
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;
Expand All @@ -31,6 +29,9 @@ public class ProcessSynchronousPlayerLogout implements SynchronousProcess {
@Inject
private AuthMe plugin;

@Inject
private BungeeService bungeeService;

@Inject
private ProcessService service;

Expand All @@ -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)) {
Expand All @@ -78,9 +69,9 @@ public void processSyncLogout(Player player) {

// 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");
}
Expand Down
Original file line number Diff line number Diff line change
@@ -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;
Expand All @@ -10,9 +8,9 @@
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;
Expand All @@ -32,6 +30,9 @@ public class ProcessSyncPasswordRegister implements SynchronousProcess {
@Inject
private AuthMe plugin;

@Inject
private BungeeService bungeeService;

@Inject
private ProcessService service;

Expand All @@ -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()));
Expand Down Expand Up @@ -127,19 +118,8 @@ public void processPasswordRegister(Player player) {
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);
}
}
107 changes: 107 additions & 0 deletions src/main/java/fr/xephi/authme/service/BungeeService.java
Original file line number Diff line number Diff line change
@@ -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);
}
}

0 comments on commit 351431d

Please sign in to comment.