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