Removes unnecessary hashmap copying

This commit is contained in:
2024-11-08 18:45:10 +01:00
parent 7dd01995ed
commit 864eaac940
7 changed files with 72 additions and 79 deletions

View File

@@ -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;
}

View File

@@ -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<String> portalsInNetwork = PortalHandler.getNetwork(portal.getCleanNetwork());
if (portalsInNetwork != null) {
Stargate.debug("cycleDestination", "Network Size: " + portalsInNetwork.size());
}
Stargate.debug("cycleDestination", "Player has access to: " + destinations.size());
}

View File

@@ -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<String> networkList = PortalHandler.getAllPortalNetworks().get(portal.getCleanNetwork());
List<String> 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));

View File

@@ -42,24 +42,14 @@ public class PortalHandler {
return PortalRegistry.getAllPortalNetworks();
}
/**
* Gets a copy of all bungee portals
*
* @return <p>A copy of all bungee portals</p>
*/
@NotNull
public static Map<String, Portal> getBungeePortals() {
return PortalRegistry.getBungeePortals();
}
/**
* Gets names of all portals within a network
*
* @param network <p>The network to get portals from</p>
* @return <p>A list of portal names</p>
*/
@NotNull
public static List<String> getNetwork(String network) {
@Nullable
public static List<String> getNetwork(@NotNull String network) {
return PortalRegistry.getNetwork(network);
}
@@ -75,7 +65,11 @@ public class PortalHandler {
public static List<String> getDestinations(@NotNull Portal entrancePortal, @Nullable Player player,
@NotNull String network) {
List<String> destinations = new ArrayList<>();
for (String destination : PortalRegistry.getAllPortalNetworks().get(network)) {
List<String> 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<PortalOption, Boolean> 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 <p>The newly created portal</p>
*/
public static void updatePortalsPointingAtNewPortal(@NotNull Portal portal) {
for (String originName : PortalRegistry.getAllPortalNetworks().get(portal.getCleanNetwork())) {
List<String> 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<String, Map<String, Portal>> 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);
}
/**

View File

@@ -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 <p>A copy of the frame block lookup map</p>
* @param blockLocation <p>The location that might be a frame block</p>
* @return <p>The portal the frame block belongs to, or null</p>
*/
@NotNull
public static Map<BlockLocation, Portal> 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 <p>A copy of the control block lookup map</p>
* @param blockLocation <p>The location that might be a portal control block</p>
* @return <p>The portal the control block belongs to, or null</p>
*/
@NotNull
public static Map<BlockLocation, Portal> 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 <p>A copy of the network portal lookup map</p>
* @param networkName <p>The name of the network the portal belongs to</p>
* @param portalName <p>The name of the portal</p>
* @return <p>The portal, or null if no such network and/or portal exists</p>
*/
@NotNull
public static Map<String, Map<String, Portal>> getPortalLookupByNetwork() {
return new HashMap<>(portalLookupByNetwork);
@Nullable
public static Portal getPortalInNetwork(@NotNull String networkName, @NotNull String portalName) {
Map<String, Portal> 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 <p>A location that might be a portal's entrance</p>
* @return <p>A portal, or null</p>
*/
@Nullable
@@ -145,13 +154,14 @@ public class PortalRegistry {
}
/**
* Gets a copy of all bungee portals
* Gets the BungeeCord portal with the given name
*
* @return <p>A copy of all bungee portals</p>
* @param portalName <p>The name of the portal to get</p>
* @return <p>The portal, or null</p>
*/
@NotNull
public static Map<String, Portal> 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 <p>The network to get portals from</p>
* @return <p>A list of portal names</p>
*/
@NotNull
@Nullable
public static List<String> getNetwork(@NotNull String network) {
return allPortalNetworks.get(network.toLowerCase());
}

View File

@@ -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 {

View File

@@ -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 <p>The string to strip color from</p>
* @return <p>The string without color codes</p>
*/
@NotNull
private static String stripColor(@NotNull String string) {
return ChatColor.stripColor(ChatColor.translateAlternateColorCodes('&', string));
}
}