Permission improvement

This commit is contained in:
Felix Cravic 2020-12-11 21:42:32 +01:00
parent 76c3ea52ac
commit 90102e1562
3 changed files with 20 additions and 4 deletions

View File

@ -9,6 +9,10 @@ import java.util.Objects;
/** /**
* Representation of a permission granted to a {@link CommandSender}. * 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.
* <p>
* The class is immutable.
*/ */
public class Permission { public class Permission {

View File

@ -37,6 +37,15 @@ public interface PermissionHandler {
getAllPermissions().remove(permission); 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}. * Gets if this handler has the permission {@code permission}.
* <p> * <p>
@ -87,9 +96,7 @@ public interface PermissionHandler {
if (permission != null) { if (permission != null) {
// Verify using the permission verifier // Verify using the permission verifier
return permissionVerifier != null ? return permissionVerifier == null || permissionVerifier.isValid(permission.getNBTData());
permissionVerifier.isValid(permission.getNBTData()) :
true;
} }
return false; return false;
} }

View File

@ -65,7 +65,12 @@ public class TestPermissions {
player.addPermission(permission1); player.addPermission(permission1);
assertTrue(player.hasPermission("perm.name")); assertTrue(player.hasPermission("perm.name"));
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); player.addPermission(permission2);
assertFalse(player.hasPermission("perm.name2", nbtCompound -> nbtCompound != null)); assertFalse(player.hasPermission("perm.name2", nbtCompound -> nbtCompound != null));