From ffd787f2b099b42550b415eb455d0b7e5615a0bb Mon Sep 17 00:00:00 2001 From: Daniel Saukel Date: Sat, 24 Oct 2020 15:36:05 +0200 Subject: [PATCH] Use Bukkit permission system --- .../dungeonsxl/player/DGlobalPlayer.java | 2 +- .../dungeonsxl/player/DPermission.java | 61 +++---------------- .../requirement/PermissionRequirement.java | 3 +- .../dungeonsxl/sign/DSignListener.java | 3 +- 4 files changed, 13 insertions(+), 56 deletions(-) diff --git a/core/src/main/java/de/erethon/dungeonsxl/player/DGlobalPlayer.java b/core/src/main/java/de/erethon/dungeonsxl/player/DGlobalPlayer.java index e72e509f..95db5010 100644 --- a/core/src/main/java/de/erethon/dungeonsxl/player/DGlobalPlayer.java +++ b/core/src/main/java/de/erethon/dungeonsxl/player/DGlobalPlayer.java @@ -253,7 +253,7 @@ public class DGlobalPlayer implements GlobalPlayer { @Override public boolean hasPermission(String permission) { - return DPermission.hasPermission(player, permission); + return player.hasPermission(permission); } public boolean hasPermission(DPermission permission) { diff --git a/core/src/main/java/de/erethon/dungeonsxl/player/DPermission.java b/core/src/main/java/de/erethon/dungeonsxl/player/DPermission.java index df6c3104..69032287 100644 --- a/core/src/main/java/de/erethon/dungeonsxl/player/DPermission.java +++ b/core/src/main/java/de/erethon/dungeonsxl/player/DPermission.java @@ -16,7 +16,6 @@ */ package de.erethon.dungeonsxl.player; -import de.erethon.commons.misc.EnumUtil; import java.util.ArrayList; import java.util.Arrays; import java.util.List; @@ -99,32 +98,33 @@ public enum DPermission { public static final String PREFIX = "dxl."; - private String node; - private PermissionDefault isDefault; + private Permission node; private List children = new ArrayList<>(); DPermission(String node, PermissionDefault isDefault) { - this.node = node; - this.isDefault = isDefault; + this.node = new Permission(PREFIX + node, isDefault); } DPermission(String node, PermissionDefault isDefault, DPermission... children) { this(node, isDefault); this.children = Arrays.asList(children); + for (DPermission child : children) { + child.node.addParent(node, true); + } } /** * @return the permission node String */ public String getNode() { - return PREFIX + node; + return node.getName(); } /** * @return if a player has the node by default */ public PermissionDefault isDefault() { - return isDefault; + return node.getDefault(); } /** @@ -161,48 +161,7 @@ public enum DPermission { * @return if the player has the permission */ public static boolean hasPermission(CommandSender sender, DPermission permission) { - if (sender.hasPermission(permission.getNode())) { - return true; - } - - for (DPermission parent : DPermission.values()) { - if (parent.getChildren().contains(permission) && sender.hasPermission(parent.getNode())) { - return true; - } - } - - return false; - } - - /** - * @param sender the CommandSender - * @param permission the permission to check - * @return if the player has the permission - */ - public static boolean hasPermission(CommandSender sender, String permission) { - if (sender.hasPermission(permission)) { - return true; - } - - DPermission dPermission = null; - if (EnumUtil.isValidEnum(DPermission.class, permission)) { - dPermission = DPermission.valueOf(permission); - - } else if (DPermission.getByNode(permission) != null) { - dPermission = DPermission.getByNode(permission); - } - - if (dPermission == null) { - return false; - } - - for (DPermission parent : DPermission.values()) { - if (parent.getChildren().contains(dPermission) && sender.hasPermission(parent.getNode())) { - return true; - } - } - - return false; + return sender.hasPermission(permission.getNode()); } /** @@ -210,7 +169,7 @@ public enum DPermission { */ public static void register() { for (DPermission permission : values()) { - Bukkit.getPluginManager().addPermission(new Permission(permission.getNode(), permission.isDefault())); + Bukkit.getPluginManager().addPermission(permission.node); } } @@ -219,7 +178,7 @@ public enum DPermission { */ public static void unregister() { for (DPermission permission : values()) { - Bukkit.getPluginManager().removePermission(permission.getNode()); + Bukkit.getPluginManager().removePermission(permission.node); } } diff --git a/core/src/main/java/de/erethon/dungeonsxl/requirement/PermissionRequirement.java b/core/src/main/java/de/erethon/dungeonsxl/requirement/PermissionRequirement.java index acf3273d..bfeb4d86 100644 --- a/core/src/main/java/de/erethon/dungeonsxl/requirement/PermissionRequirement.java +++ b/core/src/main/java/de/erethon/dungeonsxl/requirement/PermissionRequirement.java @@ -19,7 +19,6 @@ package de.erethon.dungeonsxl.requirement; import de.erethon.dungeonsxl.api.DungeonsAPI; import de.erethon.dungeonsxl.api.Requirement; import de.erethon.dungeonsxl.config.DMessage; -import de.erethon.dungeonsxl.player.DPermission; import java.util.ArrayList; import java.util.List; import net.md_5.bungee.api.ChatColor; @@ -62,7 +61,7 @@ public class PermissionRequirement implements Requirement { @Override public boolean check(Player player) { for (String permission : permissions) { - if (!DPermission.hasPermission(player, permission)) { + if (!player.hasPermission(permission)) { return false; } } diff --git a/core/src/main/java/de/erethon/dungeonsxl/sign/DSignListener.java b/core/src/main/java/de/erethon/dungeonsxl/sign/DSignListener.java index f3524e08..a16ee507 100644 --- a/core/src/main/java/de/erethon/dungeonsxl/sign/DSignListener.java +++ b/core/src/main/java/de/erethon/dungeonsxl/sign/DSignListener.java @@ -24,7 +24,6 @@ import de.erethon.dungeonsxl.api.player.GamePlayer; import de.erethon.dungeonsxl.api.sign.DungeonSign; import de.erethon.dungeonsxl.api.world.EditWorld; import de.erethon.dungeonsxl.config.DMessage; -import de.erethon.dungeonsxl.player.DPermission; import de.erethon.dungeonsxl.player.DPlayerListener; import de.erethon.dungeonsxl.trigger.InteractTrigger; import de.erethon.dungeonsxl.world.DGameWorld; @@ -119,7 +118,7 @@ public class DSignListener implements Listener { return; } - if (!DPermission.hasPermission(player, dsign.getBuildPermission())) { + if (!player.hasPermission(dsign.getBuildPermission())) { MessageUtil.sendMessage(player, DMessage.ERROR_NO_PERMISSIONS.getMessage()); return; }