-
Notifications
You must be signed in to change notification settings - Fork 49
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #148 from Tanguygab/has-permissions-requirement
- Loading branch information
Showing
3 changed files
with
76 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
32 changes: 32 additions & 0 deletions
32
src/main/java/com/extendedclip/deluxemenus/requirement/HasPermissionsRequirement.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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; | ||
} | ||
|
||
|
||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters