mirror of
https://github.com/PlaceholderAPI/Javascript-Expansion.git
synced 2025-05-23 18:42:44 +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>
|
<modelVersion>4.0.0</modelVersion>
|
||||||
<groupId>com.extendedclip.papi.expansion.javascript</groupId>
|
<groupId>com.extendedclip.papi.expansion.javascript</groupId>
|
||||||
<artifactId>javascript-expansion</artifactId>
|
<artifactId>javascript-expansion</artifactId>
|
||||||
<version>1.5.3</version>
|
<version>1.5.4</version>
|
||||||
<name>PAPI-Expansion-Javascript</name>
|
<name>PAPI-Expansion-Javascript</name>
|
||||||
<description>PlaceholderAPI expansion for javascript placeholders</description>
|
<description>PlaceholderAPI expansion for javascript placeholders</description>
|
||||||
|
|
||||||
|
@ -21,16 +21,6 @@
|
|||||||
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 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.Cacheable;
|
||||||
import me.clip.placeholderapi.expansion.Configurable;
|
import me.clip.placeholderapi.expansion.Configurable;
|
||||||
import me.clip.placeholderapi.expansion.PlaceholderExpansion;
|
import me.clip.placeholderapi.expansion.PlaceholderExpansion;
|
||||||
@ -38,222 +28,232 @@ import org.bukkit.Bukkit;
|
|||||||
import org.bukkit.OfflinePlayer;
|
import org.bukkit.OfflinePlayer;
|
||||||
import org.bukkit.command.CommandMap;
|
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 {
|
public class JavascriptExpansion extends PlaceholderExpansion implements Cacheable, Configurable {
|
||||||
|
|
||||||
private ScriptEngine globalEngine = null;
|
private ScriptEngine globalEngine = null;
|
||||||
|
|
||||||
private JavascriptPlaceholdersConfig config;
|
private JavascriptPlaceholdersConfig config;
|
||||||
private final Set<JavascriptPlaceholder> scripts = new HashSet<>();
|
private final Set<JavascriptPlaceholder> scripts = new HashSet<>();
|
||||||
private final String VERSION = getClass().getPackage().getImplementationVersion();
|
private final String VERSION = getClass().getPackage().getImplementationVersion();
|
||||||
private static JavascriptExpansion instance;
|
private static JavascriptExpansion instance;
|
||||||
private boolean debug;
|
private boolean debug;
|
||||||
private GithubScriptManager githubManager;
|
private GithubScriptManager githubManager;
|
||||||
private JavascriptExpansionCommands commands;
|
private JavascriptExpansionCommands commands;
|
||||||
private CommandMap commandMap;
|
private CommandMap commandMap;
|
||||||
|
|
||||||
public JavascriptExpansion() {
|
public JavascriptExpansion() {
|
||||||
instance = this;
|
instance = this;
|
||||||
try {
|
try {
|
||||||
final Field f = Bukkit.getServer().getClass().getDeclaredField("commandMap");
|
final Field f = Bukkit.getServer().getClass().getDeclaredField("commandMap");
|
||||||
f.setAccessible(true);
|
f.setAccessible(true);
|
||||||
commandMap = (CommandMap) f.get(Bukkit.getServer());
|
commandMap = (CommandMap) f.get(Bukkit.getServer());
|
||||||
} catch (Exception e) {
|
} catch (Exception e) {
|
||||||
e.printStackTrace();
|
e.printStackTrace();
|
||||||
}
|
}
|
||||||
}
|
|
||||||
|
|
||||||
@Override
|
|
||||||
public String getAuthor() {
|
|
||||||
return "clip";
|
|
||||||
}
|
|
||||||
|
|
||||||
@Override
|
|
||||||
public String getIdentifier() {
|
|
||||||
return "javascript";
|
|
||||||
}
|
|
||||||
|
|
||||||
@Override
|
|
||||||
public String getPlugin() {
|
|
||||||
return null;
|
|
||||||
}
|
|
||||||
|
|
||||||
@Override
|
|
||||||
public String getVersion() {
|
|
||||||
return VERSION;
|
|
||||||
}
|
|
||||||
|
|
||||||
@Override
|
|
||||||
public boolean register() {
|
|
||||||
if (globalEngine == null) {
|
|
||||||
try {
|
|
||||||
globalEngine = new ScriptEngineManager().getEngineByName(getString("engine", "nashorn"));
|
|
||||||
} catch (NullPointerException ex) {
|
|
||||||
getPlaceholderAPI().getLogger()
|
|
||||||
.warning("Javascript engine type was invalid! Defaulting to 'nashorn'");
|
|
||||||
globalEngine = new ScriptEngineManager().getEngineByName("nashorn");
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
|
||||||
debug = (boolean) get("debug", false);
|
@Override
|
||||||
config = new JavascriptPlaceholdersConfig(this);
|
public String getAuthor() {
|
||||||
config.loadPlaceholders();
|
return "clip";
|
||||||
|
|
||||||
if (debug) {
|
|
||||||
System.out.println("Java version: " + System.getProperty("java.version"));
|
|
||||||
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(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)) {
|
@Override
|
||||||
githubManager = new GithubScriptManager(this);
|
public String getIdentifier() {
|
||||||
githubManager.fetch();
|
return "javascript";
|
||||||
}
|
}
|
||||||
|
|
||||||
registerCommand();
|
@Override
|
||||||
return super.register();
|
public String getPlugin() {
|
||||||
}
|
return null;
|
||||||
|
|
||||||
@Override
|
|
||||||
public void clear() {
|
|
||||||
unregisterCommand();
|
|
||||||
scripts.forEach(s -> {
|
|
||||||
s.saveData();
|
|
||||||
s.cleanup();
|
|
||||||
});
|
|
||||||
|
|
||||||
if (githubManager != null) {
|
|
||||||
githubManager.clear();
|
|
||||||
githubManager = null;
|
|
||||||
}
|
}
|
||||||
|
|
||||||
scripts.clear();
|
@Override
|
||||||
globalEngine = null;
|
public String getVersion() {
|
||||||
instance = null;
|
return VERSION;
|
||||||
}
|
|
||||||
|
|
||||||
@Override
|
|
||||||
public String onRequest(OfflinePlayer p, String identifier) {
|
|
||||||
if (p == null) {
|
|
||||||
return "";
|
|
||||||
}
|
}
|
||||||
|
|
||||||
if (scripts.isEmpty()) {
|
@Override
|
||||||
return null;
|
public boolean register() {
|
||||||
|
if (globalEngine == null) {
|
||||||
|
try {
|
||||||
|
globalEngine = new ScriptEngineManager().getEngineByName(getString("engine", "nashorn"));
|
||||||
|
} catch (NullPointerException ex) {
|
||||||
|
getPlaceholderAPI().getLogger().warning("Javascript engine type was invalid! Defaulting to 'nashorn'");
|
||||||
|
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"));
|
||||||
|
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(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)) {
|
||||||
|
githubManager = new GithubScriptManager(this);
|
||||||
|
githubManager.fetch();
|
||||||
|
}
|
||||||
|
|
||||||
|
registerCommand();
|
||||||
|
return super.register();
|
||||||
}
|
}
|
||||||
|
|
||||||
for (JavascriptPlaceholder script : scripts) {
|
@Override
|
||||||
if (identifier.startsWith(script.getIdentifier() + "_")) {
|
public void clear() {
|
||||||
identifier = identifier.replace(script.getIdentifier() + "_", "");
|
unregisterCommand();
|
||||||
return !identifier.contains(",") ? script.evaluate(p, identifier)
|
scripts.forEach(s -> {
|
||||||
: script.evaluate(p, identifier.split(","));
|
s.saveData();
|
||||||
} else if (identifier.equalsIgnoreCase(script.getIdentifier())) {
|
s.cleanup();
|
||||||
return script.evaluate(p);
|
});
|
||||||
}
|
|
||||||
}
|
|
||||||
return null;
|
|
||||||
}
|
|
||||||
|
|
||||||
public boolean addJSPlaceholder(JavascriptPlaceholder p) {
|
if (githubManager != null) {
|
||||||
if (p == null) {
|
githubManager.clear();
|
||||||
return false;
|
githubManager = null;
|
||||||
|
}
|
||||||
|
|
||||||
|
scripts.clear();
|
||||||
|
globalEngine = null;
|
||||||
|
instance = null;
|
||||||
}
|
}
|
||||||
|
|
||||||
if (scripts.isEmpty()) {
|
@Override
|
||||||
scripts.add(p);
|
public String onRequest(OfflinePlayer p, String identifier) {
|
||||||
return true;
|
if (p == null) {
|
||||||
|
return "";
|
||||||
|
}
|
||||||
|
|
||||||
|
if (scripts.isEmpty()) {
|
||||||
|
return null;
|
||||||
|
}
|
||||||
|
|
||||||
|
for (JavascriptPlaceholder script : scripts) {
|
||||||
|
if (identifier.startsWith(script.getIdentifier() + "_")) {
|
||||||
|
identifier = identifier.replace(script.getIdentifier() + "_", "");
|
||||||
|
return !identifier.contains(",") ? script.evaluate(p, identifier)
|
||||||
|
: script.evaluate(p, identifier.split(","));
|
||||||
|
} else if (identifier.equalsIgnoreCase(script.getIdentifier())) {
|
||||||
|
return script.evaluate(p);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
return null;
|
||||||
}
|
}
|
||||||
|
|
||||||
if (scripts.stream().filter(s -> s.getIdentifier().equalsIgnoreCase(p.getIdentifier()))
|
public boolean addJSPlaceholder(JavascriptPlaceholder p) {
|
||||||
.findFirst().orElse(null) != null) {
|
if (p == null) {
|
||||||
return false;
|
return false;
|
||||||
|
}
|
||||||
|
|
||||||
|
if (scripts.isEmpty()) {
|
||||||
|
scripts.add(p);
|
||||||
|
return true;
|
||||||
|
}
|
||||||
|
|
||||||
|
if (scripts.stream().filter(s -> s.getIdentifier().equalsIgnoreCase(p.getIdentifier()))
|
||||||
|
.findFirst().orElse(null) != null) {
|
||||||
|
return false;
|
||||||
|
}
|
||||||
|
|
||||||
|
scripts.add(p);
|
||||||
|
return true;
|
||||||
}
|
}
|
||||||
|
|
||||||
scripts.add(p);
|
public Set<JavascriptPlaceholder> getJSPlaceholders() {
|
||||||
return true;
|
return scripts;
|
||||||
}
|
|
||||||
|
|
||||||
public Set<JavascriptPlaceholder> getJSPlaceholders() {
|
|
||||||
return scripts;
|
|
||||||
}
|
|
||||||
|
|
||||||
public List<String> getLoadedIdentifiers() {
|
|
||||||
List<String> l = new ArrayList<>();
|
|
||||||
scripts.forEach(s -> l.add(s.getIdentifier()));
|
|
||||||
return l;
|
|
||||||
}
|
|
||||||
|
|
||||||
public JavascriptPlaceholder getJSPlaceholder(String identifier) {
|
|
||||||
return scripts.stream().filter(s -> s.getIdentifier().equalsIgnoreCase(identifier)).findFirst()
|
|
||||||
.orElse(null);
|
|
||||||
}
|
|
||||||
|
|
||||||
public int getAmountLoaded() {
|
|
||||||
return scripts.size();
|
|
||||||
}
|
|
||||||
|
|
||||||
public ScriptEngine getGlobalEngine() {
|
|
||||||
return globalEngine;
|
|
||||||
}
|
|
||||||
|
|
||||||
public JavascriptPlaceholdersConfig getConfig() {
|
|
||||||
return config;
|
|
||||||
}
|
|
||||||
|
|
||||||
@Override
|
|
||||||
public Map<String, Object> getDefaults() {
|
|
||||||
Map<String, Object> def = new HashMap<>();
|
|
||||||
def.put("engine", "javascript");
|
|
||||||
def.put("debug", false);
|
|
||||||
def.put("github_script_downloads", false);
|
|
||||||
return def;
|
|
||||||
}
|
|
||||||
|
|
||||||
protected int reloadScripts() {
|
|
||||||
scripts.forEach(s -> {
|
|
||||||
s.saveData();
|
|
||||||
s.cleanup();
|
|
||||||
});
|
|
||||||
scripts.clear();
|
|
||||||
config.reload();
|
|
||||||
return config.loadPlaceholders();
|
|
||||||
}
|
|
||||||
|
|
||||||
public static JavascriptExpansion getInstance() {
|
|
||||||
return instance;
|
|
||||||
}
|
|
||||||
|
|
||||||
public GithubScriptManager getGithubScriptManager() {
|
|
||||||
return githubManager;
|
|
||||||
}
|
|
||||||
|
|
||||||
public void setGithubScriptManager(GithubScriptManager manager) {
|
|
||||||
this.githubManager = manager;
|
|
||||||
}
|
|
||||||
|
|
||||||
private boolean unregisterCommand() {
|
|
||||||
if (commandMap == null || commands == null) {
|
|
||||||
return false;
|
|
||||||
}
|
}
|
||||||
return commands.unregister(commandMap);
|
|
||||||
}
|
|
||||||
|
|
||||||
private boolean registerCommand() {
|
public List<String> getLoadedIdentifiers() {
|
||||||
if (commandMap == null) {
|
List<String> l = new ArrayList<>();
|
||||||
return false;
|
scripts.forEach(s -> l.add(s.getIdentifier()));
|
||||||
|
return l;
|
||||||
|
}
|
||||||
|
|
||||||
|
public JavascriptPlaceholder getJSPlaceholder(String identifier) {
|
||||||
|
return scripts.stream()
|
||||||
|
.filter(s -> s.getIdentifier().equalsIgnoreCase(identifier))
|
||||||
|
.findFirst()
|
||||||
|
.orElse(null);
|
||||||
|
}
|
||||||
|
|
||||||
|
public int getAmountLoaded() {
|
||||||
|
return scripts.size();
|
||||||
|
}
|
||||||
|
|
||||||
|
public ScriptEngine getGlobalEngine() {
|
||||||
|
return globalEngine;
|
||||||
|
}
|
||||||
|
|
||||||
|
public JavascriptPlaceholdersConfig getConfig() {
|
||||||
|
return config;
|
||||||
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public Map<String, Object> getDefaults() {
|
||||||
|
Map<String, Object> def = new HashMap<>();
|
||||||
|
def.put("engine", "javascript");
|
||||||
|
def.put("debug", false);
|
||||||
|
def.put("github_script_downloads", false);
|
||||||
|
return def;
|
||||||
|
}
|
||||||
|
|
||||||
|
protected int reloadScripts() {
|
||||||
|
scripts.forEach(s -> {
|
||||||
|
s.saveData();
|
||||||
|
s.cleanup();
|
||||||
|
});
|
||||||
|
scripts.clear();
|
||||||
|
config.reload();
|
||||||
|
return config.loadPlaceholders();
|
||||||
|
}
|
||||||
|
|
||||||
|
public static JavascriptExpansion getInstance() {
|
||||||
|
return instance;
|
||||||
|
}
|
||||||
|
|
||||||
|
public GithubScriptManager getGithubScriptManager() {
|
||||||
|
return githubManager;
|
||||||
|
}
|
||||||
|
|
||||||
|
public void setGithubScriptManager(GithubScriptManager manager) {
|
||||||
|
this.githubManager = manager;
|
||||||
|
}
|
||||||
|
|
||||||
|
@SuppressWarnings("UnusedReturnValue")
|
||||||
|
private boolean unregisterCommand() {
|
||||||
|
if (commandMap == null || commands == null) {
|
||||||
|
return false;
|
||||||
|
}
|
||||||
|
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();
|
||||||
}
|
}
|
||||||
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.GithubScript;
|
||||||
import com.extendedclip.papi.expansion.javascript.cloud.GithubScriptManager;
|
import com.extendedclip.papi.expansion.javascript.cloud.GithubScriptManager;
|
||||||
import me.clip.placeholderapi.PlaceholderAPIPlugin;
|
import me.clip.placeholderapi.PlaceholderAPIPlugin;
|
||||||
|
import org.bukkit.Bukkit;
|
||||||
import org.bukkit.ChatColor;
|
import org.bukkit.ChatColor;
|
||||||
|
import org.bukkit.OfflinePlayer;
|
||||||
import org.bukkit.command.Command;
|
import org.bukkit.command.Command;
|
||||||
import org.bukkit.command.CommandSender;
|
import org.bukkit.command.CommandSender;
|
||||||
|
import org.bukkit.entity.Player;
|
||||||
|
import org.bukkit.util.StringUtil;
|
||||||
|
|
||||||
import java.util.Arrays;
|
import java.io.File;
|
||||||
import java.util.List;
|
import java.util.*;
|
||||||
import java.util.Set;
|
|
||||||
import java.util.stream.Collectors;
|
import java.util.stream.Collectors;
|
||||||
|
|
||||||
public class JavascriptExpansionCommands extends Command {
|
public class JavascriptExpansionCommands extends Command {
|
||||||
|
|
||||||
private JavascriptExpansion expansion;
|
private final JavascriptExpansion expansion;
|
||||||
private final String PERMISSION = "placeholderapi.js.admin";
|
private final String PERMISSION = "placeholderapi.js.admin";
|
||||||
private String command;
|
private final String command;
|
||||||
|
|
||||||
public JavascriptExpansionCommands(JavascriptExpansion expansion) {
|
public JavascriptExpansionCommands(JavascriptExpansion expansion) {
|
||||||
super("jsexpansion");
|
super("jsexpansion");
|
||||||
@ -47,6 +50,44 @@ public class JavascriptExpansionCommands extends Command {
|
|||||||
this.setPermission(PERMISSION);
|
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
|
@Override
|
||||||
public boolean execute(CommandSender sender, String label, String[] args) {
|
public boolean execute(CommandSender sender, String label, String[] args) {
|
||||||
if (!sender.hasPermission(PERMISSION)) {
|
if (!sender.hasPermission(PERMISSION)) {
|
||||||
@ -60,16 +101,17 @@ public class JavascriptExpansionCommands extends Command {
|
|||||||
"&eCreated by: &f" + expansion.getAuthor(),
|
"&eCreated by: &f" + expansion.getAuthor(),
|
||||||
"&eWiki: &fhttps://github.com/PlaceholderAPI/Javascript-Expansion/wiki",
|
"&eWiki: &fhttps://github.com/PlaceholderAPI/Javascript-Expansion/wiki",
|
||||||
"&r",
|
"&r",
|
||||||
"&e/" + command + " reload &7- &fReload your javascripts without reloading PlaceholderAPI",
|
"&e/" + command + " reload &7- &fReload your javascripts without reloading PlaceholderAPI.",
|
||||||
"&e/" + command + " list &7- &fList loaded script identifiers."
|
"&e/" + command + " list &7- &fList loaded script identifiers.",
|
||||||
|
"&e/" + command + " parse [me/player] [code] &7- &fTest JavaScript code in chat."
|
||||||
);
|
);
|
||||||
|
|
||||||
if (expansion.getGithubScriptManager() != null) {
|
if (expansion.getGithubScriptManager() != null) {
|
||||||
msg(sender,
|
msg(sender,
|
||||||
"&e/" + command + " git refresh &7- &fRefresh available Github scripts",
|
"&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 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()) {
|
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": {
|
case "git": {
|
||||||
if (expansion.getGithubScriptManager() == null) {
|
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;
|
return true;
|
||||||
}
|
}
|
||||||
|
|
||||||
if (args.length < 2) {
|
if (args.length < 2) {
|
||||||
msg(sender, "&cIncorrect usage!");
|
msg(sender, "&cIncorrect usage! Type '&f/" + command + "&c' for more help.");
|
||||||
return true;
|
return true;
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -123,14 +149,14 @@ public class JavascriptExpansionCommands extends Command {
|
|||||||
|
|
||||||
case "info": {
|
case "info": {
|
||||||
if (args.length < 3) {
|
if (args.length < 3) {
|
||||||
msg(sender, "&4Incorrect usage! &f/" + command + " git info <name>");
|
msg(sender, "&cIncorrect usage! &f/" + command + " git info [name]");
|
||||||
return true;
|
return true;
|
||||||
}
|
}
|
||||||
|
|
||||||
final GithubScript script = manager.getScript(args[2]);
|
final GithubScript script = manager.getScript(args[2]);
|
||||||
|
|
||||||
if (script == null) {
|
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;
|
return true;
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -146,54 +172,108 @@ public class JavascriptExpansionCommands extends Command {
|
|||||||
|
|
||||||
case "download": {
|
case "download": {
|
||||||
if (args.length < 3) {
|
if (args.length < 3) {
|
||||||
msg(sender, "&4Incorrect usage! &f/" + command + " git download <name>");
|
msg(sender, "&cIncorrect usage! &f/" + command + " git download [name]");
|
||||||
return true;
|
return true;
|
||||||
}
|
}
|
||||||
|
|
||||||
final GithubScript script = manager.getScript(args[2]);
|
final GithubScript script = manager.getScript(args[2]);
|
||||||
|
|
||||||
if (script == null) {
|
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;
|
return true;
|
||||||
}
|
}
|
||||||
|
|
||||||
manager.downloadScript(script);
|
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;
|
return true;
|
||||||
}
|
}
|
||||||
|
|
||||||
case "enabled":
|
case "enabled":
|
||||||
if (args.length < 3) {
|
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;
|
return true;
|
||||||
}
|
}
|
||||||
|
|
||||||
Boolean enabled = Boolean.parseBoolean(args[2]);
|
final boolean enabled = Boolean.parseBoolean(args[2]);
|
||||||
PlaceholderAPIPlugin papi = expansion.getPlaceholderAPI();
|
final PlaceholderAPIPlugin papi = expansion.getPlaceholderAPI();
|
||||||
papi.getConfig().set("expansions." + this.getName() + ".github_script_downloads", enabled);
|
|
||||||
papi.saveConfig();
|
papi.getConfig().set("expansions." + this.getName() + ".github_script_downloads", enabled);
|
||||||
papi.reloadConfig();
|
papi.saveConfig();
|
||||||
if (!enabled) {
|
papi.reloadConfig();
|
||||||
if (expansion.getGithubScriptManager() != null) {
|
|
||||||
expansion.getGithubScriptManager().clear();
|
if (!enabled) {
|
||||||
expansion.setGithubScriptManager(null);
|
if (expansion.getGithubScriptManager() != null) {
|
||||||
}
|
expansion.getGithubScriptManager().clear();
|
||||||
} else {
|
expansion.setGithubScriptManager(null);
|
||||||
if (expansion.getGithubScriptManager() == null) {
|
}
|
||||||
expansion.setGithubScriptManager(new GithubScriptManager(expansion));
|
} else {
|
||||||
}
|
if (expansion.getGithubScriptManager() == null) {
|
||||||
expansion.getGithubScriptManager().fetch();
|
expansion.setGithubScriptManager(new GithubScriptManager(expansion));
|
||||||
}
|
}
|
||||||
msg(sender, "&6Git script downloads set to: &e" + enabled);
|
expansion.getGithubScriptManager().fetch();
|
||||||
return true;
|
}
|
||||||
|
|
||||||
|
msg(sender, "&6Git script downloads set to: &e" + enabled);
|
||||||
|
return true;
|
||||||
|
|
||||||
default: {
|
default: {
|
||||||
msg(sender, "&4Incorrect usage! &f/" + command + " &7for more help.");
|
msg(sender, "&cIncorrect usage! Type '&f/" + command + "&c' for more help.");
|
||||||
return true;
|
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: {
|
default: {
|
||||||
return true;
|
return true;
|
||||||
}
|
}
|
||||||
@ -209,6 +289,6 @@ public class JavascriptExpansionCommands extends Command {
|
|||||||
return;
|
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.Bukkit;
|
||||||
import org.bukkit.OfflinePlayer;
|
import org.bukkit.OfflinePlayer;
|
||||||
import org.bukkit.configuration.InvalidConfigurationException;
|
import org.bukkit.configuration.InvalidConfigurationException;
|
||||||
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.ScriptEngine;
|
||||||
@ -34,16 +33,17 @@ import javax.script.ScriptException;
|
|||||||
import java.io.File;
|
import java.io.File;
|
||||||
import java.io.IOException;
|
import java.io.IOException;
|
||||||
import java.util.Set;
|
import java.util.Set;
|
||||||
|
import java.util.logging.Level;
|
||||||
|
|
||||||
public class JavascriptPlaceholder {
|
public class JavascriptPlaceholder {
|
||||||
|
|
||||||
private final String DIRECTORY = PlaceholderAPIPlugin.getInstance().getDataFolder() + "/javascripts/javascript_data";
|
private final String DIRECTORY = PlaceholderAPIPlugin.getInstance().getDataFolder() + "/javascripts/javascript_data";
|
||||||
private ScriptEngine engine;
|
private final ScriptEngine engine;
|
||||||
private String identifier;
|
private final String identifier;
|
||||||
private String script;
|
private final String script;
|
||||||
private ScriptData data;
|
private ScriptData scriptData;
|
||||||
private File dataFile;
|
private final File dataFile;
|
||||||
private FileConfiguration config;
|
private YamlConfiguration yaml;
|
||||||
|
|
||||||
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");
|
||||||
@ -55,13 +55,13 @@ public class JavascriptPlaceholder {
|
|||||||
this.script = script;
|
this.script = script;
|
||||||
final File directory = new File(DIRECTORY);
|
final File directory = new File(DIRECTORY);
|
||||||
|
|
||||||
if (directory.exists()) {
|
if (!directory.exists()) {
|
||||||
directory.mkdirs();
|
directory.mkdirs();
|
||||||
}
|
}
|
||||||
|
|
||||||
data = new ScriptData();
|
scriptData = new ScriptData();
|
||||||
dataFile = new File(DIRECTORY, identifier + "_data.yml");
|
dataFile = new File(directory, identifier + "_data.yml");
|
||||||
engine.put("Data", data);
|
engine.put("Data", scriptData);
|
||||||
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);
|
||||||
@ -99,60 +99,70 @@ public class JavascriptPlaceholder {
|
|||||||
}
|
}
|
||||||
|
|
||||||
engine.put("args", arguments);
|
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);
|
engine.put("OfflinePlayer", player);
|
||||||
Object result = engine.eval(exp);
|
Object result = engine.eval(exp);
|
||||||
return result != null ? PlaceholderAPI.setBracketPlaceholders(player, result.toString()) : "";
|
return result != null ? PlaceholderAPI.setBracketPlaceholders(player, result.toString()) : "";
|
||||||
} catch (ScriptException ex) {
|
} catch (ScriptException ex) {
|
||||||
System.out.println(ex.getMessage());
|
PlaceholderAPIPlugin.getInstance().getLogger().log(Level.SEVERE, "[JavaScript] An error occurred while executing the script '" + identifier + "'", ex);
|
||||||
ex.printStackTrace();
|
|
||||||
}
|
}
|
||||||
|
|
||||||
return "Script error";
|
return "Script error! (check console)";
|
||||||
}
|
}
|
||||||
|
|
||||||
public ScriptData getData() {
|
public ScriptData getData() {
|
||||||
// this should never be null but just in case setData(null) is called
|
// this should never be null but just in case setData(null) is called
|
||||||
if (data == null) {
|
if (scriptData == null) {
|
||||||
data = new ScriptData();
|
scriptData = new ScriptData();
|
||||||
}
|
}
|
||||||
return data;
|
return scriptData;
|
||||||
}
|
}
|
||||||
|
|
||||||
public void setData(ScriptData data) {
|
public void setData(ScriptData data) {
|
||||||
this.data = data;
|
this.scriptData = data;
|
||||||
}
|
}
|
||||||
|
|
||||||
public boolean loadData() {
|
public boolean loadData() {
|
||||||
config = new YamlConfiguration();
|
yaml = new YamlConfiguration();
|
||||||
|
dataFile.getParentFile().mkdirs();
|
||||||
|
|
||||||
if (!dataFile.exists()) {
|
if (!dataFile.exists()) {
|
||||||
return false;
|
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 {
|
try {
|
||||||
config.load(dataFile);
|
yaml.load(dataFile);
|
||||||
} catch (IOException | InvalidConfigurationException e) {
|
} catch (IOException | InvalidConfigurationException e) {
|
||||||
e.printStackTrace();
|
PlaceholderAPIPlugin.getInstance().getLogger().log(Level.SEVERE, "[JavaScript Expansion] An error occurred while loading for " + getIdentifier(), e);
|
||||||
return false;
|
return false;
|
||||||
}
|
}
|
||||||
|
|
||||||
final Set<String> keys = config.getKeys(true);
|
final Set<String> keys = yaml.getKeys(true);
|
||||||
|
|
||||||
if (keys.size() == 0) {
|
if (keys.size() == 0) {
|
||||||
return false;
|
return false;
|
||||||
}
|
}
|
||||||
|
|
||||||
if (data == null) {
|
if (scriptData == null) {
|
||||||
data = new ScriptData();
|
scriptData = new ScriptData();
|
||||||
} else {
|
} 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()) {
|
if (!scriptData.isEmpty()) {
|
||||||
this.setData(data);
|
this.setData(scriptData);
|
||||||
return true;
|
return true;
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -160,30 +170,27 @@ public class JavascriptPlaceholder {
|
|||||||
}
|
}
|
||||||
|
|
||||||
public boolean saveData() {
|
public boolean saveData() {
|
||||||
if (data == null || data.isEmpty()) {
|
if (scriptData == null || scriptData.isEmpty() || yaml == null) {
|
||||||
return false;
|
return false;
|
||||||
}
|
}
|
||||||
|
|
||||||
if (config == null) {
|
scriptData.getData().forEach((key, value) -> yaml.set(key, value));
|
||||||
return false;
|
|
||||||
}
|
|
||||||
|
|
||||||
data.getData().forEach((key, value) -> config.set(key, value));
|
|
||||||
|
|
||||||
try {
|
try {
|
||||||
config.save(dataFile);
|
yaml.save(dataFile);
|
||||||
return true;
|
return true;
|
||||||
} catch (IOException e) {
|
} catch (IOException e) {
|
||||||
|
PlaceholderAPIPlugin.getInstance().getLogger().log(Level.SEVERE, "[JavaScript Expansion] An error occurred while saving data for " + getIdentifier(), e);
|
||||||
return false;
|
return false;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
public void cleanup() {
|
public void cleanup() {
|
||||||
if (this.data != null) {
|
if (this.scriptData != null) {
|
||||||
this.data.clear();
|
this.scriptData.clear();
|
||||||
this.data = null;
|
this.scriptData = null;
|
||||||
}
|
}
|
||||||
|
|
||||||
this.config = null;
|
this.yaml = null;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
@ -113,42 +113,42 @@ public class JavascriptPlaceholdersConfig {
|
|||||||
return 0;
|
return 0;
|
||||||
}
|
}
|
||||||
|
|
||||||
File dir = new File(plugin.getDataFolder() + File.separator + "javascripts");
|
final File directory = new File(plugin.getDataFolder(), "javascripts");
|
||||||
|
|
||||||
try {
|
try {
|
||||||
if (!dir.exists()) {
|
if (!directory.exists()) {
|
||||||
dir.mkdirs();
|
directory.mkdirs();
|
||||||
plugin.getLogger().info("Creating directory: plugins/PlaceholderAPI/javascripts");
|
plugin.getLogger().info("[JavaScript Expansion] Creating directory: " + directory.getPath());
|
||||||
}
|
}
|
||||||
} catch (SecurityException e) {
|
} 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)) {
|
for (String identifier : config.getKeys(false)) {
|
||||||
if (!config.contains(identifier + ".file") || config.getString(identifier + ".file") == null) {
|
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;
|
continue;
|
||||||
}
|
}
|
||||||
|
|
||||||
File scriptFile = new File(plugin.getDataFolder() + "/javascripts", config.getString(identifier + ".file"));
|
File scriptFile = new File(plugin.getDataFolder() + "/javascripts", config.getString(identifier + ".file"));
|
||||||
|
|
||||||
if (!scriptFile.exists()) {
|
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 {
|
try {
|
||||||
scriptFile.createNewFile();
|
scriptFile.createNewFile();
|
||||||
plugin.getLogger().info(scriptFile.getName()
|
plugin.getLogger().info("[JavaScript Expansion] " + scriptFile.getName() + " created! Add your javascript to this file and use '/jsexpansion reload' to load it!");
|
||||||
+ " created! Add your javascript to this file and use /placeholderapi reload to load it!");
|
|
||||||
} catch (IOException e) {
|
} catch (IOException e) {
|
||||||
e.printStackTrace();
|
plugin.getLogger().log(Level.SEVERE, "[JavaScript Expansion] An error occurred while creating " + scriptFile.getName(), e);
|
||||||
}
|
}
|
||||||
|
|
||||||
continue;
|
continue;
|
||||||
}
|
}
|
||||||
|
|
||||||
String script = getContents(scriptFile);
|
String script = getContents(scriptFile);
|
||||||
|
|
||||||
if (script == null || script.isEmpty()) {
|
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;
|
continue;
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -160,27 +160,27 @@ public class JavascriptPlaceholdersConfig {
|
|||||||
try {
|
try {
|
||||||
engine = new ScriptEngineManager().getEngineByName(config.getString(identifier + ".engine", "nashorn"));
|
engine = new ScriptEngineManager().getEngineByName(config.getString(identifier + ".engine", "nashorn"));
|
||||||
} catch (NullPointerException e) {
|
} 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();
|
engine = ex.getGlobalEngine();
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
if (engine == null) {
|
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;
|
continue;
|
||||||
}
|
}
|
||||||
|
|
||||||
final JavascriptPlaceholder pl = new JavascriptPlaceholder(engine, identifier, script);
|
final JavascriptPlaceholder placeholder = new JavascriptPlaceholder(engine, identifier, script);
|
||||||
final boolean added = ex.addJSPlaceholder(pl);
|
final boolean added = ex.addJSPlaceholder(placeholder);
|
||||||
|
|
||||||
if (added) {
|
if (added) {
|
||||||
if (pl.loadData()) {
|
if (placeholder.loadData()) {
|
||||||
plugin.getLogger().info("Loaded data for javascript placeholder: " + identifier);
|
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 {
|
} 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 {
|
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) {
|
public GithubScript(String name, String version, String author, String description, String url) {
|
||||||
this.name = name;
|
this.name = name;
|
||||||
|
@ -29,18 +29,21 @@ import java.io.*;
|
|||||||
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 java.util.Objects;
|
||||||
|
import java.util.logging.Level;
|
||||||
|
import java.util.stream.Collectors;
|
||||||
|
|
||||||
public class GithubScriptManager {
|
public class GithubScriptManager {
|
||||||
|
|
||||||
private JavascriptExpansion expansion;
|
private JavascriptExpansion expansion;
|
||||||
private String javascriptsFolder;
|
private final String JAVASCRIPTS_FOLDER;
|
||||||
private List<GithubScript> availableScripts;
|
private List<GithubScript> availableScripts;
|
||||||
private final String MASTER_LIST_URL = "https://raw.githubusercontent.com/PlaceholderAPI/Javascript-Expansion/master/scripts/master_list.json";
|
private final String MASTER_LIST_URL = "https://raw.githubusercontent.com/PlaceholderAPI/Javascript-Expansion/master/scripts/master_list.json";
|
||||||
private final Gson GSON = new Gson();
|
private final Gson GSON = new Gson();
|
||||||
|
|
||||||
public GithubScriptManager(JavascriptExpansion expansion) {
|
public GithubScriptManager(JavascriptExpansion expansion) {
|
||||||
this.expansion = expansion;
|
this.expansion = expansion;
|
||||||
javascriptsFolder = expansion.getPlaceholderAPI().getDataFolder()
|
JAVASCRIPTS_FOLDER = expansion.getPlaceholderAPI().getDataFolder()
|
||||||
+ File.separator
|
+ File.separator
|
||||||
+ "javascripts"
|
+ "javascripts"
|
||||||
+ File.separator;
|
+ File.separator;
|
||||||
@ -51,42 +54,37 @@ public class GithubScriptManager {
|
|||||||
}
|
}
|
||||||
|
|
||||||
public void fetch() {
|
public void fetch() {
|
||||||
Bukkit.getScheduler().runTaskAsynchronously(expansion.getPlaceholderAPI(), new Runnable() {
|
Bukkit.getScheduler().runTaskAsynchronously(expansion.getPlaceholderAPI(), () -> {
|
||||||
@Override
|
final String json = getContents(MASTER_LIST_URL);
|
||||||
public void run() {
|
|
||||||
String json = getContents(MASTER_LIST_URL);
|
if (json.isEmpty()) {
|
||||||
if (json.isEmpty()) {
|
return;
|
||||||
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) {
|
public void downloadScript(GithubScript script) {
|
||||||
Bukkit.getScheduler().runTaskAsynchronously(expansion.getPlaceholderAPI(), new Runnable() {
|
Bukkit.getScheduler().runTaskAsynchronously(expansion.getPlaceholderAPI(), () -> {
|
||||||
@Override
|
final List<String> contents = read(script.getUrl());
|
||||||
public void run() {
|
|
||||||
List<String> contents = read(script.getUrl());
|
if (contents.isEmpty()) {
|
||||||
if (contents == null || contents.isEmpty()) {
|
return;
|
||||||
return;
|
|
||||||
}
|
|
||||||
File f = new File(javascriptsFolder, script.getName() + ".js");
|
|
||||||
try (PrintStream out = new PrintStream(new FileOutputStream(f))) {
|
|
||||||
contents.forEach(l -> out.println(l));
|
|
||||||
} catch (FileNotFoundException e) {
|
|
||||||
e.printStackTrace();
|
|
||||||
return;
|
|
||||||
}
|
|
||||||
Bukkit.getScheduler().runTask(expansion.getPlaceholderAPI(), new Runnable() {
|
|
||||||
@Override
|
|
||||||
public void run() {
|
|
||||||
expansion.getConfig().load().set(script.getName() + ".file", script.getName() + ".js");
|
|
||||||
expansion.getConfig().save();
|
|
||||||
}
|
|
||||||
});
|
|
||||||
}
|
}
|
||||||
|
|
||||||
|
try (final PrintStream out = new PrintStream(new FileOutputStream(new File(JAVASCRIPTS_FOLDER, script.getName() + ".js")))) {
|
||||||
|
contents.forEach(out::println);
|
||||||
|
} catch (FileNotFoundException e) {
|
||||||
|
expansion.getPlaceholderAPI().getLogger().log(Level.SEVERE, "An error occurred while downloading " + script.getName(), e);
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
|
||||||
|
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));
|
return String.join("", read(url));
|
||||||
}
|
}
|
||||||
|
|
||||||
private List<String> read(String url) {
|
private List<String> read(final String url) {
|
||||||
|
final List<String> lines = new ArrayList<>();
|
||||||
List<String> lines = new ArrayList<>();
|
|
||||||
|
|
||||||
try (BufferedReader reader = new BufferedReader(
|
|
||||||
new InputStreamReader(new URL(url).openStream()))) {
|
|
||||||
|
|
||||||
|
try (BufferedReader reader = new BufferedReader(new InputStreamReader(new URL(url).openStream()))) {
|
||||||
|
lines.addAll(reader.lines().filter(Objects::nonNull).collect(Collectors.toList()));
|
||||||
|
/*
|
||||||
String inputLine;
|
String inputLine;
|
||||||
|
|
||||||
while ((inputLine = reader.readLine()) != null) {
|
while ((inputLine = reader.readLine()) != null) {
|
||||||
lines.add(inputLine);
|
lines.add(inputLine);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
*/
|
||||||
} catch (Exception ex) {
|
} catch (Exception ex) {
|
||||||
ex.printStackTrace();
|
ex.printStackTrace();
|
||||||
}
|
}
|
||||||
@ -116,10 +116,18 @@ public class GithubScriptManager {
|
|||||||
return availableScripts;
|
return availableScripts;
|
||||||
}
|
}
|
||||||
|
|
||||||
public GithubScript getScript(String name) {
|
public GithubScript getScript(final String name) {
|
||||||
if (availableScripts == null) return null;
|
if (availableScripts == null) {
|
||||||
return availableScripts.stream().filter(s -> {
|
return null;
|
||||||
return s.getName().equalsIgnoreCase(name);
|
}
|
||||||
}).findFirst().orElse(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