Cleans permissions, and reduces redundancy in permission checking code
All checks were successful
EpicKnarvik97/Stargate/pipeline/head This commit looks good
All checks were successful
EpicKnarvik97/Stargate/pipeline/head This commit looks good
This commit is contained in:
@@ -4,12 +4,14 @@ import net.knarcraft.stargate.Stargate;
|
|||||||
import net.knarcraft.stargate.config.ConfigOption;
|
import net.knarcraft.stargate.config.ConfigOption;
|
||||||
import net.knarcraft.stargate.config.ConfigTag;
|
import net.knarcraft.stargate.config.ConfigTag;
|
||||||
import net.knarcraft.stargate.config.OptionDataType;
|
import net.knarcraft.stargate.config.OptionDataType;
|
||||||
|
import net.knarcraft.stargate.config.Permission;
|
||||||
import net.knarcraft.stargate.config.addons.DynmapManager;
|
import net.knarcraft.stargate.config.addons.DynmapManager;
|
||||||
import net.knarcraft.stargate.config.formatting.Message;
|
import net.knarcraft.stargate.config.formatting.Message;
|
||||||
import net.knarcraft.stargate.config.formatting.SGFormatBuilder;
|
import net.knarcraft.stargate.config.formatting.SGFormatBuilder;
|
||||||
import net.knarcraft.stargate.portal.Portal;
|
import net.knarcraft.stargate.portal.Portal;
|
||||||
import net.knarcraft.stargate.portal.PortalRegistry;
|
import net.knarcraft.stargate.portal.PortalRegistry;
|
||||||
import net.knarcraft.stargate.portal.PortalSignDrawer;
|
import net.knarcraft.stargate.portal.PortalSignDrawer;
|
||||||
|
import net.knarcraft.stargate.utility.PermissionHelper;
|
||||||
import net.md_5.bungee.api.ChatColor;
|
import net.md_5.bungee.api.ChatColor;
|
||||||
import org.bukkit.Material;
|
import org.bukkit.Material;
|
||||||
import org.bukkit.command.Command;
|
import org.bukkit.command.Command;
|
||||||
@@ -32,7 +34,7 @@ public class CommandConfig implements CommandExecutor {
|
|||||||
public boolean onCommand(@NotNull CommandSender commandSender, @NotNull Command command, @NotNull String s,
|
public boolean onCommand(@NotNull CommandSender commandSender, @NotNull Command command, @NotNull String s,
|
||||||
@NotNull String[] args) {
|
@NotNull String[] args) {
|
||||||
if (commandSender instanceof Player player) {
|
if (commandSender instanceof Player player) {
|
||||||
if (!player.hasPermission("stargate.admin.config")) {
|
if (!PermissionHelper.hasPermission(player, Permission.CONFIG)) {
|
||||||
new SGFormatBuilder("Permission Denied").error(commandSender);
|
new SGFormatBuilder("Permission Denied").error(commandSender);
|
||||||
return true;
|
return true;
|
||||||
}
|
}
|
||||||
|
@@ -1,7 +1,9 @@
|
|||||||
package net.knarcraft.stargate.command;
|
package net.knarcraft.stargate.command;
|
||||||
|
|
||||||
import net.knarcraft.stargate.Stargate;
|
import net.knarcraft.stargate.Stargate;
|
||||||
|
import net.knarcraft.stargate.config.Permission;
|
||||||
import net.knarcraft.stargate.config.formatting.SGFormatBuilder;
|
import net.knarcraft.stargate.config.formatting.SGFormatBuilder;
|
||||||
|
import net.knarcraft.stargate.utility.PermissionHelper;
|
||||||
import org.bukkit.command.Command;
|
import org.bukkit.command.Command;
|
||||||
import org.bukkit.command.CommandExecutor;
|
import org.bukkit.command.CommandExecutor;
|
||||||
import org.bukkit.command.CommandSender;
|
import org.bukkit.command.CommandSender;
|
||||||
@@ -17,7 +19,7 @@ public class CommandReload implements CommandExecutor {
|
|||||||
public boolean onCommand(@NotNull CommandSender commandSender, @NotNull Command command, @NotNull String s,
|
public boolean onCommand(@NotNull CommandSender commandSender, @NotNull Command command, @NotNull String s,
|
||||||
@NotNull String[] args) {
|
@NotNull String[] args) {
|
||||||
if (commandSender instanceof Player player) {
|
if (commandSender instanceof Player player) {
|
||||||
if (!player.hasPermission("stargate.admin.reload")) {
|
if (!PermissionHelper.hasPermission(player, Permission.RELOAD)) {
|
||||||
new SGFormatBuilder("Permission Denied").error(commandSender);
|
new SGFormatBuilder("Permission Denied").error(commandSender);
|
||||||
return true;
|
return true;
|
||||||
}
|
}
|
||||||
|
@@ -1,5 +1,7 @@
|
|||||||
package net.knarcraft.stargate.command;
|
package net.knarcraft.stargate.command;
|
||||||
|
|
||||||
|
import net.knarcraft.stargate.config.Permission;
|
||||||
|
import net.knarcraft.stargate.utility.PermissionHelper;
|
||||||
import org.bukkit.command.Command;
|
import org.bukkit.command.Command;
|
||||||
import org.bukkit.command.CommandSender;
|
import org.bukkit.command.CommandSender;
|
||||||
import org.bukkit.command.TabCompleter;
|
import org.bukkit.command.TabCompleter;
|
||||||
@@ -47,10 +49,10 @@ public class StarGateTabCompleter implements TabCompleter {
|
|||||||
private List<String> getAvailableCommands(@NotNull CommandSender commandSender) {
|
private List<String> getAvailableCommands(@NotNull CommandSender commandSender) {
|
||||||
List<String> commands = new ArrayList<>();
|
List<String> commands = new ArrayList<>();
|
||||||
commands.add("about");
|
commands.add("about");
|
||||||
if (!(commandSender instanceof Player player) || player.hasPermission("stargate.admin.reload")) {
|
if (!(commandSender instanceof Player player) || PermissionHelper.hasPermission(player, Permission.RELOAD)) {
|
||||||
commands.add("reload");
|
commands.add("reload");
|
||||||
}
|
}
|
||||||
if (!(commandSender instanceof Player player) || player.hasPermission("stargate.admin.config")) {
|
if (!(commandSender instanceof Player player) || PermissionHelper.hasPermission(player, Permission.CONFIG)) {
|
||||||
commands.add("config");
|
commands.add("config");
|
||||||
}
|
}
|
||||||
return commands;
|
return commands;
|
||||||
|
68
src/main/java/net/knarcraft/stargate/config/Permission.java
Normal file
68
src/main/java/net/knarcraft/stargate/config/Permission.java
Normal file
@@ -0,0 +1,68 @@
|
|||||||
|
package net.knarcraft.stargate.config;
|
||||||
|
|
||||||
|
import org.jetbrains.annotations.NotNull;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* A representation of all Stargate permissions
|
||||||
|
*/
|
||||||
|
public enum Permission {
|
||||||
|
|
||||||
|
ADMIN("admin"),
|
||||||
|
CONFIG("admin.config"),
|
||||||
|
RELOAD("admin.reload"),
|
||||||
|
CREATE_BUNGEE("admin.bungee"),
|
||||||
|
DYE_SIGN("admin.dye"),
|
||||||
|
|
||||||
|
SEE_HIDDEN("admin.hidden"),
|
||||||
|
USE_PRIVATE("admin.private"),
|
||||||
|
|
||||||
|
FREE_USAGE("free.use"),
|
||||||
|
FREE_DESTRUCTION("free.destroy"),
|
||||||
|
FREE_CREATION("free.create"),
|
||||||
|
|
||||||
|
ACCESS_SERVER("server"),
|
||||||
|
ACCESS_NETWORK("network"),
|
||||||
|
ACCESS_WORLD("world"),
|
||||||
|
|
||||||
|
CREATE_GATE("create.gate"),
|
||||||
|
CREATE_NETWORK("create.network"),
|
||||||
|
CREATE_PERSONAL("create.personal"),
|
||||||
|
|
||||||
|
DESTROY_NETWORK("destroy.network"),
|
||||||
|
DESTROY_PERSONAL("destroy.personal"),
|
||||||
|
|
||||||
|
OPTION_HIDDEN("option.hidden"),
|
||||||
|
OPTION_ALWAYS_ON("option.alwayson"),
|
||||||
|
OPTIONS_PRIVATE("options.private"),
|
||||||
|
OPTIONS_FREE("options.free"),
|
||||||
|
OPTIONS_BACKWARDS("options.backwards"),
|
||||||
|
OPTIONS_SHOW("options.show"),
|
||||||
|
OPTIONS_NO_NETWORK("options.nonetwork"),
|
||||||
|
OPTIONS_RANDOM("options.random"),
|
||||||
|
OPTIONS_BUNGEE("options.bungee"),
|
||||||
|
OPTIONS_QUIET("options.quiet"),
|
||||||
|
OPTIONS_INVISIBLE("options.invisible"),
|
||||||
|
;
|
||||||
|
|
||||||
|
private final String node;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Instantiates a new permission
|
||||||
|
*
|
||||||
|
* @param node <p>The permission node</p>
|
||||||
|
*/
|
||||||
|
Permission(@NotNull String node) {
|
||||||
|
this.node = node;
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Gets the permission node of this permission
|
||||||
|
*
|
||||||
|
* @return <p>The node of this permission</p>
|
||||||
|
*/
|
||||||
|
@NotNull
|
||||||
|
public String getNode() {
|
||||||
|
return "stargate." + this.node;
|
||||||
|
}
|
||||||
|
|
||||||
|
}
|
@@ -2,6 +2,7 @@ package net.knarcraft.stargate.config.addons;
|
|||||||
|
|
||||||
import net.knarcraft.stargate.Stargate;
|
import net.knarcraft.stargate.Stargate;
|
||||||
import net.knarcraft.stargate.config.ConfigOption;
|
import net.knarcraft.stargate.config.ConfigOption;
|
||||||
|
import net.knarcraft.stargate.config.Permission;
|
||||||
import net.knarcraft.stargate.config.formatting.Message;
|
import net.knarcraft.stargate.config.formatting.Message;
|
||||||
import net.knarcraft.stargate.config.formatting.SGFormatBuilder;
|
import net.knarcraft.stargate.config.formatting.SGFormatBuilder;
|
||||||
import net.knarcraft.stargate.portal.PortalSignDrawer;
|
import net.knarcraft.stargate.portal.PortalSignDrawer;
|
||||||
@@ -221,7 +222,7 @@ public final class EconomyConfig {
|
|||||||
* @return <p>The cost of creating the gate</p>
|
* @return <p>The cost of creating the gate</p>
|
||||||
*/
|
*/
|
||||||
public int getCreateCost(@NotNull Player player, @NotNull Gate gate) {
|
public int getCreateCost(@NotNull Player player, @NotNull Gate gate) {
|
||||||
if (isFree(player, "create")) {
|
if (isFree(player, Permission.FREE_CREATION)) {
|
||||||
return 0;
|
return 0;
|
||||||
} else {
|
} else {
|
||||||
return gate.getCreateCost();
|
return gate.getCreateCost();
|
||||||
@@ -236,7 +237,7 @@ public final class EconomyConfig {
|
|||||||
* @return <p>The cost of destroying the gate</p>
|
* @return <p>The cost of destroying the gate</p>
|
||||||
*/
|
*/
|
||||||
public int getDestroyCost(@NotNull Player player, @NotNull Gate gate) {
|
public int getDestroyCost(@NotNull Player player, @NotNull Gate gate) {
|
||||||
if (isFree(player, "destroy")) {
|
if (isFree(player, Permission.FREE_DESTRUCTION)) {
|
||||||
return 0;
|
return 0;
|
||||||
} else {
|
} else {
|
||||||
return gate.getDestroyCost();
|
return gate.getDestroyCost();
|
||||||
@@ -246,12 +247,12 @@ public final class EconomyConfig {
|
|||||||
/**
|
/**
|
||||||
* Determines if a player can do a gate action for free
|
* 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>
|
* @param permission <p>The permission necessary to allow free gate {action}</p>
|
||||||
* @return <p></p>
|
* @return <p></p>
|
||||||
*/
|
*/
|
||||||
private boolean isFree(@NotNull Player player, @NotNull String permissionNode) {
|
private boolean isFree(@NotNull Player player, @NotNull Permission permission) {
|
||||||
return !useEconomy() || PermissionHelper.hasPermission(player, "stargate.free." + permissionNode);
|
return !useEconomy() || PermissionHelper.hasPermission(player, permission.getNode());
|
||||||
}
|
}
|
||||||
|
|
||||||
}
|
}
|
||||||
|
@@ -3,6 +3,7 @@ package net.knarcraft.stargate.listener;
|
|||||||
import net.knarcraft.knarlib.formatting.FormatBuilder;
|
import net.knarcraft.knarlib.formatting.FormatBuilder;
|
||||||
import net.knarcraft.knarlib.util.UpdateChecker;
|
import net.knarcraft.knarlib.util.UpdateChecker;
|
||||||
import net.knarcraft.stargate.Stargate;
|
import net.knarcraft.stargate.Stargate;
|
||||||
|
import net.knarcraft.stargate.config.Permission;
|
||||||
import net.knarcraft.stargate.config.formatting.Message;
|
import net.knarcraft.stargate.config.formatting.Message;
|
||||||
import net.knarcraft.stargate.config.formatting.SGFormatBuilder;
|
import net.knarcraft.stargate.config.formatting.SGFormatBuilder;
|
||||||
import net.knarcraft.stargate.portal.Portal;
|
import net.knarcraft.stargate.portal.Portal;
|
||||||
@@ -53,7 +54,7 @@ public class PlayerEventListener implements Listener {
|
|||||||
//Notify joining admins about the available update
|
//Notify joining admins about the available update
|
||||||
String availableUpdate = Stargate.getUpdateAvailable();
|
String availableUpdate = Stargate.getUpdateAvailable();
|
||||||
if (availableUpdate != null && Stargate.getStargateConfig().alertAdminsAboutUpdates() &&
|
if (availableUpdate != null && Stargate.getStargateConfig().alertAdminsAboutUpdates() &&
|
||||||
player.hasPermission("stargate.admin")) {
|
PermissionHelper.hasPermission(player, Permission.ADMIN)) {
|
||||||
String updateMessage = UpdateChecker.getUpdateAvailableString(availableUpdate, Stargate.getPluginVersion());
|
String updateMessage = UpdateChecker.getUpdateAvailableString(availableUpdate, Stargate.getPluginVersion());
|
||||||
new SGFormatBuilder(updateMessage).error(player);
|
new SGFormatBuilder(updateMessage).error(player);
|
||||||
}
|
}
|
||||||
@@ -162,7 +163,7 @@ public class PlayerEventListener implements Listener {
|
|||||||
private boolean dyeSign(@NotNull PlayerInteractEvent event, @NotNull Player player, @NotNull Portal portal) {
|
private boolean dyeSign(@NotNull PlayerInteractEvent event, @NotNull Player player, @NotNull Portal portal) {
|
||||||
EquipmentSlot hand = event.getHand();
|
EquipmentSlot hand = event.getHand();
|
||||||
// Check if the player is allowed to dye the sign
|
// Check if the player is allowed to dye the sign
|
||||||
if (hand == null || (!PermissionHelper.hasPermission(player, "stargate.admin.dye") &&
|
if (hand == null || (!PermissionHelper.hasPermission(player, Permission.DYE_SIGN) &&
|
||||||
!portal.isOwner(player))) {
|
!portal.isOwner(player))) {
|
||||||
return false;
|
return false;
|
||||||
}
|
}
|
||||||
|
@@ -1,6 +1,7 @@
|
|||||||
package net.knarcraft.stargate.portal;
|
package net.knarcraft.stargate.portal;
|
||||||
|
|
||||||
import net.knarcraft.stargate.Stargate;
|
import net.knarcraft.stargate.Stargate;
|
||||||
|
import net.knarcraft.stargate.config.Permission;
|
||||||
import net.knarcraft.stargate.config.formatting.Message;
|
import net.knarcraft.stargate.config.formatting.Message;
|
||||||
import net.knarcraft.stargate.config.formatting.SGFormatBuilder;
|
import net.knarcraft.stargate.config.formatting.SGFormatBuilder;
|
||||||
import net.knarcraft.stargate.config.material.BukkitTagSpecifier;
|
import net.knarcraft.stargate.config.material.BukkitTagSpecifier;
|
||||||
@@ -148,7 +149,7 @@ public class PortalHandler {
|
|||||||
if (!portalOptions.get(PortalOption.BUNGEE)) {
|
if (!portalOptions.get(PortalOption.BUNGEE)) {
|
||||||
return true;
|
return true;
|
||||||
}
|
}
|
||||||
if (!PermissionHelper.hasPermission(player, "stargate.admin.bungee")) {
|
if (!PermissionHelper.hasPermission(player, Permission.CREATE_BUNGEE)) {
|
||||||
new SGFormatBuilder(Message.BUNGEE_CREATION_DENIED).error(player);
|
new SGFormatBuilder(Message.BUNGEE_CREATION_DENIED).error(player);
|
||||||
return false;
|
return false;
|
||||||
} else if (!Stargate.getGateConfig().enableBungee()) {
|
} else if (!Stargate.getGateConfig().enableBungee()) {
|
||||||
|
@@ -1,5 +1,6 @@
|
|||||||
package net.knarcraft.stargate.portal.property;
|
package net.knarcraft.stargate.portal.property;
|
||||||
|
|
||||||
|
import net.knarcraft.stargate.config.Permission;
|
||||||
import org.jetbrains.annotations.NotNull;
|
import org.jetbrains.annotations.NotNull;
|
||||||
|
|
||||||
/**
|
/**
|
||||||
@@ -10,71 +11,71 @@ public enum PortalOption {
|
|||||||
/**
|
/**
|
||||||
* This option allows a portal to be hidden from others
|
* This option allows a portal to be hidden from others
|
||||||
*/
|
*/
|
||||||
HIDDEN('h', "hidden", 11),
|
HIDDEN('h', Permission.OPTION_HIDDEN, 11),
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* This option allows a portal that's always on and does not need to be activated or opened each time
|
* This option allows a portal that's always on and does not need to be activated or opened each time
|
||||||
*/
|
*/
|
||||||
ALWAYS_ON('a', "alwayson", 12),
|
ALWAYS_ON('a', Permission.OPTION_ALWAYS_ON, 12),
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* This option allows a portal that's private to the stargate's owner
|
* This option allows a portal that's private to the stargate's owner
|
||||||
*/
|
*/
|
||||||
PRIVATE('p', "private", 13),
|
PRIVATE('p', Permission.OPTIONS_PRIVATE, 13),
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* This option allows a portal that's free even if stargates usually are not
|
* This option allows a portal that's free even if stargates usually are not
|
||||||
*/
|
*/
|
||||||
FREE('f', "free", 15),
|
FREE('f', Permission.OPTIONS_FREE, 15),
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* This option allows a portal where players exit through the back of the portal
|
* This option allows a portal where players exit through the back of the portal
|
||||||
*/
|
*/
|
||||||
BACKWARDS('b', "backwards", 16),
|
BACKWARDS('b', Permission.OPTIONS_BACKWARDS, 16),
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* This option shows the gate in the network list even if it's always on
|
* This option shows the gate in the network list even if it's always on
|
||||||
*/
|
*/
|
||||||
SHOW('s', "show", 17),
|
SHOW('s', Permission.OPTIONS_SHOW, 17),
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* This option hides the network name on the sign
|
* This option hides the network name on the sign
|
||||||
*/
|
*/
|
||||||
NO_NETWORK('n', "nonetwork", 18),
|
NO_NETWORK('n', Permission.OPTIONS_NO_NETWORK, 18),
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* This option allows a portal where players teleport to a random exit portal in the network
|
* This option allows a portal where players teleport to a random exit portal in the network
|
||||||
*/
|
*/
|
||||||
RANDOM('r', "random", 19),
|
RANDOM('r', Permission.OPTIONS_RANDOM, 19),
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* This option allows a portal to teleport to another server connected through BungeeCord
|
* This option allows a portal to teleport to another server connected through BungeeCord
|
||||||
*/
|
*/
|
||||||
BUNGEE('u', "bungee", 20),
|
BUNGEE('u', Permission.OPTIONS_BUNGEE, 20),
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* This option allows a portal which does not display a teleportation message, for better immersion
|
* This option allows a portal which does not display a teleportation message, for better immersion
|
||||||
*/
|
*/
|
||||||
QUIET('q', "quiet", 21),
|
QUIET('q', Permission.OPTIONS_QUIET, 21),
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* This option causes a fixed portal's sign to be removed after creation
|
* This option causes a fixed portal's sign to be removed after creation
|
||||||
*/
|
*/
|
||||||
NO_SIGN('v', "nosign", 22);
|
INVISIBLE('v', Permission.OPTIONS_INVISIBLE, 22);
|
||||||
|
|
||||||
private final char characterRepresentation;
|
private final char characterRepresentation;
|
||||||
private final String permissionString;
|
private final Permission permission;
|
||||||
private final int saveIndex;
|
private final int saveIndex;
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Instantiates a new portal options
|
* Instantiates a new portal options
|
||||||
*
|
*
|
||||||
* @param characterRepresentation <p>The character representation used on the sign to allow this option</p>
|
* @param characterRepresentation <p>The character representation used on the sign to allow this option</p>
|
||||||
* @param permissionString <p>The permission necessary to use this option</p>
|
* @param permission <p>The permission necessary to use this option</p>
|
||||||
*/
|
*/
|
||||||
PortalOption(final char characterRepresentation, @NotNull String permissionString, int saveIndex) {
|
PortalOption(final char characterRepresentation, @NotNull Permission permission, int saveIndex) {
|
||||||
this.characterRepresentation = characterRepresentation;
|
this.characterRepresentation = characterRepresentation;
|
||||||
this.permissionString = "stargate.option." + permissionString;
|
this.permission = permission;
|
||||||
this.saveIndex = saveIndex;
|
this.saveIndex = saveIndex;
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -93,8 +94,8 @@ public enum PortalOption {
|
|||||||
* @return <p>The permission necessary for this option</p>
|
* @return <p>The permission necessary for this option</p>
|
||||||
*/
|
*/
|
||||||
@NotNull
|
@NotNull
|
||||||
public String getPermissionString() {
|
public Permission getPermission() {
|
||||||
return this.permissionString;
|
return this.permission;
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
|
@@ -35,7 +35,7 @@ public class PortalOptions {
|
|||||||
}
|
}
|
||||||
|
|
||||||
if (this.hasNoSign() && !this.isFixed) {
|
if (this.hasNoSign() && !this.isFixed) {
|
||||||
this.options.put(PortalOption.NO_SIGN, false);
|
this.options.put(PortalOption.INVISIBLE, false);
|
||||||
Stargate.debug("PortalOptions", "Gate marked with no sign, but not fixed. Setting NoSign = false");
|
Stargate.debug("PortalOptions", "Gate marked with no sign, but not fixed. Setting NoSign = false");
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
@@ -191,7 +191,7 @@ public class PortalOptions {
|
|||||||
* @return <p>Whether this portal has no sign</p>
|
* @return <p>Whether this portal has no sign</p>
|
||||||
*/
|
*/
|
||||||
public boolean hasNoSign() {
|
public boolean hasNoSign() {
|
||||||
return this.options.get(PortalOption.NO_SIGN);
|
return this.options.get(PortalOption.INVISIBLE);
|
||||||
}
|
}
|
||||||
|
|
||||||
}
|
}
|
||||||
|
@@ -2,6 +2,7 @@ package net.knarcraft.stargate.utility;
|
|||||||
|
|
||||||
import net.knarcraft.knarlib.formatting.FormatBuilder;
|
import net.knarcraft.knarlib.formatting.FormatBuilder;
|
||||||
import net.knarcraft.stargate.Stargate;
|
import net.knarcraft.stargate.Stargate;
|
||||||
|
import net.knarcraft.stargate.config.Permission;
|
||||||
import net.knarcraft.stargate.config.addons.EconomyConfig;
|
import net.knarcraft.stargate.config.addons.EconomyConfig;
|
||||||
import net.knarcraft.stargate.config.formatting.Message;
|
import net.knarcraft.stargate.config.formatting.Message;
|
||||||
import net.knarcraft.stargate.config.formatting.SGFormatBuilder;
|
import net.knarcraft.stargate.config.formatting.SGFormatBuilder;
|
||||||
@@ -142,7 +143,7 @@ public final class EconomyHelper {
|
|||||||
return 0;
|
return 0;
|
||||||
}
|
}
|
||||||
//Player gets free gate use
|
//Player gets free gate use
|
||||||
if (PermissionHelper.hasPermission(player, "stargate.free.use")) {
|
if (PermissionHelper.hasPermission(player, Permission.FREE_USAGE)) {
|
||||||
return 0;
|
return 0;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@@ -1,6 +1,7 @@
|
|||||||
package net.knarcraft.stargate.utility;
|
package net.knarcraft.stargate.utility;
|
||||||
|
|
||||||
import net.knarcraft.stargate.Stargate;
|
import net.knarcraft.stargate.Stargate;
|
||||||
|
import net.knarcraft.stargate.config.Permission;
|
||||||
import net.knarcraft.stargate.config.formatting.Message;
|
import net.knarcraft.stargate.config.formatting.Message;
|
||||||
import net.knarcraft.stargate.config.formatting.SGFormatBuilder;
|
import net.knarcraft.stargate.config.formatting.SGFormatBuilder;
|
||||||
import net.knarcraft.stargate.event.StargateAccessEvent;
|
import net.knarcraft.stargate.event.StargateAccessEvent;
|
||||||
@@ -172,6 +173,19 @@ public final class PermissionHelper {
|
|||||||
return player.hasPermission(permission);
|
return player.hasPermission(permission);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* 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 permission <p>The permission to check</p>
|
||||||
|
* @return <p>True if the player has the permission</p>
|
||||||
|
*/
|
||||||
|
public static boolean hasPermission(@NotNull Player player, @NotNull Permission permission) {
|
||||||
|
return hasPermission(player, permission.getNode());
|
||||||
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Check if a player has been given a permission implicitly
|
* Check if a player has been given a permission implicitly
|
||||||
*
|
*
|
||||||
@@ -183,13 +197,14 @@ public final class PermissionHelper {
|
|||||||
* @return <p>True if the player has the permission implicitly or explicitly</p>
|
* @return <p>True if the player has the permission implicitly or explicitly</p>
|
||||||
*/
|
*/
|
||||||
public static boolean hasPermissionImplicit(@NotNull Player player, @NotNull String permission) {
|
public static boolean hasPermissionImplicit(@NotNull Player player, @NotNull String permission) {
|
||||||
|
boolean debug = Stargate.getStargateConfig().isPermissionDebuggingEnabled();
|
||||||
if (!player.isPermissionSet(permission)) {
|
if (!player.isPermissionSet(permission)) {
|
||||||
if (Stargate.getStargateConfig().isPermissionDebuggingEnabled()) {
|
if (debug) {
|
||||||
Stargate.debug("hasPermissionImplicit::Permission", permission + " => implicitly true");
|
Stargate.debug("hasPermissionImplicit::Permission", permission + " => implicitly true");
|
||||||
}
|
}
|
||||||
return true;
|
return true;
|
||||||
}
|
}
|
||||||
if (Stargate.getStargateConfig().isPermissionDebuggingEnabled()) {
|
if (debug) {
|
||||||
Stargate.debug("hasPermissionImplicit::Permission", permission + " => " +
|
Stargate.debug("hasPermissionImplicit::Permission", permission + " => " +
|
||||||
player.hasPermission(permission));
|
player.hasPermission(permission));
|
||||||
}
|
}
|
||||||
@@ -204,13 +219,7 @@ public final class PermissionHelper {
|
|||||||
* @return <p>False if the player should be allowed to access the world</p>
|
* @return <p>False if the player should be allowed to access the world</p>
|
||||||
*/
|
*/
|
||||||
public static boolean cannotAccessWorld(@NotNull Player player, @NotNull String world) {
|
public static boolean cannotAccessWorld(@NotNull Player player, @NotNull String world) {
|
||||||
//The player can access all worlds
|
return hasPermission(player, Permission.ACCESS_WORLD, world);
|
||||||
if (hasPermission(player, "stargate.world")) {
|
|
||||||
//Check if the world permission has been explicitly denied
|
|
||||||
return !hasPermissionImplicit(player, "stargate.world." + world);
|
|
||||||
}
|
|
||||||
//The player can access the destination world
|
|
||||||
return !hasPermission(player, "stargate.world." + world);
|
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
@@ -221,21 +230,16 @@ public final class PermissionHelper {
|
|||||||
* @return <p>True if the player is denied from accessing the network</p>
|
* @return <p>True if the player is denied from accessing the network</p>
|
||||||
*/
|
*/
|
||||||
public static boolean cannotAccessNetwork(@NotNull Player player, @NotNull String network) {
|
public static boolean cannotAccessNetwork(@NotNull Player player, @NotNull String network) {
|
||||||
//The player can access all networks
|
if (hasPermission(player, Permission.ACCESS_NETWORK, network)) {
|
||||||
if (hasPermission(player, "stargate.network")) {
|
|
||||||
//Check if the world permission has been explicitly denied
|
|
||||||
return !hasPermissionImplicit(player, "stargate.network." + network);
|
|
||||||
}
|
|
||||||
//Check if the player can access this network
|
|
||||||
if (hasPermission(player, "stargate.network." + network)) {
|
|
||||||
return false;
|
return false;
|
||||||
}
|
}
|
||||||
|
|
||||||
//Is able to create personal gates (Assumption is made they can also access them)
|
//Is able to create personal gates (Assumption is made they can also access them)
|
||||||
String playerName = player.getName();
|
String playerName = player.getName();
|
||||||
if (playerName.length() > getMaxNameNetworkLength()) {
|
if (playerName.length() > getMaxNameNetworkLength()) {
|
||||||
playerName = playerName.substring(0, getMaxNameNetworkLength());
|
playerName = playerName.substring(0, getMaxNameNetworkLength());
|
||||||
}
|
}
|
||||||
return !network.equals(playerName) || !hasPermission(player, "stargate.create.personal");
|
return !network.equals(playerName) || !hasPermission(player, Permission.CREATE_PERSONAL);
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
@@ -246,13 +250,7 @@ public final class PermissionHelper {
|
|||||||
* @return <p>True if the player is allowed to access the given server</p>
|
* @return <p>True if the player is allowed to access the given server</p>
|
||||||
*/
|
*/
|
||||||
public static boolean canAccessServer(@NotNull Player player, @NotNull String server) {
|
public static boolean canAccessServer(@NotNull Player player, @NotNull String server) {
|
||||||
//The player can access all servers
|
return hasPermission(player, Permission.ACCESS_SERVER, server);
|
||||||
if (hasPermission(player, "stargate.server")) {
|
|
||||||
//Check if the server permission has been explicitly denied
|
|
||||||
return hasPermissionImplicit(player, "stargate.server." + server);
|
|
||||||
}
|
|
||||||
//The player can access the destination server
|
|
||||||
return hasPermission(player, "stargate.server." + server);
|
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
@@ -269,7 +267,7 @@ public final class PermissionHelper {
|
|||||||
return true;
|
return true;
|
||||||
}
|
}
|
||||||
//Player can use this portal for free
|
//Player can use this portal for free
|
||||||
if (hasPermission(player, "stargate.free.use")) {
|
if (hasPermission(player, Permission.FREE_USAGE)) {
|
||||||
return true;
|
return true;
|
||||||
}
|
}
|
||||||
//Don't charge for free destinations unless specified in the config
|
//Don't charge for free destinations unless specified in the config
|
||||||
@@ -291,7 +289,7 @@ public final class PermissionHelper {
|
|||||||
return true;
|
return true;
|
||||||
}
|
}
|
||||||
//The player can see all hidden portals
|
//The player can see all hidden portals
|
||||||
if (hasPermission(player, "stargate.admin.hidden")) {
|
if (hasPermission(player, Permission.SEE_HIDDEN)) {
|
||||||
return true;
|
return true;
|
||||||
}
|
}
|
||||||
//The player is the owner of the portal
|
//The player is the owner of the portal
|
||||||
@@ -311,7 +309,7 @@ public final class PermissionHelper {
|
|||||||
return true;
|
return true;
|
||||||
}
|
}
|
||||||
//The player is an admin with the ability to use private gates
|
//The player is an admin with the ability to use private gates
|
||||||
return hasPermission(player, "stargate.admin.private");
|
return hasPermission(player, Permission.USE_PRIVATE);
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
@@ -322,7 +320,7 @@ public final class PermissionHelper {
|
|||||||
* @return <p>True if the player is allowed to create a portal with the given option</p>
|
* @return <p>True if the player is allowed to create a portal with the given option</p>
|
||||||
*/
|
*/
|
||||||
public static boolean canUseOption(@NotNull Player player, @NotNull PortalOption option) {
|
public static boolean canUseOption(@NotNull Player player, @NotNull PortalOption option) {
|
||||||
return hasPermission(player, option.getPermissionString());
|
return hasPermission(player, option.getPermission());
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
@@ -333,13 +331,7 @@ public final class PermissionHelper {
|
|||||||
* @return <p>True if the player is allowed to create the new gate</p>
|
* @return <p>True if the player is allowed to create the new gate</p>
|
||||||
*/
|
*/
|
||||||
public static boolean canCreateNetworkGate(@NotNull Player player, @NotNull String network) {
|
public static boolean canCreateNetworkGate(@NotNull Player player, @NotNull String network) {
|
||||||
//Check if the player is allowed to create a portal on any network
|
return hasPermission(player, Permission.CREATE_NETWORK, network);
|
||||||
if (hasPermission(player, "stargate.create.network")) {
|
|
||||||
//Check if the network has been explicitly denied
|
|
||||||
return hasPermissionImplicit(player, "stargate.create.network." + network);
|
|
||||||
}
|
|
||||||
//Check if the player is allowed to create on this specific network
|
|
||||||
return hasPermission(player, "stargate.create.network." + network);
|
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
@@ -349,7 +341,7 @@ public final class PermissionHelper {
|
|||||||
* @return <p>True if the player is allowed</p>
|
* @return <p>True if the player is allowed</p>
|
||||||
*/
|
*/
|
||||||
public static boolean canCreatePersonalPortal(@NotNull Player player) {
|
public static boolean canCreatePersonalPortal(@NotNull Player player) {
|
||||||
return hasPermission(player, "stargate.create.personal");
|
return hasPermission(player, Permission.CREATE_PERSONAL);
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
@@ -360,13 +352,7 @@ public final class PermissionHelper {
|
|||||||
* @return <p>True if the player is allowed to create a portal with the given gate layout</p>
|
* @return <p>True if the player is allowed to create a portal with the given gate layout</p>
|
||||||
*/
|
*/
|
||||||
public static boolean canCreatePortal(@NotNull Player player, @NotNull String gate) {
|
public static boolean canCreatePortal(@NotNull Player player, @NotNull String gate) {
|
||||||
//Check if the player is allowed to create all gates
|
return hasPermission(player, Permission.CREATE_GATE, gate);
|
||||||
if (hasPermission(player, "stargate.create.gate")) {
|
|
||||||
//Check if the gate type has been explicitly denied
|
|
||||||
return hasPermissionImplicit(player, "stargate.create.gate." + gate);
|
|
||||||
}
|
|
||||||
//Check if the player can create the specific gate type
|
|
||||||
return hasPermission(player, "stargate.create.gate." + gate);
|
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
@@ -381,20 +367,14 @@ public final class PermissionHelper {
|
|||||||
|
|
||||||
//Use a special check for bungee portals
|
//Use a special check for bungee portals
|
||||||
if (portal.getOptions().isBungee()) {
|
if (portal.getOptions().isBungee()) {
|
||||||
return hasPermission(player, "stargate.admin.bungee");
|
return hasPermission(player, Permission.CREATE_BUNGEE);
|
||||||
}
|
}
|
||||||
|
|
||||||
//Check if the player is allowed to destroy on all networks
|
if (hasPermission(player, Permission.DESTROY_NETWORK, network)) {
|
||||||
if (hasPermission(player, "stargate.destroy.network")) {
|
|
||||||
//Check if the network has been explicitly denied
|
|
||||||
return hasPermissionImplicit(player, "stargate.destroy.network." + network);
|
|
||||||
}
|
|
||||||
//Check if the player is allowed to destroy on the network
|
|
||||||
if (hasPermission(player, "stargate.destroy.network." + network)) {
|
|
||||||
return true;
|
return true;
|
||||||
}
|
}
|
||||||
//Check if personal portal and if the player is allowed to destroy it
|
//Check if personal portal and if the player is allowed to destroy it
|
||||||
return portal.isOwner(player) && hasPermission(player, "stargate.destroy.personal");
|
return portal.isOwner(player) && hasPermission(player, Permission.DESTROY_PERSONAL);
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
@@ -448,4 +428,23 @@ public final class PermissionHelper {
|
|||||||
return false;
|
return false;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Checks if the given player has a permission for the given value, or has the catch-all permission
|
||||||
|
*
|
||||||
|
* @param player <p>The player trying to create a portal</p>
|
||||||
|
* @param permission <p>The parent permission to check</p>
|
||||||
|
* @param value <p>The child node to check</p>
|
||||||
|
* @return <p>True if the player has the explicit child node, or gets the permission implicitly from the parent</p>
|
||||||
|
*/
|
||||||
|
private static boolean hasPermission(@NotNull Player player, @NotNull Permission permission, @NotNull String value) {
|
||||||
|
String fullNode = permission.getNode() + "." + value;
|
||||||
|
//Check if the player is allowed to create all gates
|
||||||
|
if (hasPermission(player, permission)) {
|
||||||
|
//Check if the gate type has been explicitly denied
|
||||||
|
return hasPermissionImplicit(player, fullNode);
|
||||||
|
}
|
||||||
|
//Check if the player can create the specific gate type
|
||||||
|
return hasPermission(player, fullNode);
|
||||||
|
}
|
||||||
|
|
||||||
}
|
}
|
||||||
|
@@ -99,7 +99,7 @@ permissions:
|
|||||||
stargate.option.nonetwork: true
|
stargate.option.nonetwork: true
|
||||||
stargate.option.random: true
|
stargate.option.random: true
|
||||||
stargate.option.quiet: true
|
stargate.option.quiet: true
|
||||||
stargate.option.nosign: true
|
stargate.option.invisible: true
|
||||||
stargate.option.hidden:
|
stargate.option.hidden:
|
||||||
description: Allows the creation of a hidden stargate
|
description: Allows the creation of a hidden stargate
|
||||||
default: false
|
default: false
|
||||||
@@ -127,7 +127,7 @@ permissions:
|
|||||||
stargate.option.quiet:
|
stargate.option.quiet:
|
||||||
description: Allows the creation of a stargate which does not output anything to the chat
|
description: Allows the creation of a stargate which does not output anything to the chat
|
||||||
default: false
|
default: false
|
||||||
stargate.option.nosign:
|
stargate.option.invisible:
|
||||||
description: Allows the creation of a stargate which has no sign
|
description: Allows the creation of a stargate which has no sign
|
||||||
default: false
|
default: false
|
||||||
stargate.admin.hidden:
|
stargate.admin.hidden:
|
||||||
|
Reference in New Issue
Block a user