diff --git a/src/main/java/net/knarcraft/stargate/listener/PlayerEventListener.java b/src/main/java/net/knarcraft/stargate/listener/PlayerEventListener.java index 7291f02..fe57541 100644 --- a/src/main/java/net/knarcraft/stargate/listener/PlayerEventListener.java +++ b/src/main/java/net/knarcraft/stargate/listener/PlayerEventListener.java @@ -82,6 +82,7 @@ public class PlayerEventListener implements Listener { //Check if the player is waiting to be teleported to a stargate String destination = BungeeHelper.removeFromQueue(player.getUniqueId()); if (destination == null) { + Stargate.debug("PlayerJoin", "No bungee request found in queue"); return; } diff --git a/src/main/java/net/knarcraft/stargate/portal/PortalActivator.java b/src/main/java/net/knarcraft/stargate/portal/PortalActivator.java index 718982d..9514fdb 100644 --- a/src/main/java/net/knarcraft/stargate/portal/PortalActivator.java +++ b/src/main/java/net/knarcraft/stargate/portal/PortalActivator.java @@ -79,10 +79,10 @@ public class PortalActivator { } //Get one random destination String randomDestination = ListHelper.getRandom(destinations); - return PortalHandler.getByName(Portal.cleanString(randomDestination), portalNetwork); + return PortalHandler.getByName(randomDestination, portalNetwork); } else { //Just return the normal fixed destination - return PortalHandler.getByName(Portal.cleanString(destination), portalNetwork); + return PortalHandler.getByName(destination, portalNetwork); } } @@ -249,8 +249,10 @@ public class PortalActivator { } activate = true; - Stargate.debug("cycleDestination", "Network Size: " + - PortalHandler.getNetwork(portal.getCleanNetwork()).size()); + List portalsInNetwork = PortalHandler.getNetwork(portal.getCleanNetwork()); + if (portalsInNetwork != null) { + Stargate.debug("cycleDestination", "Network Size: " + portalsInNetwork.size()); + } Stargate.debug("cycleDestination", "Player has access to: " + destinations.size()); } diff --git a/src/main/java/net/knarcraft/stargate/portal/PortalCreator.java b/src/main/java/net/knarcraft/stargate/portal/PortalCreator.java index 822b012..785201a 100644 --- a/src/main/java/net/knarcraft/stargate/portal/PortalCreator.java +++ b/src/main/java/net/knarcraft/stargate/portal/PortalCreator.java @@ -303,7 +303,7 @@ public class PortalCreator { if (portal.getOptions().isBungee()) { //Check if the bungee portal's name has been duplicated - if (PortalHandler.getBungeePortals().get(portal.getCleanName()) != null) { + if (PortalRegistry.getBungeePortal(portal.getCleanName()) != null) { Stargate.debug(route, "Gate name duplicate"); Stargate.getMessageSender().sendErrorMessage(player, Stargate.getString(Message.CREATION_NAME_COLLISION)); return false; @@ -317,7 +317,7 @@ public class PortalCreator { } //Check if the number of portals in the network has been surpassed - List networkList = PortalHandler.getAllPortalNetworks().get(portal.getCleanNetwork()); + List networkList = PortalHandler.getNetwork(portal.getCleanNetwork()); int maxGates = Stargate.getGateConfig().maxGatesEachNetwork(); if (maxGates > 0 && networkList != null && networkList.size() >= maxGates) { Stargate.getMessageSender().sendErrorMessage(player, Stargate.getString(Message.CREATION_NETWORK_FULL)); diff --git a/src/main/java/net/knarcraft/stargate/portal/PortalHandler.java b/src/main/java/net/knarcraft/stargate/portal/PortalHandler.java index 21b6433..ce90e7d 100644 --- a/src/main/java/net/knarcraft/stargate/portal/PortalHandler.java +++ b/src/main/java/net/knarcraft/stargate/portal/PortalHandler.java @@ -42,24 +42,14 @@ public class PortalHandler { return PortalRegistry.getAllPortalNetworks(); } - /** - * Gets a copy of all bungee portals - * - * @return

A copy of all bungee portals

- */ - @NotNull - public static Map getBungeePortals() { - return PortalRegistry.getBungeePortals(); - } - /** * Gets names of all portals within a network * * @param network

The network to get portals from

* @return

A list of portal names

*/ - @NotNull - public static List getNetwork(String network) { + @Nullable + public static List getNetwork(@NotNull String network) { return PortalRegistry.getNetwork(network); } @@ -75,7 +65,11 @@ public class PortalHandler { public static List getDestinations(@NotNull Portal entrancePortal, @Nullable Player player, @NotNull String network) { List destinations = new ArrayList<>(); - for (String destination : PortalRegistry.getAllPortalNetworks().get(network)) { + List portalsInNetwork = PortalRegistry.getNetwork(network); + if (portalsInNetwork == null) { + return List.of(); + } + for (String destination : portalsInNetwork) { Portal portal = getByName(destination, network); if (portal == null) { continue; @@ -148,17 +142,18 @@ public class PortalHandler { */ public static boolean isValidBungeePortal(@NotNull Map portalOptions, @NotNull Player player, @NotNull String destinationName, String network) { - if (portalOptions.get(PortalOption.BUNGEE)) { - if (!PermissionHelper.hasPermission(player, "stargate.admin.bungee")) { - Stargate.getMessageSender().sendErrorMessage(player, Stargate.getString(Message.BUNGEE_CREATION_DENIED)); - return false; - } else if (!Stargate.getGateConfig().enableBungee()) { - Stargate.getMessageSender().sendErrorMessage(player, Stargate.getString(Message.BUNGEE_DISABLED)); - return false; - } else if (destinationName.isEmpty() || network.isEmpty()) { - Stargate.getMessageSender().sendErrorMessage(player, Stargate.getString(Message.BUNGEE_MISSING_INFO)); - return false; - } + if (!portalOptions.get(PortalOption.BUNGEE)) { + return true; + } + if (!PermissionHelper.hasPermission(player, "stargate.admin.bungee")) { + Stargate.getMessageSender().sendErrorMessage(player, Stargate.getString(Message.BUNGEE_CREATION_DENIED)); + return false; + } else if (!Stargate.getGateConfig().enableBungee()) { + Stargate.getMessageSender().sendErrorMessage(player, Stargate.getString(Message.BUNGEE_DISABLED)); + return false; + } else if (destinationName.isEmpty() || network.isEmpty()) { + Stargate.getMessageSender().sendErrorMessage(player, Stargate.getString(Message.BUNGEE_MISSING_INFO)); + return false; } return true; } @@ -215,7 +210,11 @@ public class PortalHandler { * @param portal

The newly created portal

*/ public static void updatePortalsPointingAtNewPortal(@NotNull Portal portal) { - for (String originName : PortalRegistry.getAllPortalNetworks().get(portal.getCleanNetwork())) { + List portalsInNetwork = PortalRegistry.getNetwork(portal.getCleanNetwork()); + if (portalsInNetwork == null) { + return; + } + for (String originName : portalsInNetwork) { Portal origin = getByName(originName, portal.getCleanNetwork()); if (origin == null || !Portal.cleanString(origin.getDestinationName()).equals(portal.getCleanName()) || @@ -283,12 +282,7 @@ public class PortalHandler { */ @Nullable public static Portal getByName(@NotNull String name, @NotNull String network) { - Map> lookupMap = PortalRegistry.getPortalLookupByNetwork(); - if (!lookupMap.containsKey(network.toLowerCase())) { - return null; - } - return lookupMap.get(network.toLowerCase()).get(name.toLowerCase()); - + return PortalRegistry.getPortalInNetwork(Portal.cleanString(network), Portal.cleanString(name)); } /** @@ -371,7 +365,7 @@ public class PortalHandler { */ @Nullable public static Portal getByControl(@NotNull Block block) { - return PortalRegistry.getLookupControls().get(new BlockLocation(block)); + return PortalRegistry.getPortalFromControl(new BlockLocation(block)); } /** @@ -382,7 +376,7 @@ public class PortalHandler { */ @Nullable public static Portal getByBlock(@NotNull Block block) { - return PortalRegistry.getLookupBlocks().get(new BlockLocation(block)); + return PortalRegistry.getPortalFromFrame(new BlockLocation(block)); } /** @@ -393,7 +387,7 @@ public class PortalHandler { */ @Nullable public static Portal getBungeePortal(@NotNull String name) { - return PortalRegistry.getBungeePortals().get(name.toLowerCase()); + return PortalRegistry.getBungeePortal(name); } /** diff --git a/src/main/java/net/knarcraft/stargate/portal/PortalRegistry.java b/src/main/java/net/knarcraft/stargate/portal/PortalRegistry.java index 5977920..1948059 100644 --- a/src/main/java/net/knarcraft/stargate/portal/PortalRegistry.java +++ b/src/main/java/net/knarcraft/stargate/portal/PortalRegistry.java @@ -95,38 +95,47 @@ public class PortalRegistry { } /** - * Gets a copy of the lookup map for finding a portal by its frame + * Gets a portal that the given frame block belongs to * - * @return

A copy of the frame block lookup map

+ * @param blockLocation

The location that might be a frame block

+ * @return

The portal the frame block belongs to, or null

*/ - @NotNull - public static Map getLookupBlocks() { - return new HashMap<>(lookupBlocks); + @Nullable + public static Portal getPortalFromFrame(@NotNull BlockLocation blockLocation) { + return lookupBlocks.get(blockLocation); } /** - * Gets a copy of the lookup map for finding a portal by its control block + * Gets the portal that the given control block belongs to * - * @return

A copy of the control block lookup map

+ * @param blockLocation

The location that might be a portal control block

+ * @return

The portal the control block belongs to, or null

*/ - @NotNull - public static Map getLookupControls() { - return new HashMap<>(lookupControls); + @Nullable + public static Portal getPortalFromControl(@NotNull BlockLocation blockLocation) { + return lookupControls.get(blockLocation); } /** - * Gets a copy of the lookup map for finding all portals in a network + * Gets the portal identified by the given network name and portal name * - * @return

A copy of the network portal lookup map

+ * @param networkName

The name of the network the portal belongs to

+ * @param portalName

The name of the portal

+ * @return

The portal, or null if no such network and/or portal exists

*/ - @NotNull - public static Map> getPortalLookupByNetwork() { - return new HashMap<>(portalLookupByNetwork); + @Nullable + public static Portal getPortalInNetwork(@NotNull String networkName, @NotNull String portalName) { + Map portalsInNetwork = portalLookupByNetwork.get(Portal.cleanString(networkName)); + if (portalsInNetwork == null) { + return null; + } + return portalsInNetwork.get(Portal.cleanString(portalName)); } /** * Gets a portal from the location of a possible entrance * + * @param blockLocation

A location that might be a portal's entrance

* @return

A portal, or null

*/ @Nullable @@ -145,13 +154,14 @@ public class PortalRegistry { } /** - * Gets a copy of all bungee portals + * Gets the BungeeCord portal with the given name * - * @return

A copy of all bungee portals

+ * @param portalName

The name of the portal to get

+ * @return

The portal, or null

*/ - @NotNull - public static Map getBungeePortals() { - return new HashMap<>(bungeePortals); + @Nullable + public static Portal getBungeePortal(@NotNull String portalName) { + return bungeePortals.get(Portal.cleanString(portalName)); } /** @@ -160,7 +170,7 @@ public class PortalRegistry { * @param network

The network to get portals from

* @return

A list of portal names

*/ - @NotNull + @Nullable public static List getNetwork(@NotNull String network) { return allPortalNetworks.get(network.toLowerCase()); } diff --git a/src/main/java/net/knarcraft/stargate/portal/PortalSignDrawer.java b/src/main/java/net/knarcraft/stargate/portal/PortalSignDrawer.java index 4583457..c48548b 100644 --- a/src/main/java/net/knarcraft/stargate/portal/PortalSignDrawer.java +++ b/src/main/java/net/knarcraft/stargate/portal/PortalSignDrawer.java @@ -358,8 +358,7 @@ public class PortalSignDrawer { private void drawFixedSign(@NotNull SignData signData, @NotNull String[] output) { ChatColor highlightColor = signData.getHighlightSignColor(); ChatColor mainColor = signData.getMainSignColor(); - Portal destinationPortal = PortalHandler.getByName(Portal.cleanString(portal.getDestinationName()), - portal.getCleanNetwork()); + Portal destinationPortal = PortalHandler.getByName(portal.getDestinationName(), portal.getCleanNetwork()); String destinationName = portal.getOptions().isRandom() ? Stargate.getString(Message.SIGN_RANDOM) : (destinationPortal != null ? destinationPortal.getName() : portal.getDestinationName()); setLine(signData, 1, highlightColor + ">" + mainColor + translateAllColorCodes(destinationName) + @@ -371,8 +370,7 @@ public class PortalSignDrawer { setLine(signData, 2, highlightColor + "(" + mainColor + translateAllColorCodes(portal.getNetwork()) + highlightColor + ")", output); } - Portal destination = PortalHandler.getByName(Portal.cleanString(portal.getDestinationName()), - portal.getNetwork()); + Portal destination = PortalHandler.getByName(portal.getDestinationName(), portal.getNetwork()); if (destination == null && !portal.getOptions().isRandom()) { setLine(signData, 3, errorColor + Stargate.getString(Message.SIGN_DISCONNECTED), output); } else { diff --git a/src/main/java/net/knarcraft/stargate/utility/BungeeHelper.java b/src/main/java/net/knarcraft/stargate/utility/BungeeHelper.java index d586b2c..891c797 100644 --- a/src/main/java/net/knarcraft/stargate/utility/BungeeHelper.java +++ b/src/main/java/net/knarcraft/stargate/utility/BungeeHelper.java @@ -5,7 +5,6 @@ import net.knarcraft.stargate.config.Message; import net.knarcraft.stargate.portal.Portal; import net.knarcraft.stargate.portal.PortalHandler; import net.knarcraft.stargate.portal.teleporter.PlayerTeleporter; -import net.md_5.bungee.api.ChatColor; import org.bukkit.entity.Player; import org.bukkit.event.player.PlayerMoveEvent; import org.jetbrains.annotations.NotNull; @@ -77,7 +76,7 @@ public final class BungeeHelper { //Build the message data and send it over the SGBungee BungeeCord channel dataOutputStream.writeUTF("Forward"); //Send the message to the server defined in the entrance portal's network line - dataOutputStream.writeUTF(stripColor(entrancePortal.getNetwork())); + dataOutputStream.writeUTF(Portal.cleanString(entrancePortal.getNetwork())); //Specify the sub-channel/tag to make it recognizable on arrival dataOutputStream.writeUTF(bungeeSubChannel); //Write the length of the message @@ -107,7 +106,7 @@ public final class BungeeHelper { //Send a connect-message to connect the player to the server defined in the entrance portal's network line dataOutputStream.writeUTF("Connect"); - dataOutputStream.writeUTF(stripColor(entrancePortal.getNetwork())); + dataOutputStream.writeUTF(Portal.cleanString(entrancePortal.getNetwork())); //Send the plugin message player.sendPluginMessage(Stargate.getInstance(), bungeeChannel, byteArrayOutputStream.toByteArray()); @@ -212,15 +211,4 @@ public final class BungeeHelper { return true; } - /** - * Strips all color tags from a string - * - * @param string

The string to strip color from

- * @return

The string without color codes

- */ - @NotNull - private static String stripColor(@NotNull String string) { - return ChatColor.stripColor(ChatColor.translateAlternateColorCodes('&', string)); - } - }