mirror of
https://github.com/PlaceholderAPI/Javascript-Expansion.git
synced 2025-05-23 18:42:44 +00:00
Persistent data accessible within the javascript placeholder
This commit is contained in:
parent
9c0193109c
commit
4a803e2883
2
pom.xml
2
pom.xml
@ -2,7 +2,7 @@
|
|||||||
<modelVersion>4.0.0</modelVersion>
|
<modelVersion>4.0.0</modelVersion>
|
||||||
<groupId>com.extendedclip.papi.expansion.javascript</groupId>
|
<groupId>com.extendedclip.papi.expansion.javascript</groupId>
|
||||||
<artifactId>javascript-expansion</artifactId>
|
<artifactId>javascript-expansion</artifactId>
|
||||||
<version>1.3.1-dev-${BUILD_NUMBER}</version>
|
<version>1.4.0-dev-1</version>
|
||||||
<name>PAPI-Expansion-Javascript</name>
|
<name>PAPI-Expansion-Javascript</name>
|
||||||
<description>PlaceholderAPI expansion for javascript placeholders</description>
|
<description>PlaceholderAPI expansion for javascript placeholders</description>
|
||||||
|
|
||||||
|
@ -40,19 +40,13 @@ import org.bukkit.entity.Player;
|
|||||||
public class JavascriptExpansion extends PlaceholderExpansion implements Cacheable, Configurable {
|
public class JavascriptExpansion extends PlaceholderExpansion implements Cacheable, Configurable {
|
||||||
|
|
||||||
private ScriptEngine engine = null;
|
private ScriptEngine engine = null;
|
||||||
|
|
||||||
private String engineType = "javascript";
|
private String engineType = "javascript";
|
||||||
|
|
||||||
private JavascriptPlaceholdersConfig config;
|
private JavascriptPlaceholdersConfig config;
|
||||||
|
|
||||||
private final Set<JavascriptPlaceholder> scripts = new HashSet<JavascriptPlaceholder>();
|
private final Set<JavascriptPlaceholder> scripts = new HashSet<JavascriptPlaceholder>();
|
||||||
|
|
||||||
private final String VERSION = getClass().getPackage().getImplementationVersion();
|
private final String VERSION = getClass().getPackage().getImplementationVersion();
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
public boolean register() {
|
public boolean register() {
|
||||||
|
|
||||||
|
|
||||||
engineType = getString("engine", "javascript");
|
engineType = getString("engine", "javascript");
|
||||||
|
|
||||||
if (engine == null) {
|
if (engine == null) {
|
||||||
@ -67,21 +61,24 @@ public class JavascriptExpansion extends PlaceholderExpansion implements Cacheab
|
|||||||
}
|
}
|
||||||
|
|
||||||
config = new JavascriptPlaceholdersConfig(this);
|
config = new JavascriptPlaceholdersConfig(this);
|
||||||
|
|
||||||
config.loadPlaceholders();
|
config.loadPlaceholders();
|
||||||
|
return super.register();
|
||||||
return PlaceholderAPI.registerPlaceholderHook(getIdentifier(), this);
|
|
||||||
}
|
}
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
public void clear() {
|
public void clear() {
|
||||||
|
if (!scripts.isEmpty()) {
|
||||||
|
scripts.stream().forEach(s -> {
|
||||||
|
s.saveData();
|
||||||
|
s.cleanup();
|
||||||
|
});
|
||||||
|
}
|
||||||
scripts.clear();
|
scripts.clear();
|
||||||
engine = null;
|
engine = null;
|
||||||
}
|
}
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
public String onPlaceholderRequest(Player p, String identifier) {
|
public String onPlaceholderRequest(Player p, String identifier) {
|
||||||
|
|
||||||
if (p == null) {
|
if (p == null) {
|
||||||
return "";
|
return "";
|
||||||
}
|
}
|
||||||
@ -91,20 +88,19 @@ public class JavascriptExpansion extends PlaceholderExpansion implements Cacheab
|
|||||||
}
|
}
|
||||||
|
|
||||||
for (JavascriptPlaceholder script : scripts) {
|
for (JavascriptPlaceholder script : scripts) {
|
||||||
|
|
||||||
if (identifier.startsWith(script.getIdentifier() + "_")) {
|
if (identifier.startsWith(script.getIdentifier() + "_")) {
|
||||||
|
|
||||||
identifier = identifier.replace(script.getIdentifier() + "_", "");
|
identifier = identifier.replace(script.getIdentifier() + "_", "");
|
||||||
|
|
||||||
if (identifier.indexOf(",") == -1) {
|
if (identifier.indexOf(",") == -1) {
|
||||||
return script.evaluate(engine, p, identifier);
|
return script.evaluate(engine, p, identifier);
|
||||||
} else {
|
} else {
|
||||||
return script.evaluate(engine, p, identifier.split(","));
|
return script.evaluate(engine, p, identifier.split(","));
|
||||||
}
|
}
|
||||||
|
|
||||||
} else if (identifier.equalsIgnoreCase(script.getIdentifier())) {
|
} else if (identifier.equalsIgnoreCase(script.getIdentifier())) {
|
||||||
return script.evaluate(engine, p);
|
return script.evaluate(engine, p);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
return null;
|
return null;
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -135,7 +131,6 @@ public class JavascriptExpansion extends PlaceholderExpansion implements Cacheab
|
|||||||
}
|
}
|
||||||
|
|
||||||
public boolean addJavascriptPlaceholder(JavascriptPlaceholder p) {
|
public boolean addJavascriptPlaceholder(JavascriptPlaceholder p) {
|
||||||
|
|
||||||
if (p == null) {
|
if (p == null) {
|
||||||
return false;
|
return false;
|
||||||
}
|
}
|
||||||
@ -144,7 +139,6 @@ public class JavascriptExpansion extends PlaceholderExpansion implements Cacheab
|
|||||||
scripts.add(p);
|
scripts.add(p);
|
||||||
return true;
|
return true;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
for (JavascriptPlaceholder pl : scripts) {
|
for (JavascriptPlaceholder pl : scripts) {
|
||||||
if (pl.getIdentifier().equalsIgnoreCase(p.getIdentifier())) {
|
if (pl.getIdentifier().equalsIgnoreCase(p.getIdentifier())) {
|
||||||
|
@ -20,9 +20,17 @@
|
|||||||
*/
|
*/
|
||||||
package com.extendedclip.papi.expansion.javascript;
|
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.ScriptEngine;
|
||||||
import javax.script.ScriptException;
|
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 org.bukkit.entity.Player;
|
||||||
|
|
||||||
import me.clip.placeholderapi.PlaceholderAPI;
|
import me.clip.placeholderapi.PlaceholderAPI;
|
||||||
@ -40,8 +48,15 @@ public class JavascriptPlaceholder {
|
|||||||
|
|
||||||
private JavascriptReturnType type;
|
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) {
|
public JavascriptPlaceholder(String identifier, JavascriptReturnType type, String expression, String trueResult, String falseResult) {
|
||||||
|
|
||||||
if (type == null) {
|
if (type == null) {
|
||||||
throw new IllegalArgumentException("Javascript placeholder type must set as 'boolean' or 'string'!");
|
throw new IllegalArgumentException("Javascript placeholder type must set as 'boolean' or 'string'!");
|
||||||
}
|
}
|
||||||
@ -53,23 +68,97 @@ public class JavascriptPlaceholder {
|
|||||||
} else if (expression == null) {
|
} else if (expression == null) {
|
||||||
throw new IllegalArgumentException("Javascript placeholder expression must not be null!");
|
throw new IllegalArgumentException("Javascript placeholder expression must not be null!");
|
||||||
}
|
}
|
||||||
|
|
||||||
this.identifier = identifier;
|
|
||||||
|
|
||||||
|
this.identifier = identifier;
|
||||||
this.expression = expression;
|
this.expression = expression;
|
||||||
|
|
||||||
if (type == JavascriptReturnType.BOOLEAN) {
|
if (type == JavascriptReturnType.BOOLEAN) {
|
||||||
|
|
||||||
if (trueResult == null) {
|
if (trueResult == null) {
|
||||||
throw new IllegalArgumentException("Javascript boolean placeholder must contain a true_result!");
|
throw new IllegalArgumentException("Javascript boolean placeholder must contain a true_result!");
|
||||||
} else if (falseResult == null) {
|
} else if (falseResult == null) {
|
||||||
throw new IllegalArgumentException("Javascript boolean placeholder must contain a false_result!");
|
throw new IllegalArgumentException("Javascript boolean placeholder must contain a false_result!");
|
||||||
}
|
}
|
||||||
|
|
||||||
this.trueResult = trueResult;
|
this.trueResult = trueResult;
|
||||||
|
|
||||||
this.falseResult = falseResult;
|
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);
|
String exp = PlaceholderAPI.setPlaceholders(p, expression);
|
||||||
|
|
||||||
try {
|
try {
|
||||||
|
|
||||||
String[] c = null;
|
String[] c = null;
|
||||||
|
|
||||||
if (args != null && args.length > 0) {
|
if (args != null && args.length > 0) {
|
||||||
|
|
||||||
for (int i = 0 ; i < args.length ; i++) {
|
for (int i = 0 ; i < args.length ; i++) {
|
||||||
if (args[i] == null || args[i].isEmpty()) {
|
if (args[i] == null || args[i].isEmpty()) {
|
||||||
continue;
|
continue;
|
||||||
}
|
}
|
||||||
|
|
||||||
String s = PlaceholderAPI.setBracketPlaceholders(p, args[i]);
|
String s = PlaceholderAPI.setBracketPlaceholders(p, args[i]);
|
||||||
|
|
||||||
if (c == null) {
|
if (c == null) {
|
||||||
c = new String[args.length];
|
c = new String[args.length];
|
||||||
}
|
}
|
||||||
|
|
||||||
c[i] = s;
|
c[i] = s;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
@ -120,6 +210,8 @@ public class JavascriptPlaceholder {
|
|||||||
|
|
||||||
engine.put("args", c);
|
engine.put("args", c);
|
||||||
|
|
||||||
|
engine.put("Data", getData());
|
||||||
|
|
||||||
engine.put("BukkitPlayer", p);
|
engine.put("BukkitPlayer", p);
|
||||||
|
|
||||||
Object result = engine.eval(exp);
|
Object result = engine.eval(exp);
|
||||||
@ -149,4 +241,23 @@ public class JavascriptPlaceholder {
|
|||||||
}
|
}
|
||||||
return "invalid javascript";
|
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;
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
@ -255,6 +255,9 @@ public class JavascriptPlaceholdersConfig {
|
|||||||
|
|
||||||
if (added) {
|
if (added) {
|
||||||
plugin.getLogger().info("Javascript " + type.getType() + " placeholder %javascript_" + identifier + "% has been loaded!");
|
plugin.getLogger().info("Javascript " + type.getType() + " placeholder %javascript_" + identifier + "% has been loaded!");
|
||||||
|
if (pl.loadData()) {
|
||||||
|
plugin.getLogger().info("Loaded data for: %javascript_" + identifier + "%");
|
||||||
|
}
|
||||||
} else {
|
} else {
|
||||||
plugin.getLogger().warning("Javascript " + type.getType() + " placeholder %javascript_" + identifier + "% is a duplicate!");
|
plugin.getLogger().warning("Javascript " + type.getType() + " placeholder %javascript_" + identifier + "% is a duplicate!");
|
||||||
}
|
}
|
||||||
|
@ -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();
|
||||||
|
}
|
||||||
|
}
|
Loading…
x
Reference in New Issue
Block a user