mirror of
https://github.com/PlaceholderAPI/Javascript-Expansion.git
synced 2025-05-23 10:39:04 +00:00
[1.5.4] Parse command, tab completion and fixed data files not being created
This commit is contained in:
parent
50d5698093
commit
c68d05bf1a
2
pom.xml
2
pom.xml
@ -2,7 +2,7 @@
|
||||
<modelVersion>4.0.0</modelVersion>
|
||||
<groupId>com.extendedclip.papi.expansion.javascript</groupId>
|
||||
<artifactId>javascript-expansion</artifactId>
|
||||
<version>1.5.3</version>
|
||||
<version>1.5.4</version>
|
||||
<name>PAPI-Expansion-Javascript</name>
|
||||
<description>PlaceholderAPI expansion for javascript placeholders</description>
|
||||
|
||||
|
@ -21,16 +21,6 @@
|
||||
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 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;
|
||||
@ -38,6 +28,12 @@ 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 java.lang.reflect.Field;
|
||||
import java.util.*;
|
||||
|
||||
public class JavascriptExpansion extends PlaceholderExpansion implements Cacheable, Configurable {
|
||||
|
||||
private ScriptEngine globalEngine = null;
|
||||
@ -88,8 +84,7 @@ public class JavascriptExpansion extends PlaceholderExpansion implements Cacheab
|
||||
try {
|
||||
globalEngine = new ScriptEngineManager().getEngineByName(getString("engine", "nashorn"));
|
||||
} catch (NullPointerException ex) {
|
||||
getPlaceholderAPI().getLogger()
|
||||
.warning("Javascript engine type was invalid! Defaulting to 'nashorn'");
|
||||
getPlaceholderAPI().getLogger().warning("Javascript engine type was invalid! Defaulting to 'nashorn'");
|
||||
globalEngine = new ScriptEngineManager().getEngineByName("nashorn");
|
||||
}
|
||||
}
|
||||
@ -194,7 +189,9 @@ public class JavascriptExpansion extends PlaceholderExpansion implements Cacheab
|
||||
}
|
||||
|
||||
public JavascriptPlaceholder getJSPlaceholder(String identifier) {
|
||||
return scripts.stream().filter(s -> s.getIdentifier().equalsIgnoreCase(identifier)).findFirst()
|
||||
return scripts.stream()
|
||||
.filter(s -> s.getIdentifier().equalsIgnoreCase(identifier))
|
||||
.findFirst()
|
||||
.orElse(null);
|
||||
}
|
||||
|
||||
@ -241,6 +238,7 @@ public class JavascriptExpansion extends PlaceholderExpansion implements Cacheab
|
||||
this.githubManager = manager;
|
||||
}
|
||||
|
||||
@SuppressWarnings("UnusedReturnValue")
|
||||
private boolean unregisterCommand() {
|
||||
if (commandMap == null || commands == null) {
|
||||
return false;
|
||||
@ -248,10 +246,12 @@ public class JavascriptExpansion extends PlaceholderExpansion implements Cacheab
|
||||
return commands.unregister(commandMap);
|
||||
}
|
||||
|
||||
@SuppressWarnings("UnusedReturnValue")
|
||||
private boolean registerCommand() {
|
||||
if (commandMap == null) {
|
||||
return false;
|
||||
}
|
||||
|
||||
commands = new JavascriptExpansionCommands(this);
|
||||
commandMap.register("papi" + commands.getName(), commands);
|
||||
return commands.isRegistered();
|
||||
|
@ -23,20 +23,23 @@ package com.extendedclip.papi.expansion.javascript;
|
||||
import com.extendedclip.papi.expansion.javascript.cloud.GithubScript;
|
||||
import com.extendedclip.papi.expansion.javascript.cloud.GithubScriptManager;
|
||||
import me.clip.placeholderapi.PlaceholderAPIPlugin;
|
||||
import org.bukkit.Bukkit;
|
||||
import org.bukkit.ChatColor;
|
||||
import org.bukkit.OfflinePlayer;
|
||||
import org.bukkit.command.Command;
|
||||
import org.bukkit.command.CommandSender;
|
||||
import org.bukkit.entity.Player;
|
||||
import org.bukkit.util.StringUtil;
|
||||
|
||||
import java.util.Arrays;
|
||||
import java.util.List;
|
||||
import java.util.Set;
|
||||
import java.io.File;
|
||||
import java.util.*;
|
||||
import java.util.stream.Collectors;
|
||||
|
||||
public class JavascriptExpansionCommands extends Command {
|
||||
|
||||
private JavascriptExpansion expansion;
|
||||
private final JavascriptExpansion expansion;
|
||||
private final String PERMISSION = "placeholderapi.js.admin";
|
||||
private String command;
|
||||
private final String command;
|
||||
|
||||
public JavascriptExpansionCommands(JavascriptExpansion expansion) {
|
||||
super("jsexpansion");
|
||||
@ -47,6 +50,44 @@ public class JavascriptExpansionCommands extends Command {
|
||||
this.setPermission(PERMISSION);
|
||||
}
|
||||
|
||||
@Override
|
||||
public List<String> tabComplete(CommandSender sender, String alias, String[] args) throws IllegalArgumentException {
|
||||
if (!sender.hasPermission(PERMISSION)) {
|
||||
return Collections.emptyList();
|
||||
}
|
||||
|
||||
final List<String> commands = new ArrayList<>(Arrays.asList("list", "parse", "reload"));
|
||||
final List<String> completion = new ArrayList<>();
|
||||
|
||||
if (expansion.getGithubScriptManager() != null) {
|
||||
commands.add(0, "git");
|
||||
}
|
||||
|
||||
if (args.length == 1) {
|
||||
return StringUtil.copyPartialMatches(args[0], commands, completion);
|
||||
}
|
||||
|
||||
if (args[0].equalsIgnoreCase("git")) {
|
||||
if (expansion.getGithubScriptManager() == null) {
|
||||
return Collections.emptyList();
|
||||
}
|
||||
|
||||
if (args.length == 2) {
|
||||
return StringUtil.copyPartialMatches(args[1], Arrays.asList("download", "enable", "info", "list", "refresh"), completion);
|
||||
}
|
||||
|
||||
if (args.length == 3 && args[1].equalsIgnoreCase("download")) {
|
||||
if (expansion.getGithubScriptManager().getAvailableScripts() == null) {
|
||||
return Collections.emptyList();
|
||||
}
|
||||
|
||||
return StringUtil.copyPartialMatches(args[2], expansion.getGithubScriptManager().getAvailableScripts().stream().map(GithubScript::getName).collect(Collectors.toList()), completion);
|
||||
}
|
||||
}
|
||||
|
||||
return Collections.emptyList();
|
||||
}
|
||||
|
||||
@Override
|
||||
public boolean execute(CommandSender sender, String label, String[] args) {
|
||||
if (!sender.hasPermission(PERMISSION)) {
|
||||
@ -60,16 +101,17 @@ public class JavascriptExpansionCommands extends Command {
|
||||
"&eCreated by: &f" + expansion.getAuthor(),
|
||||
"&eWiki: &fhttps://github.com/PlaceholderAPI/Javascript-Expansion/wiki",
|
||||
"&r",
|
||||
"&e/" + command + " reload &7- &fReload your javascripts without reloading PlaceholderAPI",
|
||||
"&e/" + command + " list &7- &fList loaded script identifiers."
|
||||
"&e/" + command + " reload &7- &fReload your javascripts without reloading PlaceholderAPI.",
|
||||
"&e/" + command + " list &7- &fList loaded script identifiers.",
|
||||
"&e/" + command + " parse [me/player] [code] &7- &fTest JavaScript code in chat."
|
||||
);
|
||||
|
||||
if (expansion.getGithubScriptManager() != null) {
|
||||
msg(sender,
|
||||
"&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 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."
|
||||
"&e/" + command + " git info [name] &7- &fGet the description and url of a specific script."
|
||||
);
|
||||
}
|
||||
|
||||
@ -77,30 +119,14 @@ public class JavascriptExpansionCommands extends Command {
|
||||
}
|
||||
|
||||
switch (args[0].toLowerCase()) {
|
||||
case "reload": {
|
||||
msg(sender, "&aJavascriptExpansion reloading...");
|
||||
final int scripts = expansion.reloadScripts();
|
||||
msg(sender, scripts + " &7script" + plural(scripts) + " loaded");
|
||||
return true;
|
||||
}
|
||||
|
||||
case "list": {
|
||||
final List<String> loaded = expansion.getLoadedIdentifiers();
|
||||
msg(sender,
|
||||
loaded.size() + " &7script" + plural(loaded.size()) + " loaded.",
|
||||
String.join(", ", loaded)
|
||||
);
|
||||
return true;
|
||||
}
|
||||
|
||||
case "git": {
|
||||
if (expansion.getGithubScriptManager() == null) {
|
||||
msg(sender, "&8This feature is disabled in the PlaceholderAPI config.");
|
||||
msg(sender, "&cThis feature is disabled in the PlaceholderAPI config.");
|
||||
return true;
|
||||
}
|
||||
|
||||
if (args.length < 2) {
|
||||
msg(sender, "&cIncorrect usage!");
|
||||
msg(sender, "&cIncorrect usage! Type '&f/" + command + "&c' for more help.");
|
||||
return true;
|
||||
}
|
||||
|
||||
@ -123,14 +149,14 @@ public class JavascriptExpansionCommands extends Command {
|
||||
|
||||
case "info": {
|
||||
if (args.length < 3) {
|
||||
msg(sender, "&4Incorrect usage! &f/" + command + " git info <name>");
|
||||
msg(sender, "&cIncorrect usage! &f/" + command + " git info [name]");
|
||||
return true;
|
||||
}
|
||||
|
||||
final GithubScript script = manager.getScript(args[2]);
|
||||
|
||||
if (script == null) {
|
||||
msg(sender, "&4The script &f" + args[2] + " &4does not exist!");
|
||||
msg(sender, "&cThe script &f" + args[2] + " &cdoes not exist!");
|
||||
return true;
|
||||
}
|
||||
|
||||
@ -146,33 +172,40 @@ public class JavascriptExpansionCommands extends Command {
|
||||
|
||||
case "download": {
|
||||
if (args.length < 3) {
|
||||
msg(sender, "&4Incorrect usage! &f/" + command + " git download <name>");
|
||||
msg(sender, "&cIncorrect usage! &f/" + command + " git download [name]");
|
||||
return true;
|
||||
}
|
||||
|
||||
final GithubScript script = manager.getScript(args[2]);
|
||||
|
||||
if (script == null) {
|
||||
msg(sender, "&4The script &f" + args[2] + " &4does not exist!");
|
||||
msg(sender, "&cThe script &f" + args[2] + " &cdoes not exist!");
|
||||
return true;
|
||||
}
|
||||
|
||||
if (new File(expansion.getGithubScriptManager().getJavascriptsFolder(), script.getName() + ".js").exists()) {
|
||||
msg(sender, "&cCould not download " + script.getName() + " because a file with the same name already exist in the javascripts folder.");
|
||||
return true;
|
||||
}
|
||||
|
||||
manager.downloadScript(script);
|
||||
msg(sender, "&6Download started... &eCheck the scripts folder in a moment...");
|
||||
msg(sender, "&aDownload started. &eCheck the scripts folder in a moment...");
|
||||
return true;
|
||||
}
|
||||
|
||||
case "enabled":
|
||||
if (args.length < 3) {
|
||||
msg(sender, "&4Incorrect usage! &f/" + command + " git enabled <true/false>");
|
||||
msg(sender, "&cIncorrect usage! &f/" + command + " git enabled [true/false]");
|
||||
return true;
|
||||
}
|
||||
|
||||
Boolean enabled = Boolean.parseBoolean(args[2]);
|
||||
PlaceholderAPIPlugin papi = expansion.getPlaceholderAPI();
|
||||
final boolean enabled = Boolean.parseBoolean(args[2]);
|
||||
final PlaceholderAPIPlugin papi = expansion.getPlaceholderAPI();
|
||||
|
||||
papi.getConfig().set("expansions." + this.getName() + ".github_script_downloads", enabled);
|
||||
papi.saveConfig();
|
||||
papi.reloadConfig();
|
||||
|
||||
if (!enabled) {
|
||||
if (expansion.getGithubScriptManager() != null) {
|
||||
expansion.getGithubScriptManager().clear();
|
||||
@ -184,16 +217,63 @@ public class JavascriptExpansionCommands extends Command {
|
||||
}
|
||||
expansion.getGithubScriptManager().fetch();
|
||||
}
|
||||
|
||||
msg(sender, "&6Git script downloads set to: &e" + enabled);
|
||||
return true;
|
||||
|
||||
default: {
|
||||
msg(sender, "&4Incorrect usage! &f/" + command + " &7for more help.");
|
||||
msg(sender, "&cIncorrect usage! Type '&f/" + command + "&c' for more help.");
|
||||
return true;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
case "list": {
|
||||
final List<String> loaded = expansion.getLoadedIdentifiers();
|
||||
msg(sender,
|
||||
loaded.size() + " &7script" + plural(loaded.size()) + " loaded.",
|
||||
String.join(", ", loaded)
|
||||
);
|
||||
return true;
|
||||
}
|
||||
|
||||
case "parse": {
|
||||
if (args.length < 3) {
|
||||
msg(sender, "&cIncorrect usage! &f/" + command + " parse [me/player] [code]");
|
||||
return true;
|
||||
}
|
||||
|
||||
final String script = String.join(" ", Arrays.copyOfRange(args, 2, args.length));
|
||||
final JavascriptPlaceholder placeholder = new JavascriptPlaceholder(expansion.getGlobalEngine(), "parse-command", String.join(" ", script));
|
||||
|
||||
if ("me".equalsIgnoreCase(args[1])) {
|
||||
if (!(sender instanceof Player)) {
|
||||
msg(sender, "&cOnly players can run this command!");
|
||||
return true;
|
||||
}
|
||||
|
||||
sender.sendMessage(placeholder.evaluate((Player) sender));
|
||||
return true;
|
||||
}
|
||||
|
||||
final OfflinePlayer player = Bukkit.getOfflinePlayer(args[1]);
|
||||
|
||||
if (!player.hasPlayedBefore() || player.getName() == null) {
|
||||
msg(sender, "&cUnknown player " + args[1]);
|
||||
return true;
|
||||
}
|
||||
|
||||
sender.sendMessage(placeholder.evaluate(player));
|
||||
return true;
|
||||
}
|
||||
|
||||
case "reload": {
|
||||
msg(sender, "&aJavascriptExpansion reloading...");
|
||||
final int scripts = expansion.reloadScripts();
|
||||
msg(sender, scripts + " &7script" + plural(scripts) + " loaded");
|
||||
return true;
|
||||
}
|
||||
|
||||
default: {
|
||||
return true;
|
||||
}
|
||||
@ -209,6 +289,6 @@ public class JavascriptExpansionCommands extends Command {
|
||||
return;
|
||||
}
|
||||
|
||||
Arrays.stream(text).forEach(line -> sender.sendMessage(ChatColor.translateAlternateColorCodes('&', line)));
|
||||
sender.sendMessage(ChatColor.translateAlternateColorCodes('&', Arrays.stream(text).filter(Objects::nonNull).collect(Collectors.joining("\n"))));
|
||||
}
|
||||
}
|
||||
|
@ -26,7 +26,6 @@ import org.apache.commons.lang.Validate;
|
||||
import org.bukkit.Bukkit;
|
||||
import org.bukkit.OfflinePlayer;
|
||||
import org.bukkit.configuration.InvalidConfigurationException;
|
||||
import org.bukkit.configuration.file.FileConfiguration;
|
||||
import org.bukkit.configuration.file.YamlConfiguration;
|
||||
|
||||
import javax.script.ScriptEngine;
|
||||
@ -34,16 +33,17 @@ import javax.script.ScriptException;
|
||||
import java.io.File;
|
||||
import java.io.IOException;
|
||||
import java.util.Set;
|
||||
import java.util.logging.Level;
|
||||
|
||||
public class JavascriptPlaceholder {
|
||||
|
||||
private final String DIRECTORY = PlaceholderAPIPlugin.getInstance().getDataFolder() + "/javascripts/javascript_data";
|
||||
private ScriptEngine engine;
|
||||
private String identifier;
|
||||
private String script;
|
||||
private ScriptData data;
|
||||
private File dataFile;
|
||||
private FileConfiguration config;
|
||||
private final ScriptEngine engine;
|
||||
private final String identifier;
|
||||
private final String script;
|
||||
private ScriptData scriptData;
|
||||
private final File dataFile;
|
||||
private YamlConfiguration yaml;
|
||||
|
||||
public JavascriptPlaceholder(ScriptEngine engine, String identifier, String script) {
|
||||
Validate.notNull(engine, "ScriptEngine can not be null");
|
||||
@ -55,13 +55,13 @@ public class JavascriptPlaceholder {
|
||||
this.script = script;
|
||||
final File directory = new File(DIRECTORY);
|
||||
|
||||
if (directory.exists()) {
|
||||
if (!directory.exists()) {
|
||||
directory.mkdirs();
|
||||
}
|
||||
|
||||
data = new ScriptData();
|
||||
dataFile = new File(DIRECTORY, identifier + "_data.yml");
|
||||
engine.put("Data", data);
|
||||
scriptData = new ScriptData();
|
||||
dataFile = new File(directory, identifier + "_data.yml");
|
||||
engine.put("Data", scriptData);
|
||||
engine.put("BukkitServer", Bukkit.getServer());
|
||||
engine.put("Expansion", JavascriptExpansion.getInstance());
|
||||
engine.put("Placeholder", this);
|
||||
@ -99,60 +99,70 @@ public class JavascriptPlaceholder {
|
||||
}
|
||||
|
||||
engine.put("args", arguments);
|
||||
engine.put("BukkitPlayer", player != null && player.isOnline() ? player.getPlayer() : null);
|
||||
|
||||
if (player != null && player.isOnline()) {
|
||||
engine.put("BukkitPlayer", player.getPlayer());
|
||||
engine.put("Player", player.getPlayer());
|
||||
}
|
||||
|
||||
engine.put("OfflinePlayer", player);
|
||||
Object result = engine.eval(exp);
|
||||
return result != null ? PlaceholderAPI.setBracketPlaceholders(player, result.toString()) : "";
|
||||
} catch (ScriptException ex) {
|
||||
System.out.println(ex.getMessage());
|
||||
ex.printStackTrace();
|
||||
PlaceholderAPIPlugin.getInstance().getLogger().log(Level.SEVERE, "[JavaScript] An error occurred while executing the script '" + identifier + "'", ex);
|
||||
}
|
||||
|
||||
return "Script error";
|
||||
return "Script error! (check console)";
|
||||
}
|
||||
|
||||
public ScriptData getData() {
|
||||
// this should never be null but just in case setData(null) is called
|
||||
if (data == null) {
|
||||
data = new ScriptData();
|
||||
if (scriptData == null) {
|
||||
scriptData = new ScriptData();
|
||||
}
|
||||
return data;
|
||||
return scriptData;
|
||||
}
|
||||
|
||||
public void setData(ScriptData data) {
|
||||
this.data = data;
|
||||
this.scriptData = data;
|
||||
}
|
||||
|
||||
public boolean loadData() {
|
||||
config = new YamlConfiguration();
|
||||
yaml = new YamlConfiguration();
|
||||
dataFile.getParentFile().mkdirs();
|
||||
|
||||
if (!dataFile.exists()) {
|
||||
try {
|
||||
dataFile.createNewFile();
|
||||
} catch (IOException e) {
|
||||
PlaceholderAPIPlugin.getInstance().getLogger().log(Level.SEVERE, "[JavaScript Expansion] An error occurred while creating data file for " + getIdentifier(), e);
|
||||
return false;
|
||||
}
|
||||
}
|
||||
|
||||
try {
|
||||
config.load(dataFile);
|
||||
yaml.load(dataFile);
|
||||
} catch (IOException | InvalidConfigurationException e) {
|
||||
e.printStackTrace();
|
||||
PlaceholderAPIPlugin.getInstance().getLogger().log(Level.SEVERE, "[JavaScript Expansion] An error occurred while loading for " + getIdentifier(), e);
|
||||
return false;
|
||||
}
|
||||
|
||||
final Set<String> keys = config.getKeys(true);
|
||||
final Set<String> keys = yaml.getKeys(true);
|
||||
|
||||
if (keys.size() == 0) {
|
||||
return false;
|
||||
}
|
||||
|
||||
if (data == null) {
|
||||
data = new ScriptData();
|
||||
if (scriptData == null) {
|
||||
scriptData = new ScriptData();
|
||||
} else {
|
||||
data.clear();
|
||||
scriptData.clear();
|
||||
}
|
||||
|
||||
keys.forEach(key -> data.set(key, config.get(key)));
|
||||
keys.forEach(key -> scriptData.set(key, yaml.get(key)));
|
||||
|
||||
if (!data.isEmpty()) {
|
||||
this.setData(data);
|
||||
if (!scriptData.isEmpty()) {
|
||||
this.setData(scriptData);
|
||||
return true;
|
||||
}
|
||||
|
||||
@ -160,30 +170,27 @@ public class JavascriptPlaceholder {
|
||||
}
|
||||
|
||||
public boolean saveData() {
|
||||
if (data == null || data.isEmpty()) {
|
||||
if (scriptData == null || scriptData.isEmpty() || yaml == null) {
|
||||
return false;
|
||||
}
|
||||
|
||||
if (config == null) {
|
||||
return false;
|
||||
}
|
||||
|
||||
data.getData().forEach((key, value) -> config.set(key, value));
|
||||
scriptData.getData().forEach((key, value) -> yaml.set(key, value));
|
||||
|
||||
try {
|
||||
config.save(dataFile);
|
||||
yaml.save(dataFile);
|
||||
return true;
|
||||
} catch (IOException e) {
|
||||
PlaceholderAPIPlugin.getInstance().getLogger().log(Level.SEVERE, "[JavaScript Expansion] An error occurred while saving data for " + getIdentifier(), e);
|
||||
return false;
|
||||
}
|
||||
}
|
||||
|
||||
public void cleanup() {
|
||||
if (this.data != null) {
|
||||
this.data.clear();
|
||||
this.data = null;
|
||||
if (this.scriptData != null) {
|
||||
this.scriptData.clear();
|
||||
this.scriptData = null;
|
||||
}
|
||||
|
||||
this.config = null;
|
||||
this.yaml = null;
|
||||
}
|
||||
}
|
||||
|
@ -113,42 +113,42 @@ public class JavascriptPlaceholdersConfig {
|
||||
return 0;
|
||||
}
|
||||
|
||||
File dir = new File(plugin.getDataFolder() + File.separator + "javascripts");
|
||||
final File directory = new File(plugin.getDataFolder(), "javascripts");
|
||||
|
||||
try {
|
||||
if (!dir.exists()) {
|
||||
dir.mkdirs();
|
||||
plugin.getLogger().info("Creating directory: plugins/PlaceholderAPI/javascripts");
|
||||
if (!directory.exists()) {
|
||||
directory.mkdirs();
|
||||
plugin.getLogger().info("[JavaScript Expansion] Creating directory: " + directory.getPath());
|
||||
}
|
||||
} catch (SecurityException e) {
|
||||
plugin.getLogger().severe("Could not create directory: plugins/PlaceholderAPI/javascripts");
|
||||
plugin.getLogger().log(Level.SEVERE, "[JavaScript Expansion] Could not create directory: " + directory.getPath(), e);
|
||||
}
|
||||
|
||||
for (String identifier : config.getKeys(false)) {
|
||||
if (!config.contains(identifier + ".file") || config.getString(identifier + ".file") == null) {
|
||||
plugin.getLogger().warning("Javascript placeholder: " + identifier + " does not have a file specified");
|
||||
plugin.getLogger().warning("[JavaScript Expansion] Javascript placeholder: " + identifier + " does not have a file specified");
|
||||
continue;
|
||||
}
|
||||
|
||||
File scriptFile = new File(plugin.getDataFolder() + "/javascripts", config.getString(identifier + ".file"));
|
||||
|
||||
if (!scriptFile.exists()) {
|
||||
plugin.getLogger().info(scriptFile.getName() + " does not exist. Creating file...");
|
||||
plugin.getLogger().info("[JavaScript Expansion] " +scriptFile.getName() + " does not exist. Creating file...");
|
||||
|
||||
try {
|
||||
scriptFile.createNewFile();
|
||||
plugin.getLogger().info(scriptFile.getName()
|
||||
+ " created! Add your javascript to this file and use /placeholderapi reload to load it!");
|
||||
plugin.getLogger().info("[JavaScript Expansion] " + scriptFile.getName() + " created! Add your javascript to this file and use '/jsexpansion reload' to load it!");
|
||||
} catch (IOException e) {
|
||||
e.printStackTrace();
|
||||
plugin.getLogger().log(Level.SEVERE, "[JavaScript Expansion] An error occurred while creating " + scriptFile.getName(), e);
|
||||
}
|
||||
|
||||
continue;
|
||||
}
|
||||
|
||||
String script = getContents(scriptFile);
|
||||
|
||||
if (script == null || script.isEmpty()) {
|
||||
plugin.getLogger().warning("File: " + scriptFile.getName() + " for javascript placeholder: " + identifier + " is empty");
|
||||
plugin.getLogger().warning("[JavaScript Expansion] File: " + scriptFile.getName() + " for javascript placeholder: " + identifier + " is empty");
|
||||
continue;
|
||||
}
|
||||
|
||||
@ -160,27 +160,27 @@ public class JavascriptPlaceholdersConfig {
|
||||
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");
|
||||
plugin.getLogger().warning("[JavaScript Expansion] 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("[JavaScript Expansion] Failed to set ScriptEngine for javascript placeholder: " + identifier);
|
||||
continue;
|
||||
}
|
||||
|
||||
final JavascriptPlaceholder pl = new JavascriptPlaceholder(engine, identifier, script);
|
||||
final boolean added = ex.addJSPlaceholder(pl);
|
||||
final JavascriptPlaceholder placeholder = new JavascriptPlaceholder(engine, identifier, script);
|
||||
final boolean added = ex.addJSPlaceholder(placeholder);
|
||||
|
||||
if (added) {
|
||||
if (pl.loadData()) {
|
||||
plugin.getLogger().info("Loaded data for javascript placeholder: " + identifier);
|
||||
if (placeholder.loadData()) {
|
||||
plugin.getLogger().info("[JavaScript Expansion] Loaded data for javascript placeholder: " + identifier);
|
||||
}
|
||||
|
||||
plugin.getLogger().info("%javascript_" + identifier + "% has been loaded!");
|
||||
plugin.getLogger().info("[JavaScript Expansion] %javascript_" + identifier + "% has been loaded!");
|
||||
} else {
|
||||
plugin.getLogger().warning("Javascript placeholder %javascript_" + identifier + "% is a duplicate!");
|
||||
plugin.getLogger().warning("[JavaScript Expansion] Javascript placeholder %javascript_" + identifier + "% is a duplicate!");
|
||||
}
|
||||
}
|
||||
|
||||
|
@ -22,7 +22,11 @@ package com.extendedclip.papi.expansion.javascript.cloud;
|
||||
|
||||
public class GithubScript {
|
||||
|
||||
private String name, version, author, description, url;
|
||||
private final String name;
|
||||
private final String version;
|
||||
private final String author;
|
||||
private final String description;
|
||||
private final String url;
|
||||
|
||||
public GithubScript(String name, String version, String author, String description, String url) {
|
||||
this.name = name;
|
||||
|
@ -29,18 +29,21 @@ import java.io.*;
|
||||
import java.net.URL;
|
||||
import java.util.ArrayList;
|
||||
import java.util.List;
|
||||
import java.util.Objects;
|
||||
import java.util.logging.Level;
|
||||
import java.util.stream.Collectors;
|
||||
|
||||
public class GithubScriptManager {
|
||||
|
||||
private JavascriptExpansion expansion;
|
||||
private String javascriptsFolder;
|
||||
private final String JAVASCRIPTS_FOLDER;
|
||||
private List<GithubScript> availableScripts;
|
||||
private final String MASTER_LIST_URL = "https://raw.githubusercontent.com/PlaceholderAPI/Javascript-Expansion/master/scripts/master_list.json";
|
||||
private final Gson GSON = new Gson();
|
||||
|
||||
public GithubScriptManager(JavascriptExpansion expansion) {
|
||||
this.expansion = expansion;
|
||||
javascriptsFolder = expansion.getPlaceholderAPI().getDataFolder()
|
||||
JAVASCRIPTS_FOLDER = expansion.getPlaceholderAPI().getDataFolder()
|
||||
+ File.separator
|
||||
+ "javascripts"
|
||||
+ File.separator;
|
||||
@ -51,42 +54,37 @@ public class GithubScriptManager {
|
||||
}
|
||||
|
||||
public void fetch() {
|
||||
Bukkit.getScheduler().runTaskAsynchronously(expansion.getPlaceholderAPI(), new Runnable() {
|
||||
@Override
|
||||
public void run() {
|
||||
String json = getContents(MASTER_LIST_URL);
|
||||
Bukkit.getScheduler().runTaskAsynchronously(expansion.getPlaceholderAPI(), () -> {
|
||||
final String json = getContents(MASTER_LIST_URL);
|
||||
|
||||
if (json.isEmpty()) {
|
||||
return;
|
||||
}
|
||||
availableScripts = GSON.fromJson(json, new TypeToken<ArrayList<GithubScript>>() {
|
||||
}.getType());
|
||||
}
|
||||
|
||||
availableScripts = GSON.fromJson(json, new TypeToken<ArrayList<GithubScript>>() {}.getType());
|
||||
});
|
||||
}
|
||||
|
||||
@SuppressWarnings("ResultOfMethodCallIgnored")
|
||||
public void downloadScript(GithubScript script) {
|
||||
Bukkit.getScheduler().runTaskAsynchronously(expansion.getPlaceholderAPI(), new Runnable() {
|
||||
@Override
|
||||
public void run() {
|
||||
List<String> contents = read(script.getUrl());
|
||||
if (contents == null || contents.isEmpty()) {
|
||||
Bukkit.getScheduler().runTaskAsynchronously(expansion.getPlaceholderAPI(), () -> {
|
||||
final List<String> contents = read(script.getUrl());
|
||||
|
||||
if (contents.isEmpty()) {
|
||||
return;
|
||||
}
|
||||
File f = new File(javascriptsFolder, script.getName() + ".js");
|
||||
try (PrintStream out = new PrintStream(new FileOutputStream(f))) {
|
||||
contents.forEach(l -> out.println(l));
|
||||
|
||||
try (final PrintStream out = new PrintStream(new FileOutputStream(new File(JAVASCRIPTS_FOLDER, script.getName() + ".js")))) {
|
||||
contents.forEach(out::println);
|
||||
} catch (FileNotFoundException e) {
|
||||
e.printStackTrace();
|
||||
expansion.getPlaceholderAPI().getLogger().log(Level.SEVERE, "An error occurred while downloading " + script.getName(), e);
|
||||
return;
|
||||
}
|
||||
Bukkit.getScheduler().runTask(expansion.getPlaceholderAPI(), new Runnable() {
|
||||
@Override
|
||||
public void run() {
|
||||
|
||||
Bukkit.getScheduler().runTask(expansion.getPlaceholderAPI(), () -> {
|
||||
expansion.getConfig().load().set(script.getName() + ".file", script.getName() + ".js");
|
||||
expansion.getConfig().save();
|
||||
}
|
||||
});
|
||||
}
|
||||
});
|
||||
}
|
||||
|
||||
@ -94,17 +92,19 @@ public class GithubScriptManager {
|
||||
return String.join("", read(url));
|
||||
}
|
||||
|
||||
private List<String> read(String url) {
|
||||
|
||||
List<String> lines = new ArrayList<>();
|
||||
|
||||
try (BufferedReader reader = new BufferedReader(
|
||||
new InputStreamReader(new URL(url).openStream()))) {
|
||||
private List<String> read(final String url) {
|
||||
final List<String> lines = new ArrayList<>();
|
||||
|
||||
try (BufferedReader reader = new BufferedReader(new InputStreamReader(new URL(url).openStream()))) {
|
||||
lines.addAll(reader.lines().filter(Objects::nonNull).collect(Collectors.toList()));
|
||||
/*
|
||||
String inputLine;
|
||||
|
||||
while ((inputLine = reader.readLine()) != null) {
|
||||
lines.add(inputLine);
|
||||
}
|
||||
|
||||
*/
|
||||
} catch (Exception ex) {
|
||||
ex.printStackTrace();
|
||||
}
|
||||
@ -116,10 +116,18 @@ public class GithubScriptManager {
|
||||
return availableScripts;
|
||||
}
|
||||
|
||||
public GithubScript getScript(String name) {
|
||||
if (availableScripts == null) return null;
|
||||
return availableScripts.stream().filter(s -> {
|
||||
return s.getName().equalsIgnoreCase(name);
|
||||
}).findFirst().orElse(null);
|
||||
public GithubScript getScript(final String name) {
|
||||
if (availableScripts == null) {
|
||||
return null;
|
||||
}
|
||||
|
||||
return availableScripts.stream()
|
||||
.filter(s -> s.getName().equalsIgnoreCase(name))
|
||||
.findFirst()
|
||||
.orElse(null);
|
||||
}
|
||||
|
||||
public String getJavascriptsFolder() {
|
||||
return JAVASCRIPTS_FOLDER;
|
||||
}
|
||||
}
|
||||
|
Loading…
x
Reference in New Issue
Block a user