Skip to content

Commit

Permalink
Merge pull request #148 from Tanguygab/has-permissions-requirement
Browse files Browse the repository at this point in the history
  • Loading branch information
BlitzOffline authored Nov 21, 2024
2 parents b8cefce + e222803 commit 8027cbe
Show file tree
Hide file tree
Showing 3 changed files with 76 additions and 0 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -1142,6 +1142,44 @@ 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;
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;
}
List<String> 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,
Level.WARNING,
"Has Permissions requirement at path: " + rPath + " does not contain permissions: entry"
);
}
break;
case JAVASCRIPT:
if (c.contains(rPath + ".expression")) {
req = new JavascriptRequirement(c.getString(rPath + ".expression"));
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
package com.extendedclip.deluxemenus.requirement;

import com.extendedclip.deluxemenus.menu.MenuHolder;

import java.util.List;

public class HasPermissionsRequirement extends Requirement {

private final List<String> permissions;
private final int minimum;
private final boolean invert;

public HasPermissionsRequirement(List<String> permissions, int minimum, boolean invert) {
this.permissions = permissions;
this.minimum = minimum;
this.invert = invert;
}

@Override
public boolean evaluate(MenuHolder holder) {
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;
}


}
Original file line number Diff line number Diff line change
Expand Up @@ -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",
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"),
Expand Down

0 comments on commit 8027cbe

Please sign in to comment.