Uses matcher.find instead of matcher.matches

Also removes some code smells
This commit is contained in:
2024-02-20 19:45:41 +01:00
parent 9abf6db27a
commit cde20e35d5
4 changed files with 42 additions and 20 deletions

View File

@@ -434,12 +434,15 @@ public final class StargateConfig {
} else {
portalFolder = replacePluginFolderPath(portalFolder);
}
Stargate.debug("StargateConfig::loadConfig", "Portal folder is " + portalFolder);
gateFolder = (String) configOptions.get(ConfigOption.GATE_FOLDER);
if (gateFolder.isEmpty()) {
gateFolder = dataFolderPath + "/gates/";
} else {
gateFolder = replacePluginFolderPath(gateFolder);
}
Stargate.debug("StargateConfig::loadConfig", "Gate folder is " + gateFolder);
//If users have an outdated config, assume they also need to update their default gates
if (isMigrating) {
@@ -466,7 +469,7 @@ public final class StargateConfig {
private String replacePluginFolderPath(@NotNull String input) {
Pattern pattern = Pattern.compile("(?i)^plugins[\\\\\\/]Stargate");
Matcher matcher = pattern.matcher(input);
if (matcher.matches()) {
if (matcher.find()) {
return dataFolderPath + matcher.replaceAll("");
} else {
return input;

View File

@@ -4,6 +4,7 @@ import net.knarcraft.stargate.Stargate;
import net.knarcraft.stargate.config.material.BukkitMaterialSpecifier;
import net.knarcraft.stargate.config.material.MaterialSpecifier;
import org.bukkit.Material;
import org.bukkit.configuration.InvalidConfigurationException;
import org.jetbrains.annotations.NotNull;
import java.util.ArrayList;
@@ -108,11 +109,11 @@ public final class GateReader {
* @param line <p>The line to read</p>
* @param characterMaterialMap <p>The character to material map to store to</p>
* @param config <p>The config value map to store to</p>
* @throws Exception <p>If an invalid material is encountered</p>
* @throws InvalidConfigurationException <p>If an invalid material is encountered</p>
*/
private static void readGateConfigValue(@NotNull String line,
@NotNull Map<Character, List<MaterialSpecifier>> characterMaterialMap,
@NotNull Map<String, String> config) throws Exception {
@NotNull Map<String, String> config) throws InvalidConfigurationException {
String[] split = line.split("=");
String key = split[0].trim();
String value = split[1].trim();
@@ -125,7 +126,7 @@ public final class GateReader {
if (!materials.isEmpty()) {
characterMaterialMap.put(symbol, materials);
} else {
throw new Exception("Invalid material in line: " + line);
throw new InvalidConfigurationException("Invalid material in line: " + line);
}
} else {
//Read a normal config value

View File

@@ -53,13 +53,31 @@ public final class PermissionHelper {
return;
}
// Check if the player is able to open the portal
if (canNotOpen(player, portal, destination)) {
return;
}
//Open the portal
portal.getPortalOpener().openPortal(player, false);
}
/**
* Checks whether something prevents the player from opening the given portal to the given destination
*
* @param player <p>The player trying to open the portal</p>
* @param portal <p>The portal to open</p>
* @param destination <p>The destination the player is attempting to open</p>
* @return <p>True if the player cannot open the portal</p>
*/
private static boolean canNotOpen(Player player, Portal portal, Portal destination) {
//Deny access if another player has activated the portal, and it's still in use
if (!portal.getOptions().isFixed() && portal.getPortalActivator().isActive() &&
portal.getActivePlayer() != player) {
if (!portal.getOptions().isSilent()) {
Stargate.getMessageSender().sendErrorMessage(player, Stargate.getString(Message.ACCESS_DENIED));
}
return;
return true;
}
//Check if the player can use the private gate
@@ -67,7 +85,7 @@ public final class PermissionHelper {
if (!portal.getOptions().isSilent()) {
Stargate.getMessageSender().sendErrorMessage(player, Stargate.getString(Message.ACCESS_DENIED));
}
return;
return true;
}
//Destination is currently in use by another player, blocking teleportation
@@ -75,11 +93,10 @@ public final class PermissionHelper {
if (!portal.getOptions().isSilent()) {
Stargate.getMessageSender().sendErrorMessage(player, Stargate.getString(Message.DESTINATION_BLOCKED));
}
return;
return true;
}
//Open the portal
portal.getPortalOpener().openPortal(player, false);
return false;
}
/**
@@ -109,26 +126,27 @@ public final class PermissionHelper {
public static boolean cannotAccessPortal(@NotNull Player player, @NotNull Portal entrancePortal,
@Nullable Portal destination) {
boolean deny = false;
String route = "PermissionHelper::cannotAccessPortal";
if (entrancePortal.getOptions().isBungee()) {
if (!PermissionHelper.canAccessServer(player, entrancePortal.getCleanNetwork())) {
//If the portal is a bungee portal, and the player cannot access the server, deny
Stargate.debug("cannotAccessPortal", "Cannot access server");
Stargate.debug(route, "Cannot access server");
deny = true;
}
} else if (PermissionHelper.cannotAccessNetwork(player, entrancePortal.getCleanNetwork())) {
//If the player does not have access to the network, deny
Stargate.debug("cannotAccessPortal", "Cannot access network");
Stargate.debug(route, "Cannot access network");
deny = true;
} else {
if (destination == null) {
//If there is no destination, deny
Stargate.debug("cannotAccessPortal", "Portal has no destination");
Stargate.debug(route, "Portal has no destination");
deny = true;
} else if (destination.getWorld() != null &&
PermissionHelper.cannotAccessWorld(player, destination.getWorld().getName())) {
//If the player does not have access to the portal's world, deny
Stargate.debug("cannotAccessPortal", "Cannot access world");
Stargate.debug(route, "Cannot access world");
deny = true;
}
}

View File

@@ -59,14 +59,14 @@ public final class PortalFileHelper {
if (portal.getWorld() == null) {
Stargate.logSevere(String.format("Could not save portal %s because its world is null",
portal.getName()));
continue;
} else {
String worldName = portal.getWorld().getName();
if (!worldName.equalsIgnoreCase(world.getName())) {
continue;
}
//Save the portal
savePortal(bufferedWriter, portal);
}
String worldName = portal.getWorld().getName();
if (!worldName.equalsIgnoreCase(world.getName())) {
continue;
}
//Save the portal
savePortal(bufferedWriter, portal);
}
bufferedWriter.close();