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

boolean requirement #156

Open
wants to merge 1 commit into
base: main
Choose a base branch
from
Open
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 @@ -1194,6 +1194,19 @@ private RequirementList getRequirements(FileConfiguration c, String path) {
);
}
break;
case BOOLEAN:
case REVERSE_BOOLEAN:
if (c.contains(rPath + ".input")) {
invert = type == RequirementType.REVERSE_BOOLEAN;
req = new BooleanRequirement(c.getString(rPath + ".input"), invert);
} else {
DeluxeMenus.debug(
DebugLevel.HIGHEST,
Level.WARNING,
"Boolean requirement at path: " + rPath + " does not contain a input: 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,29 @@
package com.extendedclip.deluxemenus.requirement;

import com.extendedclip.deluxemenus.menu.MenuHolder;

public class BooleanRequirement extends Requirement {

private final String bool;
private final boolean invert;

public BooleanRequirement(String bool, boolean invert) {
this.bool = bool;
this.invert = invert;
}

@Override
public boolean evaluate(MenuHolder holder) {
String check = holder.setPlaceholdersAndArguments(bool);
try {
boolean bool = Boolean.parseBoolean(check);
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I am not 100% sure if we want to use this method of parsing a boolean. It might cause confusion since if the value is anything other than "true" (ignoring case), it is considered to be "false". Maybe, instead, we might want to change the requirement name to "is true" or something similar to that?

if (invert) {
return !bool;
} else {
return bool;
}
} catch (Exception e) {
return false;
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -53,6 +53,10 @@ public enum RequirementType {
"Checks if a string does not contain another string", Arrays.asList("input", "output")),
STRING_EQUALS(Arrays.asList("string equals", "stringequals", "equals"),
"Checks if a string equals another string", Arrays.asList("input", "output")),
BOOLEAN(Arrays.asList("boolean", "bool"),
"Checks if a player has a set amount of permissions", Collections.singletonList("input")),
REVERSE_BOOLEAN(Arrays.asList("!boolean", "!bool"),
"Checks if a player has a set amount of permissions", Collections.singletonList("input")),
STRING_DOES_NOT_EQUAL(Arrays.asList("!string equals", "!stringequals", "!equals"),
"Checks if a string does not equal another string", Arrays.asList("input", "output")),
STRING_EQUALS_IGNORECASE(
Expand Down