Uses matcher.find instead of matcher.matches
Also removes some code smells
This commit is contained in:
@@ -434,12 +434,15 @@ public final class StargateConfig {
|
|||||||
} else {
|
} else {
|
||||||
portalFolder = replacePluginFolderPath(portalFolder);
|
portalFolder = replacePluginFolderPath(portalFolder);
|
||||||
}
|
}
|
||||||
|
Stargate.debug("StargateConfig::loadConfig", "Portal folder is " + portalFolder);
|
||||||
|
|
||||||
gateFolder = (String) configOptions.get(ConfigOption.GATE_FOLDER);
|
gateFolder = (String) configOptions.get(ConfigOption.GATE_FOLDER);
|
||||||
if (gateFolder.isEmpty()) {
|
if (gateFolder.isEmpty()) {
|
||||||
gateFolder = dataFolderPath + "/gates/";
|
gateFolder = dataFolderPath + "/gates/";
|
||||||
} else {
|
} else {
|
||||||
gateFolder = replacePluginFolderPath(gateFolder);
|
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 users have an outdated config, assume they also need to update their default gates
|
||||||
if (isMigrating) {
|
if (isMigrating) {
|
||||||
@@ -466,7 +469,7 @@ public final class StargateConfig {
|
|||||||
private String replacePluginFolderPath(@NotNull String input) {
|
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);
|
Matcher matcher = pattern.matcher(input);
|
||||||
if (matcher.matches()) {
|
if (matcher.find()) {
|
||||||
return dataFolderPath + matcher.replaceAll("");
|
return dataFolderPath + matcher.replaceAll("");
|
||||||
} else {
|
} else {
|
||||||
return input;
|
return input;
|
||||||
|
@@ -4,6 +4,7 @@ import net.knarcraft.stargate.Stargate;
|
|||||||
import net.knarcraft.stargate.config.material.BukkitMaterialSpecifier;
|
import net.knarcraft.stargate.config.material.BukkitMaterialSpecifier;
|
||||||
import net.knarcraft.stargate.config.material.MaterialSpecifier;
|
import net.knarcraft.stargate.config.material.MaterialSpecifier;
|
||||||
import org.bukkit.Material;
|
import org.bukkit.Material;
|
||||||
|
import org.bukkit.configuration.InvalidConfigurationException;
|
||||||
import org.jetbrains.annotations.NotNull;
|
import org.jetbrains.annotations.NotNull;
|
||||||
|
|
||||||
import java.util.ArrayList;
|
import java.util.ArrayList;
|
||||||
@@ -108,11 +109,11 @@ public final class GateReader {
|
|||||||
* @param line <p>The line to read</p>
|
* @param line <p>The line to read</p>
|
||||||
* @param characterMaterialMap <p>The character to material map to store to</p>
|
* @param characterMaterialMap <p>The character to material map to store to</p>
|
||||||
* @param config <p>The config value 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,
|
private static void readGateConfigValue(@NotNull String line,
|
||||||
@NotNull Map<Character, List<MaterialSpecifier>> characterMaterialMap,
|
@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[] split = line.split("=");
|
||||||
String key = split[0].trim();
|
String key = split[0].trim();
|
||||||
String value = split[1].trim();
|
String value = split[1].trim();
|
||||||
@@ -125,7 +126,7 @@ public final class GateReader {
|
|||||||
if (!materials.isEmpty()) {
|
if (!materials.isEmpty()) {
|
||||||
characterMaterialMap.put(symbol, materials);
|
characterMaterialMap.put(symbol, materials);
|
||||||
} else {
|
} else {
|
||||||
throw new Exception("Invalid material in line: " + line);
|
throw new InvalidConfigurationException("Invalid material in line: " + line);
|
||||||
}
|
}
|
||||||
} else {
|
} else {
|
||||||
//Read a normal config value
|
//Read a normal config value
|
||||||
|
@@ -53,13 +53,31 @@ public final class PermissionHelper {
|
|||||||
return;
|
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
|
//Deny access if another player has activated the portal, and it's still in use
|
||||||
if (!portal.getOptions().isFixed() && portal.getPortalActivator().isActive() &&
|
if (!portal.getOptions().isFixed() && portal.getPortalActivator().isActive() &&
|
||||||
portal.getActivePlayer() != player) {
|
portal.getActivePlayer() != player) {
|
||||||
if (!portal.getOptions().isSilent()) {
|
if (!portal.getOptions().isSilent()) {
|
||||||
Stargate.getMessageSender().sendErrorMessage(player, Stargate.getString(Message.ACCESS_DENIED));
|
Stargate.getMessageSender().sendErrorMessage(player, Stargate.getString(Message.ACCESS_DENIED));
|
||||||
}
|
}
|
||||||
return;
|
return true;
|
||||||
}
|
}
|
||||||
|
|
||||||
//Check if the player can use the private gate
|
//Check if the player can use the private gate
|
||||||
@@ -67,7 +85,7 @@ public final class PermissionHelper {
|
|||||||
if (!portal.getOptions().isSilent()) {
|
if (!portal.getOptions().isSilent()) {
|
||||||
Stargate.getMessageSender().sendErrorMessage(player, Stargate.getString(Message.ACCESS_DENIED));
|
Stargate.getMessageSender().sendErrorMessage(player, Stargate.getString(Message.ACCESS_DENIED));
|
||||||
}
|
}
|
||||||
return;
|
return true;
|
||||||
}
|
}
|
||||||
|
|
||||||
//Destination is currently in use by another player, blocking teleportation
|
//Destination is currently in use by another player, blocking teleportation
|
||||||
@@ -75,11 +93,10 @@ public final class PermissionHelper {
|
|||||||
if (!portal.getOptions().isSilent()) {
|
if (!portal.getOptions().isSilent()) {
|
||||||
Stargate.getMessageSender().sendErrorMessage(player, Stargate.getString(Message.DESTINATION_BLOCKED));
|
Stargate.getMessageSender().sendErrorMessage(player, Stargate.getString(Message.DESTINATION_BLOCKED));
|
||||||
}
|
}
|
||||||
return;
|
return true;
|
||||||
}
|
}
|
||||||
|
|
||||||
//Open the portal
|
return false;
|
||||||
portal.getPortalOpener().openPortal(player, false);
|
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
@@ -109,26 +126,27 @@ public final class PermissionHelper {
|
|||||||
public static boolean cannotAccessPortal(@NotNull Player player, @NotNull Portal entrancePortal,
|
public static boolean cannotAccessPortal(@NotNull Player player, @NotNull Portal entrancePortal,
|
||||||
@Nullable Portal destination) {
|
@Nullable Portal destination) {
|
||||||
boolean deny = false;
|
boolean deny = false;
|
||||||
|
String route = "PermissionHelper::cannotAccessPortal";
|
||||||
|
|
||||||
if (entrancePortal.getOptions().isBungee()) {
|
if (entrancePortal.getOptions().isBungee()) {
|
||||||
if (!PermissionHelper.canAccessServer(player, entrancePortal.getCleanNetwork())) {
|
if (!PermissionHelper.canAccessServer(player, entrancePortal.getCleanNetwork())) {
|
||||||
//If the portal is a bungee portal, and the player cannot access the server, deny
|
//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;
|
deny = true;
|
||||||
}
|
}
|
||||||
} else if (PermissionHelper.cannotAccessNetwork(player, entrancePortal.getCleanNetwork())) {
|
} else if (PermissionHelper.cannotAccessNetwork(player, entrancePortal.getCleanNetwork())) {
|
||||||
//If the player does not have access to the network, deny
|
//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;
|
deny = true;
|
||||||
} else {
|
} else {
|
||||||
if (destination == null) {
|
if (destination == null) {
|
||||||
//If there is no destination, deny
|
//If there is no destination, deny
|
||||||
Stargate.debug("cannotAccessPortal", "Portal has no destination");
|
Stargate.debug(route, "Portal has no destination");
|
||||||
deny = true;
|
deny = true;
|
||||||
} else if (destination.getWorld() != null &&
|
} else if (destination.getWorld() != null &&
|
||||||
PermissionHelper.cannotAccessWorld(player, destination.getWorld().getName())) {
|
PermissionHelper.cannotAccessWorld(player, destination.getWorld().getName())) {
|
||||||
//If the player does not have access to the portal's world, deny
|
//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;
|
deny = true;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
@@ -59,14 +59,14 @@ public final class PortalFileHelper {
|
|||||||
if (portal.getWorld() == null) {
|
if (portal.getWorld() == null) {
|
||||||
Stargate.logSevere(String.format("Could not save portal %s because its world is null",
|
Stargate.logSevere(String.format("Could not save portal %s because its world is null",
|
||||||
portal.getName()));
|
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();
|
bufferedWriter.close();
|
||||||
|
Reference in New Issue
Block a user