Use Bukkit permission system

This commit is contained in:
Daniel Saukel 2020-10-24 15:36:05 +02:00
parent 32a6651ecf
commit ffd787f2b0
4 changed files with 13 additions and 56 deletions

View File

@ -253,7 +253,7 @@ public class DGlobalPlayer implements GlobalPlayer {
@Override @Override
public boolean hasPermission(String permission) { public boolean hasPermission(String permission) {
return DPermission.hasPermission(player, permission); return player.hasPermission(permission);
} }
public boolean hasPermission(DPermission permission) { public boolean hasPermission(DPermission permission) {

View File

@ -16,7 +16,6 @@
*/ */
package de.erethon.dungeonsxl.player; package de.erethon.dungeonsxl.player;
import de.erethon.commons.misc.EnumUtil;
import java.util.ArrayList; import java.util.ArrayList;
import java.util.Arrays; import java.util.Arrays;
import java.util.List; import java.util.List;
@ -99,32 +98,33 @@ public enum DPermission {
public static final String PREFIX = "dxl."; public static final String PREFIX = "dxl.";
private String node; private Permission node;
private PermissionDefault isDefault;
private List<DPermission> children = new ArrayList<>(); private List<DPermission> children = new ArrayList<>();
DPermission(String node, PermissionDefault isDefault) { DPermission(String node, PermissionDefault isDefault) {
this.node = node; this.node = new Permission(PREFIX + node, isDefault);
this.isDefault = isDefault;
} }
DPermission(String node, PermissionDefault isDefault, DPermission... children) { DPermission(String node, PermissionDefault isDefault, DPermission... children) {
this(node, isDefault); this(node, isDefault);
this.children = Arrays.asList(children); this.children = Arrays.asList(children);
for (DPermission child : children) {
child.node.addParent(node, true);
}
} }
/** /**
* @return the permission node String * @return the permission node String
*/ */
public String getNode() { public String getNode() {
return PREFIX + node; return node.getName();
} }
/** /**
* @return if a player has the node by default * @return if a player has the node by default
*/ */
public PermissionDefault isDefault() { public PermissionDefault isDefault() {
return isDefault; return node.getDefault();
} }
/** /**
@ -161,48 +161,7 @@ public enum DPermission {
* @return if the player has the permission * @return if the player has the permission
*/ */
public static boolean hasPermission(CommandSender sender, DPermission permission) { public static boolean hasPermission(CommandSender sender, DPermission permission) {
if (sender.hasPermission(permission.getNode())) { return 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;
} }
/** /**
@ -210,7 +169,7 @@ public enum DPermission {
*/ */
public static void register() { public static void register() {
for (DPermission permission : values()) { 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() { public static void unregister() {
for (DPermission permission : values()) { for (DPermission permission : values()) {
Bukkit.getPluginManager().removePermission(permission.getNode()); Bukkit.getPluginManager().removePermission(permission.node);
} }
} }

View File

@ -19,7 +19,6 @@ package de.erethon.dungeonsxl.requirement;
import de.erethon.dungeonsxl.api.DungeonsAPI; import de.erethon.dungeonsxl.api.DungeonsAPI;
import de.erethon.dungeonsxl.api.Requirement; import de.erethon.dungeonsxl.api.Requirement;
import de.erethon.dungeonsxl.config.DMessage; import de.erethon.dungeonsxl.config.DMessage;
import de.erethon.dungeonsxl.player.DPermission;
import java.util.ArrayList; import java.util.ArrayList;
import java.util.List; import java.util.List;
import net.md_5.bungee.api.ChatColor; import net.md_5.bungee.api.ChatColor;
@ -62,7 +61,7 @@ public class PermissionRequirement implements Requirement {
@Override @Override
public boolean check(Player player) { public boolean check(Player player) {
for (String permission : permissions) { for (String permission : permissions) {
if (!DPermission.hasPermission(player, permission)) { if (!player.hasPermission(permission)) {
return false; return false;
} }
} }

View File

@ -24,7 +24,6 @@ import de.erethon.dungeonsxl.api.player.GamePlayer;
import de.erethon.dungeonsxl.api.sign.DungeonSign; import de.erethon.dungeonsxl.api.sign.DungeonSign;
import de.erethon.dungeonsxl.api.world.EditWorld; import de.erethon.dungeonsxl.api.world.EditWorld;
import de.erethon.dungeonsxl.config.DMessage; import de.erethon.dungeonsxl.config.DMessage;
import de.erethon.dungeonsxl.player.DPermission;
import de.erethon.dungeonsxl.player.DPlayerListener; import de.erethon.dungeonsxl.player.DPlayerListener;
import de.erethon.dungeonsxl.trigger.InteractTrigger; import de.erethon.dungeonsxl.trigger.InteractTrigger;
import de.erethon.dungeonsxl.world.DGameWorld; import de.erethon.dungeonsxl.world.DGameWorld;
@ -119,7 +118,7 @@ public class DSignListener implements Listener {
return; return;
} }
if (!DPermission.hasPermission(player, dsign.getBuildPermission())) { if (!player.hasPermission(dsign.getBuildPermission())) {
MessageUtil.sendMessage(player, DMessage.ERROR_NO_PERMISSIONS.getMessage()); MessageUtil.sendMessage(player, DMessage.ERROR_NO_PERMISSIONS.getMessage());
return; return;
} }