From 147173c9b67a260d44cef2b37ffb865fb7f397ed Mon Sep 17 00:00:00 2001 From: Tanguygab Date: Sun, 17 Nov 2024 01:11:01 +0100 Subject: [PATCH 1/4] Added has permissions requirement type --- .../deluxemenus/config/DeluxeMenusConfig.java | 13 +++++++ .../HasPermissionsRequirement.java | 34 +++++++++++++++++++ .../requirement/RequirementType.java | 6 ++++ 3 files changed, 53 insertions(+) create mode 100644 src/main/java/com/extendedclip/deluxemenus/requirement/HasPermissionsRequirement.java diff --git a/src/main/java/com/extendedclip/deluxemenus/config/DeluxeMenusConfig.java b/src/main/java/com/extendedclip/deluxemenus/config/DeluxeMenusConfig.java index d7e24d7..1f92588 100644 --- a/src/main/java/com/extendedclip/deluxemenus/config/DeluxeMenusConfig.java +++ b/src/main/java/com/extendedclip/deluxemenus/config/DeluxeMenusConfig.java @@ -1142,6 +1142,19 @@ private RequirementList getRequirements(FileConfiguration c, String path) { ); } break; + case HAS_PERMISSIONS: + case DOES_NOT_HAVE_PERMISSIONS: + if (c.contains(rPath + ".permissions")) { + invert = type == RequirementType.DOES_NOT_HAVE_PERMISSIONS; + req = new HasPermissionsRequirement(c.getStringList(rPath + ".permissions"), c.getInt(rPath + ".minimum", -1), invert); + } else { + DeluxeMenus.debug( + DebugLevel.HIGHEST, + Level.WARNING, + "Has Permission requirement at path: " + rPath + " does not contain a permission: entry" + ); + } + break; case JAVASCRIPT: if (c.contains(rPath + ".expression")) { req = new JavascriptRequirement(c.getString(rPath + ".expression")); diff --git a/src/main/java/com/extendedclip/deluxemenus/requirement/HasPermissionsRequirement.java b/src/main/java/com/extendedclip/deluxemenus/requirement/HasPermissionsRequirement.java new file mode 100644 index 0000000..6e0465d --- /dev/null +++ b/src/main/java/com/extendedclip/deluxemenus/requirement/HasPermissionsRequirement.java @@ -0,0 +1,34 @@ +package com.extendedclip.deluxemenus.requirement; + +import com.extendedclip.deluxemenus.menu.MenuHolder; + +import java.util.List; + +public class HasPermissionsRequirement extends Requirement { + + private final List permissions; + private final int minimum; + private final boolean invert; + + public HasPermissionsRequirement(List permissions, int minimum, boolean invert) { + this.permissions = permissions; + this.minimum = minimum; + this.invert = invert; + } + + @Override + public boolean evaluate(MenuHolder holder) { + int amount = 0; + for (String permission : permissions) { + String check = holder.setPlaceholdersAndArguments(permission); + if (holder.getViewer().hasPermission(check)) { + ++amount; + continue; + } + if (minimum == -1) return invert; + } + if (invert) return permissions.size()-amount >= minimum; + return amount >= minimum; + } + +} diff --git a/src/main/java/com/extendedclip/deluxemenus/requirement/RequirementType.java b/src/main/java/com/extendedclip/deluxemenus/requirement/RequirementType.java index aeb15ae..d1787a6 100644 --- a/src/main/java/com/extendedclip/deluxemenus/requirement/RequirementType.java +++ b/src/main/java/com/extendedclip/deluxemenus/requirement/RequirementType.java @@ -41,6 +41,12 @@ public enum RequirementType { Arrays.asList("!has permission", "!has perm", "!haspermission", "!hasperm", "!perm"), "Checks if a player does not have a specific permission", Collections.singletonList("permission")), + HAS_PERMISSIONS(Arrays.asList("has permissions", "has perms", "haspermissions", "hasperms", "perms"), + "Checks if a player has a set amount of permissions", Collections.singletonList("permissions")), + DOES_NOT_HAVE_PERMISSIONS( + Arrays.asList("!has permissions", "!has perms", "!haspermissions", "!hasperms", "!perms"), + "Checks if a player does not have a set amount of permission", + Collections.singletonList("permissions")), STRING_CONTAINS(Arrays.asList("string contains", "stringcontains", "contains"), "Checks if a string contains another string", Arrays.asList("input", "output")), STRING_DOES_NOT_CONTAIN(Arrays.asList("!string contains", "!stringcontains", "!contains"), From 3ae385c7edea7bd5c86d5cece11d708c0ef75d5f Mon Sep 17 00:00:00 2001 From: Tanguygab Date: Sun, 17 Nov 2024 15:42:57 +0100 Subject: [PATCH 2/4] Check for Has Permissions requirement minimum being lower than 1 --- .../deluxemenus/config/DeluxeMenusConfig.java | 13 +++++++++++-- 1 file changed, 11 insertions(+), 2 deletions(-) diff --git a/src/main/java/com/extendedclip/deluxemenus/config/DeluxeMenusConfig.java b/src/main/java/com/extendedclip/deluxemenus/config/DeluxeMenusConfig.java index 1f92588..97ead7c 100644 --- a/src/main/java/com/extendedclip/deluxemenus/config/DeluxeMenusConfig.java +++ b/src/main/java/com/extendedclip/deluxemenus/config/DeluxeMenusConfig.java @@ -1146,12 +1146,21 @@ private RequirementList getRequirements(FileConfiguration c, String path) { case DOES_NOT_HAVE_PERMISSIONS: if (c.contains(rPath + ".permissions")) { invert = type == RequirementType.DOES_NOT_HAVE_PERMISSIONS; - req = new HasPermissionsRequirement(c.getStringList(rPath + ".permissions"), c.getInt(rPath + ".minimum", -1), invert); + int minimum = -1; + if (c.contains(rPath + ".minimum") && (minimum = c.getInt(rPath + ".minimum")) < 1) { + DeluxeMenus.debug( + DebugLevel.HIGHEST, + Level.WARNING, + "Has Permissions requirement at path: " + rPath + " has a minimum lower than 1. All permissions will be checked" + ); + minimum = -1; + } + req = new HasPermissionsRequirement(c.getStringList(rPath + ".permissions"), minimum, invert); } else { DeluxeMenus.debug( DebugLevel.HIGHEST, Level.WARNING, - "Has Permission requirement at path: " + rPath + " does not contain a permission: entry" + "Has Permissions requirement at path: " + rPath + " does not contain permissions: entry" ); } break; From bbbe525eec51c0094f7b3e15b9274eead2f36aa2 Mon Sep 17 00:00:00 2001 From: Tanguygab Date: Sun, 17 Nov 2024 23:40:48 +0100 Subject: [PATCH 3/4] Fix HasPermissionsRequirement logic for invert & add more warns --- .../deluxemenus/config/DeluxeMenusConfig.java | 18 ++++++++++++++++- .../HasPermissionsRequirement.java | 20 +++++++++---------- 2 files changed, 26 insertions(+), 12 deletions(-) diff --git a/src/main/java/com/extendedclip/deluxemenus/config/DeluxeMenusConfig.java b/src/main/java/com/extendedclip/deluxemenus/config/DeluxeMenusConfig.java index 97ead7c..8ef1b78 100644 --- a/src/main/java/com/extendedclip/deluxemenus/config/DeluxeMenusConfig.java +++ b/src/main/java/com/extendedclip/deluxemenus/config/DeluxeMenusConfig.java @@ -1155,7 +1155,23 @@ private RequirementList getRequirements(FileConfiguration c, String path) { ); minimum = -1; } - req = new HasPermissionsRequirement(c.getStringList(rPath + ".permissions"), minimum, invert); + List permissions = c.getStringList(rPath + ".permissions"); + if (permissions.isEmpty()) { + DeluxeMenus.debug( + DebugLevel.HIGHEST, + Level.WARNING, + "Has Permissions requirement at path: " + rPath + " has no permissions to check. Ignoring..." + ); + break; + } else if (minimum > permissions.size()) { + DeluxeMenus.debug( + DebugLevel.HIGHEST, + Level.WARNING, + "Has Permissions requirement at path: " + rPath + " has a minimum higher than the amount of permissions. Using "+permissions.size()+" instead" + ); + minimum = permissions.size(); + } + req = new HasPermissionsRequirement(permissions, minimum, invert); } else { DeluxeMenus.debug( DebugLevel.HIGHEST, diff --git a/src/main/java/com/extendedclip/deluxemenus/requirement/HasPermissionsRequirement.java b/src/main/java/com/extendedclip/deluxemenus/requirement/HasPermissionsRequirement.java index 6e0465d..05fa074 100644 --- a/src/main/java/com/extendedclip/deluxemenus/requirement/HasPermissionsRequirement.java +++ b/src/main/java/com/extendedclip/deluxemenus/requirement/HasPermissionsRequirement.java @@ -18,17 +18,15 @@ public HasPermissionsRequirement(List permissions, int minimum, boolean @Override public boolean evaluate(MenuHolder holder) { - int amount = 0; - for (String permission : permissions) { - String check = holder.setPlaceholdersAndArguments(permission); - if (holder.getViewer().hasPermission(check)) { - ++amount; - continue; - } - if (minimum == -1) return invert; - } - if (invert) return permissions.size()-amount >= minimum; - return amount >= minimum; + final int count = permissions.stream() + .map(holder::setPlaceholdersAndArguments) + .map(holder.getViewer()::hasPermission) + .mapToInt(hasPermission -> hasPermission ? 1 : 0) + .sum(); + return invert + ? count + minimum <= permissions.size() + : count >= minimum; } + } From e222803d5fda95c161ebc8581b3b5a52c34d409f Mon Sep 17 00:00:00 2001 From: Tanguygab Date: Sun, 17 Nov 2024 23:42:02 +0100 Subject: [PATCH 4/4] Added "minimum" in requirement options --- .../extendedclip/deluxemenus/requirement/RequirementType.java | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/main/java/com/extendedclip/deluxemenus/requirement/RequirementType.java b/src/main/java/com/extendedclip/deluxemenus/requirement/RequirementType.java index d1787a6..ef39bcc 100644 --- a/src/main/java/com/extendedclip/deluxemenus/requirement/RequirementType.java +++ b/src/main/java/com/extendedclip/deluxemenus/requirement/RequirementType.java @@ -46,7 +46,7 @@ public enum RequirementType { DOES_NOT_HAVE_PERMISSIONS( Arrays.asList("!has permissions", "!has perms", "!haspermissions", "!hasperms", "!perms"), "Checks if a player does not have a set amount of permission", - Collections.singletonList("permissions")), + Arrays.asList("permissions", "minimum")), STRING_CONTAINS(Arrays.asList("string contains", "stringcontains", "contains"), "Checks if a string contains another string", Arrays.asList("input", "output")), STRING_DOES_NOT_CONTAIN(Arrays.asList("!string contains", "!stringcontains", "!contains"),