mirror of
https://github.com/PlaceholderAPI/Javascript-Expansion.git
synced 2025-05-23 10:39:04 +00:00
Code cleanup and PlaceholderAPI script variable added (use PlaceholderAPI.static to access it)
This commit is contained in:
parent
69a514f62d
commit
be00944461
@ -21,26 +21,18 @@
|
||||
package com.extendedclip.papi.expansion.javascript;
|
||||
|
||||
import com.extendedclip.papi.expansion.javascript.cloud.GithubScriptManager;
|
||||
import java.lang.reflect.Field;
|
||||
import java.util.ArrayList;
|
||||
import java.util.HashMap;
|
||||
import java.util.HashSet;
|
||||
import java.util.List;
|
||||
import java.util.Map;
|
||||
import java.util.Set;
|
||||
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.OfflinePlayer;
|
||||
import org.bukkit.command.CommandMap;
|
||||
|
||||
import javax.script.ScriptEngine;
|
||||
import javax.script.ScriptEngineFactory;
|
||||
import javax.script.ScriptEngineManager;
|
||||
|
||||
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.OfflinePlayer;
|
||||
import org.bukkit.command.Command;
|
||||
import org.bukkit.command.CommandMap;
|
||||
import java.lang.reflect.Field;
|
||||
import java.util.*;
|
||||
|
||||
public class JavascriptExpansion extends PlaceholderExpansion implements Cacheable, Configurable {
|
||||
|
||||
@ -97,27 +89,32 @@ public class JavascriptExpansion extends PlaceholderExpansion implements Cacheab
|
||||
globalEngine = new ScriptEngineManager().getEngineByName("nashorn");
|
||||
}
|
||||
}
|
||||
|
||||
debug = (boolean) get("debug", false);
|
||||
config = new JavascriptPlaceholdersConfig(this);
|
||||
config.loadPlaceholders();
|
||||
|
||||
if (debug) {
|
||||
System.out.println("Java version: " + System.getProperty("java.version"));
|
||||
|
||||
ScriptEngineManager manager = new ScriptEngineManager();
|
||||
List<ScriptEngineFactory> factories = manager.getEngineFactories();
|
||||
System.out.println("displaying all script engine factories:");
|
||||
final ScriptEngineManager manager = new ScriptEngineManager();
|
||||
final List<ScriptEngineFactory> factories = manager.getEngineFactories();
|
||||
System.out.println("Displaying all script engine factories.");
|
||||
|
||||
for (ScriptEngineFactory factory : factories) {
|
||||
System.out.println("Engine name: " + factory.getEngineName());
|
||||
System.out.println("version: " + factory.getEngineVersion());
|
||||
System.out.println("lang name: " + factory.getLanguageName());
|
||||
System.out.println("lang version: " + factory.getLanguageVersion());
|
||||
System.out.println("extensions: " + factory.getExtensions());
|
||||
System.out.println("mime types: " + factory.getMimeTypes());
|
||||
System.out.println("names: " + factory.getNames());
|
||||
System.out.println(factory.getEngineName());
|
||||
System.out.println(" Version: " + factory.getEngineVersion());
|
||||
System.out.println(" Lang name: " + factory.getLanguageName());
|
||||
System.out.println(" Lang version: " + factory.getLanguageVersion());
|
||||
System.out.println(" Extensions: ." + String.join(", .", factory.getExtensions()));
|
||||
System.out.println(" Mime types: " + String.join(", ", factory.getMimeTypes()));
|
||||
System.out.println(" Names: " + String.join(", ", factory.getNames()));
|
||||
}
|
||||
}
|
||||
if ((Boolean) get("github_script_downloads", false)) {
|
||||
|
||||
githubScripts = new GithubScriptManager(this);
|
||||
|
||||
if ((Boolean) get("github_script_downloads", false)) {
|
||||
githubScripts.fetch();
|
||||
}
|
||||
|
||||
@ -132,17 +129,19 @@ public class JavascriptExpansion extends PlaceholderExpansion implements Cacheab
|
||||
s.saveData();
|
||||
s.cleanup();
|
||||
});
|
||||
|
||||
if (githubScripts != null) {
|
||||
githubScripts.clear();
|
||||
githubScripts = null;
|
||||
}
|
||||
|
||||
scripts.clear();
|
||||
globalEngine = null;
|
||||
instance = null;
|
||||
}
|
||||
|
||||
@Override
|
||||
public String onRequest( OfflinePlayer p, String identifier) {
|
||||
public String onRequest(OfflinePlayer p, String identifier) {
|
||||
if (p == null) {
|
||||
return "";
|
||||
}
|
||||
@ -186,9 +185,7 @@ public class JavascriptExpansion extends PlaceholderExpansion implements Cacheab
|
||||
|
||||
public List<String> getLoadedIdentifiers() {
|
||||
List<String> l = new ArrayList<>();
|
||||
scripts.forEach(s -> {
|
||||
l.add(s.getIdentifier());
|
||||
});
|
||||
scripts.forEach(s -> l.add(s.getIdentifier()));
|
||||
return l;
|
||||
}
|
||||
|
||||
@ -210,7 +207,7 @@ public class JavascriptExpansion extends PlaceholderExpansion implements Cacheab
|
||||
|
||||
@Override
|
||||
public Map<String, Object> getDefaults() {
|
||||
Map<String, Object> def = new HashMap<String, Object>();
|
||||
Map<String, Object> def = new HashMap<>();
|
||||
def.put("engine", "javascript");
|
||||
def.put("debug", false);
|
||||
def.put("github_script_downloads", false);
|
||||
|
@ -22,132 +22,167 @@ package com.extendedclip.papi.expansion.javascript;
|
||||
|
||||
import com.extendedclip.papi.expansion.javascript.cloud.GithubScript;
|
||||
import com.extendedclip.papi.expansion.javascript.cloud.GithubScriptManager;
|
||||
import java.util.Arrays;
|
||||
import java.util.List;
|
||||
import java.util.stream.Collectors;
|
||||
import org.bukkit.ChatColor;
|
||||
import org.bukkit.command.Command;
|
||||
import org.bukkit.command.CommandSender;
|
||||
|
||||
import java.util.Arrays;
|
||||
import java.util.List;
|
||||
import java.util.Set;
|
||||
import java.util.stream.Collectors;
|
||||
|
||||
public class JavascriptExpansionCommands extends Command {
|
||||
|
||||
private JavascriptExpansion expansion;
|
||||
private final String PERMISSION = "placeholderapi.js.admin";
|
||||
private String command;
|
||||
|
||||
public JavascriptExpansionCommands(JavascriptExpansion expansion) {
|
||||
super("jsexpansion");
|
||||
command = getName();
|
||||
this.expansion = expansion;
|
||||
this.setDescription("Javascript expansion commands");
|
||||
this.setUsage("/jsexpansion <arg>");
|
||||
this.setPermission("placeholderapi.js.admin");
|
||||
this.setUsage("/" + command + " <args>");
|
||||
this.setPermission(PERMISSION);
|
||||
}
|
||||
|
||||
@Override
|
||||
public boolean execute(CommandSender s, String label, String[] args) {
|
||||
if (!s.hasPermission(this.getPermission())) {
|
||||
msg(s, "&cYou don't have permission to do that!");
|
||||
public boolean execute(CommandSender sender, String label, String[] args) {
|
||||
if (!sender.hasPermission(PERMISSION)) {
|
||||
msg(sender, "&cYou don't have permission to do that!");
|
||||
return true;
|
||||
}
|
||||
|
||||
if (args.length == 0) {
|
||||
msg(s, "&eJavascript expansion &7v: &f" + expansion.getVersion(),
|
||||
msg(sender,
|
||||
"&eJavascript expansion &7v: &f" + expansion.getVersion(),
|
||||
"&eCreated by: &f" + expansion.getAuthor(),
|
||||
"&eWiki: &ahttps://github.com/PlaceholderAPI-Expansions/Javascript-Expansion/wiki",
|
||||
"&eWiki: &fhttps://github.com/PlaceholderAPI-Expansions/Javascript-Expansion/wiki",
|
||||
"&r",
|
||||
"&e/jsexpansion reload &7- &fReload your javascripts without reloading PlaceholderAPI",
|
||||
"&e/jsexpansion list &7- &fList loaded script identifiers.");
|
||||
"&e/" + command + " reload &7- &fReload your javascripts without reloading PlaceholderAPI",
|
||||
"&e/" + command + " list &7- &fList loaded script identifiers."
|
||||
);
|
||||
|
||||
if (expansion.getGithubScriptManager() != null) {
|
||||
msg(s, "&e&e/jsexpansion git refresh &7- &fRefresh available Github scripts",
|
||||
"&e/jsexpansion git download <name> &7- &fDownload a script from the js expansion github.",
|
||||
"&e/jsexpansion git list &7- &fList available scripts in the js expansion github.",
|
||||
"&e/jsexpansion git info (name) &7- &fGet the description and url of a specific script.");
|
||||
msg(sender,
|
||||
"&e&e/" + command + " git refresh &7- &fRefresh available Github scripts",
|
||||
"&e/" + command + " git download <name> &7- &fDownload a script from the js expansion github.",
|
||||
"&e/" + command + " git list &7- &fList available scripts in the js expansion github.",
|
||||
"&e/" + command + " git info (name) &7- &fGet the description and url of a specific script."
|
||||
);
|
||||
}
|
||||
|
||||
return true;
|
||||
}
|
||||
|
||||
if (args[0].equalsIgnoreCase("reload")) {
|
||||
msg(s, "&aJavascriptExpansion reloading...");
|
||||
int l = expansion.reloadScripts();
|
||||
msg(s, l + " &7script" + (l == 1 ? "" : "s") + " loaded");
|
||||
switch (args[0].toLowerCase()) {
|
||||
case "reload": {
|
||||
msg(sender, "&aJavascriptExpansion reloading...");
|
||||
final int scripts = expansion.reloadScripts();
|
||||
msg(sender, scripts + " &7script" + plural(scripts) + " loaded");
|
||||
return true;
|
||||
}
|
||||
|
||||
if (args[0].equalsIgnoreCase("list")) {
|
||||
List<String> loaded = expansion.getLoadedIdentifiers();
|
||||
msg(s, loaded.size() + " &7script" + (loaded.size() == 1 ? "" : "s") + " loaded");
|
||||
msg(s, String.join(", ", loaded));
|
||||
case "list": {
|
||||
final List<String> loaded = expansion.getLoadedIdentifiers();
|
||||
msg(sender,
|
||||
loaded.size() + " &7script" + plural(loaded.size()) + " loaded.",
|
||||
String.join(", ", loaded)
|
||||
);
|
||||
return true;
|
||||
}
|
||||
|
||||
if (args[0].equalsIgnoreCase("git")) {
|
||||
case "git": {
|
||||
if (expansion.getGithubScriptManager() == null) {
|
||||
msg(s, "&8This feature is disabled in the PlaceholderAPI config.");
|
||||
msg(sender, "&8This feature is disabled in the PlaceholderAPI config.");
|
||||
return true;
|
||||
}
|
||||
|
||||
if (args.length < 2) {
|
||||
msg(s, "&cIncorrect usage!");
|
||||
msg(sender, "&cIncorrect usage!");
|
||||
return true;
|
||||
}
|
||||
|
||||
GithubScriptManager manager = expansion.getGithubScriptManager();
|
||||
final GithubScriptManager manager = expansion.getGithubScriptManager();
|
||||
|
||||
if (args[1].equalsIgnoreCase("refresh")) {
|
||||
switch (args[1].toLowerCase()) {
|
||||
case "refresh": {
|
||||
expansion.getGithubScriptManager().fetch();
|
||||
msg(s, "&aFetching available scripts... Check back in a sec!");
|
||||
msg(sender, "&aFetching available scripts... Check back in a sec!");
|
||||
return true;
|
||||
}
|
||||
|
||||
if (args[1].equalsIgnoreCase("list")) {
|
||||
msg(s, manager.getAvailableScripts().size() + " &escript"
|
||||
+ (manager.getAvailableScripts().size() == 1 ? "" : "s") + " available on Github.",
|
||||
String.join(", ", manager.getAvailableScripts().stream()
|
||||
.map(GithubScript::getName)
|
||||
.collect(Collectors.toSet())));
|
||||
case "list": {
|
||||
final List<GithubScript> availableScripts = manager.getAvailableScripts();
|
||||
final Set<String> scripts = availableScripts.stream().map(GithubScript::getName).collect(Collectors.toSet());
|
||||
|
||||
msg(sender, availableScripts.size() + " &escript" + plural(availableScripts.size()) + " available on Github.", String.join(", ", scripts));
|
||||
return true;
|
||||
}
|
||||
|
||||
if (args[1].equalsIgnoreCase("info")) {
|
||||
case "info": {
|
||||
if (args.length < 3) {
|
||||
msg(s, "&4Incorrect usage! &f" + this.getName() + " git info <name>");
|
||||
msg(sender, "&4Incorrect usage! &f/" + command + " git info <name>");
|
||||
return true;
|
||||
}
|
||||
GithubScript script = manager.getScript(args[2]);
|
||||
|
||||
final GithubScript script = manager.getScript(args[2]);
|
||||
|
||||
if (script == null) {
|
||||
msg(s, "&4The script &f" + args[2] + " &4does not exist!");
|
||||
msg(sender, "&4The script &f" + args[2] + " &4does not exist!");
|
||||
return true;
|
||||
}
|
||||
msg(s, "&eName: &f" + script.getName(),
|
||||
|
||||
msg(sender,
|
||||
"&eName: &f" + script.getName(),
|
||||
"&eVersion: &f" + script.getVersion(),
|
||||
"&eDescription: &f" + script.getDescription(),
|
||||
"&eAuthor: &f" + script.getAuthor(),
|
||||
"&eSource URL: &f" + script.getUrl());
|
||||
"&eSource URL: &f" + script.getUrl()
|
||||
);
|
||||
return true;
|
||||
}
|
||||
if (args[1].equalsIgnoreCase("download")) {
|
||||
|
||||
case "download": {
|
||||
if (args.length < 3) {
|
||||
msg(s, "&4Incorrect usage! &f" + this.getName() + " git download <name>");
|
||||
msg(sender, "&4Incorrect usage! &f/" + command + " git download <name>");
|
||||
return true;
|
||||
}
|
||||
GithubScript script = manager.getScript(args[2]);
|
||||
|
||||
final GithubScript script = manager.getScript(args[2]);
|
||||
|
||||
if (script == null) {
|
||||
msg(s, "&4The script &f" + args[2] + " &4does not exist!");
|
||||
msg(sender, "&4The script &f" + args[2] + " &4does not exist!");
|
||||
return true;
|
||||
}
|
||||
|
||||
manager.downloadScript(script);
|
||||
msg(s, "&aDownload started... &eCheck the scripts folder in a moment...");
|
||||
return true;
|
||||
}
|
||||
msg(s, "&4Incorrect usage! &f" + this.getName() + " &7for more help.");
|
||||
msg(sender, "&6Download started... &eCheck the scripts folder in a moment...");
|
||||
return true;
|
||||
}
|
||||
|
||||
default: {
|
||||
msg(sender, "&4Incorrect usage! &f/" + command + " &7for more help.");
|
||||
return true;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
public void msg(CommandSender s, String... text) {
|
||||
Arrays.stream(text)
|
||||
.forEach(line -> s.sendMessage(ChatColor.translateAlternateColorCodes('&', line)));
|
||||
default: {
|
||||
return true;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
private String plural(final int amount) {
|
||||
return amount > 1 ? "s" : "";
|
||||
}
|
||||
|
||||
public void msg(CommandSender sender, String... text) {
|
||||
if (text == null) {
|
||||
return;
|
||||
}
|
||||
|
||||
Arrays.stream(text).forEach(line -> sender.sendMessage(ChatColor.translateAlternateColorCodes('&', line)));
|
||||
}
|
||||
}
|
||||
|
@ -20,11 +20,6 @@
|
||||
*/
|
||||
package com.extendedclip.papi.expansion.javascript;
|
||||
|
||||
import java.io.File;
|
||||
import java.io.IOException;
|
||||
import java.util.Set;
|
||||
import javax.script.ScriptEngine;
|
||||
import javax.script.ScriptException;
|
||||
import me.clip.placeholderapi.PlaceholderAPI;
|
||||
import me.clip.placeholderapi.PlaceholderAPIPlugin;
|
||||
import org.apache.commons.lang.Validate;
|
||||
@ -34,40 +29,43 @@ import org.bukkit.configuration.InvalidConfigurationException;
|
||||
import org.bukkit.configuration.file.FileConfiguration;
|
||||
import org.bukkit.configuration.file.YamlConfiguration;
|
||||
|
||||
import javax.script.ScriptEngine;
|
||||
import javax.script.ScriptException;
|
||||
import java.io.File;
|
||||
import java.io.IOException;
|
||||
import java.util.Set;
|
||||
|
||||
public class JavascriptPlaceholder {
|
||||
|
||||
private final String FILEDIR =
|
||||
PlaceholderAPIPlugin.getInstance().getDataFolder() + File.separator + "javascripts"
|
||||
+ File.separator + "javascript_data";
|
||||
private ScriptEngine engine = null;
|
||||
private final String DIRECTORY = PlaceholderAPIPlugin.getInstance().getDataFolder() + "/javascripts/javascript_data";
|
||||
private ScriptEngine engine;
|
||||
private String identifier;
|
||||
private String script;
|
||||
private ScriptData data = null;
|
||||
private ScriptData data;
|
||||
private File dataFile;
|
||||
private FileConfiguration cfg;
|
||||
private FileConfiguration config;
|
||||
|
||||
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");
|
||||
Validate.notNull(script, "Script can not be null");
|
||||
|
||||
this.engine = engine;
|
||||
this.identifier = identifier;
|
||||
this.script = script;
|
||||
File dir = new File(FILEDIR);
|
||||
final File directory = new File(DIRECTORY);
|
||||
|
||||
try {
|
||||
if (!dir.exists()) {
|
||||
dir.mkdirs();
|
||||
}
|
||||
} catch (SecurityException e) {
|
||||
e.printStackTrace();
|
||||
if (directory.exists()) {
|
||||
directory.mkdirs();
|
||||
}
|
||||
|
||||
data = new ScriptData();
|
||||
dataFile = new File(FILEDIR, identifier + "_data.yml");
|
||||
dataFile = new File(DIRECTORY, identifier + "_data.yml");
|
||||
engine.put("Data", data);
|
||||
engine.put("BukkitServer", Bukkit.getServer());
|
||||
engine.put("Expansion", JavascriptExpansion.getInstance());
|
||||
engine.put("Placeholder", this);
|
||||
engine.put("PlaceholderAPI", PlaceholderAPI.class);
|
||||
}
|
||||
|
||||
public String getIdentifier() {
|
||||
@ -78,40 +76,38 @@ public class JavascriptPlaceholder {
|
||||
return script;
|
||||
}
|
||||
|
||||
public String evaluate(OfflinePlayer p, String... args) {
|
||||
String exp = PlaceholderAPI.setPlaceholders(p, script);
|
||||
public String evaluate(OfflinePlayer player, String... args) {
|
||||
String exp = PlaceholderAPI.setPlaceholders(player, script);
|
||||
|
||||
try {
|
||||
String[] c = null;
|
||||
String[] arguments = null;
|
||||
|
||||
if (args != null && args.length > 0) {
|
||||
arguments = new String[args.length];
|
||||
|
||||
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;
|
||||
arguments[i] = PlaceholderAPI.setBracketPlaceholders(player, args[i]);
|
||||
}
|
||||
}
|
||||
|
||||
if (c == null) {
|
||||
c = new String[]{};
|
||||
if (arguments == null) {
|
||||
arguments = new String[]{};
|
||||
}
|
||||
|
||||
engine.put("args", c);
|
||||
engine.put("BukkitPlayer", p != null && p.isOnline() ? p.getPlayer() : null);
|
||||
engine.put("OfflinePlayer", p);
|
||||
engine.put("args", arguments);
|
||||
engine.put("BukkitPlayer", player != null && player.isOnline() ? player.getPlayer() : null);
|
||||
engine.put("OfflinePlayer", player);
|
||||
Object result = engine.eval(exp);
|
||||
return result != null ? PlaceholderAPI.setBracketPlaceholders(p, result.toString()) : "";
|
||||
return result != null ? PlaceholderAPI.setBracketPlaceholders(player, result.toString()) : "";
|
||||
} catch (ScriptException ex) {
|
||||
System.out.println(ex.getMessage());
|
||||
ex.printStackTrace();
|
||||
}
|
||||
|
||||
return "Script error";
|
||||
}
|
||||
|
||||
@ -128,25 +124,22 @@ public class JavascriptPlaceholder {
|
||||
}
|
||||
|
||||
public boolean loadData() {
|
||||
|
||||
cfg = new YamlConfiguration();
|
||||
config = new YamlConfiguration();
|
||||
|
||||
if (!dataFile.exists()) {
|
||||
return false;
|
||||
}
|
||||
|
||||
try {
|
||||
cfg.load(dataFile);
|
||||
} catch (IOException e) {
|
||||
e.printStackTrace();
|
||||
return false;
|
||||
} catch (InvalidConfigurationException e) {
|
||||
config.load(dataFile);
|
||||
} catch (IOException | InvalidConfigurationException e) {
|
||||
e.printStackTrace();
|
||||
return false;
|
||||
}
|
||||
|
||||
final Set<String> keys = cfg.getKeys(true);
|
||||
final Set<String> keys = config.getKeys(true);
|
||||
|
||||
if (keys == null || keys.isEmpty()) {
|
||||
if (keys.size() == 0) {
|
||||
return false;
|
||||
}
|
||||
|
||||
@ -156,14 +149,13 @@ public class JavascriptPlaceholder {
|
||||
data.clear();
|
||||
}
|
||||
|
||||
keys.stream().forEach(k -> {
|
||||
data.set(k, cfg.get(k));
|
||||
});
|
||||
keys.forEach(key -> data.set(key, config.get(key)));
|
||||
|
||||
if (!data.isEmpty()) {
|
||||
this.setData(data);
|
||||
return true;
|
||||
}
|
||||
|
||||
return false;
|
||||
}
|
||||
|
||||
@ -172,16 +164,14 @@ public class JavascriptPlaceholder {
|
||||
return false;
|
||||
}
|
||||
|
||||
if (cfg == null) {
|
||||
if (config == null) {
|
||||
return false;
|
||||
}
|
||||
|
||||
data.getData().entrySet().forEach(e -> {
|
||||
cfg.set(e.getKey(), e.getValue());
|
||||
});
|
||||
data.getData().forEach((key, value) -> config.set(key, value));
|
||||
|
||||
try {
|
||||
cfg.save(dataFile);
|
||||
config.save(dataFile);
|
||||
return true;
|
||||
} catch (IOException e) {
|
||||
return false;
|
||||
@ -193,6 +183,7 @@ public class JavascriptPlaceholder {
|
||||
this.data.clear();
|
||||
this.data = null;
|
||||
}
|
||||
this.cfg = null;
|
||||
|
||||
this.config = null;
|
||||
}
|
||||
}
|
||||
|
@ -20,16 +20,17 @@
|
||||
*/
|
||||
package com.extendedclip.papi.expansion.javascript;
|
||||
|
||||
import me.clip.placeholderapi.PlaceholderAPIPlugin;
|
||||
import org.bukkit.configuration.file.FileConfiguration;
|
||||
import org.bukkit.configuration.file.YamlConfiguration;
|
||||
|
||||
import javax.script.ScriptEngine;
|
||||
import javax.script.ScriptEngineManager;
|
||||
import java.io.File;
|
||||
import java.io.FileNotFoundException;
|
||||
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;
|
||||
import org.bukkit.configuration.file.YamlConfiguration;
|
||||
|
||||
public class JavascriptPlaceholdersConfig {
|
||||
|
||||
@ -80,10 +81,11 @@ public class JavascriptPlaceholdersConfig {
|
||||
+ "\n file: 'my_placeholder.js'"
|
||||
+ "\n engine: 'nashorn'");
|
||||
|
||||
if (config.getKeys(false) == null || config.getKeys(false).isEmpty()) {
|
||||
if (config.getKeys(false).isEmpty()) {
|
||||
config.set("example.file", "example.js");
|
||||
config.set("example.engine", "nashorn");
|
||||
}
|
||||
|
||||
save();
|
||||
}
|
||||
|
||||
@ -107,7 +109,7 @@ public class JavascriptPlaceholdersConfig {
|
||||
}
|
||||
|
||||
public int loadPlaceholders() {
|
||||
if (config == null || config.getKeys(false) == null || config.getKeys(false).isEmpty()) {
|
||||
if (config == null || config.getKeys(false).isEmpty()) {
|
||||
return 0;
|
||||
}
|
||||
|
||||
@ -116,27 +118,19 @@ public class JavascriptPlaceholdersConfig {
|
||||
try {
|
||||
if (!dir.exists()) {
|
||||
dir.mkdirs();
|
||||
plugin.getLogger().info(
|
||||
"Creating directory: plugins" + File.separator + "PlaceholderAPI" + File.separator
|
||||
+ "javascripts");
|
||||
} else {
|
||||
plugin.getLogger().info("Creating directory: plugins/PlaceholderAPI/javascripts");
|
||||
}
|
||||
} catch (SecurityException e) {
|
||||
plugin.getLogger().severe(
|
||||
"Could not create directory: plugins" + File.separator + "PlaceholderAPI" + File.separator
|
||||
+ "javascripts");
|
||||
plugin.getLogger().severe("Could not create directory: plugins/PlaceholderAPI/javascripts");
|
||||
}
|
||||
|
||||
for (String identifier : config.getKeys(false)) {
|
||||
if (!config.contains(identifier + ".file")
|
||||
|| config.getString(identifier + ".file", null) == null) {
|
||||
plugin.getLogger()
|
||||
.warning("Javascript placeholder: " + identifier + " does not have a file specified");
|
||||
if (!config.contains(identifier + ".file") || config.getString(identifier + ".file") == null) {
|
||||
plugin.getLogger().warning("Javascript placeholder: " + identifier + " does not have a file specified");
|
||||
continue;
|
||||
}
|
||||
|
||||
File scriptFile = new File(plugin.getDataFolder() + File.separator + "javascripts",
|
||||
config.getString(identifier + ".file"));
|
||||
File scriptFile = new File(plugin.getDataFolder() + "/javascripts", config.getString(identifier + ".file"));
|
||||
|
||||
if (!scriptFile.exists()) {
|
||||
plugin.getLogger().info(scriptFile.getName() + " does not exist. Creating file...");
|
||||
@ -154,56 +148,50 @@ public class JavascriptPlaceholdersConfig {
|
||||
String script = getContents(scriptFile);
|
||||
|
||||
if (script == null || script.isEmpty()) {
|
||||
plugin.getLogger().warning(
|
||||
"File: " + scriptFile.getName() + " for javascript placeholder: " + identifier
|
||||
+ " is empty");
|
||||
plugin.getLogger().warning("File: " + scriptFile.getName() + " for javascript placeholder: " + identifier + " is empty");
|
||||
continue;
|
||||
}
|
||||
|
||||
ScriptEngine engine = null;
|
||||
ScriptEngine engine;
|
||||
|
||||
if (!config.contains(identifier + ".engine")) {
|
||||
engine = ex.getGlobalEngine();
|
||||
} else {
|
||||
try {
|
||||
engine = new ScriptEngineManager()
|
||||
.getEngineByName(config.getString(identifier + ".engine", "nashorn"));
|
||||
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");
|
||||
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);
|
||||
plugin.getLogger().warning("Failed to set ScriptEngine for javascript placeholder: " + identifier);
|
||||
continue;
|
||||
}
|
||||
|
||||
JavascriptPlaceholder pl = new JavascriptPlaceholder(engine, identifier, script);
|
||||
|
||||
boolean added = ex.addJSPlaceholder(pl);
|
||||
final JavascriptPlaceholder pl = new JavascriptPlaceholder(engine, identifier, script);
|
||||
final boolean added = ex.addJSPlaceholder(pl);
|
||||
|
||||
if (added) {
|
||||
|
||||
if (pl.loadData()) {
|
||||
plugin.getLogger().info("Loaded data for javascript placeholder: " + identifier);
|
||||
}
|
||||
|
||||
plugin.getLogger().info("%javascript_" + identifier + "% has been loaded!");
|
||||
} else {
|
||||
plugin.getLogger()
|
||||
.warning("Javascript placeholder %javascript_" + identifier + "% is a duplicate!");
|
||||
plugin.getLogger().warning("Javascript placeholder %javascript_" + identifier + "% is a duplicate!");
|
||||
}
|
||||
}
|
||||
|
||||
return ex.getAmountLoaded();
|
||||
}
|
||||
|
||||
private String getContents(File f) {
|
||||
StringBuilder sb = new StringBuilder();
|
||||
private String getContents(File file) {
|
||||
final StringBuilder sb = new StringBuilder();
|
||||
|
||||
try {
|
||||
Scanner scanner = new Scanner(f);
|
||||
Scanner scanner = new Scanner(file);
|
||||
|
||||
while (scanner.hasNextLine()) {
|
||||
String line = scanner.nextLine();
|
||||
@ -221,7 +209,7 @@ public class JavascriptPlaceholdersConfig {
|
||||
if (line.startsWith("//")) {
|
||||
continue;
|
||||
}
|
||||
sb.append(line + " ");
|
||||
sb.append(line).append(" ");
|
||||
}
|
||||
scanner.close();
|
||||
} catch (FileNotFoundException e) {
|
||||
|
@ -23,16 +23,12 @@ package com.extendedclip.papi.expansion.javascript.cloud;
|
||||
import com.extendedclip.papi.expansion.javascript.JavascriptExpansion;
|
||||
import com.google.gson.Gson;
|
||||
import com.google.gson.reflect.TypeToken;
|
||||
import java.io.BufferedReader;
|
||||
import java.io.File;
|
||||
import java.io.FileNotFoundException;
|
||||
import java.io.FileOutputStream;
|
||||
import java.io.InputStreamReader;
|
||||
import java.io.PrintStream;
|
||||
import org.bukkit.Bukkit;
|
||||
|
||||
import java.io.*;
|
||||
import java.net.URL;
|
||||
import java.util.ArrayList;
|
||||
import java.util.List;
|
||||
import org.bukkit.Bukkit;
|
||||
|
||||
public class GithubScriptManager {
|
||||
|
||||
@ -122,6 +118,8 @@ public class GithubScriptManager {
|
||||
|
||||
public GithubScript getScript(String name) {
|
||||
if (availableScripts == null) return null;
|
||||
return availableScripts.stream().filter(s -> {return s.getName().equalsIgnoreCase(name);}).findFirst().orElse(null);
|
||||
return availableScripts.stream().filter(s -> {
|
||||
return s.getName().equalsIgnoreCase(name);
|
||||
}).findFirst().orElse(null);
|
||||
}
|
||||
}
|
||||
|
Loading…
x
Reference in New Issue
Block a user