mirror of
https://github.com/PlaceholderAPI/Javascript-Expansion.git
synced 2025-05-23 18:42:44 +00:00
Experimenting...
Added a listener to the expansion so I can execute a reload command that does not reload all expansions, only the javascript placeholders. Added "Expansion" var to get the expansion instance. Added some methods related to javascript placeholders
This commit is contained in:
parent
62a496bb58
commit
b17620cdbf
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.4.0</version>
|
||||
<version>1.4.1</version>
|
||||
<name>PAPI-Expansion-Javascript</name>
|
||||
<description>PlaceholderAPI expansion for javascript placeholders</description>
|
||||
|
||||
|
@ -20,8 +20,10 @@
|
||||
*/
|
||||
package com.extendedclip.papi.expansion.javascript;
|
||||
|
||||
import java.util.ArrayList;
|
||||
import java.util.HashMap;
|
||||
import java.util.HashSet;
|
||||
import java.util.List;
|
||||
import java.util.Map;
|
||||
import java.util.Set;
|
||||
|
||||
@ -32,19 +34,77 @@ import me.clip.placeholderapi.PlaceholderAPIPlugin;
|
||||
import me.clip.placeholderapi.expansion.Cacheable;
|
||||
import me.clip.placeholderapi.expansion.Configurable;
|
||||
import me.clip.placeholderapi.expansion.PlaceholderExpansion;
|
||||
import me.clip.placeholderapi.util.Msg;
|
||||
|
||||
import org.bukkit.entity.Player;
|
||||
import org.bukkit.event.EventHandler;
|
||||
import org.bukkit.event.Listener;
|
||||
import org.bukkit.event.player.PlayerCommandPreprocessEvent;
|
||||
|
||||
public class JavascriptExpansion extends PlaceholderExpansion implements Cacheable, Configurable {
|
||||
public class JavascriptExpansion extends PlaceholderExpansion implements Cacheable, Configurable, Listener {
|
||||
|
||||
private ScriptEngine globalEngine = null;
|
||||
|
||||
private JavascriptPlaceholdersConfig config;
|
||||
|
||||
private final Set<JavascriptPlaceholder> scripts = new HashSet<JavascriptPlaceholder>();
|
||||
private final Set<JavascriptPlaceholder> scripts = new HashSet<>();
|
||||
|
||||
private final String VERSION = getClass().getPackage().getImplementationVersion();
|
||||
|
||||
private static JavascriptExpansion instance;
|
||||
|
||||
public JavascriptExpansion() {
|
||||
instance = this;
|
||||
}
|
||||
|
||||
/*
|
||||
* I am just testing the waters here because there is no command system for expansions...
|
||||
*/
|
||||
@EventHandler
|
||||
public void onCmdExecute(PlayerCommandPreprocessEvent event) {
|
||||
|
||||
String msg = event.getMessage();
|
||||
|
||||
if (!msg.startsWith("/papijsp")) {
|
||||
return;
|
||||
}
|
||||
|
||||
if (!event.getPlayer().hasPermission("placeholderapi.admin")) {
|
||||
return;
|
||||
}
|
||||
|
||||
event.setCancelled(true);
|
||||
|
||||
Player p = event.getPlayer();
|
||||
|
||||
// default command
|
||||
if (msg.indexOf(" ") == -1) {
|
||||
Msg.msg(p, "&7Javascript expansion v: &f" + getVersion());
|
||||
Msg.msg(p, "&7Created by: &f" + getAuthor());
|
||||
Msg.msg(p, "&fWiki: &ahttps://github.com/PlaceholderAPI-Expansions/Javascript-Expansion/wiki");
|
||||
Msg.msg(p, "&r");
|
||||
Msg.msg(p, "&7/papijsp reload &7- &fReload your javascripts without reloading PlaceholderAPI");
|
||||
Msg.msg(p, "&7/papijsp list &7- &fList loaded script identifiers.");
|
||||
return;
|
||||
}
|
||||
|
||||
if (msg.equals("/papijsp reload")) {
|
||||
Msg.msg(p, "&aReloading...");
|
||||
int l = this.reloadScripts();
|
||||
Msg.msg(p, l + " &7script" + (l == 1 ? "" : "s")+ " loaded");
|
||||
return;
|
||||
}
|
||||
|
||||
if (msg.equals("/papijsp list")) {
|
||||
List<String> loaded = this.getLoadedIdentifiers();
|
||||
Msg.msg(p, loaded.size() + " &7script" + (loaded.size() == 1 ? "" : "s")+ " loaded");
|
||||
Msg.msg(p, loaded.toString());
|
||||
return;
|
||||
}
|
||||
|
||||
event.getPlayer().sendMessage("&cIncorrect usage &7- &f/papijsp");
|
||||
}
|
||||
|
||||
@Override
|
||||
public String getAuthor() {
|
||||
return "clip";
|
||||
@ -65,11 +125,6 @@ public class JavascriptExpansion extends PlaceholderExpansion implements Cacheab
|
||||
return VERSION;
|
||||
}
|
||||
|
||||
@Override
|
||||
public boolean canRegister() {
|
||||
return true;
|
||||
}
|
||||
|
||||
@Override
|
||||
public boolean register() {
|
||||
if (globalEngine == null) {
|
||||
@ -96,6 +151,7 @@ public class JavascriptExpansion extends PlaceholderExpansion implements Cacheab
|
||||
}
|
||||
scripts.clear();
|
||||
globalEngine = null;
|
||||
instance = null;
|
||||
}
|
||||
|
||||
@Override
|
||||
@ -119,7 +175,7 @@ public class JavascriptExpansion extends PlaceholderExpansion implements Cacheab
|
||||
return null;
|
||||
}
|
||||
|
||||
public boolean addJavascriptPlaceholder(JavascriptPlaceholder p) {
|
||||
public boolean addJSPlaceholder(JavascriptPlaceholder p) {
|
||||
if (p == null) {
|
||||
return false;
|
||||
}
|
||||
@ -129,17 +185,32 @@ public class JavascriptExpansion extends PlaceholderExpansion implements Cacheab
|
||||
return true;
|
||||
}
|
||||
|
||||
for (JavascriptPlaceholder pl : scripts) {
|
||||
if (pl.getIdentifier().equalsIgnoreCase(p.getIdentifier())) {
|
||||
if (scripts.stream().filter(s -> s.getIdentifier().equalsIgnoreCase(p.getIdentifier())).findFirst().orElse(null) != null) {
|
||||
return false;
|
||||
}
|
||||
}
|
||||
|
||||
scripts.add(p);
|
||||
return true;
|
||||
}
|
||||
|
||||
public int getJavascriptPlaceholdersAmount() {
|
||||
return scripts == null ? 0 : scripts.size();
|
||||
public Set<JavascriptPlaceholder> getJSPlaceholders() {
|
||||
return scripts;
|
||||
}
|
||||
|
||||
public List<String> getLoadedIdentifiers() {
|
||||
List<String> l = new ArrayList<>();
|
||||
scripts.stream().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() {
|
||||
@ -152,4 +223,20 @@ public class JavascriptExpansion extends PlaceholderExpansion implements Cacheab
|
||||
def.put("engine", "javascript");
|
||||
return def;
|
||||
}
|
||||
|
||||
private int reloadScripts() {
|
||||
if (!scripts.isEmpty()) {
|
||||
scripts.stream().forEach(s -> {
|
||||
s.saveData();
|
||||
s.cleanup();
|
||||
});
|
||||
}
|
||||
scripts.clear();
|
||||
config.reload();
|
||||
return config.loadPlaceholders();
|
||||
}
|
||||
|
||||
public static JavascriptExpansion getInstance() {
|
||||
return instance;
|
||||
}
|
||||
}
|
||||
|
@ -58,6 +58,9 @@ public class JavascriptPlaceholder {
|
||||
Validate.notNull(identifier, "Identifier can not be null");
|
||||
Validate.notNull(script, "script can not be null");
|
||||
this.engine = engine;
|
||||
engine.put("Data", getData());
|
||||
engine.put("BukkitServer", Bukkit.getServer());
|
||||
engine.put("Expansion", JavascriptExpansion.getInstance());
|
||||
this.identifier = identifier;
|
||||
this.script = script;
|
||||
File dir = new File(FILEDIR);
|
||||
@ -108,8 +111,6 @@ public class JavascriptPlaceholder {
|
||||
}
|
||||
|
||||
engine.put("args", c);
|
||||
engine.put("Data", getData());
|
||||
engine.put("BukkitServer", Bukkit.getServer());
|
||||
engine.put("BukkitPlayer", p);
|
||||
Object result = engine.eval(exp);
|
||||
return result != null ? PlaceholderAPI.setBracketPlaceholders(p, result.toString()) : "";
|
||||
@ -118,7 +119,7 @@ public class JavascriptPlaceholder {
|
||||
PlaceholderAPIPlugin.getInstance().getLogger().severe("Error in javascript for placeholder - " + this.identifier);
|
||||
ex.printStackTrace();
|
||||
}
|
||||
return "invalid javascript";
|
||||
return "Script error";
|
||||
}
|
||||
|
||||
public ScriptData getData() {
|
||||
|
@ -173,7 +173,7 @@ public class JavascriptPlaceholdersConfig {
|
||||
|
||||
JavascriptPlaceholder pl = new JavascriptPlaceholder(engine, identifier, script);
|
||||
|
||||
boolean added = ex.addJavascriptPlaceholder(pl);
|
||||
boolean added = ex.addJSPlaceholder(pl);
|
||||
|
||||
if (added) {
|
||||
|
||||
@ -185,7 +185,7 @@ public class JavascriptPlaceholdersConfig {
|
||||
plugin.getLogger().warning("Javascript placeholder %javascript_" + identifier + "% is a duplicate!");
|
||||
}
|
||||
}
|
||||
return ex.getJavascriptPlaceholdersAmount();
|
||||
return ex.getAmountLoaded();
|
||||
}
|
||||
|
||||
private String getContents(File f) {
|
||||
|
Loading…
x
Reference in New Issue
Block a user