-
Notifications
You must be signed in to change notification settings - Fork 518
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Move Bungee interactions to new service class
- Loading branch information
1 parent
4f68589
commit 351431d
Showing
5 changed files
with
135 additions
and
93 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
107 changes: 107 additions & 0 deletions
107
src/main/java/fr/xephi/authme/service/BungeeService.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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); | ||
} | ||
} |