Allow arguments in javascript placeholders.

This commit is contained in:
extendedclip 2018-03-06 11:38:25 -05:00
parent 07fb6f911b
commit b496a3244e
3 changed files with 34 additions and 5 deletions

View File

@ -89,8 +89,15 @@ public class JavascriptExpansion extends PlaceholderExpansion implements Cacheab
for (JavascriptPlaceholder script : scripts) {
if (script.getIdentifier().equalsIgnoreCase(identifier)) {
if (identifier.startsWith(script.getIdentifier() + "_")) {
identifier = identifier.replace(script.getIdentifier() + "_", "");
if (identifier.indexOf(",") == -1) {
return script.evaluate(engine, p, identifier);
} else {
return script.evaluate(engine, p, identifier.split(","));
}
} else if (identifier.equalsIgnoreCase(script.getIdentifier())) {
return script.evaluate(engine, p);
}
}
@ -121,7 +128,7 @@ public class JavascriptExpansion extends PlaceholderExpansion implements Cacheab
@Override
public String getVersion() {
return "1.2.0";
return "1.3.0";
}
public boolean addJavascriptPlaceholder(JavascriptPlaceholder p) {

View File

@ -38,6 +38,7 @@ public class JavascriptPlaceholder {
private String falseResult;
private JavascriptReturnType type;
public JavascriptPlaceholder(String identifier, JavascriptReturnType type, String expression, String trueResult, String falseResult) {
@ -93,12 +94,33 @@ public class JavascriptPlaceholder {
return type;
}
public String evaluate(ScriptEngine engine, Player p) {
public String evaluate(ScriptEngine engine, Player p, String... args) {
String exp = PlaceholderAPI.setPlaceholders(p, expression);
try {
String[] c = null;
if (args != null && args.length > 0) {
for (int i = 0 ; i < args.length ; i++) {
if (args[i] == null || args[i].isEmpty()) {
continue;
}
String s = PlaceholderAPI.setBracketPlaceholders(p, args[i]);
if (c == null) {
c = new String[args.length];
}
c[i] = s;
}
}
if (c == null) {
c = new String[]{};
}
engine.put("args", c);
engine.put("BukkitPlayer", p);
Object result = engine.eval(exp);

View File

@ -292,7 +292,7 @@ public class JavascriptPlaceholdersConfig {
String line = scanner.nextLine();
if (line == null || line.isEmpty()) {
if (line == null || line.isEmpty() || line.startsWith("//")) {
continue;
}