Refactors a lot of code, and extracts permission-related functions to the PermissionHelper class

This commit is contained in:
2021-09-20 13:56:30 +02:00
parent b57f988b62
commit f681db629f
23 changed files with 497 additions and 381 deletions

View File

@ -1,8 +1,8 @@
package net.knarcraft.stargate.portal;
import net.knarcraft.stargate.Stargate;
import net.knarcraft.stargate.container.BlockLocation;
import net.knarcraft.stargate.container.RelativeBlockVector;
import net.knarcraft.stargate.Stargate;
import net.knarcraft.stargate.utility.DirectionHelper;
import net.knarcraft.stargate.utility.EconomyHandler;
import org.bukkit.Material;

View File

@ -1,10 +1,11 @@
package net.knarcraft.stargate.portal;
import net.knarcraft.stargate.utility.EconomyHandler;
import net.knarcraft.stargate.Stargate;
import net.knarcraft.stargate.utility.EconomyHandler;
import net.knarcraft.stargate.utility.MaterialHelper;
import org.bukkit.Material;
import org.bukkit.block.Block;
import java.io.File;
import java.io.InputStream;
import java.util.ArrayList;
@ -109,9 +110,9 @@ public class GateHandler {
/**
* Loads a gate
*
* @param fileName <p>The name of the file containing the gate layout</p>
* @param fileName <p>The name of the file containing the gate layout</p>
* @param parentFolder <p>The parent folder of the layout file</p>
* @param scanner <p>The scanner to use for reading the gate layout</p>
* @param scanner <p>The scanner to use for reading the gate layout</p>
* @return <p>The loaded gate or null if unable to load the gate</p>
*/
private static Gate loadGate(String fileName, String parentFolder, Scanner scanner) {
@ -145,10 +146,10 @@ public class GateHandler {
/**
* Creates a new gate
*
* @param config <p>The config map to get configuration values from</p>
* @param config <p>The config map to get configuration values from</p>
* @param fileName <p>The name of the saved gate config file</p>
* @param layout <p>The layout matrix of the new gate</p>
* @param types <p>The mapping for used gate material types</p>
* @param layout <p>The layout matrix of the new gate</p>
* @param types <p>The mapping for used gate material types</p>
* @return <p>A new gate or null if the config is invalid</p>
*/
private static Gate createGate(Map<String, String> config, String fileName, Character[][] layout,
@ -173,7 +174,7 @@ public class GateHandler {
/**
* Validate that a gate is valid
*
* @param gate <p>The gate to validate</p>
* @param gate <p>The gate to validate</p>
* @param fileName <p>The filename of the loaded gate file</p>
* @return <p>True if the gate is valid. False otherwise</p>
*/
@ -196,7 +197,7 @@ public class GateHandler {
* Generates a matrix storing the gate layout
*
* @param design <p>The design of the gate layout</p>
* @param cols <p>The largest amount of columns in the design</p>
* @param cols <p>The largest amount of columns in the design</p>
* @return <p>A matrix containing the gate's layout</p>
*/
private static Character[][] generateLayoutMatrix(List<List<Character>> design, int cols) {
@ -222,12 +223,12 @@ public class GateHandler {
/**
* Reads the gate file
*
* @param scanner <p>The scanner to read from</p>
* @param types <p>The map of characters to store valid symbols in</p>
* @param fileName <p>The filename of the loaded gate config file</p>
* @param design <p>The list to store the loaded design to</p>
* @param scanner <p>The scanner to read from</p>
* @param types <p>The map of characters to store valid symbols in</p>
* @param fileName <p>The filename of the loaded gate config file</p>
* @param design <p>The list to store the loaded design to</p>
* @param frameTypes <p>The set of gate frame types to store to</p>
* @param config <p>The map of config values to store to</p>
* @param config <p>The map of config values to store to</p>
* @return <p>The column count/width of the loaded gate</p>
*/
private static int readGateFile(Scanner scanner, Map<Character, Material> types, String fileName,
@ -265,11 +266,11 @@ public class GateHandler {
/**
* Reads one design line of the gate layout file
*
* @param line <p>The line to read</p>
* @param cols <p>The current max columns value of the design</p>
* @param types <p>The map of characters to check for valid symbols</p>
* @param line <p>The line to read</p>
* @param cols <p>The current max columns value of the design</p>
* @param types <p>The map of characters to check for valid symbols</p>
* @param fileName <p>The filename of the loaded gate config file</p>
* @param design <p>The list to store the loaded design to</p>
* @param design <p>The list to store the loaded design to</p>
* @return <p>The new max columns value of the design</p>
*/
private static int readGateDesignLine(String line, int cols, Map<Character, Material> types, String fileName,
@ -295,10 +296,10 @@ public class GateHandler {
/**
* Reads one config value from the gate layout file
*
* @param line <p>The line to read</p>
* @param types <p>The map of characters to materials to store to</p>
* @param line <p>The line to read</p>
* @param types <p>The map of characters to materials to store to</p>
* @param frameTypes <p>The set of gate frame types to store to</p>
* @param config <p>The map of config values to store to</p>
* @param config <p>The map of config values to store to</p>
* @throws Exception <p>If an invalid material is encountered</p>
*/
private static void readGateConfigValue(String line, Map<Character, Material> types, Set<Material> frameTypes,
@ -322,9 +323,10 @@ public class GateHandler {
/**
* Reads an integer configuration key
* @param config <p>The configuration to read</p>
*
* @param config <p>The configuration to read</p>
* @param fileName <p>The filename of the config file</p>
* @param key <p>The config key to read</p>
* @param key <p>The config key to read</p>
* @return <p>The read value, or -1 if it cannot be read</p>
*/
private static int readConfig(Map<String, String> config, String fileName, String key) {

View File

@ -16,7 +16,7 @@ import java.util.List;
*/
public class GateLayout {
private final Character [][] layout;
private final Character[][] layout;
private final List<RelativeBlockVector> exits = new ArrayList<>();
private RelativeBlockVector[] entrances = new RelativeBlockVector[0];
private RelativeBlockVector[] border = new RelativeBlockVector[0];
@ -39,7 +39,7 @@ public class GateLayout {
* @return <p>Two of the gate's corners</p>
*/
public RelativeBlockVector[] getCorners() {
return new RelativeBlockVector[] {
return new RelativeBlockVector[]{
new RelativeBlockVector(0, 0, 0),
new RelativeBlockVector(layout[0].length - 1, layout.length - 1, 1)
};
@ -157,9 +157,9 @@ public class GateLayout {
/**
* Reads the given layout matrix, filling in the given lists of relative block vectors
*
* @param controlList <p>The list of control blocks to save to</p>
* @param controlList <p>The list of control blocks to save to</p>
* @param entranceList <p>The list of entrances to save to</p>
* @param borderList <p>The list of border blocks to save to</p>
* @param borderList <p>The list of border blocks to save to</p>
* @return <p>A list of depths of possible extra exits</p>
*/
private int[] readLayout(List<RelativeBlockVector> controlList, List<RelativeBlockVector> entranceList,
@ -181,13 +181,13 @@ public class GateLayout {
/**
* Parses one character of the layout
*
* @param key <p>The character read</p>
* @param rowIndex <p>The row of the read character</p>
* @param lineIndex <p>The line of the read character</p>
* @param exitDepths <p>The list of exit depths to save to</p>
* @param controlList <p>The list of control blocks to save to</p>
* @param key <p>The character read</p>
* @param rowIndex <p>The row of the read character</p>
* @param lineIndex <p>The line of the read character</p>
* @param exitDepths <p>The list of exit depths to save to</p>
* @param controlList <p>The list of control blocks to save to</p>
* @param entranceList <p>The list of entrances to save to</p>
* @param borderList <p>The list of border blocks to save to</p>
* @param borderList <p>The list of border blocks to save to</p>
*/
private void parseLayoutCharacter(Character key, int rowIndex, int lineIndex, int[] exitDepths,
List<RelativeBlockVector> controlList, List<RelativeBlockVector> entranceList,

View File

@ -1,9 +1,9 @@
package net.knarcraft.stargate.portal;
import net.knarcraft.stargate.container.BlockLocation;
import net.knarcraft.stargate.container.BlockChangeRequest;
import net.knarcraft.stargate.container.RelativeBlockVector;
import net.knarcraft.stargate.Stargate;
import net.knarcraft.stargate.container.BlockChangeRequest;
import net.knarcraft.stargate.container.BlockLocation;
import net.knarcraft.stargate.container.RelativeBlockVector;
import net.knarcraft.stargate.event.StargateActivateEvent;
import net.knarcraft.stargate.event.StargateCloseEvent;
import net.knarcraft.stargate.event.StargateDeactivateEvent;
@ -86,7 +86,7 @@ public class Portal {
* @param topLeft <p>The top-left block of the portal. This is used to decide the positions of the rest of the portal</p>
* @param modX <p></p>
* @param modZ <p></p>
* @param yaw <p></p>
* @param yaw <p></p>
* @param id <p>The location of the portal's id block, which is the sign which activated the portal</p>
* @param button <p>The location of the portal's open button</p>
* @param destination <p>The destination defined on the sign's destination line</p>
@ -921,6 +921,7 @@ public class Portal {
/**
* Loads one chunk
*
* @param chunk <p>The chunk to load</p>
*/
private void loadOneChunk(Chunk chunk) {

View File

@ -3,5 +3,4 @@ package net.knarcraft.stargate.portal;
public class PortalDirection {
}

View File

@ -1,13 +1,14 @@
package net.knarcraft.stargate.portal;
import net.knarcraft.stargate.Stargate;
import net.knarcraft.stargate.container.BlockLocation;
import net.knarcraft.stargate.container.RelativeBlockVector;
import net.knarcraft.stargate.Stargate;
import net.knarcraft.stargate.container.TwoTuple;
import net.knarcraft.stargate.event.StargateCreateEvent;
import net.knarcraft.stargate.utility.DirectionHelper;
import net.knarcraft.stargate.utility.EconomyHandler;
import net.knarcraft.stargate.utility.EconomyHelper;
import net.knarcraft.stargate.utility.PermissionHelper;
import org.bukkit.Bukkit;
import org.bukkit.Location;
import org.bukkit.OfflinePlayer;
@ -90,12 +91,12 @@ public class PortalHandler {
continue;
}
// Check if this player can access the dest world
if (Stargate.cannotAccessWorld(player, portal.getWorld().getName())) {
if (PermissionHelper.cannotAccessWorld(player, portal.getWorld().getName())) {
Stargate.log.info("cannot access world");
continue;
}
// Visible to this player.
if (Stargate.canSee(player, portal)) {
if (PermissionHelper.canSeePortal(player, portal)) {
destinations.add(portal.getName());
}
}
@ -314,7 +315,7 @@ public class PortalHandler {
//If the player is trying to create a Bungee gate without permissions, drop out here
if (options.indexOf(PortalOption.BUNGEE.getCharacterRepresentation()) != -1) {
if (!Stargate.hasPermission(player, "stargate.admin.bungee")) {
if (!PermissionHelper.hasPermission(player, "stargate.admin.bungee")) {
Stargate.sendMessage(player, Stargate.getString("bungeeDeny"));
return null;
}
@ -344,9 +345,9 @@ public class PortalHandler {
String denyMsg = "";
// Check if the player can create gates on this network
if (!portalOptions.get(PortalOption.BUNGEE) && !Stargate.canCreate(player, network)) {
if (!portalOptions.get(PortalOption.BUNGEE) && !PermissionHelper.canCreateNetworkGate(player, network)) {
Stargate.debug("createPortal", "Player doesn't have create permissions on network. Trying personal");
if (Stargate.canCreatePersonal(player)) {
if (PermissionHelper.canCreatePersonalGate(player)) {
network = player.getName();
if (network.length() > 11) network = network.substring(0, 11);
Stargate.debug("createPortal", "Creating personal portal");
@ -362,7 +363,7 @@ public class PortalHandler {
// Check if the player can create this gate layout
String gateName = gate.getFilename();
gateName = gateName.substring(0, gateName.indexOf('.'));
if (!deny && !Stargate.canCreateGate(player, gateName)) {
if (!deny && !PermissionHelper.canCreateGate(player, gateName)) {
Stargate.debug("createPortal", "Player does not have access to gate layout");
deny = true;
denyMsg = Stargate.getString("createGateDeny");
@ -373,8 +374,8 @@ public class PortalHandler {
Portal p = getByName(destinationName, network);
if (p != null) {
String world = p.getWorld().getName();
if (Stargate.cannotAccessWorld(player, world)) {
Stargate.debug("canCreate", "Player does not have access to destination world");
if (PermissionHelper.cannotAccessWorld(player, world)) {
Stargate.debug("canCreateNetworkGate", "Player does not have access to destination world");
deny = true;
denyMsg = Stargate.getString("createWorldDeny");
}
@ -506,7 +507,7 @@ public class PortalHandler {
Map<PortalOption, Boolean> portalOptions = new HashMap<>();
for (PortalOption option : PortalOption.values()) {
portalOptions.put(option, options.indexOf(option.getCharacterRepresentation()) != -1 &&
Stargate.canOption(player, option));
PermissionHelper.canUseOption(player, option));
}
// Can not create a non-fixed always-on gate.
@ -583,7 +584,7 @@ public class PortalHandler {
* Gets a portal given a location adjacent to its entrance
*
* @param location <p>A location adjacent to the portal's entrance</p>
* @param range <p>The range to scan for portals</p>
* @param range <p>The range to scan for portals</p>
* @return <p>The portal adjacent to the given location</p>
*/
public static Portal getByAdjacentEntrance(Location location, int range) {