Changes BungeeCord messages to use UUID instead of player name

This commit is contained in:
Kristian Knarvik 2021-10-24 21:15:43 +02:00
parent 3367d4bb76
commit 822f8fb2b5
5 changed files with 62 additions and 43 deletions

View File

@ -9,8 +9,8 @@ import org.jetbrains.annotations.NotNull;
* This listener teleports a user if a valid message is received from BungeeCord
*
* <p>Specifically, if a string starts with SGBungee encoded to be readable by readUTF followed by
* PlayerName#@#DestinationPortal is received on the BungeeCord channel, this listener will teleport the player to the
* destination portal.</p>
* [PlayerUUID]delimiter[DestinationPortal] is received on the BungeeCord channel, this listener will teleport the
* player to the destination portal.</p>
*/
public class BungeeCordListener implements PluginMessageListener {
@ -24,7 +24,7 @@ public class BungeeCordListener implements PluginMessageListener {
@Override
public void onPluginMessageReceived(@NotNull String channel, @NotNull Player unused, byte[] message) {
//Ignore plugin messages if some other plugin message is received
if (!channel.equals("BungeeCord")) {
if (!channel.equals(BungeeHelper.getBungeeChannel())) {
return;
}

View File

@ -48,7 +48,7 @@ public class PlayerEventListener implements Listener {
Player player = event.getPlayer();
//Check if the player is waiting to be teleported to a stargate
String destination = BungeeHelper.removeFromQueue(player.getName());
String destination = BungeeHelper.removeFromQueue(player.getUniqueId());
if (destination == null) {
return;
}

View File

@ -110,7 +110,7 @@ public class PortalCreator {
//Check if the player can create portals on this network. If not, create a personal portal
if (!portalOptions.get(PortalOption.BUNGEE) && !PermissionHelper.canCreateNetworkGate(player, network)) {
Stargate.debug("createPortal", "Player doesn't have create permissions on network. Trying personal");
if (PermissionHelper.canCreatePersonalGate(player)) {
if (PermissionHelper.canCreatePersonalPortal(player)) {
network = player.getName();
if (network.length() > 11) {
network = network.substring(0, 11);
@ -127,7 +127,7 @@ public class PortalCreator {
//Check if the player can create this gate layout
String gateName = gate.getFilename();
gateName = gateName.substring(0, gateName.indexOf('.'));
if (!deny && !PermissionHelper.canCreateGate(player, gateName)) {
if (!deny && !PermissionHelper.canCreatePortal(player, gateName)) {
Stargate.debug("createPortal", "Player does not have access to gate layout");
deny = true;
denyMessage = Stargate.getString("createGateDeny");

View File

@ -13,6 +13,7 @@ import java.io.DataOutputStream;
import java.io.IOException;
import java.util.HashMap;
import java.util.Map;
import java.util.UUID;
/**
* This class contains helpful functions to help with sending and receiving BungeeCord plugin messages
@ -22,12 +23,21 @@ public final class BungeeHelper {
private final static String bungeeSubChannel = "SGBungee";
private final static String bungeeChannel = "BungeeCord";
private final static String teleportMessageDelimiter = "#@#";
private final static Map<String, String> bungeeQueue = new HashMap<>();
private final static Map<UUID, String> bungeeQueue = new HashMap<>();
private BungeeHelper() {
}
/**
* Get the plugin message channel use for BungeeCord messages
*
* @return <p>The bungee plugin channel</p>
*/
public static String getBungeeChannel() {
return bungeeChannel;
}
/**
* Removes a player from the queue of players teleporting through BungeeCord
*
@ -35,38 +45,43 @@ public final class BungeeHelper {
* server, it'll be added to this queue. Once the player joins this server, the player should be removed from the
* queue and teleported to the destination.</p>
*
* @param playerName <p>The name of the player to remove</p>
* @param playerUUID <p>The UUID of the player to remove</p>
* @return <p>The name of the destination portal the player should be teleported to</p>
*/
public static String removeFromQueue(String playerName) {
return bungeeQueue.remove(playerName.toLowerCase());
public static String removeFromQueue(UUID playerUUID) {
return bungeeQueue.remove(playerUUID);
}
/**
* Sends a plugin message to BungeeCord allowing the target server to catch it
*
* @param player <p>The player teleporting</p>
* @param player <p>The teleporting player</p>
* @param entrancePortal <p>The portal the player is teleporting from</p>
* @return <p>True if the message was successfully sent</p>
*/
public static boolean sendTeleportationMessage(Player player, Portal entrancePortal) {
try {
// Build the message, format is <player>#@#<destination>
String message = player.getName() + teleportMessageDelimiter + entrancePortal.getDestinationName();
// Build the message data, sent over the SGBungee BungeeCord channel
//Build the teleportation message, format is <player identifier>delimiter<destination>
String message = player.getUniqueId() + teleportMessageDelimiter + entrancePortal.getDestinationName();
ByteArrayOutputStream byteArrayOutputStream = new ByteArrayOutputStream();
DataOutputStream dataOutputStream = new DataOutputStream(byteArrayOutputStream);
//Build the message data and send it over the SGBungee BungeeCord channel
dataOutputStream.writeUTF("Forward");
dataOutputStream.writeUTF(entrancePortal.getNetwork()); // Server
//Specify SGBungee channel/tag
//Send the message to the server defined in the entrance portal's network line
dataOutputStream.writeUTF(entrancePortal.getNetwork());
//Specify the sub-channel/tag to make it recognizable on arrival
dataOutputStream.writeUTF(bungeeSubChannel);
//Length of the message
//Write the length of the message
dataOutputStream.writeShort(message.length());
//The data to send
//Write the actual message
dataOutputStream.writeBytes(message);
//Send the plugin message
player.sendPluginMessage(Stargate.stargate, bungeeChannel, byteArrayOutputStream.toByteArray());
} catch (IOException ex) {
Stargate.getConsoleLogger().severe(Stargate.getString("prefix") + "Error sending BungeeCord teleport packet");
Stargate.getConsoleLogger().severe(Stargate.getString("prefix") + "Error sending BungeeCord " +
"teleport packet");
ex.printStackTrace();
return false;
}
@ -74,21 +89,23 @@ public final class BungeeHelper {
}
/**
* Sends the bungee message necessary to change the server
* Sends the bungee message necessary to make a player connect to another server
*
* @param player <p>The player to teleport</p>
* @param entrancePortal <p>The bungee portal the player teleports from</p>
* @return <p>True if able to send the plugin message</p>
* @param entrancePortal <p>The bungee portal the player is teleporting from</p>
* @return <p>True if the plugin message was sent successfully</p>
*/
public static boolean changeServer(Player player, Portal entrancePortal) {
try {
ByteArrayOutputStream byteArrayOutputStream = new ByteArrayOutputStream();
DataOutputStream dataOutputStream = new DataOutputStream(byteArrayOutputStream);
//Send a connect-message to connect the player to the server defined in the entrance portal's network line
dataOutputStream.writeUTF("Connect");
dataOutputStream.writeUTF(entrancePortal.getNetwork());
//Send the plugin message
player.sendPluginMessage(Stargate.stargate, bungeeChannel, byteArrayOutputStream.toByteArray());
byteArrayOutputStream.reset();
} catch (IOException ex) {
Stargate.getConsoleLogger().severe(Stargate.getString("prefix") +
"Error sending BungeeCord connect packet");
@ -102,7 +119,7 @@ public final class BungeeHelper {
* Reads a plugin message byte array to a string if it's sent from another stargate plugin
*
* @param message <p>The byte array to read</p>
* @return <p>The message contained in the byte array or null on failure</p>
* @return <p>The message contained in the byte array, or null on failure</p>
*/
public static String readPluginMessage(byte[] message) {
// Get data from message
@ -114,8 +131,12 @@ public final class BungeeHelper {
if (!subChannel.equals(bungeeSubChannel)) {
return null;
}
//Get the length of the contained message
short dataLength = dataInputStream.readShort();
//Prepare a byte array for the sent message
data = new byte[dataLength];
//Read the message to the prepared array
dataInputStream.readFully(data);
} catch (IOException ex) {
Stargate.getConsoleLogger().severe(Stargate.getString("prefix") +
@ -123,29 +144,27 @@ public final class BungeeHelper {
ex.printStackTrace();
return null;
}
// Data should be player name, and destination gate name
return new String(data);
}
/**
* Handles the receival of a teleport message
*
* @param receivedMessage <p>The received message</p>
* @param receivedMessage <p>The received teleport message</p>
*/
public static void handleTeleportMessage(String receivedMessage) {
//Get the player id and destination from the message
String[] messageParts = receivedMessage.split(teleportMessageDelimiter);
String playerName = messageParts[0];
UUID playerUUID = UUID.fromString(messageParts[0]);
String destination = messageParts[1];
// Check if the player is online, if so, teleport, otherwise, queue
Player player = Stargate.server.getPlayer(playerName);
//Check if the player is online, if so, teleport, otherwise, queue
Player player = Stargate.server.getPlayer(playerUUID);
if (player == null) {
bungeeQueue.put(playerName.toLowerCase(), destination);
bungeeQueue.put(playerUUID, destination);
} else {
Portal destinationPortal = PortalHandler.getBungeePortal(destination);
// Specified an invalid gate. For now, we'll just let them connect at their current location
//If teleporting to an invalid portal, let the server decide where the player arrives
if (destinationPortal == null) {
Stargate.getConsoleLogger().info(Stargate.getString("prefix") + "Bungee gate " +
destination + " does not exist");

View File

@ -59,7 +59,7 @@ public final class PermissionHelper {
}
//Check if the player can use the private gate
if (portal.getOptions().isPrivate() && !PermissionHelper.canPrivate(player, portal)) {
if (portal.getOptions().isPrivate() && !PermissionHelper.canUsePrivatePortal(player, portal)) {
Stargate.getMessageSender().sendErrorMessage(player, Stargate.getString("denyMsg"));
return;
}
@ -121,15 +121,16 @@ public final class PermissionHelper {
*
* <p>This is the same as player.hasPermission(), but this function allows for printing permission debugging info.</p>
*
* @param player <p>The player to check</p>
* @param perm <p>The permission to check</p>
* @param player <p>The player to check</p>
* @param permission <p>The permission to check</p>
* @return <p>True if the player has the permission</p>
*/
public static boolean hasPermission(Player player, String perm) {
public static boolean hasPermission(Player player, String permission) {
if (Stargate.getStargateConfig().isPermissionDebuggingEnabled()) {
Stargate.debug("hasPerm::SuperPerm(" + player.getName() + ")", perm + " => " + player.hasPermission(perm));
Stargate.debug("hasPerm::SuperPerm(" + player.getName() + ")", permission + " => " +
player.hasPermission(permission));
}
return player.hasPermission(perm);
return player.hasPermission(permission);
}
/**
@ -264,7 +265,7 @@ public final class PermissionHelper {
* @param portal <p>The private portal used</p>
* @return <p>True if the player is allowed to use the portal</p>
*/
public static boolean canPrivate(Player player, Portal portal) {
public static boolean canUsePrivatePortal(Player player, Portal portal) {
//Check if the player is the owner of the gate
if (portal.isOwner(player)) {
return true;
@ -308,7 +309,6 @@ public final class PermissionHelper {
}
//Check for this specific network
return hasPermission(player, "stargate.create.network." + network);
}
/**
@ -317,7 +317,7 @@ public final class PermissionHelper {
* @param player <p>The player trying to create the new gate</p>
* @return <p>True if the player is allowed</p>
*/
public static boolean canCreatePersonalGate(Player player) {
public static boolean canCreatePersonalPortal(Player player) {
//Check for general create
if (hasPermission(player, "stargate.create")) {
return true;
@ -333,7 +333,7 @@ public final class PermissionHelper {
* @param gate <p>The gate type of the new portal</p>
* @return <p>True if the player is allowed to create a portal with the given gate layout</p>
*/
public static boolean canCreateGate(Player player, String gate) {
public static boolean canCreatePortal(Player player, String gate) {
//Check for general create
if (hasPermission(player, "stargate.create")) {
return true;