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>
|
<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.2</version>
|
<version>1.5.3</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,6 +21,16 @@
|
|||||||
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;
|
||||||
@ -28,219 +38,222 @@ 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 githubScripts;
|
private GithubScriptManager githubManager;
|
||||||
private JavascriptExpansionCommands commands;
|
private JavascriptExpansionCommands commands;
|
||||||
private CommandMap commandMap;
|
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() {
|
@Override
|
||||||
instance = this;
|
public String getAuthor() {
|
||||||
try {
|
return "clip";
|
||||||
final Field f = Bukkit.getServer().getClass().getDeclaredField("commandMap");
|
}
|
||||||
f.setAccessible(true);
|
|
||||||
commandMap = (CommandMap) f.get(Bukkit.getServer());
|
@Override
|
||||||
} catch (Exception e) {
|
public String getIdentifier() {
|
||||||
e.printStackTrace();
|
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
|
debug = (boolean) get("debug", false);
|
||||||
public String getAuthor() {
|
config = new JavascriptPlaceholdersConfig(this);
|
||||||
return "clip";
|
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
|
if ((Boolean) get("github_script_downloads", false)) {
|
||||||
public String getIdentifier() {
|
githubManager = new GithubScriptManager(this);
|
||||||
return "javascript";
|
githubManager.fetch();
|
||||||
}
|
}
|
||||||
|
|
||||||
@Override
|
registerCommand();
|
||||||
public String getPlugin() {
|
return super.register();
|
||||||
return null;
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public void clear() {
|
||||||
|
unregisterCommand();
|
||||||
|
scripts.forEach(s -> {
|
||||||
|
s.saveData();
|
||||||
|
s.cleanup();
|
||||||
|
});
|
||||||
|
|
||||||
|
if (githubManager != null) {
|
||||||
|
githubManager.clear();
|
||||||
|
githubManager = null;
|
||||||
}
|
}
|
||||||
|
|
||||||
@Override
|
scripts.clear();
|
||||||
public String getVersion() {
|
globalEngine = null;
|
||||||
return VERSION;
|
instance = null;
|
||||||
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public String onRequest(OfflinePlayer p, String identifier) {
|
||||||
|
if (p == null) {
|
||||||
|
return "";
|
||||||
}
|
}
|
||||||
|
|
||||||
@Override
|
if (scripts.isEmpty()) {
|
||||||
public boolean register() {
|
return null;
|
||||||
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();
|
|
||||||
}
|
}
|
||||||
|
|
||||||
@Override
|
for (JavascriptPlaceholder script : scripts) {
|
||||||
public void clear() {
|
if (identifier.startsWith(script.getIdentifier() + "_")) {
|
||||||
unregisterCommand();
|
identifier = identifier.replace(script.getIdentifier() + "_", "");
|
||||||
scripts.forEach(s -> {
|
return !identifier.contains(",") ? script.evaluate(p, identifier)
|
||||||
s.saveData();
|
: script.evaluate(p, identifier.split(","));
|
||||||
s.cleanup();
|
} else if (identifier.equalsIgnoreCase(script.getIdentifier())) {
|
||||||
});
|
return script.evaluate(p);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
return null;
|
||||||
|
}
|
||||||
|
|
||||||
if (githubScripts != null) {
|
public boolean addJSPlaceholder(JavascriptPlaceholder p) {
|
||||||
githubScripts.clear();
|
if (p == null) {
|
||||||
githubScripts = null;
|
return false;
|
||||||
}
|
|
||||||
|
|
||||||
scripts.clear();
|
|
||||||
globalEngine = null;
|
|
||||||
instance = null;
|
|
||||||
}
|
}
|
||||||
|
|
||||||
@Override
|
if (scripts.isEmpty()) {
|
||||||
public String onRequest(OfflinePlayer p, String identifier) {
|
scripts.add(p);
|
||||||
if (p == null) {
|
return true;
|
||||||
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;
|
|
||||||
}
|
}
|
||||||
|
|
||||||
public boolean addJSPlaceholder(JavascriptPlaceholder p) {
|
if (scripts.stream().filter(s -> s.getIdentifier().equalsIgnoreCase(p.getIdentifier()))
|
||||||
if (p == null) {
|
.findFirst().orElse(null) != 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;
|
|
||||||
}
|
}
|
||||||
|
|
||||||
public Set<JavascriptPlaceholder> getJSPlaceholders() {
|
scripts.add(p);
|
||||||
return scripts;
|
return true;
|
||||||
}
|
}
|
||||||
|
|
||||||
public List<String> getLoadedIdentifiers() {
|
public Set<JavascriptPlaceholder> getJSPlaceholders() {
|
||||||
List<String> l = new ArrayList<>();
|
return scripts;
|
||||||
scripts.forEach(s -> l.add(s.getIdentifier()));
|
}
|
||||||
return l;
|
|
||||||
}
|
|
||||||
|
|
||||||
public JavascriptPlaceholder getJSPlaceholder(String identifier) {
|
public List<String> getLoadedIdentifiers() {
|
||||||
return scripts.stream().filter(s -> s.getIdentifier().equalsIgnoreCase(identifier)).findFirst().orElse(null);
|
List<String> l = new ArrayList<>();
|
||||||
}
|
scripts.forEach(s -> l.add(s.getIdentifier()));
|
||||||
|
return l;
|
||||||
|
}
|
||||||
|
|
||||||
public int getAmountLoaded() {
|
public JavascriptPlaceholder getJSPlaceholder(String identifier) {
|
||||||
return scripts.size();
|
return scripts.stream().filter(s -> s.getIdentifier().equalsIgnoreCase(identifier)).findFirst()
|
||||||
}
|
.orElse(null);
|
||||||
|
}
|
||||||
|
|
||||||
public ScriptEngine getGlobalEngine() {
|
public int getAmountLoaded() {
|
||||||
return globalEngine;
|
return scripts.size();
|
||||||
}
|
}
|
||||||
|
|
||||||
public JavascriptPlaceholdersConfig getConfig() {
|
public ScriptEngine getGlobalEngine() {
|
||||||
return config;
|
return globalEngine;
|
||||||
}
|
}
|
||||||
|
|
||||||
@Override
|
public JavascriptPlaceholdersConfig getConfig() {
|
||||||
public Map<String, Object> getDefaults() {
|
return config;
|
||||||
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() {
|
@Override
|
||||||
scripts.forEach(s -> {
|
public Map<String, Object> getDefaults() {
|
||||||
s.saveData();
|
Map<String, Object> def = new HashMap<>();
|
||||||
s.cleanup();
|
def.put("engine", "javascript");
|
||||||
});
|
def.put("debug", false);
|
||||||
scripts.clear();
|
def.put("github_script_downloads", false);
|
||||||
config.reload();
|
return def;
|
||||||
return config.loadPlaceholders();
|
}
|
||||||
}
|
|
||||||
|
|
||||||
public static JavascriptExpansion getInstance() {
|
protected int reloadScripts() {
|
||||||
return instance;
|
scripts.forEach(s -> {
|
||||||
}
|
s.saveData();
|
||||||
|
s.cleanup();
|
||||||
|
});
|
||||||
|
scripts.clear();
|
||||||
|
config.reload();
|
||||||
|
return config.loadPlaceholders();
|
||||||
|
}
|
||||||
|
|
||||||
public GithubScriptManager getGithubScriptManager() {
|
public static JavascriptExpansion getInstance() {
|
||||||
return githubScripts;
|
return instance;
|
||||||
}
|
}
|
||||||
|
|
||||||
private boolean unregisterCommand() {
|
public GithubScriptManager getGithubScriptManager() {
|
||||||
if (commandMap == null || commands == null) return false;
|
return githubManager;
|
||||||
return commands.unregister(commandMap);
|
}
|
||||||
}
|
|
||||||
|
|
||||||
private boolean registerCommand() {
|
public void setGithubScriptManager(GithubScriptManager manager) {
|
||||||
if (commandMap == null) return false;
|
this.githubManager = manager;
|
||||||
commands = new JavascriptExpansionCommands(this);
|
}
|
||||||
commandMap.register("papi" + commands.getName(), commands);
|
|
||||||
return commands.isRegistered();
|
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.GithubScript;
|
||||||
import com.extendedclip.papi.expansion.javascript.cloud.GithubScriptManager;
|
import com.extendedclip.papi.expansion.javascript.cloud.GithubScriptManager;
|
||||||
|
import me.clip.placeholderapi.PlaceholderAPIPlugin;
|
||||||
import org.bukkit.ChatColor;
|
import org.bukkit.ChatColor;
|
||||||
import org.bukkit.command.Command;
|
import org.bukkit.command.Command;
|
||||||
import org.bukkit.command.CommandSender;
|
import org.bukkit.command.CommandSender;
|
||||||
@ -161,6 +162,31 @@ public class JavascriptExpansionCommands extends Command {
|
|||||||
return true;
|
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: {
|
default: {
|
||||||
msg(sender, "&4Incorrect usage! &f/" + command + " &7for more help.");
|
msg(sender, "&4Incorrect usage! &f/" + command + " &7for more help.");
|
||||||
return true;
|
return true;
|
||||||
|
Loading…
x
Reference in New Issue
Block a user