From 90102e1562710561f1dd4a2372794e9511ccd9f5 Mon Sep 17 00:00:00 2001 From: Felix Cravic Date: Fri, 11 Dec 2020 21:42:32 +0100 Subject: [PATCH] Permission improvement --- .../net/minestom/server/permission/Permission.java | 4 ++++ .../server/permission/PermissionHandler.java | 13 ++++++++++--- src/test/java/permissions/TestPermissions.java | 7 ++++++- 3 files changed, 20 insertions(+), 4 deletions(-) diff --git a/src/main/java/net/minestom/server/permission/Permission.java b/src/main/java/net/minestom/server/permission/Permission.java index 6b4d2825c..1959e2569 100644 --- a/src/main/java/net/minestom/server/permission/Permission.java +++ b/src/main/java/net/minestom/server/permission/Permission.java @@ -9,6 +9,10 @@ import java.util.Objects; /** * Representation of a permission granted to a {@link CommandSender}. + * Each permission has a string representation used as an identifier, and an optional + * {@link NBTCompound} used to store additional data. + *

+ * The class is immutable. */ public class Permission { diff --git a/src/main/java/net/minestom/server/permission/PermissionHandler.java b/src/main/java/net/minestom/server/permission/PermissionHandler.java index 186f82203..08ccb9a55 100644 --- a/src/main/java/net/minestom/server/permission/PermissionHandler.java +++ b/src/main/java/net/minestom/server/permission/PermissionHandler.java @@ -37,6 +37,15 @@ public interface PermissionHandler { getAllPermissions().remove(permission); } + /** + * Removes a {@link Permission} based on its string identifier. + * + * @param permissionName the permission name + */ + default void removePermission(@NotNull String permissionName) { + getAllPermissions().removeIf(permission -> permission.getPermissionName().equals(permissionName)); + } + /** * Gets if this handler has the permission {@code permission}. *

@@ -87,9 +96,7 @@ public interface PermissionHandler { if (permission != null) { // Verify using the permission verifier - return permissionVerifier != null ? - permissionVerifier.isValid(permission.getNBTData()) : - true; + return permissionVerifier == null || permissionVerifier.isValid(permission.getNBTData()); } return false; } diff --git a/src/test/java/permissions/TestPermissions.java b/src/test/java/permissions/TestPermissions.java index 8afffee35..eebcf215e 100644 --- a/src/test/java/permissions/TestPermissions.java +++ b/src/test/java/permissions/TestPermissions.java @@ -65,7 +65,12 @@ public class TestPermissions { player.addPermission(permission1); assertTrue(player.hasPermission("perm.name")); assertTrue(player.hasPermission("perm.name", - nbtCompound -> nbtCompound != null && nbtCompound.getString("name").equals("Minestom"))); + nbtCompound -> { + if (nbtCompound != null && nbtCompound.containsKey("name")) { + return nbtCompound.getString("name").equals("Minestom"); + } + return false; + })); player.addPermission(permission2); assertFalse(player.hasPermission("perm.name2", nbtCompound -> nbtCompound != null));