mirror of
https://github.com/PlaceholderAPI/Javascript-Expansion.git
synced 2025-05-23 18:42:44 +00:00
Offline player supporrt
This commit is contained in:
parent
28d35230bc
commit
c7d421a9a4
132
pom.xml
132
pom.xml
@ -1,66 +1,66 @@
|
||||
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
|
||||
<modelVersion>4.0.0</modelVersion>
|
||||
<groupId>com.extendedclip.papi.expansion.javascript</groupId>
|
||||
<artifactId>javascript-expansion</artifactId>
|
||||
<version>1.4.2</version>
|
||||
<name>PAPI-Expansion-Javascript</name>
|
||||
<description>PlaceholderAPI expansion for javascript placeholders</description>
|
||||
|
||||
<repositories>
|
||||
<repository>
|
||||
<id>spigot-repo</id>
|
||||
<url>https://hub.spigotmc.org/nexus/content/repositories/snapshots/</url>
|
||||
</repository>
|
||||
<repository>
|
||||
<id>placeholderapi</id>
|
||||
<url>http://repo.extendedclip.com/content/repositories/placeholderapi/</url>
|
||||
</repository>
|
||||
</repositories>
|
||||
|
||||
<dependencies>
|
||||
<dependency>
|
||||
<groupId>org.spigotmc</groupId>
|
||||
<artifactId>spigot-api</artifactId>
|
||||
<version>1.12.2-R0.1-SNAPSHOT</version>
|
||||
<scope>provided</scope>
|
||||
</dependency>
|
||||
<dependency>
|
||||
<groupId>me.clip</groupId>
|
||||
<artifactId>placeholderapi</artifactId>
|
||||
<version>LATEST</version>
|
||||
<scope>provided</scope>
|
||||
</dependency>
|
||||
</dependencies>
|
||||
|
||||
<build>
|
||||
<plugins>
|
||||
<plugin>
|
||||
<groupId>org.apache.maven.plugins</groupId>
|
||||
<artifactId>maven-jar-plugin</artifactId>
|
||||
<version>3.0.2</version>
|
||||
<configuration>
|
||||
<finalName>${name}</finalName>
|
||||
<archive>
|
||||
<manifest>
|
||||
<addDefaultImplementationEntries>true</addDefaultImplementationEntries>
|
||||
<addDefaultSpecificationEntries>true</addDefaultSpecificationEntries>
|
||||
</manifest>
|
||||
</archive>
|
||||
</configuration>
|
||||
</plugin>
|
||||
<plugin>
|
||||
<groupId>org.apache.maven.plugins</groupId>
|
||||
<artifactId>maven-compiler-plugin</artifactId>
|
||||
<version>3.7.0</version>
|
||||
<configuration>
|
||||
<target>1.8</target>
|
||||
<source>1.8</source>
|
||||
<encoding>UTF-8</encoding>
|
||||
<useIncrementalCompilation>false</useIncrementalCompilation>
|
||||
</configuration>
|
||||
</plugin>
|
||||
</plugins>
|
||||
</build>
|
||||
|
||||
|
||||
</project>
|
||||
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
|
||||
<modelVersion>4.0.0</modelVersion>
|
||||
<groupId>com.extendedclip.papi.expansion.javascript</groupId>
|
||||
<artifactId>javascript-expansion</artifactId>
|
||||
<version>1.5.0</version>
|
||||
<name>PAPI-Expansion-Javascript</name>
|
||||
<description>PlaceholderAPI expansion for javascript placeholders</description>
|
||||
|
||||
<repositories>
|
||||
<repository>
|
||||
<id>spigot-repo</id>
|
||||
<url>https://hub.spigotmc.org/nexus/content/repositories/snapshots/</url>
|
||||
</repository>
|
||||
<repository>
|
||||
<id>placeholderapi</id>
|
||||
<url>https://repo.extendedclip.com/content/repositories/placeholderapi/</url>
|
||||
</repository>
|
||||
</repositories>
|
||||
|
||||
<dependencies>
|
||||
<dependency>
|
||||
<groupId>org.spigotmc</groupId>
|
||||
<artifactId>spigot-api</artifactId>
|
||||
<version>1.14.1-R0.1-SNAPSHOT</version>
|
||||
<scope>provided</scope>
|
||||
</dependency>
|
||||
<dependency>
|
||||
<groupId>me.clip</groupId>
|
||||
<artifactId>placeholderapi</artifactId>
|
||||
<version>2.10.2</version>
|
||||
<scope>provided</scope>
|
||||
</dependency>
|
||||
</dependencies>
|
||||
|
||||
<build>
|
||||
<plugins>
|
||||
<plugin>
|
||||
<groupId>org.apache.maven.plugins</groupId>
|
||||
<artifactId>maven-jar-plugin</artifactId>
|
||||
<version>3.0.2</version>
|
||||
<configuration>
|
||||
<finalName>${name}</finalName>
|
||||
<archive>
|
||||
<manifest>
|
||||
<addDefaultImplementationEntries>true</addDefaultImplementationEntries>
|
||||
<addDefaultSpecificationEntries>true</addDefaultSpecificationEntries>
|
||||
</manifest>
|
||||
</archive>
|
||||
</configuration>
|
||||
</plugin>
|
||||
<plugin>
|
||||
<groupId>org.apache.maven.plugins</groupId>
|
||||
<artifactId>maven-compiler-plugin</artifactId>
|
||||
<version>3.7.0</version>
|
||||
<configuration>
|
||||
<target>1.8</target>
|
||||
<source>1.8</source>
|
||||
<encoding>UTF-8</encoding>
|
||||
<useIncrementalCompilation>false</useIncrementalCompilation>
|
||||
</configuration>
|
||||
</plugin>
|
||||
</plugins>
|
||||
</build>
|
||||
|
||||
|
||||
</project>
|
||||
|
@ -1,237 +1,262 @@
|
||||
/*
|
||||
*
|
||||
* Javascript-Expansion
|
||||
* Copyright (C) 2018 Ryan McCarthy
|
||||
*
|
||||
* This program is free software: you can redistribute it and/or modify
|
||||
* it under the terms of the GNU General Public License as published by
|
||||
* the Free Software Foundation, either version 3 of the License, or
|
||||
* (at your option) any later version.
|
||||
*
|
||||
* This program is distributed in the hope that it will be useful,
|
||||
* but WITHOUT ANY WARRANTY; without even the implied warranty of
|
||||
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
||||
* GNU General Public License for more details.
|
||||
*
|
||||
* You should have received a copy of the GNU General Public License
|
||||
* along with this program. If not, see <http://www.gnu.org/licenses/>.
|
||||
*
|
||||
*
|
||||
*/
|
||||
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;
|
||||
|
||||
import javax.script.ScriptEngine;
|
||||
import javax.script.ScriptEngineManager;
|
||||
|
||||
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, Listener {
|
||||
|
||||
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;
|
||||
|
||||
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.contains(" ")) {
|
||||
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, String.join(", ", loaded));
|
||||
return;
|
||||
}
|
||||
|
||||
event.getPlayer().sendMessage("&cIncorrect usage &7- &f/papijsp");
|
||||
}
|
||||
|
||||
@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", "javascript"));
|
||||
} catch (NullPointerException ex) {
|
||||
getPlaceholderAPI().getLogger().warning("Javascript engine type was invalid! Defaulting to 'javascript'");
|
||||
globalEngine = new ScriptEngineManager().getEngineByName("javascript");
|
||||
}
|
||||
}
|
||||
|
||||
config = new JavascriptPlaceholdersConfig(this);
|
||||
config.loadPlaceholders();
|
||||
return super.register();
|
||||
}
|
||||
|
||||
@Override
|
||||
public void clear() {
|
||||
scripts.forEach(s -> {
|
||||
s.saveData();
|
||||
s.cleanup();
|
||||
});
|
||||
scripts.clear();
|
||||
globalEngine = null;
|
||||
instance = null;
|
||||
}
|
||||
|
||||
@Override
|
||||
public String onPlaceholderRequest(Player 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;
|
||||
}
|
||||
|
||||
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;
|
||||
}
|
||||
|
||||
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;
|
||||
}
|
||||
|
||||
@Override
|
||||
public Map<String, Object> getDefaults() {
|
||||
Map<String, Object> def = new HashMap<String, Object>();
|
||||
def.put("engine", "javascript");
|
||||
return def;
|
||||
}
|
||||
|
||||
private int reloadScripts() {
|
||||
scripts.forEach(s -> {
|
||||
s.saveData();
|
||||
s.cleanup();
|
||||
});
|
||||
scripts.clear();
|
||||
config.reload();
|
||||
return config.loadPlaceholders();
|
||||
}
|
||||
|
||||
public static JavascriptExpansion getInstance() {
|
||||
return instance;
|
||||
}
|
||||
}
|
||||
/*
|
||||
*
|
||||
* Javascript-Expansion
|
||||
* Copyright (C) 2018 Ryan McCarthy
|
||||
*
|
||||
* This program is free software: you can redistribute it and/or modify
|
||||
* it under the terms of the GNU General Public License as published by
|
||||
* the Free Software Foundation, either version 3 of the License, or
|
||||
* (at your option) any later version.
|
||||
*
|
||||
* This program is distributed in the hope that it will be useful,
|
||||
* but WITHOUT ANY WARRANTY; without even the implied warranty of
|
||||
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
||||
* GNU General Public License for more details.
|
||||
*
|
||||
* You should have received a copy of the GNU General Public License
|
||||
* along with this program. If not, see <http://www.gnu.org/licenses/>.
|
||||
*
|
||||
*
|
||||
*/
|
||||
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;
|
||||
|
||||
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;
|
||||
|
||||
import org.bukkit.ChatColor;
|
||||
import org.bukkit.OfflinePlayer;
|
||||
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, Listener {
|
||||
|
||||
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;
|
||||
|
||||
public JavascriptExpansion() {
|
||||
instance = this;
|
||||
}
|
||||
|
||||
private boolean debug = false;
|
||||
|
||||
/*
|
||||
* 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.contains(" ")) {
|
||||
msg(p, "&7Javascript expansion v: &f" + getVersion());
|
||||
msg(p, "&7Created by: &f" + getAuthor());
|
||||
msg(p, "&fWiki: &ahttps://github.com/PlaceholderAPI-Expansions/Javascript-Expansion/wiki");
|
||||
msg(p, "&r");
|
||||
msg(p, "&7/papijsp reload &7- &fReload your javascripts without reloading PlaceholderAPI");
|
||||
msg(p, "&7/papijsp list &7- &fList loaded script identifiers.");
|
||||
return;
|
||||
}
|
||||
|
||||
if (msg.equals("/papijsp reload")) {
|
||||
msg(p, "&aReloading...");
|
||||
int l = this.reloadScripts();
|
||||
msg(p, l + " &7script" + (l == 1 ? "" : "s")+ " loaded");
|
||||
return;
|
||||
}
|
||||
|
||||
if (msg.equals("/papijsp list")) {
|
||||
List<String> loaded = this.getLoadedIdentifiers();
|
||||
msg(p, loaded.size() + " &7script" + (loaded.size() == 1 ? "" : "s")+ " loaded");
|
||||
msg(p, String.join(", ", loaded));
|
||||
return;
|
||||
}
|
||||
|
||||
event.getPlayer().sendMessage("&cIncorrect usage &7- &f/papijsp");
|
||||
}
|
||||
|
||||
public void msg(Player p, String text) {
|
||||
p.sendMessage(ChatColor.translateAlternateColorCodes('&', text));
|
||||
}
|
||||
|
||||
@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);
|
||||
config = new JavascriptPlaceholdersConfig(this);
|
||||
config.loadPlaceholders();
|
||||
if (debug) {
|
||||
System.out.println("Java version: " + System.getProperty("java.version"));
|
||||
|
||||
ScriptEngineManager manager = new ScriptEngineManager();
|
||||
List<ScriptEngineFactory> factories = manager.getEngineFactories();
|
||||
System.out.println("displaying all script engine factories:");
|
||||
for (ScriptEngineFactory factory : factories) {
|
||||
System.out.println("Engine name: " + 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: " + factory.getExtensions());
|
||||
System.out.println("mime types: " + factory.getMimeTypes());
|
||||
System.out.println("names: " + factory.getNames());
|
||||
}
|
||||
}
|
||||
return super.register();
|
||||
}
|
||||
|
||||
@Override
|
||||
public void clear() {
|
||||
scripts.forEach(s -> {
|
||||
s.saveData();
|
||||
s.cleanup();
|
||||
});
|
||||
scripts.clear();
|
||||
globalEngine = null;
|
||||
instance = null;
|
||||
}
|
||||
|
||||
@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;
|
||||
}
|
||||
|
||||
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;
|
||||
}
|
||||
|
||||
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;
|
||||
}
|
||||
|
||||
@Override
|
||||
public Map<String, Object> getDefaults() {
|
||||
Map<String, Object> def = new HashMap<String, Object>();
|
||||
def.put("engine", "javascript");
|
||||
def.put("debug", false);
|
||||
return def;
|
||||
}
|
||||
|
||||
private int reloadScripts() {
|
||||
scripts.forEach(s -> {
|
||||
s.saveData();
|
||||
s.cleanup();
|
||||
});
|
||||
scripts.clear();
|
||||
config.reload();
|
||||
return config.loadPlaceholders();
|
||||
}
|
||||
|
||||
public static JavascriptExpansion getInstance() {
|
||||
return instance;
|
||||
}
|
||||
}
|
||||
|
@ -1,205 +1,197 @@
|
||||
/*
|
||||
*
|
||||
* Javascript-Expansion
|
||||
* Copyright (C) 2018 Ryan McCarthy
|
||||
*
|
||||
* This program is free software: you can redistribute it and/or modify
|
||||
* it under the terms of the GNU General Public License as published by
|
||||
* the Free Software Foundation, either version 3 of the License, or
|
||||
* (at your option) any later version.
|
||||
*
|
||||
* This program is distributed in the hope that it will be useful,
|
||||
* but WITHOUT ANY WARRANTY; without even the implied warranty of
|
||||
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
||||
* GNU General Public License for more details.
|
||||
*
|
||||
* You should have received a copy of the GNU General Public License
|
||||
* along with this program. If not, see <http://www.gnu.org/licenses/>.
|
||||
*
|
||||
*
|
||||
*/
|
||||
package com.extendedclip.papi.expansion.javascript;
|
||||
|
||||
import java.io.File;
|
||||
import java.io.IOException;
|
||||
import java.util.Set;
|
||||
|
||||
import javax.script.ScriptEngine;
|
||||
import javax.script.ScriptException;
|
||||
|
||||
import org.apache.commons.lang.Validate;
|
||||
import org.bukkit.Bukkit;
|
||||
import org.bukkit.configuration.InvalidConfigurationException;
|
||||
import org.bukkit.configuration.file.FileConfiguration;
|
||||
import org.bukkit.configuration.file.YamlConfiguration;
|
||||
import org.bukkit.entity.Player;
|
||||
|
||||
import me.clip.placeholderapi.PlaceholderAPI;
|
||||
import me.clip.placeholderapi.PlaceholderAPIPlugin;
|
||||
|
||||
public class JavascriptPlaceholder {
|
||||
|
||||
private ScriptEngine engine = null;
|
||||
|
||||
private String identifier;
|
||||
|
||||
private String script;
|
||||
|
||||
private ScriptData data = null;
|
||||
|
||||
private File dataFile;
|
||||
|
||||
private FileConfiguration cfg;
|
||||
|
||||
private final String FILEDIR = PlaceholderAPIPlugin.getInstance().getDataFolder() + File.separator + "javascripts" + File.separator + "javascript_data";
|
||||
|
||||
public JavascriptPlaceholder(ScriptEngine engine, String identifier, String script) {
|
||||
Validate.notNull(engine, "ScriptEngine can not be null");
|
||||
Validate.notNull(identifier, "Identifier can not be null");
|
||||
Validate.notNull(script, "script can not be null");
|
||||
this.engine = engine;
|
||||
this.identifier = identifier;
|
||||
this.script = script;
|
||||
File dir = new File(FILEDIR);
|
||||
|
||||
try {
|
||||
if (!dir.exists()) {
|
||||
dir.mkdirs();
|
||||
}
|
||||
} catch (SecurityException e) {
|
||||
e.printStackTrace();
|
||||
}
|
||||
data = new ScriptData();
|
||||
dataFile = new File(FILEDIR, identifier + "_data.yml");
|
||||
engine.put("Data", data);
|
||||
engine.put("BukkitServer", Bukkit.getServer());
|
||||
engine.put("Expansion", JavascriptExpansion.getInstance());
|
||||
engine.put("Placeholder", this);
|
||||
}
|
||||
|
||||
public String getIdentifier() {
|
||||
return identifier;
|
||||
}
|
||||
|
||||
public String getScript() {
|
||||
return script;
|
||||
}
|
||||
|
||||
public String evaluate(Player p, String... args) {
|
||||
String exp = PlaceholderAPI.setPlaceholders(p, script);
|
||||
|
||||
try {
|
||||
String[] c = null;
|
||||
|
||||
if (args != null && args.length > 0) {
|
||||
for (int i = 0 ; i < args.length ; i++) {
|
||||
if (args[i] == null || args[i].isEmpty()) {
|
||||
continue;
|
||||
}
|
||||
|
||||
String s = PlaceholderAPI.setBracketPlaceholders(p, args[i]);
|
||||
|
||||
if (c == null) {
|
||||
c = new String[args.length];
|
||||
}
|
||||
|
||||
c[i] = s;
|
||||
}
|
||||
}
|
||||
|
||||
if (c == null) {
|
||||
c = new String[]{};
|
||||
}
|
||||
|
||||
engine.put("args", c);
|
||||
engine.put("BukkitPlayer", p);
|
||||
Object result = engine.eval(exp);
|
||||
return result != null ? PlaceholderAPI.setBracketPlaceholders(p, result.toString()) : "";
|
||||
|
||||
} catch (ScriptException ex) {
|
||||
ex.printStackTrace();
|
||||
}
|
||||
return "Script error";
|
||||
}
|
||||
|
||||
public ScriptData getData() {
|
||||
// this should never be null but just in case setData(null) is called
|
||||
if (data == null) {
|
||||
data = new ScriptData();
|
||||
}
|
||||
return data;
|
||||
}
|
||||
|
||||
public void setData(ScriptData data) {
|
||||
this.data = data;
|
||||
}
|
||||
|
||||
public boolean loadData() {
|
||||
|
||||
cfg = new YamlConfiguration();
|
||||
|
||||
if (!dataFile.exists()) {
|
||||
return false;
|
||||
}
|
||||
try {
|
||||
cfg.load(dataFile);
|
||||
} catch (IOException e) {
|
||||
e.printStackTrace();
|
||||
return false;
|
||||
} catch (InvalidConfigurationException e) {
|
||||
e.printStackTrace();
|
||||
return false;
|
||||
}
|
||||
|
||||
final Set<String> keys = cfg.getKeys(true);
|
||||
|
||||
if (keys == null || keys.isEmpty()) {
|
||||
return false;
|
||||
}
|
||||
|
||||
if (data == null) {
|
||||
data = new ScriptData();
|
||||
} else {
|
||||
data.clear();
|
||||
}
|
||||
|
||||
keys.stream().forEach(k -> {
|
||||
data.set(k, cfg.get(k));
|
||||
});
|
||||
|
||||
if (!data.isEmpty()) {
|
||||
this.setData(data);
|
||||
return true;
|
||||
}
|
||||
return false;
|
||||
}
|
||||
|
||||
public boolean saveData() {
|
||||
if (data == null || data.isEmpty()) {
|
||||
return false;
|
||||
}
|
||||
|
||||
if (cfg == null) {
|
||||
return false;
|
||||
}
|
||||
|
||||
data.getData().entrySet().forEach(e -> {
|
||||
cfg.set(e.getKey(), e.getValue());
|
||||
});
|
||||
|
||||
try {
|
||||
cfg.save(dataFile);
|
||||
return true;
|
||||
} catch (IOException e) {
|
||||
return false;
|
||||
}
|
||||
}
|
||||
|
||||
public void cleanup() {
|
||||
if (this.data != null) {
|
||||
this.data.clear();
|
||||
this.data = null;
|
||||
}
|
||||
this.cfg = null;
|
||||
}
|
||||
}
|
||||
/*
|
||||
*
|
||||
* Javascript-Expansion
|
||||
* Copyright (C) 2018 Ryan McCarthy
|
||||
*
|
||||
* This program is free software: you can redistribute it and/or modify
|
||||
* it under the terms of the GNU General Public License as published by
|
||||
* the Free Software Foundation, either version 3 of the License, or
|
||||
* (at your option) any later version.
|
||||
*
|
||||
* This program is distributed in the hope that it will be useful,
|
||||
* but WITHOUT ANY WARRANTY; without even the implied warranty of
|
||||
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
||||
* GNU General Public License for more details.
|
||||
*
|
||||
* You should have received a copy of the GNU General Public License
|
||||
* along with this program. If not, see <http://www.gnu.org/licenses/>.
|
||||
*
|
||||
*
|
||||
*/
|
||||
package com.extendedclip.papi.expansion.javascript;
|
||||
|
||||
import java.io.File;
|
||||
import java.io.IOException;
|
||||
import java.util.Set;
|
||||
import javax.script.ScriptEngine;
|
||||
import javax.script.ScriptException;
|
||||
import me.clip.placeholderapi.PlaceholderAPI;
|
||||
import me.clip.placeholderapi.PlaceholderAPIPlugin;
|
||||
import org.apache.commons.lang.Validate;
|
||||
import org.bukkit.Bukkit;
|
||||
import org.bukkit.OfflinePlayer;
|
||||
import org.bukkit.configuration.InvalidConfigurationException;
|
||||
import org.bukkit.configuration.file.FileConfiguration;
|
||||
import org.bukkit.configuration.file.YamlConfiguration;
|
||||
|
||||
public class JavascriptPlaceholder {
|
||||
|
||||
private ScriptEngine engine = null;
|
||||
private String identifier;
|
||||
private String script;
|
||||
private ScriptData data = null;
|
||||
private File dataFile;
|
||||
private FileConfiguration cfg;
|
||||
|
||||
private final String FILEDIR = PlaceholderAPIPlugin.getInstance().getDataFolder() + File.separator + "javascripts"+ File.separator + "javascript_data";
|
||||
|
||||
public JavascriptPlaceholder(ScriptEngine engine, String identifier, String script) {
|
||||
Validate.notNull(engine, "ScriptEngine can not be null");
|
||||
Validate.notNull(identifier, "Identifier can not be null");
|
||||
Validate.notNull(script, "script can not be null");
|
||||
this.engine = engine;
|
||||
this.identifier = identifier;
|
||||
this.script = script;
|
||||
File dir = new File(FILEDIR);
|
||||
|
||||
try {
|
||||
if (!dir.exists()) {
|
||||
dir.mkdirs();
|
||||
}
|
||||
} catch (SecurityException e) {
|
||||
e.printStackTrace();
|
||||
}
|
||||
data = new ScriptData();
|
||||
dataFile = new File(FILEDIR, identifier + "_data.yml");
|
||||
engine.put("Data", data);
|
||||
engine.put("BukkitServer", Bukkit.getServer());
|
||||
engine.put("Expansion", JavascriptExpansion.getInstance());
|
||||
engine.put("Placeholder", this);
|
||||
}
|
||||
|
||||
public String getIdentifier() {
|
||||
return identifier;
|
||||
}
|
||||
|
||||
public String getScript() {
|
||||
return script;
|
||||
}
|
||||
|
||||
public String evaluate(OfflinePlayer p, String... args) {
|
||||
String exp = PlaceholderAPI.setPlaceholders(p, script);
|
||||
|
||||
try {
|
||||
String[] c = null;
|
||||
|
||||
if (args != null && args.length > 0) {
|
||||
for (int i = 0; i < args.length; i++) {
|
||||
if (args[i] == null || args[i].isEmpty()) {
|
||||
continue;
|
||||
}
|
||||
|
||||
String s = PlaceholderAPI.setBracketPlaceholders(p, args[i]);
|
||||
|
||||
if (c == null) {
|
||||
c = new String[args.length];
|
||||
}
|
||||
|
||||
c[i] = s;
|
||||
}
|
||||
}
|
||||
|
||||
if (c == null) {
|
||||
c = new String[]{};
|
||||
}
|
||||
|
||||
engine.put("args", c);
|
||||
engine.put("BukkitPlayer", p != null && p.isOnline() ? p.getPlayer() : null);
|
||||
engine.put("OfflinePlayer", p);
|
||||
Object result = engine.eval(exp);
|
||||
return result != null ? PlaceholderAPI.setBracketPlaceholders(p, result.toString()) : "";
|
||||
} catch (ScriptException ex) {
|
||||
ex.printStackTrace();
|
||||
}
|
||||
return "Script error";
|
||||
}
|
||||
|
||||
public ScriptData getData() {
|
||||
// this should never be null but just in case setData(null) is called
|
||||
if (data == null) {
|
||||
data = new ScriptData();
|
||||
}
|
||||
return data;
|
||||
}
|
||||
|
||||
public void setData(ScriptData data) {
|
||||
this.data = data;
|
||||
}
|
||||
|
||||
public boolean loadData() {
|
||||
|
||||
cfg = new YamlConfiguration();
|
||||
|
||||
if (!dataFile.exists()) {
|
||||
return false;
|
||||
}
|
||||
try {
|
||||
cfg.load(dataFile);
|
||||
} catch (IOException e) {
|
||||
e.printStackTrace();
|
||||
return false;
|
||||
} catch (InvalidConfigurationException e) {
|
||||
e.printStackTrace();
|
||||
return false;
|
||||
}
|
||||
|
||||
final Set<String> keys = cfg.getKeys(true);
|
||||
|
||||
if (keys == null || keys.isEmpty()) {
|
||||
return false;
|
||||
}
|
||||
|
||||
if (data == null) {
|
||||
data = new ScriptData();
|
||||
} else {
|
||||
data.clear();
|
||||
}
|
||||
|
||||
keys.stream().forEach(k -> {
|
||||
data.set(k, cfg.get(k));
|
||||
});
|
||||
|
||||
if (!data.isEmpty()) {
|
||||
this.setData(data);
|
||||
return true;
|
||||
}
|
||||
return false;
|
||||
}
|
||||
|
||||
public boolean saveData() {
|
||||
if (data == null || data.isEmpty()) {
|
||||
return false;
|
||||
}
|
||||
|
||||
if (cfg == null) {
|
||||
return false;
|
||||
}
|
||||
|
||||
data.getData().entrySet().forEach(e -> {
|
||||
cfg.set(e.getKey(), e.getValue());
|
||||
});
|
||||
|
||||
try {
|
||||
cfg.save(dataFile);
|
||||
return true;
|
||||
} catch (IOException e) {
|
||||
return false;
|
||||
}
|
||||
}
|
||||
|
||||
public void cleanup() {
|
||||
if (this.data != null) {
|
||||
this.data.clear();
|
||||
this.data = null;
|
||||
}
|
||||
this.cfg = null;
|
||||
}
|
||||
}
|
||||
|
Loading…
x
Reference in New Issue
Block a user