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.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;

View File

@ -41,6 +41,7 @@ public final class DirectionHelper {
/**
* Gets a block face given a yaw
*
* @param yaw <p>The yaw to use</p>
* @return <p>The block face the yaw corresponds to</p>
*/
@ -63,6 +64,7 @@ public final class DirectionHelper {
/**
* Gets a direction vector given a yaw
*
* @param yaw <p>The yaw to use</p>
* @return <p>The direction vector of the yaw</p>
*/
@ -95,12 +97,13 @@ public final class DirectionHelper {
/**
* Adds a relative block vector to a location, accounting for direction
*
* @param location <p>The location to adjust</p>
* @param right <p>The amount of blocks to the right to adjust</p>
* @param depth <p>The amount of blocks upward to adjust</p>
* @param right <p>The amount of blocks to the right to adjust</p>
* @param depth <p>The amount of blocks upward to adjust</p>
* @param distance <p>The distance outward to adjust</p>
* @param modX <p>The x modifier to use</p>
* @param modZ <p>The z modifier to use</p>
* @param modX <p>The x modifier to use</p>
* @param modZ <p>The z modifier to use</p>
* @return <p>The altered location</p>
*/
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 <p>The yaw to normalize</p>
* @return <p>The normalized yaw</p>
*/

View File

@ -90,8 +90,9 @@ public final class EconomyHandler {
/**
* Charges the player for an action, if required
*
* @param player <p>The player to take money from</p>
* @param cost <p>The cost of the transaction</p>
* @param cost <p>The cost of the transaction</p>
* @return <p>True if the player was charged successfully</p>
*/
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 <p>The player to take money from</p>
* @param target <p>The target to pay</p>
* @param cost <p>The cost of the transaction</p>
* @param cost <p>The cost of the transaction</p>
* @return <p>True if the player was charged successfully</p>
*/
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 <p>The cost of the transaction</p>
* @return <p>True if the transaction should be skipped</p>
*/
@ -180,8 +183,8 @@ public final class EconomyHandler {
/**
* Determines the cost of using a gate
*
* @param player <p>The player trying to use the gate</p>
* @param source <p>The source/entry portal</p>
* @param player <p>The player trying to use the gate</p>
* @param source <p>The source/entry portal</p>
* @param destination <p>The destination portal</p>
* @return <p>The cost of using the portal</p>
*/
@ -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 <p>The player creating the gate</p>
* @param gate <p>The gate type used</p>
* @param gate <p>The gate type used</p>
* @return <p>The cost of creating the gate</p>
*/
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 <p>The player creating the gate</p>
* @param gate <p>The gate type used</p>
* @param gate <p>The gate type used</p>
* @return <p>The cost of destroying the gate</p>
*/
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 <p>The player to check</p>
*
* @param player <p>The player to check</p>
* @param permissionNode <p>The free.permissionNode necessary to allow free gate {action}</p>
* @return <p></p>
*/
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);
}
/**

View File

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

View File

@ -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
*
* <p>This is the same as player.hasPermission(), but this function allows for printing permission debugging info.</p>
*
* @param player <p>The player to check</p>
* @param perm <p>The permission to check</p>
* @return <p>True if the player has the permission</p>
*/
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
*
* <p>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</p>
*
* @param player <p>The player to check</p>
* @param permission <p>The permission to check</p>
* @return <p>True if the player has the permission or it is not set</p>
*/
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 <p>The player trying to access the world</p>
* @param world <p>The world the player is trying to access</p>
* @return <p>False if the player should be allowed to access the world</p>
*/
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 <p>The player to check</p>
* @param network <p>The network to check</p>
* @return <p>True if the player is denied from accessing the network</p>
*/
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 <p>The player trying to teleport</p>
* @param server <p>The server the player is trying to connect to</p>
* @return <p>True if the player is allowed to access the given server</p>
*/
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 <p>The player trying to teleport</p>
* @param src <p>The portal the player is entering</p>
* @param dest <p>The portal the player wants to teleport to</p>
* @return <p>True if the player can travel for free</p>
*/
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)
*
* <p>This decides if the player can see the gate on the network selection screen</p>
*
* @param player <p>The player to check</p>
* @param portal <p>The portal to check</p>
* @return <p>True if the given player can see the given portal</p>
*/
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 <p>The player trying to use the portal</p>
* @param portal <p>The private portal used</p>
* @return <p>True if the player is allowed to use the portal</p>
*/
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 <p>The player trying to use the option</p>
* @param option <p>The option the player is trying to use</p>
* @return <p>True if the player is allowed to create a portal with the given option</p>
*/
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 <p>The player trying to create a new gate</p>
* @param network <p>The network the player is trying to create a gate on</p>
* @return <p>True if the player is allowed to create the new gate</p>
*/
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 <p>The player trying to create the new gate</p>
* @return <p>True if the player is allowed</p>
*/
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 <p>The player trying to create a portal</p>
* @param gate <p>The gate type of the new portal</p>
* @return <p>True if the player is allowed to create a portal with the given gate layout</p>
*/
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 <p>The player trying to destroy the portal</p>
* @param portal <p>The portal to destroy</p>
* @return <p>True if the player is allowed to destroy the portal</p>
*/
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");
}
}

View File

@ -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 <p>The sign to draw on</p>
*/
@ -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 <p>Whether to display free gates in a green color</p>
* @param sign <p>The sign to draw on</p>
* @param signLineIndex <p>The line to draw on</p>
* @param sign <p>The sign to draw on</p>
* @param signLineIndex <p>The line to draw on</p>
*/
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 <p>Whether to display free gates in a green color</p>
* @param sign <p>The sign to draw on</p>
* @param signLineIndex <p>The line to draw on</p>
* @param freeGatesGreen <p>Whether to display free gates in a green color</p>
* @param sign <p>The sign to draw on</p>
* @param signLineIndex <p>The line to draw on</p>
* @param destinationIndex <p>The index of the destination to draw</p>
*/
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));