From 9abf60bb31d7081121d4eecda62b40e15bd1567e Mon Sep 17 00:00:00 2001 From: EpicKnarvik97 Date: Tue, 20 Feb 2024 20:48:29 +0100 Subject: [PATCH] Reduces some code complexity --- .../stargate/config/StargateConfig.java | 2 +- .../stargate/portal/PortalCreator.java | 123 ++++++++++++------ .../stargate/portal/PortalHandler.java | 70 ++++++---- .../stargate/portal/PortalRegistry.java | 58 +++++---- .../stargate/portal/property/gate/Gate.java | 19 +-- .../portal/property/gate/GateCosts.java | 12 ++ .../portal/property/gate/GateHandler.java | 13 +- .../portal/teleporter/Teleporter.java | 2 +- 8 files changed, 190 insertions(+), 109 deletions(-) create mode 100644 src/main/java/net/knarcraft/stargate/portal/property/gate/GateCosts.java diff --git a/src/main/java/net/knarcraft/stargate/config/StargateConfig.java b/src/main/java/net/knarcraft/stargate/config/StargateConfig.java index de57922..7a0004d 100644 --- a/src/main/java/net/knarcraft/stargate/config/StargateConfig.java +++ b/src/main/java/net/knarcraft/stargate/config/StargateConfig.java @@ -467,7 +467,7 @@ public final class StargateConfig { */ @NotNull private String replacePluginFolderPath(@NotNull String input) { - Pattern pattern = Pattern.compile("(?i)^plugins[\\\\\\/]Stargate"); + Pattern pattern = Pattern.compile("(?i)^plugins[\\\\/]Stargate"); Matcher matcher = pattern.matcher(input); if (matcher.find()) { return dataFolderPath + matcher.replaceAll(""); diff --git a/src/main/java/net/knarcraft/stargate/portal/PortalCreator.java b/src/main/java/net/knarcraft/stargate/portal/PortalCreator.java index 942931f..fb1cc65 100644 --- a/src/main/java/net/knarcraft/stargate/portal/PortalCreator.java +++ b/src/main/java/net/knarcraft/stargate/portal/PortalCreator.java @@ -79,6 +79,8 @@ public class PortalCreator { String network = PortalHandler.filterName(event.getLine(2)); String options = PortalHandler.filterName(event.getLine(3)).toLowerCase(); + PortalStrings portalStrings = new PortalStrings(portalName, network, destinationName); + //Get portal options available to the player creating the portal Map portalOptions = PortalHandler.getPortalOptions(player, destinationName, options); @@ -94,6 +96,12 @@ public class PortalCreator { Stargate.debug("createPortal", "Finished getting all portal info"); + return createPortal(portalStrings, portalOptions, yaw, portalLocation); + } + + @Nullable + private Portal createPortal(@NotNull PortalStrings portalStrings, @NotNull Map portalOptions, + float yaw, @NotNull PortalLocation portalLocation) { //Try and find a gate matching the new portal Gate gate = PortalHandler.findMatchingGate(portalLocation, player.getWorld()); if ((gate == null) || (portalLocation.getButtonVector() == null)) { @@ -102,7 +110,8 @@ public class PortalCreator { } //If the portal is a bungee portal and invalid, abort here - if (!PortalHandler.isValidBungeePortal(portalOptions, player, destinationName, network)) { + if (!PortalHandler.isValidBungeePortal(portalOptions, player, portalStrings.destination(), + portalStrings.network())) { Stargate.debug("createPortal", "Portal is an invalid bungee portal"); return null; } @@ -114,51 +123,27 @@ public class PortalCreator { } Stargate.debug("createPortal", builder.toString()); - //Use default network if a proper alternative is not set - if (!portalOptions.get(PortalOption.BUNGEE) && (network.length() < 1 || network.length() > - getMaxNameNetworkLength())) { - network = Stargate.getDefaultNetwork(); - } - boolean deny = false; String denyMessage = ""; - //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.canCreatePersonalPortal(player)) { - network = player.getName(); - if (network.length() > getMaxNameNetworkLength()) { - network = network.substring(0, getMaxNameNetworkLength()); - } - Stargate.debug("createPortal", "Creating personal portal"); - Stargate.getMessageSender().sendErrorMessage(player, Stargate.getString(Message.CREATION_PERSONAL)); - } else { - Stargate.debug("createPortal", "Player does not have access to network"); + if (!portalOptions.get(PortalOption.BUNGEE)) { + String networkName = getNetworkName(portalStrings); + if (networkName == null) { deny = true; denyMessage = Stargate.getString(Message.CREATION_NETWORK_DENIED); + } else { + portalStrings = new PortalStrings(portalStrings.name(), networkName, portalStrings.destination()); } } - //Check if the player can create this gate layout - String gateName = gate.getFilename(); - gateName = gateName.substring(0, gateName.indexOf('.')); - if (!deny && !PermissionHelper.canCreatePortal(player, gateName)) { - Stargate.debug("createPortal", "Player does not have access to gate layout"); - deny = true; - denyMessage = Stargate.getString(Message.CREATION_GATE_DENIED); - } - - //Check if the user can create portals to this world. - if (!portalOptions.get(PortalOption.BUNGEE) && !deny && destinationName.length() > 0) { - Portal portal = PortalHandler.getByName(destinationName, network); - if (portal != null && portal.getWorld() != null) { - String world = portal.getWorld().getName(); - if (PermissionHelper.cannotAccessWorld(player, world)) { - Stargate.debug("canCreateNetworkGate", "Player does not have access to destination world"); - deny = true; - denyMessage = Stargate.getString(Message.CREATION_WORLD_DENIED); - } + // Check whether the player can create a portal with the specified gate in the specified world + if (!deny) { + denyMessage = canCreatePortal(portalOptions.get(PortalOption.BUNGEE), portalStrings.network(), gate, + portalStrings.destination()); + if (denyMessage != null) { + deny = true; + } else { + denyMessage = ""; } } @@ -168,11 +153,71 @@ public class PortalCreator { } PortalOwner owner = new PortalOwner(player); - PortalStrings portalStrings = new PortalStrings(portalName, network, destinationName); this.portal = new Portal(portalLocation, null, portalStrings, gate, owner, portalOptions); return validatePortal(denyMessage, event.getLines(), deny); } + /** + * Gets the network name to use for the new portal + * + * @param portalStrings

The string values for the new portal

+ * @return

The new network name, or null if the player does not have the necessary permission for any networks

+ */ + @Nullable + private String getNetworkName(@NotNull PortalStrings portalStrings) { + String network = portalStrings.network(); + + //Use default network if a proper alternative is not set + if (portalStrings.network().length() < 1 || portalStrings.network().length() > getMaxNameNetworkLength()) { + network = Stargate.getDefaultNetwork(); + } + + //Check if the player can create portals on this network. If not, create a personal portal + if (!PermissionHelper.canCreateNetworkGate(player, network)) { + Stargate.debug("createPortal", "Player doesn't have create permissions on network. Trying personal"); + if (PermissionHelper.canCreatePersonalPortal(player)) { + network = player.getName(); + if (network.length() > getMaxNameNetworkLength()) { + network = network.substring(0, getMaxNameNetworkLength()); + } + Stargate.debug("createPortal", "Creating personal portal"); + Stargate.getMessageSender().sendErrorMessage(player, Stargate.getString(Message.CREATION_PERSONAL)); + return network; + } else { + Stargate.debug("createPortal", "Player does not have access to network"); + return null; + } + } + + return network; + } + + @Nullable + private String canCreatePortal(boolean bungee, @NotNull String network, + @NotNull Gate gate, @NotNull String destinationName) { + //Check if the player can create this gate layout + String gateName = gate.getFilename(); + gateName = gateName.substring(0, gateName.indexOf('.')); + if (!PermissionHelper.canCreatePortal(player, gateName)) { + Stargate.debug("createPortal", "Player does not have access to gate layout"); + return Stargate.getString(Message.CREATION_GATE_DENIED); + } + + //Check if the user can create portals to this world. + if (!bungee && destinationName.length() > 0) { + Portal portal = PortalHandler.getByName(destinationName, network); + if (portal != null && portal.getWorld() != null) { + String world = portal.getWorld().getName(); + if (PermissionHelper.cannotAccessWorld(player, world)) { + Stargate.debug("canCreateNetworkGate", "Player does not have access to destination world"); + return Stargate.getString(Message.CREATION_WORLD_DENIED); + } + } + } + + return null; + } + /** * Validates the newly created portal assigned to this portal validator * diff --git a/src/main/java/net/knarcraft/stargate/portal/PortalHandler.java b/src/main/java/net/knarcraft/stargate/portal/PortalHandler.java index 9f57fc7..e0cfdd0 100644 --- a/src/main/java/net/knarcraft/stargate/portal/PortalHandler.java +++ b/src/main/java/net/knarcraft/stargate/portal/PortalHandler.java @@ -80,40 +80,54 @@ public class PortalHandler { if (portal == null) { continue; } - //Check if destination is a random portal - if (portal.getOptions().isRandom()) { - continue; - } - //Check if destination is always open (Don't show if so) - if (portal.getOptions().isAlwaysOn() && !portal.getOptions().isShown()) { - continue; - } - //Check if destination is this portal - if (destination.equals(entrancePortal.getCleanName())) { - continue; - } - //Check if destination is a fixed portal not pointing to this portal - if (portal.getOptions().isFixed() && - !Portal.cleanString(portal.getDestinationName()).equals(entrancePortal.getCleanName())) { - continue; - } - //Allow random use by non-players (Minecarts) - if (player == null) { - destinations.add(portal.getName()); - continue; - } - //Check if this player can access the destination world - if (portal.getWorld() != null && PermissionHelper.cannotAccessWorld(player, portal.getWorld().getName())) { - continue; - } - //The portal is visible to the player - if (PermissionHelper.canSeePortal(player, portal)) { + + if (isDestinationAvailable(entrancePortal, portal, player)) { destinations.add(portal.getName()); } } return destinations; } + /** + * Checks whether the given destination is available to the given player + * + * @param entrancePortal

The portal the player has activated

+ * @param destinationPortal

The destination portal to check if is valid

+ * @param player

The player trying to attempt the destination

+ * @return

True if the destination is available

+ */ + private static boolean isDestinationAvailable(@NotNull Portal entrancePortal, @NotNull Portal destinationPortal, + @Nullable Player player) { + //Check if destination is a random portal + if (destinationPortal.getOptions().isRandom()) { + return false; + } + //Check if destination is always open (Don't show if so) + if (destinationPortal.getOptions().isAlwaysOn() && !destinationPortal.getOptions().isShown()) { + return false; + } + //Check if destination is this portal + if (destinationPortal.equals(entrancePortal)) { + return false; + } + //Check if destination is a fixed portal not pointing to this portal + if (destinationPortal.getOptions().isFixed() && + !Portal.cleanString(destinationPortal.getDestinationName()).equals(entrancePortal.getCleanName())) { + return false; + } + //Allow random use by non-players (Minecarts) + if (player == null) { + return true; + } + //Check if this player can access the destination world + if (destinationPortal.getWorld() != null && PermissionHelper.cannotAccessWorld(player, + destinationPortal.getWorld().getName())) { + return false; + } + //The portal is visible to the player + return PermissionHelper.canSeePortal(player, destinationPortal); + } + /** * Registers a portal * diff --git a/src/main/java/net/knarcraft/stargate/portal/PortalRegistry.java b/src/main/java/net/knarcraft/stargate/portal/PortalRegistry.java index ac0d016..f2def13 100644 --- a/src/main/java/net/knarcraft/stargate/portal/PortalRegistry.java +++ b/src/main/java/net/knarcraft/stargate/portal/PortalRegistry.java @@ -178,30 +178,7 @@ public class PortalRegistry { String portalName = portal.getCleanName(); String networkName = portal.getCleanNetwork(); - //Remove portal from lookup blocks - for (BlockLocation block : portal.getStructure().getFrame()) { - lookupBlocks.remove(block); - } - - //Remove registered info about the lookup controls and blocks - lookupBlocks.remove(portal.getSignLocation()); - lookupControls.remove(portal.getSignLocation()); - - BlockLocation button = portal.getStructure().getButton(); - if (button != null) { - lookupBlocks.remove(button); - lookupControls.remove(button); - } - - //Remove entrances - for (BlockLocation entrance : portal.getStructure().getEntrances()) { - lookupEntrances.remove(entrance); - } - - //Remove the portal from the list of all portals - if (removeAll) { - allPortals.remove(portal); - } + clearLookupMaps(portal, removeAll); if (portal.getOptions().isBungee()) { //Remove the bungee listing @@ -239,6 +216,39 @@ public class PortalRegistry { DynmapManager.removePortalMarker(portal); } + /** + * Clears the given portal's presence from lookup maps + * + * @param portal

The portal to clear

+ * @param removeAll

Whether to remove the portal from the list of all portals

+ */ + private static void clearLookupMaps(@NotNull Portal portal, boolean removeAll) { + //Remove portal from lookup blocks + for (BlockLocation block : portal.getStructure().getFrame()) { + lookupBlocks.remove(block); + } + + //Remove registered info about the lookup controls and blocks + lookupBlocks.remove(portal.getSignLocation()); + lookupControls.remove(portal.getSignLocation()); + + BlockLocation button = portal.getStructure().getButton(); + if (button != null) { + lookupBlocks.remove(button); + lookupControls.remove(button); + } + + //Remove entrances + for (BlockLocation entrance : portal.getStructure().getEntrances()) { + lookupEntrances.remove(entrance); + } + + //Remove the portal from the list of all portals + if (removeAll) { + allPortals.remove(portal); + } + } + /** * Registers a portal * diff --git a/src/main/java/net/knarcraft/stargate/portal/property/gate/Gate.java b/src/main/java/net/knarcraft/stargate/portal/property/gate/Gate.java index efcfc30..f405f11 100644 --- a/src/main/java/net/knarcraft/stargate/portal/property/gate/Gate.java +++ b/src/main/java/net/knarcraft/stargate/portal/property/gate/Gate.java @@ -49,27 +49,23 @@ public class Gate { * @param portalOpenMaterials

The material to set the opening to when the portal is open

* @param portalClosedMaterials

The material to set the opening to when the portal is closed

* @param portalButtonMaterials

The material to use for the portal button

- * @param useCost

The cost of using a portal with this gate layout (-1 to disable)

- * @param createCost

The cost of creating a portal with this gate layout (-1 to disable)

- * @param destroyCost

The cost of destroying a portal with this gate layout (-1 to disable)

- * @param toOwner

Whether any payment should go to the owner of the gate, as opposed to just disappearing

+ * @param gateCosts

The costs and other economy information for the gate

*/ public Gate(@NotNull String filename, @NotNull GateLayout layout, @NotNull Map> characterMaterialsMap, @NotNull List portalOpenMaterials, @NotNull List portalClosedMaterials, - @NotNull List portalButtonMaterials, int useCost, int createCost, int destroyCost, - boolean toOwner) { + @NotNull List portalButtonMaterials, @NotNull GateCosts gateCosts) { this.filename = filename; this.layout = layout; this.characterMaterialMap = characterMaterialsMap; this.portalOpenMaterials = portalOpenMaterials; this.portalClosedMaterials = portalClosedMaterials; this.portalButtonMaterials = portalButtonMaterials; - this.useCost = useCost; - this.createCost = createCost; - this.destroyCost = destroyCost; - this.toOwner = toOwner; + this.useCost = gateCosts.useCost(); + this.createCost = gateCosts.createCost(); + this.destroyCost = gateCosts.destroyCost(); + this.toOwner = gateCosts.toOwner(); } /** @@ -226,7 +222,6 @@ public class Gate { * @return

True if all border blocks of the gate match the layout

*/ private boolean verifyGateBorderMatches(@NotNull BlockLocation topLeft, double yaw) { - Map> characterMaterialMap = new HashMap<>(this.characterMaterialMap); for (RelativeBlockVector borderVector : layout.getBorder()) { int rowIndex = borderVector.right(); int lineIndex = borderVector.down(); @@ -246,7 +241,7 @@ public class Gate { * recognized, but still allowed in previous checks, verify the gate as long as all such instances of * the character correspond to the same material in the physical gate. All subsequent gates will also * need to match the first verified gate. */ - characterMaterialMap.put(key, List.of(new BukkitMaterialSpecifier(materialAtLocation))); + this.characterMaterialMap.put(key, List.of(new BukkitMaterialSpecifier(materialAtLocation))); Stargate.debug("Gate::Matches", String.format("Missing layout material in %s. Using %s from the" + " physical portal.", getFilename(), materialAtLocation)); } diff --git a/src/main/java/net/knarcraft/stargate/portal/property/gate/GateCosts.java b/src/main/java/net/knarcraft/stargate/portal/property/gate/GateCosts.java new file mode 100644 index 0000000..93a224d --- /dev/null +++ b/src/main/java/net/knarcraft/stargate/portal/property/gate/GateCosts.java @@ -0,0 +1,12 @@ +package net.knarcraft.stargate.portal.property.gate; + +/** + * The costs assigned to a gate + * + * @param useCost

The cost for using (entering) the gate

+ * @param createCost

The cost for creating a portal with the gate type

+ * @param destroyCost

The cost for destroying a portal with the gate type

+ * @param toOwner

Whether the use cost is paid to the gate's owner

+ */ +public record GateCosts(int useCost, int createCost, int destroyCost, boolean toOwner) { +} diff --git a/src/main/java/net/knarcraft/stargate/portal/property/gate/GateHandler.java b/src/main/java/net/knarcraft/stargate/portal/property/gate/GateHandler.java index 7a26ef4..87e491c 100644 --- a/src/main/java/net/knarcraft/stargate/portal/property/gate/GateHandler.java +++ b/src/main/java/net/knarcraft/stargate/portal/property/gate/GateHandler.java @@ -115,11 +115,15 @@ public class GateHandler { @Nullable private static Gate loadGate(@NotNull File file) { try (Scanner scanner = new Scanner(file)) { - return loadGate(file.getName(), file.getParent(), scanner); + Gate gate = loadGate(file.getName(), file.getParent(), scanner); + if (gate != null) { + return gate; + } } catch (Exception exception) { Stargate.logSevere(String.format("Could not load Gate %s - %s", file.getName(), exception.getMessage())); - return null; } + + return null; } /** @@ -184,10 +188,11 @@ public class GateHandler { int destroyCost = readGateConfig(config, fileName, "destroycost"); boolean toOwner = (config.containsKey("toowner") ? Boolean.parseBoolean(config.get("toowner")) : Stargate.getEconomyConfig().sendPaymentToOwner()); + GateCosts gateCosts = new GateCosts(useCost, createCost, destroyCost, toOwner); //Create the new gate - Gate gate = new Gate(fileName, new GateLayout(layout), characterMaterialMap, portalOpenBlock, - portalClosedBlock, portalButton, useCost, createCost, destroyCost, toOwner); + Gate gate = new Gate(fileName, new GateLayout(layout), characterMaterialMap, portalOpenBlock, portalClosedBlock, + portalButton, gateCosts); if (!validateGate(gate, fileName)) { return null; diff --git a/src/main/java/net/knarcraft/stargate/portal/teleporter/Teleporter.java b/src/main/java/net/knarcraft/stargate/portal/teleporter/Teleporter.java index 3322b4d..9894d6b 100644 --- a/src/main/java/net/knarcraft/stargate/portal/teleporter/Teleporter.java +++ b/src/main/java/net/knarcraft/stargate/portal/teleporter/Teleporter.java @@ -57,7 +57,7 @@ public abstract class Teleporter { * @param portal

The portal which is the target of the teleportation

* @param teleportedEntity

The entity teleported by this teleporter

*/ - public Teleporter(@NotNull Portal portal, @NotNull Entity teleportedEntity) { + protected Teleporter(@NotNull Portal portal, @NotNull Entity teleportedEntity) { this.portal = portal; this.scheduler = Stargate.getInstance().getServer().getScheduler(); this.teleportedEntity = teleportedEntity;