Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Added has permissions requirement type #148

Merged
merged 4 commits into from
Nov 21, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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