From 822f8fb2b52f809df766da3ad6493d0c3d04e508 Mon Sep 17 00:00:00 2001 From: EpicKnarvik97 Date: Sun, 24 Oct 2021 21:15:43 +0200 Subject: [PATCH] Changes BungeeCord messages to use UUID instead of player name --- .../stargate/listener/BungeeCordListener.java | 6 +- .../listener/PlayerEventListener.java | 2 +- .../stargate/portal/PortalCreator.java | 4 +- .../stargate/utility/BungeeHelper.java | 73 ++++++++++++------- .../stargate/utility/PermissionHelper.java | 20 ++--- 5 files changed, 62 insertions(+), 43 deletions(-) diff --git a/src/main/java/net/knarcraft/stargate/listener/BungeeCordListener.java b/src/main/java/net/knarcraft/stargate/listener/BungeeCordListener.java index 2a7d2e1..fba579d 100644 --- a/src/main/java/net/knarcraft/stargate/listener/BungeeCordListener.java +++ b/src/main/java/net/knarcraft/stargate/listener/BungeeCordListener.java @@ -9,8 +9,8 @@ import org.jetbrains.annotations.NotNull; * This listener teleports a user if a valid message is received from BungeeCord * *

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.

+ * [PlayerUUID]delimiter[DestinationPortal] is received on the BungeeCord channel, this listener will teleport the + * player to the destination portal.

*/ 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; } diff --git a/src/main/java/net/knarcraft/stargate/listener/PlayerEventListener.java b/src/main/java/net/knarcraft/stargate/listener/PlayerEventListener.java index d63ad65..4ba8599 100644 --- a/src/main/java/net/knarcraft/stargate/listener/PlayerEventListener.java +++ b/src/main/java/net/knarcraft/stargate/listener/PlayerEventListener.java @@ -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; } diff --git a/src/main/java/net/knarcraft/stargate/portal/PortalCreator.java b/src/main/java/net/knarcraft/stargate/portal/PortalCreator.java index cd5ecba..0714e2a 100644 --- a/src/main/java/net/knarcraft/stargate/portal/PortalCreator.java +++ b/src/main/java/net/knarcraft/stargate/portal/PortalCreator.java @@ -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"); diff --git a/src/main/java/net/knarcraft/stargate/utility/BungeeHelper.java b/src/main/java/net/knarcraft/stargate/utility/BungeeHelper.java index e0040a8..18577e8 100644 --- a/src/main/java/net/knarcraft/stargate/utility/BungeeHelper.java +++ b/src/main/java/net/knarcraft/stargate/utility/BungeeHelper.java @@ -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 bungeeQueue = new HashMap<>(); + private final static Map bungeeQueue = new HashMap<>(); private BungeeHelper() { } + /** + * Get the plugin message channel use for BungeeCord messages + * + * @return

The bungee plugin channel

+ */ + 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.

* - * @param playerName

The name of the player to remove

+ * @param playerUUID

The UUID of the player to remove

* @return

The name of the destination portal the player should be teleported to

*/ - 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

The player teleporting

+ * @param player

The teleporting player

* @param entrancePortal

The portal the player is teleporting from

* @return

True if the message was successfully sent

*/ public static boolean sendTeleportationMessage(Player player, Portal entrancePortal) { try { - // Build the message, format is #@# - String message = player.getName() + teleportMessageDelimiter + entrancePortal.getDestinationName(); - // Build the message data, sent over the SGBungee BungeeCord channel + //Build the teleportation message, format is delimiter + 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

The player to teleport

- * @param entrancePortal

The bungee portal the player teleports from

- * @return

True if able to send the plugin message

+ * @param entrancePortal

The bungee portal the player is teleporting from

+ * @return

True if the plugin message was sent successfully

*/ 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

The byte array to read

- * @return

The message contained in the byte array or null on failure

+ * @return

The message contained in the byte array, or null on failure

*/ 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

The received message

+ * @param receivedMessage

The received teleport message

*/ 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"); diff --git a/src/main/java/net/knarcraft/stargate/utility/PermissionHelper.java b/src/main/java/net/knarcraft/stargate/utility/PermissionHelper.java index 0b86582..3592b56 100644 --- a/src/main/java/net/knarcraft/stargate/utility/PermissionHelper.java +++ b/src/main/java/net/knarcraft/stargate/utility/PermissionHelper.java @@ -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 { * *

This is the same as player.hasPermission(), but this function allows for printing permission debugging info.

* - * @param player

The player to check

- * @param perm

The permission to check

+ * @param player

The player to check

+ * @param permission

The permission to check

* @return

True if the player has the permission

*/ - 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

The private portal used

* @return

True if the player is allowed to use the portal

*/ - 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

The player trying to create the new gate

* @return

True if the player is allowed

*/ - 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

The gate type of the new portal

* @return

True if the player is allowed to create a portal with the given gate layout

*/ - 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;