diff --git a/pom.xml b/pom.xml index 42e8ca0..6167e7a 100644 --- a/pom.xml +++ b/pom.xml @@ -2,7 +2,7 @@ 4.0.0 com.extendedclip.papi.expansion.javascript javascript-expansion - 1.4.0-dev-2 + 1.4.0 PAPI-Expansion-Javascript PlaceholderAPI expansion for javascript placeholders diff --git a/src/main/java/com/extendedclip/papi/expansion/javascript/JavascriptExpansion.java b/src/main/java/com/extendedclip/papi/expansion/javascript/JavascriptExpansion.java index cff7002..179e640 100644 --- a/src/main/java/com/extendedclip/papi/expansion/javascript/JavascriptExpansion.java +++ b/src/main/java/com/extendedclip/papi/expansion/javascript/JavascriptExpansion.java @@ -28,86 +28,22 @@ import java.util.Set; import javax.script.ScriptEngine; import javax.script.ScriptEngineManager; -import me.clip.placeholderapi.PlaceholderAPI; import me.clip.placeholderapi.PlaceholderAPIPlugin; import me.clip.placeholderapi.expansion.Cacheable; import me.clip.placeholderapi.expansion.Configurable; import me.clip.placeholderapi.expansion.PlaceholderExpansion; -import org.bukkit.Bukkit; import org.bukkit.entity.Player; public class JavascriptExpansion extends PlaceholderExpansion implements Cacheable, Configurable { - private ScriptEngine engine = null; - private String engineType = "javascript"; + private ScriptEngine globalEngine = null; + private JavascriptPlaceholdersConfig config; + private final Set scripts = new HashSet(); + private final String VERSION = getClass().getPackage().getImplementationVersion(); - - @Override - public boolean register() { - engineType = getString("engine", "javascript"); - - if (engine == null) { - try { - engine = new ScriptEngineManager().getEngineByName(engineType); - } catch (NullPointerException ex) { - PlaceholderAPIPlugin.getInstance().getLogger().warning("Javascript engine type was invalid! Defaulting to 'javascript'"); - engine = new ScriptEngineManager().getEngineByName("javascript"); - } - - engine.put("BukkitServer", Bukkit.getServer()); - } - - config = new JavascriptPlaceholdersConfig(this); - config.loadPlaceholders(); - 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 ""; - } - - if (scripts.isEmpty() || engine == null) { - return null; - } - - 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; - } - - @Override - public boolean canRegister() { - return true; - } @Override public String getAuthor() { @@ -124,12 +60,65 @@ public class JavascriptExpansion extends PlaceholderExpansion implements Cacheab return null; } - @Override public String getVersion() { return VERSION; } + @Override + public boolean canRegister() { + return true; + } + + @Override + public boolean register() { + if (globalEngine == null) { + try { + globalEngine = new ScriptEngineManager().getEngineByName(getString("engine", "javascript")); + } catch (NullPointerException ex) { + PlaceholderAPIPlugin.getInstance().getLogger().warning("Javascript engine type was invalid! Defaulting to 'javascript'"); + globalEngine = new ScriptEngineManager().getEngineByName("javascript"); + } + } + + config = new JavascriptPlaceholdersConfig(this); + config.loadPlaceholders(); + return super.register(); + } + + @Override + public void clear() { + if (!scripts.isEmpty()) { + scripts.stream().forEach(s -> { + s.saveData(); + s.cleanup(); + }); + } + scripts.clear(); + globalEngine = null; + } + + @Override + public String onPlaceholderRequest(Player p, String identifier) { + if (p == null) { + return ""; + } + + if (scripts.isEmpty()) { + return null; + } + + for (JavascriptPlaceholder script : scripts) { + if (identifier.startsWith(script.getIdentifier() + "_")) { + identifier = identifier.replace(script.getIdentifier() + "_", ""); + return identifier.indexOf(",") == -1 ? script.evaluate(p, identifier) : script.evaluate(p, identifier.split(",")); + } else if (identifier.equalsIgnoreCase(script.getIdentifier())) { + return script.evaluate(p); + } + } + return null; + } + public boolean addJavascriptPlaceholder(JavascriptPlaceholder p) { if (p == null) { return false; @@ -152,6 +141,10 @@ public class JavascriptExpansion extends PlaceholderExpansion implements Cacheab public int getJavascriptPlaceholdersAmount() { return scripts == null ? 0 : scripts.size(); } + + public ScriptEngine getGlobalEngine() { + return globalEngine; + } @Override public Map getDefaults() { diff --git a/src/main/java/com/extendedclip/papi/expansion/javascript/JavascriptPlaceholder.java b/src/main/java/com/extendedclip/papi/expansion/javascript/JavascriptPlaceholder.java index 25b4c87..74d1670 100644 --- a/src/main/java/com/extendedclip/papi/expansion/javascript/JavascriptPlaceholder.java +++ b/src/main/java/com/extendedclip/papi/expansion/javascript/JavascriptPlaceholder.java @@ -27,6 +27,8 @@ import java.util.Set; import javax.script.ScriptEngine; import javax.script.ScriptException; +import org.apache.commons.lang.Validate; +import org.bukkit.Bukkit; import org.bukkit.configuration.InvalidConfigurationException; import org.bukkit.configuration.file.FileConfiguration; import org.bukkit.configuration.file.YamlConfiguration; @@ -36,18 +38,14 @@ import me.clip.placeholderapi.PlaceholderAPI; import me.clip.placeholderapi.PlaceholderAPIPlugin; public class JavascriptPlaceholder { + + private ScriptEngine engine = null; private String identifier; - private String expression; + private String script; - private String trueResult; - - private String falseResult; - - private JavascriptReturnType type; - - private PlaceholderData data = null; + private ScriptData data = null; private File dataFile; @@ -55,32 +53,13 @@ public class JavascriptPlaceholder { 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'!"); - } - - this.type = type; - - if (identifier == null) { - throw new IllegalArgumentException("Javascript placeholder identifier must not be null!"); - } else if (expression == null) { - throw new IllegalArgumentException("Javascript placeholder expression must not be null!"); - } - + public JavascriptPlaceholder(ScriptEngine engine, String identifier, String script) { + Validate.notNull(engine, "ScriptEngine can not be null"); + Validate.notNull(identifier, "Identifier can not be null"); + Validate.notNull(script, "script can not be null"); + this.engine = engine; 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; - } - + this.script = script; File dir = new File(FILEDIR); try { @@ -94,6 +73,65 @@ public class JavascriptPlaceholder { dataFile = new File(FILEDIR, identifier + "_data.yml"); } + public String getIdentifier() { + return identifier; + } + + public String getScript() { + return script; + } + + public String evaluate(Player p, String... args) { + String exp = PlaceholderAPI.setPlaceholders(p, script); + + 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("Data", getData()); + engine.put("BukkitServer", Bukkit.getServer()); + engine.put("BukkitPlayer", p); + Object result = engine.eval(exp); + return result != null ? PlaceholderAPI.setBracketPlaceholders(p, result.toString()) : ""; + + } catch (ScriptException ex) { + PlaceholderAPIPlugin.getInstance().getLogger().severe("Error in javascript for placeholder - " + this.identifier); + ex.printStackTrace(); + } + return "invalid javascript"; + } + + public ScriptData getData() { + if (data == null) { + data = new ScriptData(); + } + return data; + } + + public void setData(ScriptData data) { + this.data = data; + } + public boolean loadData() { cfg = new YamlConfiguration(); @@ -116,7 +154,7 @@ public class JavascriptPlaceholder { return false; } - PlaceholderData data = new PlaceholderData(); + ScriptData data = new ScriptData(); keys.stream().forEach(k -> { data.set(k, cfg.get(k)); @@ -150,98 +188,6 @@ public class JavascriptPlaceholder { } } - public String getIdentifier() { - return identifier; - } - - public String getExpression() { - return expression; - } - - public String getTrueResult() { - return trueResult; - } - - public String getFalseResult() { - return falseResult; - } - - public JavascriptReturnType getType() { - return type; - } - - 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("Data", getData()); - - engine.put("BukkitPlayer", p); - - Object result = engine.eval(exp); - - if (type == JavascriptReturnType.BOOLEAN) { - - if (!(result instanceof Boolean)) { - return "invalid javascript"; - } - - if ((boolean) result) { - return PlaceholderAPI.setPlaceholders(p, trueResult); - } else { - return PlaceholderAPI.setPlaceholders(p, falseResult); - } - } - - if (result instanceof String) { - String res = PlaceholderAPI.setBracketPlaceholders(p, (String)result); - return res != null ? res : ""; - } - - return result != null ? PlaceholderAPI.setBracketPlaceholders(p, result.toString()) : ""; - - } catch (ScriptException ex) { - PlaceholderAPIPlugin.getInstance().getLogger().severe("Error in javascript format for placeholder - " + this.identifier); - ex.printStackTrace(); - } - 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(); diff --git a/src/main/java/com/extendedclip/papi/expansion/javascript/JavascriptPlaceholdersConfig.java b/src/main/java/com/extendedclip/papi/expansion/javascript/JavascriptPlaceholdersConfig.java index 292585d..212d40f 100644 --- a/src/main/java/com/extendedclip/papi/expansion/javascript/JavascriptPlaceholdersConfig.java +++ b/src/main/java/com/extendedclip/papi/expansion/javascript/JavascriptPlaceholdersConfig.java @@ -26,6 +26,9 @@ import java.io.IOException; import java.util.Scanner; import java.util.logging.Level; +import javax.script.ScriptEngine; +import javax.script.ScriptEngineManager; + import me.clip.placeholderapi.PlaceholderAPIPlugin; import org.bukkit.configuration.file.FileConfiguration; @@ -48,121 +51,46 @@ public class JavascriptPlaceholdersConfig { } public void reload() { - if (file == null) { file = new File(plugin.getDataFolder(), "javascript_placeholders.yml"); } config = YamlConfiguration.loadConfiguration(file); - - config.options().header("javascript_placeholders.yml" - + "\nYou can create custom placeholders which utilize javascript to determine the result of the custom placeholder you create." - + "\nYou can specify if the result is based on a boolean or the actual javascript." + config.options().header("Javascript Expansion: " + ex.getVersion() + + "\nThis is the main configuration file for the Javascript Expansion." + "\n" - + "\nIf you do not specify a type: the placeholder will default to a boolean type" - + "\nA boolean type must contain a true_result: and false_result:" + + "\nYou will define your javascript placeholders in this file." + "\n" - + "\nA string type only requires the expression: entry" + + "\nJavascript files must be located in the:" + + "\n /plugins/placeholderapi/javascripts/ folder" + "\n" - + "\nJavascript placeholders can contain normal placeholders in the expression, true_result, or false_result" - + "\nThese placeholders will be parsed to the correct values before the expression is evaluated." + + "\nA detailed guide on how to create your own javascript placeholders" + + "\ncan be found here:" + + "\nhttps://github.com/PlaceholderAPI-Expansions/Javascript-Expansion/wiki" + "\n" + "\nYour javascript placeholders will be identified by: %javascript_%" + "\n" - + "\nJavascript placeholder format:" + + "\nConfiguration format:" + "\n" - + "\n BOOLEAN TYPE" + "\n:" - + "\n expression: " - + "\n type: 'boolean'" - + "\n true_result: " - + "\n false_result: " - + "\n" - + "\n STRING TYPE" - + "\n:" - + "\n expression: " - + "\n type: 'string'" + + "\n file: ." + + "\n engine: (name of script engine)" + "\n" + "\n" - + "\n ==== ADVANCED VARIABLES ====" - + "\nDO NOT USE THESE VARIABLES UNLESS YOU KNOW WHAT YOU ARE DOING!" + + "\nExample:" + "\n" - + "\nYou can access a few Bukkit API classes and methods using certain keywords:" - + "\n" - + "\nUsing \"BukkitServer\" in your javascript will return Bukkit.getServer()" - + "\nYou can use any methods inside of the Server class:" - + "\n" - + "\nExample: BukkitServer.getBannedPlayers().size().toFixed()" - + "\nWill return how many players are banned" - + "\n" - + "\nThis variable is handy if you want to iterate through all online players.'" - + "\n" - + "\nUsing \"BukkitPlayer\" in your javascript will return the Player object you are setting placeholders for." - + "\nYou can use any methods inside of the Player class:" - + "\n" - + "\nExample: BukkitPlayer.hasPermission(\"some.permission\")" - + "\nWill return if the player has a specific permission" - + "\nThis variable is handy if you want to check a players permission node, or access other methods inside of" - + "\nthe player class for the specified player." - + "\n" - + "\nMore advanced variables are coming soon! Only use these variables if you know what you are doing!" - + "\n" - + "\n ==================" - + "\n" - + "\n" - + "\nJavascript placeholder examples:" - + "\n" - + "\nmillionaire:" - + "\n expression: '%vaulteco_balance% >= 1000000'" - + "\n type: 'boolean'" - + "\n true_result: '&aMillionaire'" - + "\n false_result: '&cbroke'" - + "\nis_staff:" - + "\n expression: '\"%vault_group%\" == \"Moderator\" || \"%vault_group%\" == \"Admin\" || \"%vault_group%\" == \"Owner\"'" - + "\n type: 'boolean'" - + "\n true_result: '&bStaff'" - + "\n false_result: '&ePlayer'" - + "\nhealth_rounded:" - + "\n expression: 'Math.round(%player_health%)'" - + "\n type: 'string'" - + "\nstaff_online:" - + "\n expression: 'var i = 0; for (var p in BukkitServer.getOnlinePlayers()) { if (BukkitServer.getOnlinePlayers()[p].hasPermission(\"staff.online\")) {i = i+1;};} i.toFixed();'" - + "\n type: 'string'" - + "\n" - + "\n" - + "\nYou can optionally specify a file that the javascript expression will be loaded from if your expression" - + "\nis bigger than 1 line. To specify javascript be loaded from a file, follow this format:" - + "\n" - + "\nis_op:" - + "\n expression: 'file: is_op.js'" - + "\n type: 'string'" - + "\n" - + "\nThe following placeholder will attempt to load javascript from the /plugins/PlaceholderAPI/javascripts/is_op.js file" - + "\nif the folder/file exists. If the folder/file does not exist it will be created." - + "\nYou must specify the file extension with the file name. Any file extension is accepted." - + "\n"); + + "\n'my_placeholder':" + + "\n file: 'my_placeholder.js'" + + "\n engine: 'nashorn'"); - if (config.getKeys(false) == null || config.getKeys(false).isEmpty()) { - config.set("millionaire.expression", "%vaulteco_balance% >= 1000000"); - config.set("millionaire.type", "boolean"); - config.set("millionaire.true_result", "&aMillionaire"); - config.set("millionaire.false_result", "&cbroke"); - config.set("is_staff.expression", "\"%vault_group%\" == \"Moderator\" || \"%vault_group%\" == \"Admin\" || \"%vault_group%\" == \"Owner\""); - config.set("is_staff.type", "boolean"); - config.set("is_staff.true_result", "&bStaff"); - config.set("is_staff.false_result", "&ePlayer"); - config.set("health_rounded.expression", "Math.round(%player_health%)"); - config.set("health_rounded.type", "string"); - config.set("staff_online", "var i = 0; for (var p in BukkitServer.getOnlinePlayers()) { if (BukkitServer.getOnlinePlayers()[p].hasPermission(\"staff.online\")) {i = i+1;};} i.toFixed();"); - config.set("staff_online.type", "string"); + config.set("example.file", "example.js"); + config.set("example.engine", "nashorn"); } - save(); } public FileConfiguration load() { - if (config == null) { reload(); } @@ -170,7 +98,6 @@ public class JavascriptPlaceholdersConfig { } public void save() { - if ((config == null) || (file == null)) { return; } @@ -183,7 +110,6 @@ public class JavascriptPlaceholdersConfig { } public int loadPlaceholders() { - if (config == null || config.getKeys(false) == null || config.getKeys(false).isEmpty()) { return 0; } @@ -201,98 +127,74 @@ public class JavascriptPlaceholdersConfig { } for (String identifier : config.getKeys(false)) { - - JavascriptReturnType type = JavascriptReturnType.BOOLEAN; - - if (config.contains(identifier + ".type")) { - - String t = config.getString(identifier + ".type"); - - if (JavascriptReturnType.getType(t) != null) { - type = JavascriptReturnType.getType(t); - } - } - - if (!isValid(identifier, type)) { - plugin.getLogger().warning("Javascript " + type.getType() + " placeholder " + identifier + " is invalid!"); + if (!config.contains(identifier + ".file") || config.getString(identifier + ".file", null) == null) { + plugin.getLogger().warning("Javascript placeholder: " + identifier + " does not have a file specified"); continue; } - JavascriptPlaceholder pl = null; + File scriptFile = new File(plugin.getDataFolder() + File.separator + "javascripts", config.getString(identifier + ".file")); - String expression = config.getString(identifier + ".expression"); + if (!scriptFile.exists()) { + plugin.getLogger().info(scriptFile.getName() + " does not exist. Creating file..."); - if (expression.startsWith("file: ")) { - - expression = expression.replace("file: ", ""); - - File f = new File(plugin.getDataFolder() + File.separator + "javascripts", expression); - - expression = loadFileExpression(f); - - if (expression == null || expression.isEmpty()) { - plugin.getLogger().info("javascript expression from file: " + f.getName() + " is empty!"); - continue; - } else { - - plugin.getLogger().info("javascript expression loaded from file: " + f.getName()); + try { + scriptFile.createNewFile(); + plugin.getLogger().info(scriptFile.getName() + " created! Add your javascript to this file and use /placeholderapi reload to load it!"); + } catch (IOException e) { + e.printStackTrace(); } + continue; } - if (type == JavascriptReturnType.BOOLEAN) { - - String trueResult = config.getString(identifier + ".true_result"); - - String falseResult = config.getString(identifier + ".false_result"); - - pl = new JavascriptPlaceholder(identifier, type, expression, trueResult, falseResult); + String script = getContents(scriptFile); + + if (script == null || script.isEmpty()) { + plugin.getLogger().warning("File: " + scriptFile.getName() + " for javascript placeholder: " + identifier + " is empty"); + continue; + } + + ScriptEngine engine = null; + + if (!config.contains(identifier + ".engine")) { + engine = ex.getGlobalEngine(); } else { - - pl = new JavascriptPlaceholder(identifier, type, expression, null, null); + try { + engine = new ScriptEngineManager().getEngineByName(config.getString(identifier + ".engine", "nashorn")); + } catch (NullPointerException e) { + plugin.getLogger().warning("ScriptEngine type for javascript placeholder: " + identifier + " is invalid! Defaulting to global"); + engine = ex.getGlobalEngine(); + } } + if (engine == null) { + plugin.getLogger().warning("Failed to set ScriptEngine for javascript placeholder: " + identifier); + continue; + } + + JavascriptPlaceholder pl = new JavascriptPlaceholder(engine, identifier, script); + boolean added = ex.addJavascriptPlaceholder(pl); 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 + "%"); + plugin.getLogger().info("Loaded data for javascript placeholder: " + identifier); } + plugin.getLogger().info("%javascript_" + identifier + "% has been loaded!"); } else { - plugin.getLogger().warning("Javascript " + type.getType() + " placeholder %javascript_" + identifier + "% is a duplicate!"); + plugin.getLogger().warning("Javascript placeholder %javascript_" + identifier + "% is a duplicate!"); } } return ex.getJavascriptPlaceholdersAmount(); } - private String loadFileExpression(File f) { - + private String getContents(File f) { StringBuilder sb = new StringBuilder(); try { - - if (!f.exists()) { - - plugin.getLogger().warning(f.getName() + " does not exist!"); - - try { - - f.createNewFile(); - - plugin.getLogger().info(f.getName() + " created! Add your javascript expression to this file and use /placeholderapi reload to load it!"); - - } catch(IOException e) { - - e.printStackTrace(); - } - - return null; - } - Scanner scanner = new Scanner(f); while (scanner.hasNextLine()) { - String line = scanner.nextLine(); if (line == null || line.isEmpty()) { @@ -300,7 +202,7 @@ public class JavascriptPlaceholdersConfig { } line = line.trim(); - + /* temp fix for single line comments * doesnt solve every case though.. * lines that start with code and may have a comment afterward still screw stuff up... @@ -308,29 +210,13 @@ public class JavascriptPlaceholdersConfig { if (line.startsWith("//")) { continue; } - - sb.append(line + " "); } - scanner.close(); - } catch (FileNotFoundException e) { - e.printStackTrace(); return null; } - return sb.toString(); } - - private boolean isValid(String identifier, JavascriptReturnType type) { - if (type == JavascriptReturnType.BOOLEAN) { - return config.isString(identifier + ".expression") - && config.isString(identifier + ".true_result") - && config.isString(identifier + ".false_result"); - } else { - return config.isString(identifier + ".expression"); - } - } } diff --git a/src/main/java/com/extendedclip/papi/expansion/javascript/PlaceholderData.java b/src/main/java/com/extendedclip/papi/expansion/javascript/PlaceholderData.java deleted file mode 100644 index bbe047d..0000000 --- a/src/main/java/com/extendedclip/papi/expansion/javascript/PlaceholderData.java +++ /dev/null @@ -1,44 +0,0 @@ -package com.extendedclip.papi.expansion.javascript; - -import java.util.HashMap; -import java.util.Map; - -public class PlaceholderData { - - private Map map = new HashMap<>(); - - public PlaceholderData(Map data) { - this.map = data; - } - - public PlaceholderData() { - } - - public Map getData() { - return map; - } - - public void clear() { - map.clear(); - } - - public boolean exists(String key) { - return map.containsKey(key) && map.get(key) != null; - } - - public Object get(String key) { - return map.get(key); - } - - public void remove(String key) { - map.put(key, null); - } - - public void set(String key, Object value) { - map.put(key, value); - } - - public boolean isEmpty() { - return map.isEmpty(); - } -} diff --git a/src/main/java/com/extendedclip/papi/expansion/javascript/JavascriptReturnType.java b/src/main/java/com/extendedclip/papi/expansion/javascript/ScriptData.java similarity index 52% rename from src/main/java/com/extendedclip/papi/expansion/javascript/JavascriptReturnType.java rename to src/main/java/com/extendedclip/papi/expansion/javascript/ScriptData.java index a5e81b9..761edea 100644 --- a/src/main/java/com/extendedclip/papi/expansion/javascript/JavascriptReturnType.java +++ b/src/main/java/com/extendedclip/papi/expansion/javascript/ScriptData.java @@ -20,27 +20,46 @@ */ package com.extendedclip.papi.expansion.javascript; -public enum JavascriptReturnType { +import java.util.HashMap; +import java.util.Map; - BOOLEAN("boolean"), STRING("string"); +public class ScriptData { - private String type; + private Map map; - JavascriptReturnType(String type) { - this.type = type; + public ScriptData(Map data) { + this.map = data; } - public String getType() { - return this.type; + public ScriptData() { + this.map = new HashMap<>(); } - public static JavascriptReturnType getType(String type){ - for(JavascriptReturnType e : values()){ - if (e.getType().equalsIgnoreCase(type)) { - return e; - } - } - return null; + public Map getData() { + return map; } + public void clear() { + map.clear(); + } + + public boolean exists(String key) { + return map.containsKey(key) && map.get(key) != null; + } + + public Object get(String key) { + return map.get(key); + } + + public void remove(String key) { + map.put(key, null); + } + + public void set(String key, Object value) { + map.put(key, value); + } + + public boolean isEmpty() { + return map.isEmpty(); + } }