From f681db629f71d9d56ac43537f847ed2f281f7745 Mon Sep 17 00:00:00 2001 From: EpicKnarvik97 Date: Mon, 20 Sep 2021 13:56:30 +0200 Subject: [PATCH] Refactors a lot of code, and extracts permission-related functions to the PermissionHelper class --- .../java/net/knarcraft/stargate/Stargate.java | 389 +++++------------- .../command/StarGateTabCompleter.java | 9 +- .../stargate/container/BlockLocation.java | 4 +- .../stargate/listener/BlockEventListener.java | 7 +- .../listener/EntityEventListener.java | 2 +- .../listener/PlayerEventListener.java | 12 +- .../listener/PluginEventListener.java | 3 +- .../listener/VehicleEventListener.java | 2 +- .../stargate/listener/WorldEventListener.java | 2 +- .../net/knarcraft/stargate/portal/Gate.java | 2 +- .../stargate/portal/GateHandler.java | 46 ++- .../knarcraft/stargate/portal/GateLayout.java | 20 +- .../net/knarcraft/stargate/portal/Portal.java | 9 +- .../stargate/portal/PortalDirection.java | 1 - .../stargate/portal/PortalHandler.java | 23 +- .../stargate/thread/BlockChangeThread.java | 6 +- .../stargate/thread/StarGateThread.java | 2 +- .../stargate/utility/BungeeHelper.java | 2 +- .../stargate/utility/DirectionHelper.java | 12 +- .../stargate/utility/EconomyHandler.java | 27 +- .../stargate/utility/EconomyHelper.java | 2 +- .../stargate/utility/PermissionHelper.java | 272 ++++++++++++ .../stargate/utility/SignHelper.java | 24 +- 23 files changed, 497 insertions(+), 381 deletions(-) create mode 100644 src/main/java/net/knarcraft/stargate/utility/PermissionHelper.java diff --git a/src/main/java/net/knarcraft/stargate/Stargate.java b/src/main/java/net/knarcraft/stargate/Stargate.java index 6cb4d43..7962c7d 100644 --- a/src/main/java/net/knarcraft/stargate/Stargate.java +++ b/src/main/java/net/knarcraft/stargate/Stargate.java @@ -15,10 +15,10 @@ import net.knarcraft.stargate.listener.WorldEventListener; import net.knarcraft.stargate.portal.GateHandler; import net.knarcraft.stargate.portal.Portal; import net.knarcraft.stargate.portal.PortalHandler; -import net.knarcraft.stargate.portal.PortalOption; import net.knarcraft.stargate.thread.BlockChangeThread; import net.knarcraft.stargate.thread.StarGateThread; import net.knarcraft.stargate.utility.EconomyHandler; +import net.knarcraft.stargate.utility.PermissionHelper; import org.bukkit.Bukkit; import org.bukkit.ChatColor; import org.bukkit.Server; @@ -28,7 +28,6 @@ import org.bukkit.command.CommandSender; import org.bukkit.command.PluginCommand; import org.bukkit.configuration.file.FileConfiguration; import org.bukkit.entity.Player; -import org.bukkit.plugin.Plugin; import org.bukkit.plugin.PluginDescriptionFile; import org.bukkit.plugin.PluginManager; import org.bukkit.plugin.java.JavaPlugin; @@ -192,7 +191,7 @@ public class Stargate extends JavaPlugin { } // Check if the player can use the private gate - if (portal.isPrivate() && !Stargate.canPrivate(player, portal)) { + if (portal.isPrivate() && !PermissionHelper.canPrivate(player, portal)) { Stargate.sendMessage(player, Stargate.getString("denyMsg")); return; } @@ -207,109 +206,14 @@ public class Stargate extends JavaPlugin { portal.open(player, false); } - /* - * Check whether the player has the given permissions. - */ - /** - * Checks whether a player has the given permission - * - *

This is the same as player.hasPermission(), but this function allows for printing permission debugging info.

- * - * @param player

The player to check

- * @param perm

The permission to check

- * @return

True if the player has the permission

- */ - public static boolean hasPermission(Player player, String perm) { - if (permissionDebuggingEnabled) { - Stargate.debug("hasPerm::SuperPerm(" + player.getName() + ")", perm + " => " + player.hasPermission(perm)); - } - return player.hasPermission(perm); - } - - /** - * Check a deep permission, this will check to see if the permissions is defined for this use - * - *

If using Permissions it will return the same as hasPerm. If using SuperPerms will return true if the node - * isn't defined, or the value of the node if it is

- * - * @param player

The player to check

- * @param permission

The permission to check

- * @return

True if the player has the permission or it is not set

- */ - public static boolean hasPermDeep(Player player, String permission) { - if (!player.isPermissionSet(permission)) { - if (permissionDebuggingEnabled) { - Stargate.debug("hasPermDeep::SuperPerm", permission + " => true"); - } - return true; - } - if (permissionDebuggingEnabled) { - Stargate.debug("hasPermDeep::SuperPerms", permission + " => " + player.hasPermission(permission)); - } - return player.hasPermission(permission); - } - - /* - * Check whether player can teleport to dest world - */ - public static boolean cannotAccessWorld(Player player, String world) { - // Can use all stargate player features or access all worlds - if (hasPermission(player, "stargate.use") || hasPermission(player, "stargate.world")) { - // Do a deep check to see if the player lacks this specific world node - return !hasPermDeep(player, "stargate.world." + world); - } - // Can access dest world - return !hasPermission(player, "stargate.world." + world); - } - - /** - * Checks whether a player can access the given network - * @param player

The player to check

- * @param network

The network to check

- * @return

True if the player is denied from accessing the network

- */ - public static boolean cannotAccessNetwork(Player player, String network) { - // Can user all stargate player features, or access all networks - if (hasPermission(player, "stargate.use") || hasPermission(player, "stargate.network")) { - // Do a deep check to see if the player lacks this specific network node - return !hasPermDeep(player, "stargate.network." + network); - } - //Check if the player can access this network - if (hasPermission(player, "stargate.network." + network)) { - return false; - } - // Is able to create personal gates (Assumption is made they can also access them) - String playerName = player.getName(); - if (playerName.length() > 11) playerName = playerName.substring(0, 11); - return !network.equals(playerName) || !hasPermission(player, "stargate.create.personal"); - } - - /* - * Check whether the player can access this server - */ - public static boolean canAccessServer(Player player, String server) { - // Can user all stargate player features, or access all servers - if (hasPermission(player, "stargate.use") || hasPermission(player, "stargate.servers")) { - // Do a deep check to see if the player lacks this specific server node - return hasPermDeep(player, "stargate.server." + server); - } - // Can access this server - return hasPermission(player, "stargate.server." + server); - } - - /* - * Call the StargateAccessPortal event, used for other plugins to bypass Permissions checks - */ - - /** - * Creates a stargate access event and gives the result + * Creates a StargateAccessPortal and gives the result * *

The event is used for other plugins to bypass the permission checks

* * @param player

The player trying to use the portal

* @param portal

The portal the player is trying to use

- * @param deny

Whether the player's access has already been denied by a check

+ * @param deny

Whether the player's access has already been denied by a check

* @return

False if the player should be allowed through the portal

*/ public static boolean cannotAccessPortal(Player player, Portal portal, boolean deny) { @@ -329,138 +233,19 @@ public class Stargate extends JavaPlugin { public static boolean cannotAccessPortal(Player player, Portal entrancePortal, Portal destination) { boolean deny = false; // Check if player has access to this server for Bungee gates - if (entrancePortal.isBungee() && !Stargate.canAccessServer(player, entrancePortal.getNetwork())) { + if (entrancePortal.isBungee() && !PermissionHelper.canAccessServer(player, entrancePortal.getNetwork())) { Stargate.debug("cannotAccessPortal", "Cannot access server"); deny = true; - } else if (Stargate.cannotAccessNetwork(player, entrancePortal.getNetwork())) { + } else if (PermissionHelper.cannotAccessNetwork(player, entrancePortal.getNetwork())) { Stargate.debug("cannotAccessPortal", "Cannot access network"); deny = true; - } else if (!entrancePortal.isBungee() && Stargate.cannotAccessWorld(player, destination.getWorld().getName())) { + } else if (!entrancePortal.isBungee() && PermissionHelper.cannotAccessWorld(player, destination.getWorld().getName())) { Stargate.debug("cannotAccessPortal", "Cannot access world"); deny = true; } return Stargate.cannotAccessPortal(player, entrancePortal, deny); } - /* - * Return true if the portal is free for the player - */ - public static boolean isFree(Player player, Portal src, Portal dest) { - // This gate is free - if (src.isFree()) return true; - // Player gets free use - if (hasPermission(player, "stargate.free") || Stargate.hasPermission(player, "stargate.free.use")) return true; - // Don't charge for free destination gates - return dest != null && !EconomyHandler.chargeFreeDestination && dest.isFree(); - } - - /* - * Check whether the player can see this gate (Hidden property check) - */ - - /** - * Checks whether the player can see this gate (Hidden property check) - * - *

This decides if the player can see the gate on the network selection screen

- * - * @param player

The player to check

- * @param portal

The portal to check

- * @return

True if the given player can see the given portal

- */ - public static boolean canSee(Player player, Portal portal) { - // The gate is not hidden - if (!portal.isHidden()) { - return true; - } - // The player is an admin with the ability to see hidden gates - if (hasPermission(player, "stargate.admin") || hasPermission(player, "stargate.admin.hidden")) { - return true; - } - // The player is the owner of the gate - return portal.isOwner(player); - } - - /* - * Check if the player can use this private gate - */ - public static boolean canPrivate(Player player, Portal portal) { - // Check if the player is the owner of the gate - if (portal.isOwner(player)) return true; - // The player is an admin with the ability to use private gates - return hasPermission(player, "stargate.admin") || hasPermission(player, "stargate.admin.private"); - } - - /* - * Check if the player has access to {option} - */ - public static boolean canOption(Player player, PortalOption option) { - // Check if the player can use all options - if (hasPermission(player, "stargate.option") || option == PortalOption.BUNGEE) { - return true; - } - // Check if they can use this specific option - return hasPermission(player, option.getPermissionString()); - } - - /* - * Check if the player can create gates on {network} - */ - public static boolean canCreate(Player player, String network) { - // Check for general create - if (hasPermission(player, "stargate.create")) return true; - // Check for all network create permission - if (hasPermission(player, "stargate.create.network")) { - // Do a deep check to see if the player lacks this specific network node - return hasPermDeep(player, "stargate.create.network." + network); - } - // Check for this specific network - return hasPermission(player, "stargate.create.network." + network); - - } - - /* - * Check if the player can create a personal gate - */ - public static boolean canCreatePersonal(Player player) { - // Check for general create - if (hasPermission(player, "stargate.create")) return true; - // Check for personal - return hasPermission(player, "stargate.create.personal"); - } - - /* - * Check if the player can create this gate layout - */ - public static boolean canCreateGate(Player player, String gate) { - // Check for general create - if (hasPermission(player, "stargate.create")) return true; - // Check for all gate create permissions - if (hasPermission(player, "stargate.create.gate")) { - // Do a deep check to see if the player lacks this specific gate node - return hasPermDeep(player, "stargate.create.gate." + gate); - } - // Check for this specific gate - return hasPermission(player, "stargate.create.gate." + gate); - } - - /* - * Check if the player can destroy this gate - */ - public static boolean canDestroy(Player player, Portal portal) { - String network = portal.getNetwork(); - // Check for general destroy - if (hasPermission(player, "stargate.destroy")) return true; - // Check for all network destroy permission - if (hasPermission(player, "stargate.destroy.network")) { - // Do a deep check to see if the player lacks permission for this network node - return hasPermDeep(player, "stargate.destroy.network." + network); - } - // Check for this specific network - if (hasPermission(player, "stargate.destroy.network." + network)) return true; - // Check for personal gate - return portal.isOwner(player) && hasPermission(player, "stargate.destroy.personal"); - } - /** * Replaces a list of variables in a string in the order they are given * @@ -518,7 +303,37 @@ public class Stargate extends JavaPlugin { log.info(pluginDescriptionFile.getName() + " v." + pluginDescriptionFile.getVersion() + " is enabled."); - // Register events before loading gates to stop weird things happening. + //Register events before loading gates to stop weird things happening. + registerEventListeners(); + + this.loadConfig(); + + //Enable the required channels for Bungee support + if (enableBungee) { + startStopBungeeListener(true); + } + + // It is important to load languages here, as they are used during reloadGates() + languageLoader = new LanguageLoader(languageFolder, Stargate.languageName); + + this.createMissingFolders(); + this.loadGates(); + this.loadAllPortals(); + + // Check to see if Economy is loaded yet. + setupVaultEconomy(); + + //Run necessary threads + getServer().getScheduler().runTaskTimer(this, new StarGateThread(), 0L, 100L); + getServer().getScheduler().runTaskTimer(this, new BlockChangeThread(), 0L, 1L); + + this.registerCommands(); + } + + /** + * Registers all event listeners + */ + private void registerEventListeners() { pluginManager.registerEvents(new PlayerEventListener(), this); pluginManager.registerEvents(new BlockEventListener(), this); @@ -527,30 +342,11 @@ public class Stargate extends JavaPlugin { pluginManager.registerEvents(new PortalEventListener(), this); pluginManager.registerEvents(new WorldEventListener(), this); pluginManager.registerEvents(new PluginEventListener(this), this); - - this.loadConfig(); - - // Enable the required channels for Bungee support - if (enableBungee) { - startStopBungeeListener(true); - } - - // It is important to load languages here, as they are used during reloadGates() - languageLoader = new LanguageLoader(languageFolder, Stargate.languageName); - - this.migrate(); - this.loadGates(); - this.loadAllPortals(); - - // Check to see if Economy is loaded yet. - setupVaultEconomy(); - - getServer().getScheduler().runTaskTimer(this, new StarGateThread(), 0L, 100L); - getServer().getScheduler().runTaskTimer(this, new BlockChangeThread(), 0L, 1L); - - this.registerCommands(); } + /** + * Registers a command for this plugin + */ private void registerCommands() { PluginCommand stargateCommand = this.getCommand("stargate"); if (stargateCommand != null) { @@ -559,34 +355,63 @@ public class Stargate extends JavaPlugin { } } + /** + * Loads all config values + */ public void loadConfig() { - reloadConfig(); + this.reloadConfig(); newConfig = this.getConfig(); // Copy default values if required newConfig.options().copyDefaults(true); - // Load values into variables + //Language + languageName = newConfig.getString("language"); + + //Folders portalFolder = newConfig.getString("folders.portalFolder"); gateFolder = newConfig.getString("folders.gateFolder"); - String defaultNetwork = newConfig.getString("gates.defaultGateNetwork"); - defaultGateNetwork = defaultNetwork != null ? defaultNetwork.trim() : null; - destroyExplosion = newConfig.getBoolean("gates.destroyedByExplosion"); - maxGates = newConfig.getInt("gates.maxGatesEachNetwork"); - languageName = newConfig.getString("language"); - rememberDestination = newConfig.getBoolean("gates.cosmetic.rememberDestination"); - ignoreEntrance = newConfig.getBoolean("gates.integrity.ignoreEntrance"); - handleVehicles = newConfig.getBoolean("gates.functionality.handleVehicles"); - sortNetworkDestinations = newConfig.getBoolean("gates.cosmetic.sortNetworkDestinations"); - protectEntrance = newConfig.getBoolean("gates.integrity.protectEntrance"); - enableBungee = newConfig.getBoolean("gates.functionality.enableBungee"); - verifyPortals = newConfig.getBoolean("gates.integrity.verifyPortals"); - // Sign color - loadSignColor(newConfig.getString("gates.cosmetic.signColor")); - // Debug + //Debug debuggingEnabled = newConfig.getBoolean("debugging.debug"); permissionDebuggingEnabled = newConfig.getBoolean("debugging.permissionDebug"); - // Economy + + //Gates + loadGateConfig(); + + //Economy + loadEconomyConfig(); + + this.saveConfig(); + } + + /** + * Loads all config values related to gates + */ + private void loadGateConfig() { + String defaultNetwork = newConfig.getString("gates.defaultGateNetwork"); + defaultGateNetwork = defaultNetwork != null ? defaultNetwork.trim() : null; + maxGates = newConfig.getInt("gates.maxGatesEachNetwork"); + + //Functionality + handleVehicles = newConfig.getBoolean("gates.functionality.handleVehicles"); + enableBungee = newConfig.getBoolean("gates.functionality.enableBungee"); + + //Integrity + protectEntrance = newConfig.getBoolean("gates.integrity.protectEntrance"); + verifyPortals = newConfig.getBoolean("gates.integrity.verifyPortals"); + ignoreEntrance = newConfig.getBoolean("gates.integrity.ignoreEntrance"); + destroyExplosion = newConfig.getBoolean("gates.integrity.destroyedByExplosion"); + + //Cosmetic + sortNetworkDestinations = newConfig.getBoolean("gates.cosmetic.sortNetworkDestinations"); + rememberDestination = newConfig.getBoolean("gates.cosmetic.rememberDestination"); + loadSignColor(newConfig.getString("gates.cosmetic.signColor")); + } + + /** + * Loads all config values related to economy + */ + private void loadEconomyConfig() { EconomyHandler.economyEnabled = newConfig.getBoolean("economy.useEconomy"); EconomyHandler.setCreateCost(newConfig.getInt("economy.createCost")); EconomyHandler.setDestroyCost(newConfig.getInt("economy.destroyCost")); @@ -594,12 +419,11 @@ public class Stargate extends JavaPlugin { EconomyHandler.toOwner = newConfig.getBoolean("economy.toOwner"); EconomyHandler.chargeFreeDestination = newConfig.getBoolean("economy.chargeFreeDestination"); EconomyHandler.freeGatesGreen = newConfig.getBoolean("economy.freeGatesGreen"); - - this.saveConfig(); } /** * Loads the correct sign color given a sign color string + * * @param signColor

A string representing a sign color

*/ private void loadSignColor(String signColor) { @@ -614,18 +438,27 @@ public class Stargate extends JavaPlugin { Stargate.signColor = ChatColor.BLACK; } + /** + * Forces all open portals to close + */ public void closeAllPortals() { // Close all gates prior to reloading - for (Portal p : openList) { - p.close(true); + for (Portal openPortal : openList) { + openPortal.close(true); } } + /** + * Loads all available gates + */ public void loadGates() { GateHandler.loadGates(gateFolder); log.info(Stargate.getString("prefix") + "Loaded " + GateHandler.getGateCount() + " gate layouts"); } + /** + * Loads all portals in all un-managed worlds + */ public void loadAllPortals() { for (World world : getServer().getWorlds()) { if (!managedWorlds.contains(world.getName())) { @@ -635,8 +468,10 @@ public class Stargate extends JavaPlugin { } } - private void migrate() { - // Only migrate if new file doesn't exist. + /** + * Creates missing folders + */ + private void createMissingFolders() { File newPortalDir = new File(portalFolder); if (!newPortalDir.exists()) { if (!newPortalDir.mkdirs()) { @@ -651,22 +486,6 @@ public class Stargate extends JavaPlugin { } } - /* - * Check if a plugin is loaded/enabled already. Returns the plugin if so, null otherwise - */ - private Plugin checkPlugin(String p) { - Plugin plugin = pluginManager.getPlugin(p); - return checkPlugin(plugin); - } - - private Plugin checkPlugin(Plugin plugin) { - if (plugin != null && plugin.isEnabled()) { - log.info(Stargate.getString("prefix") + "Found " + plugin.getDescription().getName() + " (v" + plugin.getDescription().getVersion() + ")"); - return plugin; - } - return null; - } - /** * Reloads all portals and files * diff --git a/src/main/java/net/knarcraft/stargate/command/StarGateTabCompleter.java b/src/main/java/net/knarcraft/stargate/command/StarGateTabCompleter.java index 2675e80..0242b4a 100644 --- a/src/main/java/net/knarcraft/stargate/command/StarGateTabCompleter.java +++ b/src/main/java/net/knarcraft/stargate/command/StarGateTabCompleter.java @@ -16,11 +16,16 @@ public class StarGateTabCompleter implements TabCompleter { @Override public @Nullable List onTabComplete(@NotNull CommandSender commandSender, @NotNull Command command, - @NotNull String s, @NotNull String[] strings) { + @NotNull String s, @NotNull String[] args) { List commands = new ArrayList<>(); commands.add("about"); commands.add("reload"); - return commands; + + if (args.length == 1) { + return commands; + } else { + return new ArrayList<>(); + } } } diff --git a/src/main/java/net/knarcraft/stargate/container/BlockLocation.java b/src/main/java/net/knarcraft/stargate/container/BlockLocation.java index e5ea6b6..7ea37e1 100644 --- a/src/main/java/net/knarcraft/stargate/container/BlockLocation.java +++ b/src/main/java/net/knarcraft/stargate/container/BlockLocation.java @@ -70,7 +70,7 @@ public class BlockLocation extends Location { * @param x

The x position relative to this block's position

* @param y

The y position relative to this block's position

* @param z

The z position relative to this block's position

- * @param yaw

The yaw of the location

+ * @param yaw

The yaw of the location

* @param rotY

The y rotation of the location

* @return

A new location

*/ @@ -105,7 +105,7 @@ public class BlockLocation extends Location { * @param right

* @param depth

The y position relative to the current position

* @param distance

The distance away from the previous location to the new location

- * @param yaw

The yaw of the location

+ * @param yaw

The yaw of the location

* @param rotY

Unused

* @param modX

x modifier. Defines movement along the x-axis. 0 for no movement

* @param modY

Unused

diff --git a/src/main/java/net/knarcraft/stargate/listener/BlockEventListener.java b/src/main/java/net/knarcraft/stargate/listener/BlockEventListener.java index cc5eb9e..ae9f314 100644 --- a/src/main/java/net/knarcraft/stargate/listener/BlockEventListener.java +++ b/src/main/java/net/knarcraft/stargate/listener/BlockEventListener.java @@ -1,12 +1,13 @@ package net.knarcraft.stargate.listener; -import net.knarcraft.stargate.portal.Portal; -import net.knarcraft.stargate.portal.PortalHandler; import net.knarcraft.stargate.Stargate; import net.knarcraft.stargate.event.StargateDestroyEvent; +import net.knarcraft.stargate.portal.Portal; +import net.knarcraft.stargate.portal.PortalHandler; import net.knarcraft.stargate.utility.EconomyHandler; import net.knarcraft.stargate.utility.EconomyHelper; import net.knarcraft.stargate.utility.MaterialHelper; +import net.knarcraft.stargate.utility.PermissionHelper; import org.bukkit.Material; import org.bukkit.block.Block; import org.bukkit.block.data.type.WallSign; @@ -84,7 +85,7 @@ public class BlockEventListener implements Listener { String denyMsg = ""; //Decide if the user can destroy the portal - if (!Stargate.canDestroy(player, portal)) { + if (!PermissionHelper.canDestroyPortal(player, portal)) { denyMsg = Stargate.getString("denyMsg"); deny = true; Stargate.log.info(Stargate.getString("prefix") + player.getName() + " tried to destroy gate"); diff --git a/src/main/java/net/knarcraft/stargate/listener/EntityEventListener.java b/src/main/java/net/knarcraft/stargate/listener/EntityEventListener.java index 43f5fcb..3c32226 100644 --- a/src/main/java/net/knarcraft/stargate/listener/EntityEventListener.java +++ b/src/main/java/net/knarcraft/stargate/listener/EntityEventListener.java @@ -1,8 +1,8 @@ package net.knarcraft.stargate.listener; +import net.knarcraft.stargate.Stargate; import net.knarcraft.stargate.portal.Portal; import net.knarcraft.stargate.portal.PortalHandler; -import net.knarcraft.stargate.Stargate; import net.knarcraft.stargate.utility.EntityHelper; import org.bukkit.block.Block; import org.bukkit.entity.Entity; diff --git a/src/main/java/net/knarcraft/stargate/listener/PlayerEventListener.java b/src/main/java/net/knarcraft/stargate/listener/PlayerEventListener.java index f7ac066..028fe60 100644 --- a/src/main/java/net/knarcraft/stargate/listener/PlayerEventListener.java +++ b/src/main/java/net/knarcraft/stargate/listener/PlayerEventListener.java @@ -1,13 +1,14 @@ package net.knarcraft.stargate.listener; +import net.knarcraft.stargate.Stargate; import net.knarcraft.stargate.container.BlockLocation; import net.knarcraft.stargate.portal.Portal; import net.knarcraft.stargate.portal.PortalHandler; -import net.knarcraft.stargate.Stargate; import net.knarcraft.stargate.utility.BungeeHelper; import net.knarcraft.stargate.utility.EconomyHandler; import net.knarcraft.stargate.utility.EconomyHelper; import net.knarcraft.stargate.utility.MaterialHelper; +import net.knarcraft.stargate.utility.PermissionHelper; import org.bukkit.GameMode; import org.bukkit.World; import org.bukkit.block.Block; @@ -127,10 +128,11 @@ public class PlayerEventListener implements Listener { /** * Checks whether a player move event is relevant for this plugin - * @param event

The player move event to check

- * @param player

The player which moved

+ * + * @param event

The player move event to check

+ * @param player

The player which moved

* @param fromLocation

The location the player is moving from

- * @param toLocation

The location the player is moving to

+ * @param toLocation

The location the player is moving to

* @return

True if the event is relevant

*/ private boolean isRelevantMoveEvent(PlayerMoveEvent event, Player player, BlockLocation fromLocation, BlockLocation toLocation) { @@ -232,7 +234,7 @@ public class PlayerEventListener implements Listener { * @return

True if the player should be denied

*/ private boolean cannotAccessPortal(Player player, Portal portal) { - boolean deny = Stargate.cannotAccessNetwork(player, portal.getNetwork()); + boolean deny = PermissionHelper.cannotAccessNetwork(player, portal.getNetwork()); if (Stargate.cannotAccessPortal(player, portal, deny)) { Stargate.sendMessage(player, Stargate.getString("denyMsg")); diff --git a/src/main/java/net/knarcraft/stargate/listener/PluginEventListener.java b/src/main/java/net/knarcraft/stargate/listener/PluginEventListener.java index fbe34be..a65b4e2 100644 --- a/src/main/java/net/knarcraft/stargate/listener/PluginEventListener.java +++ b/src/main/java/net/knarcraft/stargate/listener/PluginEventListener.java @@ -1,7 +1,7 @@ package net.knarcraft.stargate.listener; -import net.knarcraft.stargate.utility.EconomyHandler; import net.knarcraft.stargate.Stargate; +import net.knarcraft.stargate.utility.EconomyHandler; import org.bukkit.event.EventHandler; import org.bukkit.event.Listener; import org.bukkit.event.server.PluginDisableEvent; @@ -28,6 +28,7 @@ public class PluginEventListener implements Listener { * This event listens for and announces that the vault plugin was detected and enabled * *

Each time this event is called, the economy handler will try to enable vault

+ * * @param ignored

The actual event called. This is currently not used

*/ @EventHandler diff --git a/src/main/java/net/knarcraft/stargate/listener/VehicleEventListener.java b/src/main/java/net/knarcraft/stargate/listener/VehicleEventListener.java index eba8466..55a7986 100644 --- a/src/main/java/net/knarcraft/stargate/listener/VehicleEventListener.java +++ b/src/main/java/net/knarcraft/stargate/listener/VehicleEventListener.java @@ -1,8 +1,8 @@ package net.knarcraft.stargate.listener; +import net.knarcraft.stargate.Stargate; import net.knarcraft.stargate.portal.Portal; import net.knarcraft.stargate.portal.PortalHandler; -import net.knarcraft.stargate.Stargate; import net.knarcraft.stargate.utility.EconomyHandler; import net.knarcraft.stargate.utility.EconomyHelper; import net.knarcraft.stargate.utility.EntityHelper; diff --git a/src/main/java/net/knarcraft/stargate/listener/WorldEventListener.java b/src/main/java/net/knarcraft/stargate/listener/WorldEventListener.java index 08c248b..64cb5fa 100644 --- a/src/main/java/net/knarcraft/stargate/listener/WorldEventListener.java +++ b/src/main/java/net/knarcraft/stargate/listener/WorldEventListener.java @@ -1,7 +1,7 @@ package net.knarcraft.stargate.listener; -import net.knarcraft.stargate.portal.PortalHandler; import net.knarcraft.stargate.Stargate; +import net.knarcraft.stargate.portal.PortalHandler; import org.bukkit.World; import org.bukkit.event.EventHandler; import org.bukkit.event.Listener; diff --git a/src/main/java/net/knarcraft/stargate/portal/Gate.java b/src/main/java/net/knarcraft/stargate/portal/Gate.java index 55673fc..5f56701 100644 --- a/src/main/java/net/knarcraft/stargate/portal/Gate.java +++ b/src/main/java/net/knarcraft/stargate/portal/Gate.java @@ -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; diff --git a/src/main/java/net/knarcraft/stargate/portal/GateHandler.java b/src/main/java/net/knarcraft/stargate/portal/GateHandler.java index 1a90965..0aac91b 100644 --- a/src/main/java/net/knarcraft/stargate/portal/GateHandler.java +++ b/src/main/java/net/knarcraft/stargate/portal/GateHandler.java @@ -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

The name of the file containing the gate layout

+ * @param fileName

The name of the file containing the gate layout

* @param parentFolder

The parent folder of the layout file

- * @param scanner

The scanner to use for reading the gate layout

+ * @param scanner

The scanner to use for reading the gate layout

* @return

The loaded gate or null if unable to load the gate

*/ private static Gate loadGate(String fileName, String parentFolder, Scanner scanner) { @@ -145,10 +146,10 @@ public class GateHandler { /** * Creates a new gate * - * @param config

The config map to get configuration values from

+ * @param config

The config map to get configuration values from

* @param fileName

The name of the saved gate config file

- * @param layout

The layout matrix of the new gate

- * @param types

The mapping for used gate material types

+ * @param layout

The layout matrix of the new gate

+ * @param types

The mapping for used gate material types

* @return

A new gate or null if the config is invalid

*/ private static Gate createGate(Map config, String fileName, Character[][] layout, @@ -173,7 +174,7 @@ public class GateHandler { /** * Validate that a gate is valid * - * @param gate

The gate to validate

+ * @param gate

The gate to validate

* @param fileName

The filename of the loaded gate file

* @return

True if the gate is valid. False otherwise

*/ @@ -196,7 +197,7 @@ public class GateHandler { * Generates a matrix storing the gate layout * * @param design

The design of the gate layout

- * @param cols

The largest amount of columns in the design

+ * @param cols

The largest amount of columns in the design

* @return

A matrix containing the gate's layout

*/ private static Character[][] generateLayoutMatrix(List> design, int cols) { @@ -222,12 +223,12 @@ public class GateHandler { /** * Reads the gate file * - * @param scanner

The scanner to read from

- * @param types

The map of characters to store valid symbols in

- * @param fileName

The filename of the loaded gate config file

- * @param design

The list to store the loaded design to

+ * @param scanner

The scanner to read from

+ * @param types

The map of characters to store valid symbols in

+ * @param fileName

The filename of the loaded gate config file

+ * @param design

The list to store the loaded design to

* @param frameTypes

The set of gate frame types to store to

- * @param config

The map of config values to store to

+ * @param config

The map of config values to store to

* @return

The column count/width of the loaded gate

*/ private static int readGateFile(Scanner scanner, Map types, String fileName, @@ -265,11 +266,11 @@ public class GateHandler { /** * Reads one design line of the gate layout file * - * @param line

The line to read

- * @param cols

The current max columns value of the design

- * @param types

The map of characters to check for valid symbols

+ * @param line

The line to read

+ * @param cols

The current max columns value of the design

+ * @param types

The map of characters to check for valid symbols

* @param fileName

The filename of the loaded gate config file

- * @param design

The list to store the loaded design to

+ * @param design

The list to store the loaded design to

* @return

The new max columns value of the design

*/ private static int readGateDesignLine(String line, int cols, Map types, String fileName, @@ -295,10 +296,10 @@ public class GateHandler { /** * Reads one config value from the gate layout file * - * @param line

The line to read

- * @param types

The map of characters to materials to store to

+ * @param line

The line to read

+ * @param types

The map of characters to materials to store to

* @param frameTypes

The set of gate frame types to store to

- * @param config

The map of config values to store to

+ * @param config

The map of config values to store to

* @throws Exception

If an invalid material is encountered

*/ private static void readGateConfigValue(String line, Map types, Set frameTypes, @@ -322,9 +323,10 @@ public class GateHandler { /** * Reads an integer configuration key - * @param config

The configuration to read

+ * + * @param config

The configuration to read

* @param fileName

The filename of the config file

- * @param key

The config key to read

+ * @param key

The config key to read

* @return

The read value, or -1 if it cannot be read

*/ private static int readConfig(Map config, String fileName, String key) { diff --git a/src/main/java/net/knarcraft/stargate/portal/GateLayout.java b/src/main/java/net/knarcraft/stargate/portal/GateLayout.java index 402946f..2b734ff 100644 --- a/src/main/java/net/knarcraft/stargate/portal/GateLayout.java +++ b/src/main/java/net/knarcraft/stargate/portal/GateLayout.java @@ -16,7 +16,7 @@ import java.util.List; */ public class GateLayout { - private final Character [][] layout; + private final Character[][] layout; private final List exits = new ArrayList<>(); private RelativeBlockVector[] entrances = new RelativeBlockVector[0]; private RelativeBlockVector[] border = new RelativeBlockVector[0]; @@ -39,7 +39,7 @@ public class GateLayout { * @return

Two of the gate's corners

*/ 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

The list of control blocks to save to

+ * @param controlList

The list of control blocks to save to

* @param entranceList

The list of entrances to save to

- * @param borderList

The list of border blocks to save to

+ * @param borderList

The list of border blocks to save to

* @return

A list of depths of possible extra exits

*/ private int[] readLayout(List controlList, List entranceList, @@ -181,13 +181,13 @@ public class GateLayout { /** * Parses one character of the layout * - * @param key

The character read

- * @param rowIndex

The row of the read character

- * @param lineIndex

The line of the read character

- * @param exitDepths

The list of exit depths to save to

- * @param controlList

The list of control blocks to save to

+ * @param key

The character read

+ * @param rowIndex

The row of the read character

+ * @param lineIndex

The line of the read character

+ * @param exitDepths

The list of exit depths to save to

+ * @param controlList

The list of control blocks to save to

* @param entranceList

The list of entrances to save to

- * @param borderList

The list of border blocks to save to

+ * @param borderList

The list of border blocks to save to

*/ private void parseLayoutCharacter(Character key, int rowIndex, int lineIndex, int[] exitDepths, List controlList, List entranceList, diff --git a/src/main/java/net/knarcraft/stargate/portal/Portal.java b/src/main/java/net/knarcraft/stargate/portal/Portal.java index 001634c..b4f33f2 100644 --- a/src/main/java/net/knarcraft/stargate/portal/Portal.java +++ b/src/main/java/net/knarcraft/stargate/portal/Portal.java @@ -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

The top-left block of the portal. This is used to decide the positions of the rest of the portal

* @param modX

* @param modZ

- * @param yaw

+ * @param yaw

* @param id

The location of the portal's id block, which is the sign which activated the portal

* @param button

The location of the portal's open button

* @param destination

The destination defined on the sign's destination line

@@ -921,6 +921,7 @@ public class Portal { /** * Loads one chunk + * * @param chunk

The chunk to load

*/ private void loadOneChunk(Chunk chunk) { diff --git a/src/main/java/net/knarcraft/stargate/portal/PortalDirection.java b/src/main/java/net/knarcraft/stargate/portal/PortalDirection.java index f509e21..d6bbb4a 100644 --- a/src/main/java/net/knarcraft/stargate/portal/PortalDirection.java +++ b/src/main/java/net/knarcraft/stargate/portal/PortalDirection.java @@ -3,5 +3,4 @@ package net.knarcraft.stargate.portal; public class PortalDirection { - } diff --git a/src/main/java/net/knarcraft/stargate/portal/PortalHandler.java b/src/main/java/net/knarcraft/stargate/portal/PortalHandler.java index ac7f41d..afd1e43 100644 --- a/src/main/java/net/knarcraft/stargate/portal/PortalHandler.java +++ b/src/main/java/net/knarcraft/stargate/portal/PortalHandler.java @@ -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 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

A location adjacent to the portal's entrance

- * @param range

The range to scan for portals

+ * @param range

The range to scan for portals

* @return

The portal adjacent to the given location

*/ public static Portal getByAdjacentEntrance(Location location, int range) { diff --git a/src/main/java/net/knarcraft/stargate/thread/BlockChangeThread.java b/src/main/java/net/knarcraft/stargate/thread/BlockChangeThread.java index 3a5bfdd..985ba5a 100644 --- a/src/main/java/net/knarcraft/stargate/thread/BlockChangeThread.java +++ b/src/main/java/net/knarcraft/stargate/thread/BlockChangeThread.java @@ -1,7 +1,7 @@ package net.knarcraft.stargate.thread; -import net.knarcraft.stargate.container.BlockChangeRequest; import net.knarcraft.stargate.Stargate; +import net.knarcraft.stargate.container.BlockChangeRequest; import org.bukkit.Axis; import org.bukkit.Material; import org.bukkit.World; @@ -44,6 +44,7 @@ public class BlockChangeThread implements Runnable { /** * Prevents end gateway portal from behaving strangely + * * @param block

The block to fix

*/ private void fixEndGatewayGate(Block block) { @@ -55,8 +56,9 @@ public class BlockChangeThread implements Runnable { /** * Sets the orientation axis of the placed block + * * @param block

The block to orient

- * @param axis

The axis to use for orienting the block

+ * @param axis

The axis to use for orienting the block

*/ private void orientBlock(Block block, Axis axis) { Orientable orientable = (Orientable) block.getBlockData(); diff --git a/src/main/java/net/knarcraft/stargate/thread/StarGateThread.java b/src/main/java/net/knarcraft/stargate/thread/StarGateThread.java index 4b03cc0..65984b5 100644 --- a/src/main/java/net/knarcraft/stargate/thread/StarGateThread.java +++ b/src/main/java/net/knarcraft/stargate/thread/StarGateThread.java @@ -1,7 +1,7 @@ package net.knarcraft.stargate.thread; -import net.knarcraft.stargate.portal.Portal; import net.knarcraft.stargate.Stargate; +import net.knarcraft.stargate.portal.Portal; import java.util.Iterator; diff --git a/src/main/java/net/knarcraft/stargate/utility/BungeeHelper.java b/src/main/java/net/knarcraft/stargate/utility/BungeeHelper.java index b51cf4e..64e1a4b 100644 --- a/src/main/java/net/knarcraft/stargate/utility/BungeeHelper.java +++ b/src/main/java/net/knarcraft/stargate/utility/BungeeHelper.java @@ -1,8 +1,8 @@ package net.knarcraft.stargate.utility; +import net.knarcraft.stargate.Stargate; import net.knarcraft.stargate.portal.Portal; import net.knarcraft.stargate.portal.PortalHandler; -import net.knarcraft.stargate.Stargate; import org.bukkit.entity.Player; import java.io.ByteArrayInputStream; diff --git a/src/main/java/net/knarcraft/stargate/utility/DirectionHelper.java b/src/main/java/net/knarcraft/stargate/utility/DirectionHelper.java index 6db573d..9f747fc 100644 --- a/src/main/java/net/knarcraft/stargate/utility/DirectionHelper.java +++ b/src/main/java/net/knarcraft/stargate/utility/DirectionHelper.java @@ -41,6 +41,7 @@ public final class DirectionHelper { /** * Gets a block face given a yaw + * * @param yaw

The yaw to use

* @return

The block face the yaw corresponds to

*/ @@ -63,6 +64,7 @@ public final class DirectionHelper { /** * Gets a direction vector given a yaw + * * @param yaw

The yaw to use

* @return

The direction vector of the yaw

*/ @@ -95,12 +97,13 @@ public final class DirectionHelper { /** * Adds a relative block vector to a location, accounting for direction + * * @param location

The location to adjust

- * @param right

The amount of blocks to the right to adjust

- * @param depth

The amount of blocks upward to adjust

+ * @param right

The amount of blocks to the right to adjust

+ * @param depth

The amount of blocks upward to adjust

* @param distance

The distance outward to adjust

- * @param modX

The x modifier to use

- * @param modZ

The z modifier to use

+ * @param modX

The x modifier to use

+ * @param modZ

The z modifier to use

* @return

The altered location

*/ public static Location adjustLocation(Location location, double right, double depth, double distance, int modX, @@ -110,6 +113,7 @@ public final class DirectionHelper { /** * Normalizes a yaw to make it positive and no larger than 360 degrees + * * @param yaw

The yaw to normalize

* @return

The normalized yaw

*/ diff --git a/src/main/java/net/knarcraft/stargate/utility/EconomyHandler.java b/src/main/java/net/knarcraft/stargate/utility/EconomyHandler.java index 27908aa..bb5150f 100644 --- a/src/main/java/net/knarcraft/stargate/utility/EconomyHandler.java +++ b/src/main/java/net/knarcraft/stargate/utility/EconomyHandler.java @@ -90,8 +90,9 @@ public final class EconomyHandler { /** * Charges the player for an action, if required + * * @param player

The player to take money from

- * @param cost

The cost of the transaction

+ * @param cost

The cost of the transaction

* @return

True if the player was charged successfully

*/ public static boolean chargePlayerIfNecessary(Player player, int cost) { @@ -104,9 +105,10 @@ public final class EconomyHandler { /** * Charges the player for an action, if required + * * @param player

The player to take money from

* @param target

The target to pay

- * @param cost

The cost of the transaction

+ * @param cost

The cost of the transaction

* @return

True if the player was charged successfully

*/ public static boolean chargePlayerIfNecessary(Player player, UUID target, int cost) { @@ -170,6 +172,7 @@ public final class EconomyHandler { /** * Checks whether a payment transaction should be skipped + * * @param cost

The cost of the transaction

* @return

True if the transaction should be skipped

*/ @@ -180,8 +183,8 @@ public final class EconomyHandler { /** * Determines the cost of using a gate * - * @param player

The player trying to use the gate

- * @param source

The source/entry portal

+ * @param player

The player trying to use the gate

+ * @param source

The source/entry portal

* @param destination

The destination portal

* @return

The cost of using the portal

*/ @@ -199,7 +202,8 @@ public final class EconomyHandler { return 0; } //Player gets free gate use - if (Stargate.hasPermission(player, "stargate.free") || Stargate.hasPermission(player, "stargate.free.use")) { + if (PermissionHelper.hasPermission(player, "stargate.free") || + PermissionHelper.hasPermission(player, "stargate.free.use")) { return 0; } @@ -208,8 +212,9 @@ public final class EconomyHandler { /** * Gets the cost of creating the given gate + * * @param player

The player creating the gate

- * @param gate

The gate type used

+ * @param gate

The gate type used

* @return

The cost of creating the gate

*/ public static int getCreateCost(Player player, Gate gate) { @@ -222,8 +227,9 @@ public final class EconomyHandler { /** * Gets the cost of destroying the given gate + * * @param player

The player creating the gate

- * @param gate

The gate type used

+ * @param gate

The gate type used

* @return

The cost of destroying the gate

*/ public static int getDestroyCost(Player player, Gate gate) { @@ -236,13 +242,14 @@ public final class EconomyHandler { /** * Determines if a player can do a gate action for free - * @param player

The player to check

+ * + * @param player

The player to check

* @param permissionNode

The free.permissionNode necessary to allow free gate {action}

* @return

*/ private static boolean isFree(Player player, String permissionNode) { - return !EconomyHandler.useEconomy() || Stargate.hasPermission(player, "stargate.free") || - Stargate.hasPermission(player, "stargate.free." + permissionNode); + return !EconomyHandler.useEconomy() || PermissionHelper.hasPermission(player, "stargate.free") || + PermissionHelper.hasPermission(player, "stargate.free." + permissionNode); } /** diff --git a/src/main/java/net/knarcraft/stargate/utility/EconomyHelper.java b/src/main/java/net/knarcraft/stargate/utility/EconomyHelper.java index 25db29f..54ec4a0 100644 --- a/src/main/java/net/knarcraft/stargate/utility/EconomyHelper.java +++ b/src/main/java/net/knarcraft/stargate/utility/EconomyHelper.java @@ -1,7 +1,7 @@ package net.knarcraft.stargate.utility; -import net.knarcraft.stargate.portal.Portal; import net.knarcraft.stargate.Stargate; +import net.knarcraft.stargate.portal.Portal; import org.bukkit.entity.Player; /** diff --git a/src/main/java/net/knarcraft/stargate/utility/PermissionHelper.java b/src/main/java/net/knarcraft/stargate/utility/PermissionHelper.java new file mode 100644 index 0000000..37965b2 --- /dev/null +++ b/src/main/java/net/knarcraft/stargate/utility/PermissionHelper.java @@ -0,0 +1,272 @@ +package net.knarcraft.stargate.utility; + +import net.knarcraft.stargate.Stargate; +import net.knarcraft.stargate.portal.Portal; +import net.knarcraft.stargate.portal.PortalOption; +import org.bukkit.entity.Player; + +/** + * Helper class for deciding which actions a player is allowed to perform + */ +public final class PermissionHelper { + + private PermissionHelper() { + + } + + /** + * Checks whether a player has the given permission + * + *

This is the same as player.hasPermission(), but this function allows for printing permission debugging info.

+ * + * @param player

The player to check

+ * @param perm

The permission to check

+ * @return

True if the player has the permission

+ */ + public static boolean hasPermission(Player player, String perm) { + if (Stargate.permissionDebuggingEnabled) { + Stargate.debug("hasPerm::SuperPerm(" + player.getName() + ")", perm + " => " + player.hasPermission(perm)); + } + return player.hasPermission(perm); + } + + /** + * Check a deep permission, this will check to see if the permissions is defined for this use + * + *

If using Permissions it will return the same as hasPerm. If using SuperPerms will return true if the node + * isn't defined, or the value of the node if it is

+ * + * @param player

The player to check

+ * @param permission

The permission to check

+ * @return

True if the player has the permission or it is not set

+ */ + public static boolean hasPermDeep(Player player, String permission) { + if (!player.isPermissionSet(permission)) { + if (Stargate.permissionDebuggingEnabled) { + Stargate.debug("hasPermDeep::SuperPerm", permission + " => true"); + } + return true; + } + if (Stargate.permissionDebuggingEnabled) { + Stargate.debug("hasPermDeep::SuperPerms", permission + " => " + player.hasPermission(permission)); + } + return player.hasPermission(permission); + } + + /** + * Checks whether a player can access the given world + * @param player

The player trying to access the world

+ * @param world

The world the player is trying to access

+ * @return

False if the player should be allowed to access the world

+ */ + public static boolean cannotAccessWorld(Player player, String world) { + // Can use all stargate player features or access all worlds + if (hasPermission(player, "stargate.use") || hasPermission(player, "stargate.world")) { + // Do a deep check to see if the player lacks this specific world node + return !hasPermDeep(player, "stargate.world." + world); + } + // Can access dest world + return !hasPermission(player, "stargate.world." + world); + } + + /** + * Checks whether a player can access the given network + * + * @param player

The player to check

+ * @param network

The network to check

+ * @return

True if the player is denied from accessing the network

+ */ + public static boolean cannotAccessNetwork(Player player, String network) { + // Can user all stargate player features, or access all networks + if (hasPermission(player, "stargate.use") || hasPermission(player, "stargate.network")) { + // Do a deep check to see if the player lacks this specific network node + return !hasPermDeep(player, "stargate.network." + network); + } + //Check if the player can access this network + if (hasPermission(player, "stargate.network." + network)) { + return false; + } + //Is able to create personal gates (Assumption is made they can also access them) + String playerName = player.getName(); + if (playerName.length() > 11) { + playerName = playerName.substring(0, 11); + } + return !network.equals(playerName) || !hasPermission(player, "stargate.create.personal"); + } + + /** + * Checks whether a player can access the given bungee server + * @param player

The player trying to teleport

+ * @param server

The server the player is trying to connect to

+ * @return

True if the player is allowed to access the given server

+ */ + public static boolean canAccessServer(Player player, String server) { + //Can user all stargate player features, or access all servers + if (hasPermission(player, "stargate.use") || hasPermission(player, "stargate.servers")) { + //Do a deep check to see if the player lacks this specific server node + return hasPermDeep(player, "stargate.server." + server); + } + //Can access this server + return hasPermission(player, "stargate.server." + server); + } + + /** + * Checks whether the given player can teleport the given stretch for free + * + * @param player

The player trying to teleport

+ * @param src

The portal the player is entering

+ * @param dest

The portal the player wants to teleport to

+ * @return

True if the player can travel for free

+ */ + public static boolean isFree(Player player, Portal src, Portal dest) { + // This gate is free + if (src.isFree()) { + return true; + } + // Player gets free use + if (hasPermission(player, "stargate.free") || hasPermission(player, "stargate.free.use")) { + return true; + } + // Don't charge for free destination gates + return dest != null && !EconomyHandler.chargeFreeDestination && dest.isFree(); + } + + /** + * Checks whether the player can see this gate (Hidden property check) + * + *

This decides if the player can see the gate on the network selection screen

+ * + * @param player

The player to check

+ * @param portal

The portal to check

+ * @return

True if the given player can see the given portal

+ */ + public static boolean canSeePortal(Player player, Portal portal) { + // The gate is not hidden + if (!portal.isHidden()) { + return true; + } + // The player is an admin with the ability to see hidden gates + if (hasPermission(player, "stargate.admin") || hasPermission(player, "stargate.admin.hidden")) { + return true; + } + // The player is the owner of the gate + return portal.isOwner(player); + } + + /** + * Checks if the given player is allowed to use the given private portal + * + * @param player

The player trying to use the portal

+ * @param portal

The private portal used

+ * @return

True if the player is allowed to use the portal

+ */ + public static boolean canPrivate(Player player, Portal portal) { + //Check if the player is the owner of the gate + if (portal.isOwner(player)) { + return true; + } + //The player is an admin with the ability to use private gates + return hasPermission(player, "stargate.admin") || hasPermission(player, "stargate.admin.private"); + } + + /** + * Checks if the given player has access to the given portal option + * + * @param player

The player trying to use the option

+ * @param option

The option the player is trying to use

+ * @return

True if the player is allowed to create a portal with the given option

+ */ + public static boolean canUseOption(Player player, PortalOption option) { + //Check if the player can use all options + if (hasPermission(player, "stargate.option") || option == PortalOption.BUNGEE) { + return true; + } + //Check if they can use this specific option + return hasPermission(player, option.getPermissionString()); + } + + /** + * Checks if the given player is allowed to create gates on the given network + * + * @param player

The player trying to create a new gate

+ * @param network

The network the player is trying to create a gate on

+ * @return

True if the player is allowed to create the new gate

+ */ + public static boolean canCreateNetworkGate(Player player, String network) { + //Check for general create + if (hasPermission(player, "stargate.create")) { + return true; + } + //Check for all network create permission + if (hasPermission(player, "stargate.create.network")) { + // Do a deep check to see if the player lacks this specific network node + return hasPermDeep(player, "stargate.create.network." + network); + } + //Check for this specific network + return hasPermission(player, "stargate.create.network." + network); + + } + + /** + * Checks whether the given player is allowed to create a personal gate + * + * @param player

The player trying to create the new gate

+ * @return

True if the player is allowed

+ */ + public static boolean canCreatePersonalGate(Player player) { + //Check for general create + if (hasPermission(player, "stargate.create")) { + return true; + } + //Check for personal + return hasPermission(player, "stargate.create.personal"); + } + + /** + * Checks if the given player can create a portal with the given gate layout + * + * @param player

The player trying to create a portal

+ * @param gate

The gate type of the new portal

+ * @return

True if the player is allowed to create a portal with the given gate layout

+ */ + public static boolean canCreateGate(Player player, String gate) { + //Check for general create + if (hasPermission(player, "stargate.create")) { + return true; + } + //Check for all gate create permissions + if (hasPermission(player, "stargate.create.gate")) { + // Do a deep check to see if the player lacks this specific gate node + return hasPermDeep(player, "stargate.create.gate." + gate); + } + //Check for this specific gate + return hasPermission(player, "stargate.create.gate." + gate); + } + + /** + * Checks if the given player can destroy the given portal + * + * @param player

The player trying to destroy the portal

+ * @param portal

The portal to destroy

+ * @return

True if the player is allowed to destroy the portal

+ */ + public static boolean canDestroyPortal(Player player, Portal portal) { + String network = portal.getNetwork(); + //Check for general destroy + if (hasPermission(player, "stargate.destroy")) { + return true; + } + //Check for all network destroy permission + if (hasPermission(player, "stargate.destroy.network")) { + //Do a deep check to see if the player lacks permission for this network node + return hasPermDeep(player, "stargate.destroy.network." + network); + } + //Check for this specific network + if (hasPermission(player, "stargate.destroy.network." + network)) { + return true; + } + //Check for personal gate + return portal.isOwner(player) && hasPermission(player, "stargate.destroy.personal"); + } + +} diff --git a/src/main/java/net/knarcraft/stargate/utility/SignHelper.java b/src/main/java/net/knarcraft/stargate/utility/SignHelper.java index e902770..891c209 100644 --- a/src/main/java/net/knarcraft/stargate/utility/SignHelper.java +++ b/src/main/java/net/knarcraft/stargate/utility/SignHelper.java @@ -7,7 +7,7 @@ import org.bukkit.ChatColor; import org.bukkit.block.Sign; /** - * This class helps drawing the sign on a portal as it's a bit too complicated to be contained within the portal class + * This class helps to draw the sign on a portal as it's a bit too complicated to be contained within the portal class */ public final class SignHelper { @@ -41,7 +41,7 @@ public final class SignHelper { } /** - * Draws a sign with chooseable network locations + * Draws a sign with choose-able network locations * * @param sign

The sign to draw on

*/ @@ -51,7 +51,7 @@ public final class SignHelper { int destinationIndex = portal.getDestinations().indexOf(portal.getDestinationName()); boolean freeGatesGreen = EconomyHandler.useEconomy() && EconomyHandler.freeGatesGreen; - //Last entry, and not only entry. Draw the entry two previously + //Last, and not only entry. Draw the entry two back if ((destinationIndex == maxIndex) && (maxIndex > 1)) { drawNetworkSignLine(freeGatesGreen, sign, ++signLineIndex, destinationIndex - 2, portal); } @@ -62,8 +62,8 @@ public final class SignHelper { //Draw the chosen entry (line 2 or 3) drawNetworkSignChosenLine(freeGatesGreen, sign, ++signLineIndex, portal); //Has another entry and space on the sign - if ((maxIndex >= destinationIndex + 1) && (++signLineIndex <= 3)) { - drawNetworkSignLine(freeGatesGreen, sign, signLineIndex, destinationIndex + 1, portal); + if ((maxIndex >= destinationIndex + 1)) { + drawNetworkSignLine(freeGatesGreen, sign, ++signLineIndex, destinationIndex + 1, portal); } //Has another entry and space on the sign if ((maxIndex >= destinationIndex + 2) && (++signLineIndex <= 3)) { @@ -75,13 +75,13 @@ public final class SignHelper { * Draws the chosen destination on one sign line * * @param freeGatesGreen

Whether to display free gates in a green color

- * @param sign

The sign to draw on

- * @param signLineIndex

The line to draw on

+ * @param sign

The sign to draw on

+ * @param signLineIndex

The line to draw on

*/ private static void drawNetworkSignChosenLine(boolean freeGatesGreen, Sign sign, int signLineIndex, Portal portal) { if (freeGatesGreen) { Portal destination = PortalHandler.getByName(portal.getDestinationName(), portal.getNetwork()); - boolean green = Stargate.isFree(portal.getActivePlayer(), portal, destination); + boolean green = PermissionHelper.isFree(portal.getActivePlayer(), portal, destination); Stargate.setLine(sign, signLineIndex, (green ? ChatColor.DARK_GREEN : "") + ">" + portal.getDestinationName() + "<"); } else { Stargate.setLine(sign, signLineIndex, " >" + portal.getDestinationName() + "< "); @@ -91,16 +91,16 @@ public final class SignHelper { /** * Draws one network destination on one sign line * - * @param freeGatesGreen

Whether to display free gates in a green color

- * @param sign

The sign to draw on

- * @param signLineIndex

The line to draw on

+ * @param freeGatesGreen

Whether to display free gates in a green color

+ * @param sign

The sign to draw on

+ * @param signLineIndex

The line to draw on

* @param destinationIndex

The index of the destination to draw

*/ private static void drawNetworkSignLine(boolean freeGatesGreen, Sign sign, int signLineIndex, int destinationIndex, Portal portal) { if (freeGatesGreen) { Portal destination = PortalHandler.getByName(portal.getDestinations().get(destinationIndex), portal.getNetwork()); - boolean green = Stargate.isFree(portal.getActivePlayer(), portal, destination); + boolean green = PermissionHelper.isFree(portal.getActivePlayer(), portal, destination); Stargate.setLine(sign, signLineIndex, (green ? ChatColor.DARK_GREEN : "") + portal.getDestinations().get(destinationIndex)); } else { Stargate.setLine(sign, signLineIndex, portal.getDestinations().get(destinationIndex));