mirror of
https://github.com/PlaceholderAPI/Javascript-Expansion.git
synced 2025-05-23 10:39:04 +00:00
1.5.3
This commit is contained in:
parent
ce18d99e84
commit
d2d26e1e95
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.2</version>
|
||||
<version>1.5.3</version>
|
||||
<name>PAPI-Expansion-Javascript</name>
|
||||
<description>PlaceholderAPI expansion for javascript placeholders</description>
|
||||
|
||||
|
@ -21,6 +21,16 @@
|
||||
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;
|
||||
@ -28,219 +38,222 @@ 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;
|
||||
private ScriptEngine globalEngine = null;
|
||||
|
||||
private JavascriptPlaceholdersConfig config;
|
||||
private final Set<JavascriptPlaceholder> scripts = new HashSet<>();
|
||||
private final String VERSION = getClass().getPackage().getImplementationVersion();
|
||||
private static JavascriptExpansion instance;
|
||||
private boolean debug;
|
||||
private GithubScriptManager githubScripts;
|
||||
private JavascriptExpansionCommands commands;
|
||||
private CommandMap commandMap;
|
||||
private JavascriptPlaceholdersConfig config;
|
||||
private final Set<JavascriptPlaceholder> scripts = new HashSet<>();
|
||||
private final String VERSION = getClass().getPackage().getImplementationVersion();
|
||||
private static JavascriptExpansion instance;
|
||||
private boolean debug;
|
||||
private GithubScriptManager githubManager;
|
||||
private JavascriptExpansionCommands commands;
|
||||
private CommandMap commandMap;
|
||||
|
||||
public JavascriptExpansion() {
|
||||
instance = this;
|
||||
try {
|
||||
final Field f = Bukkit.getServer().getClass().getDeclaredField("commandMap");
|
||||
f.setAccessible(true);
|
||||
commandMap = (CommandMap) f.get(Bukkit.getServer());
|
||||
} catch (Exception e) {
|
||||
e.printStackTrace();
|
||||
}
|
||||
}
|
||||
|
||||
public JavascriptExpansion() {
|
||||
instance = this;
|
||||
try {
|
||||
final Field f = Bukkit.getServer().getClass().getDeclaredField("commandMap");
|
||||
f.setAccessible(true);
|
||||
commandMap = (CommandMap) f.get(Bukkit.getServer());
|
||||
} catch (Exception e) {
|
||||
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");
|
||||
}
|
||||
}
|
||||
|
||||
@Override
|
||||
public String getAuthor() {
|
||||
return "clip";
|
||||
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()));
|
||||
}
|
||||
}
|
||||
|
||||
@Override
|
||||
public String getIdentifier() {
|
||||
return "javascript";
|
||||
if ((Boolean) get("github_script_downloads", false)) {
|
||||
githubManager = new GithubScriptManager(this);
|
||||
githubManager.fetch();
|
||||
}
|
||||
|
||||
@Override
|
||||
public String getPlugin() {
|
||||
return null;
|
||||
registerCommand();
|
||||
return super.register();
|
||||
}
|
||||
|
||||
@Override
|
||||
public void clear() {
|
||||
unregisterCommand();
|
||||
scripts.forEach(s -> {
|
||||
s.saveData();
|
||||
s.cleanup();
|
||||
});
|
||||
|
||||
if (githubManager != null) {
|
||||
githubManager.clear();
|
||||
githubManager = null;
|
||||
}
|
||||
|
||||
@Override
|
||||
public String getVersion() {
|
||||
return VERSION;
|
||||
scripts.clear();
|
||||
globalEngine = null;
|
||||
instance = null;
|
||||
}
|
||||
|
||||
@Override
|
||||
public String onRequest(OfflinePlayer p, String identifier) {
|
||||
if (p == null) {
|
||||
return "";
|
||||
}
|
||||
|
||||
@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);
|
||||
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()));
|
||||
}
|
||||
}
|
||||
|
||||
githubScripts = new GithubScriptManager(this);
|
||||
|
||||
if ((Boolean) get("github_script_downloads", false)) {
|
||||
githubScripts.fetch();
|
||||
}
|
||||
|
||||
registerCommand();
|
||||
return super.register();
|
||||
if (scripts.isEmpty()) {
|
||||
return null;
|
||||
}
|
||||
|
||||
@Override
|
||||
public void clear() {
|
||||
unregisterCommand();
|
||||
scripts.forEach(s -> {
|
||||
s.saveData();
|
||||
s.cleanup();
|
||||
});
|
||||
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 (githubScripts != null) {
|
||||
githubScripts.clear();
|
||||
githubScripts = null;
|
||||
}
|
||||
|
||||
scripts.clear();
|
||||
globalEngine = null;
|
||||
instance = null;
|
||||
public boolean addJSPlaceholder(JavascriptPlaceholder p) {
|
||||
if (p == null) {
|
||||
return false;
|
||||
}
|
||||
|
||||
@Override
|
||||
public String onRequest(OfflinePlayer p, String identifier) {
|
||||
if (p == null) {
|
||||
return "";
|
||||
}
|
||||
|
||||
if (scripts.isEmpty()) {
|
||||
return null;
|
||||
}
|
||||
|
||||
for (JavascriptPlaceholder script : scripts) {
|
||||
if (identifier.startsWith(script.getIdentifier() + "_")) {
|
||||
identifier = identifier.replace(script.getIdentifier() + "_", "");
|
||||
return !identifier.contains(",") ? script.evaluate(p, identifier) : script.evaluate(p, identifier.split(","));
|
||||
} else if (identifier.equalsIgnoreCase(script.getIdentifier())) {
|
||||
return script.evaluate(p);
|
||||
}
|
||||
}
|
||||
return null;
|
||||
if (scripts.isEmpty()) {
|
||||
scripts.add(p);
|
||||
return true;
|
||||
}
|
||||
|
||||
public boolean addJSPlaceholder(JavascriptPlaceholder p) {
|
||||
if (p == null) {
|
||||
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;
|
||||
if (scripts.stream().filter(s -> s.getIdentifier().equalsIgnoreCase(p.getIdentifier()))
|
||||
.findFirst().orElse(null) != null) {
|
||||
return false;
|
||||
}
|
||||
|
||||
public Set<JavascriptPlaceholder> getJSPlaceholders() {
|
||||
return scripts;
|
||||
}
|
||||
scripts.add(p);
|
||||
return true;
|
||||
}
|
||||
|
||||
public List<String> getLoadedIdentifiers() {
|
||||
List<String> l = new ArrayList<>();
|
||||
scripts.forEach(s -> l.add(s.getIdentifier()));
|
||||
return l;
|
||||
}
|
||||
public Set<JavascriptPlaceholder> getJSPlaceholders() {
|
||||
return scripts;
|
||||
}
|
||||
|
||||
public JavascriptPlaceholder getJSPlaceholder(String identifier) {
|
||||
return scripts.stream().filter(s -> s.getIdentifier().equalsIgnoreCase(identifier)).findFirst().orElse(null);
|
||||
}
|
||||
public List<String> getLoadedIdentifiers() {
|
||||
List<String> l = new ArrayList<>();
|
||||
scripts.forEach(s -> l.add(s.getIdentifier()));
|
||||
return l;
|
||||
}
|
||||
|
||||
public int getAmountLoaded() {
|
||||
return scripts.size();
|
||||
}
|
||||
public JavascriptPlaceholder getJSPlaceholder(String identifier) {
|
||||
return scripts.stream().filter(s -> s.getIdentifier().equalsIgnoreCase(identifier)).findFirst()
|
||||
.orElse(null);
|
||||
}
|
||||
|
||||
public ScriptEngine getGlobalEngine() {
|
||||
return globalEngine;
|
||||
}
|
||||
public int getAmountLoaded() {
|
||||
return scripts.size();
|
||||
}
|
||||
|
||||
public JavascriptPlaceholdersConfig getConfig() {
|
||||
return config;
|
||||
}
|
||||
public ScriptEngine getGlobalEngine() {
|
||||
return globalEngine;
|
||||
}
|
||||
|
||||
@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;
|
||||
}
|
||||
public JavascriptPlaceholdersConfig getConfig() {
|
||||
return config;
|
||||
}
|
||||
|
||||
protected int reloadScripts() {
|
||||
scripts.forEach(s -> {
|
||||
s.saveData();
|
||||
s.cleanup();
|
||||
});
|
||||
scripts.clear();
|
||||
config.reload();
|
||||
return config.loadPlaceholders();
|
||||
}
|
||||
@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;
|
||||
}
|
||||
|
||||
public static JavascriptExpansion getInstance() {
|
||||
return instance;
|
||||
}
|
||||
protected int reloadScripts() {
|
||||
scripts.forEach(s -> {
|
||||
s.saveData();
|
||||
s.cleanup();
|
||||
});
|
||||
scripts.clear();
|
||||
config.reload();
|
||||
return config.loadPlaceholders();
|
||||
}
|
||||
|
||||
public GithubScriptManager getGithubScriptManager() {
|
||||
return githubScripts;
|
||||
}
|
||||
public static JavascriptExpansion getInstance() {
|
||||
return instance;
|
||||
}
|
||||
|
||||
private boolean unregisterCommand() {
|
||||
if (commandMap == null || commands == null) return false;
|
||||
return commands.unregister(commandMap);
|
||||
}
|
||||
public GithubScriptManager getGithubScriptManager() {
|
||||
return githubManager;
|
||||
}
|
||||
|
||||
private boolean registerCommand() {
|
||||
if (commandMap == null) return false;
|
||||
commands = new JavascriptExpansionCommands(this);
|
||||
commandMap.register("papi" + commands.getName(), commands);
|
||||
return commands.isRegistered();
|
||||
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() {
|
||||
if (commandMap == null) {
|
||||
return false;
|
||||
}
|
||||
commands = new JavascriptExpansionCommands(this);
|
||||
commandMap.register("papi" + commands.getName(), commands);
|
||||
return commands.isRegistered();
|
||||
}
|
||||
}
|
||||
|
@ -22,6 +22,7 @@ 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.ChatColor;
|
||||
import org.bukkit.command.Command;
|
||||
import org.bukkit.command.CommandSender;
|
||||
@ -161,6 +162,31 @@ public class JavascriptExpansionCommands extends Command {
|
||||
return true;
|
||||
}
|
||||
|
||||
case "enabled":
|
||||
if (args.length < 3) {
|
||||
msg(sender, "&4Incorrect usage! &f/" + command + " git enabled <true/false>");
|
||||
return true;
|
||||
}
|
||||
|
||||
Boolean enabled = Boolean.parseBoolean(args[2]);
|
||||
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();
|
||||
expansion.setGithubScriptManager(null);
|
||||
}
|
||||
} else {
|
||||
if (expansion.getGithubScriptManager() == null) {
|
||||
expansion.setGithubScriptManager(new GithubScriptManager(expansion));
|
||||
}
|
||||
expansion.getGithubScriptManager().fetch();
|
||||
}
|
||||
msg(sender, "&6Git script downloads set to: &e" + enabled);
|
||||
return true;
|
||||
|
||||
default: {
|
||||
msg(sender, "&4Incorrect usage! &f/" + command + " &7for more help.");
|
||||
return true;
|
||||
|
Loading…
x
Reference in New Issue
Block a user