Persistent data accessible within the javascript placeholder

This commit is contained in:
extendedclip 2018-03-08 14:05:41 -05:00
parent 9c0193109c
commit 4a803e2883
5 changed files with 177 additions and 25 deletions

View File

@ -2,7 +2,7 @@
<modelVersion>4.0.0</modelVersion>
<groupId>com.extendedclip.papi.expansion.javascript</groupId>
<artifactId>javascript-expansion</artifactId>
<version>1.3.1-dev-${BUILD_NUMBER}</version>
<version>1.4.0-dev-1</version>
<name>PAPI-Expansion-Javascript</name>
<description>PlaceholderAPI expansion for javascript placeholders</description>

View File

@ -40,19 +40,13 @@ import org.bukkit.entity.Player;
public class JavascriptExpansion extends PlaceholderExpansion implements Cacheable, Configurable {
private ScriptEngine engine = null;
private String engineType = "javascript";
private JavascriptPlaceholdersConfig config;
private final Set<JavascriptPlaceholder> scripts = new HashSet<JavascriptPlaceholder>();
private final String VERSION = getClass().getPackage().getImplementationVersion();
@Override
public boolean register() {
engineType = getString("engine", "javascript");
if (engine == null) {
@ -67,21 +61,24 @@ public class JavascriptExpansion extends PlaceholderExpansion implements Cacheab
}
config = new JavascriptPlaceholdersConfig(this);
config.loadPlaceholders();
return PlaceholderAPI.registerPlaceholderHook(getIdentifier(), this);
return super.register();
}
@Override
public void clear() {
if (!scripts.isEmpty()) {
scripts.stream().forEach(s -> {
s.saveData();
s.cleanup();
});
}
scripts.clear();
engine = null;
}
@Override
public String onPlaceholderRequest(Player p, String identifier) {
if (p == null) {
return "";
}
@ -91,20 +88,19 @@ public class JavascriptExpansion extends PlaceholderExpansion implements Cacheab
}
for (JavascriptPlaceholder script : scripts) {
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);
}
}
return null;
}
@ -135,7 +131,6 @@ public class JavascriptExpansion extends PlaceholderExpansion implements Cacheab
}
public boolean addJavascriptPlaceholder(JavascriptPlaceholder p) {
if (p == null) {
return false;
}
@ -144,7 +139,6 @@ public class JavascriptExpansion extends PlaceholderExpansion implements Cacheab
scripts.add(p);
return true;
}
for (JavascriptPlaceholder pl : scripts) {
if (pl.getIdentifier().equalsIgnoreCase(p.getIdentifier())) {

View File

@ -20,9 +20,17 @@
*/
package com.extendedclip.papi.expansion.javascript;
import java.io.File;
import java.io.IOException;
import java.util.Map.Entry;
import java.util.Set;
import javax.script.ScriptEngine;
import javax.script.ScriptException;
import org.bukkit.configuration.InvalidConfigurationException;
import org.bukkit.configuration.file.FileConfiguration;
import org.bukkit.configuration.file.YamlConfiguration;
import org.bukkit.entity.Player;
import me.clip.placeholderapi.PlaceholderAPI;
@ -40,8 +48,15 @@ public class JavascriptPlaceholder {
private JavascriptReturnType type;
private PlaceholderData data = null;
private File dataFile;
private FileConfiguration cfg;
private final String FILEDIR = PlaceholderAPIPlugin.getInstance().getDataFolder() + File.separator + "expansions" + File.separator + "javascript_data";
public JavascriptPlaceholder(String identifier, JavascriptReturnType type, String expression, String trueResult, String falseResult) {
if (type == null) {
throw new IllegalArgumentException("Javascript placeholder type must set as 'boolean' or 'string'!");
}
@ -53,23 +68,97 @@ public class JavascriptPlaceholder {
} else if (expression == null) {
throw new IllegalArgumentException("Javascript placeholder expression must not be null!");
}
this.identifier = identifier;
this.identifier = identifier;
this.expression = expression;
if (type == JavascriptReturnType.BOOLEAN) {
if (trueResult == null) {
throw new IllegalArgumentException("Javascript boolean placeholder must contain a true_result!");
} else if (falseResult == null) {
throw new IllegalArgumentException("Javascript boolean placeholder must contain a false_result!");
}
this.trueResult = trueResult;
this.falseResult = falseResult;
}
File dir = new File(FILEDIR);
try {
if (!dir.exists()) {
dir.mkdirs();
}
} catch (SecurityException e) {
e.printStackTrace();
}
dataFile = new File(FILEDIR, identifier + "_data.yml");
}
public boolean loadData() {
cfg = new YamlConfiguration();
if (!dataFile.exists()) {
return false;
}
try {
cfg.load(dataFile);
} catch (IOException e) {
e.printStackTrace();
return false;
} catch (InvalidConfigurationException e) {
e.printStackTrace();
return false;
}
final Set<String> keys = cfg.getKeys(true);
if (keys == null || keys.isEmpty()) {
return false;
}
boolean save = false;
PlaceholderData data = new PlaceholderData();
for (String k : keys) {
data.set(k, cfg.get(k));
cfg.set(k, null);
save = true;
}
if (!data.isEmpty()) {
this.setData(data);
}
if (save) {
try {
cfg.save(dataFile);
} catch (IOException e) {
e.printStackTrace();
}
}
return save;
}
public boolean saveData() {
if (data == null || data.isEmpty()) {
return false;
}
if (cfg == null) {
return false;
}
for (Entry<String, Object> d : data.getData().entrySet()) {
cfg.set(d.getKey(), d.getValue());
}
try {
cfg.save(dataFile);
return true;
} catch (IOException e) {
return false;
}
}
@ -97,19 +186,20 @@ public class JavascriptPlaceholder {
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;
}
}
@ -120,6 +210,8 @@ public class JavascriptPlaceholder {
engine.put("args", c);
engine.put("Data", getData());
engine.put("BukkitPlayer", p);
Object result = engine.eval(exp);
@ -149,4 +241,23 @@ public class JavascriptPlaceholder {
}
return "invalid javascript";
}
public PlaceholderData getData() {
if (data == null) {
data = new PlaceholderData();
}
return data;
}
public void setData(PlaceholderData data) {
this.data = data;
}
public void cleanup() {
if (this.data != null) {
this.data.clear();
this.data = null;
}
this.cfg = null;
}
}

View File

@ -255,6 +255,9 @@ public class JavascriptPlaceholdersConfig {
if (added) {
plugin.getLogger().info("Javascript " + type.getType() + " placeholder %javascript_" + identifier + "% has been loaded!");
if (pl.loadData()) {
plugin.getLogger().info("Loaded data for: %javascript_" + identifier + "%");
}
} else {
plugin.getLogger().warning("Javascript " + type.getType() + " placeholder %javascript_" + identifier + "% is a duplicate!");
}

View File

@ -0,0 +1,44 @@
package com.extendedclip.papi.expansion.javascript;
import java.util.HashMap;
import java.util.Map;
public class PlaceholderData {
private Map<String, Object> map = new HashMap<>();
public PlaceholderData(Map<String, Object> data) {
this.map = data;
}
public PlaceholderData() {
}
public Map<String, Object> getData() {
return map;
}
public void clear() {
map.clear();
}
public boolean exists(String key) {
return map.containsKey(key);
}
public Object get(String key) {
return map.get(key);
}
public boolean remove(String key) {
return map.remove(key) != null;
}
public void set(String key, Object value) {
map.put(key, value);
}
public boolean isEmpty() {
return map.isEmpty();
}
}