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">
|
<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>
|
<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.4.2</version>
|
<version>1.5.0</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>
|
||||||
|
|
||||||
<repositories>
|
<repositories>
|
||||||
<repository>
|
<repository>
|
||||||
<id>spigot-repo</id>
|
<id>spigot-repo</id>
|
||||||
<url>https://hub.spigotmc.org/nexus/content/repositories/snapshots/</url>
|
<url>https://hub.spigotmc.org/nexus/content/repositories/snapshots/</url>
|
||||||
</repository>
|
</repository>
|
||||||
<repository>
|
<repository>
|
||||||
<id>placeholderapi</id>
|
<id>placeholderapi</id>
|
||||||
<url>http://repo.extendedclip.com/content/repositories/placeholderapi/</url>
|
<url>https://repo.extendedclip.com/content/repositories/placeholderapi/</url>
|
||||||
</repository>
|
</repository>
|
||||||
</repositories>
|
</repositories>
|
||||||
|
|
||||||
<dependencies>
|
<dependencies>
|
||||||
<dependency>
|
<dependency>
|
||||||
<groupId>org.spigotmc</groupId>
|
<groupId>org.spigotmc</groupId>
|
||||||
<artifactId>spigot-api</artifactId>
|
<artifactId>spigot-api</artifactId>
|
||||||
<version>1.12.2-R0.1-SNAPSHOT</version>
|
<version>1.14.1-R0.1-SNAPSHOT</version>
|
||||||
<scope>provided</scope>
|
<scope>provided</scope>
|
||||||
</dependency>
|
</dependency>
|
||||||
<dependency>
|
<dependency>
|
||||||
<groupId>me.clip</groupId>
|
<groupId>me.clip</groupId>
|
||||||
<artifactId>placeholderapi</artifactId>
|
<artifactId>placeholderapi</artifactId>
|
||||||
<version>LATEST</version>
|
<version>2.10.2</version>
|
||||||
<scope>provided</scope>
|
<scope>provided</scope>
|
||||||
</dependency>
|
</dependency>
|
||||||
</dependencies>
|
</dependencies>
|
||||||
|
|
||||||
<build>
|
<build>
|
||||||
<plugins>
|
<plugins>
|
||||||
<plugin>
|
<plugin>
|
||||||
<groupId>org.apache.maven.plugins</groupId>
|
<groupId>org.apache.maven.plugins</groupId>
|
||||||
<artifactId>maven-jar-plugin</artifactId>
|
<artifactId>maven-jar-plugin</artifactId>
|
||||||
<version>3.0.2</version>
|
<version>3.0.2</version>
|
||||||
<configuration>
|
<configuration>
|
||||||
<finalName>${name}</finalName>
|
<finalName>${name}</finalName>
|
||||||
<archive>
|
<archive>
|
||||||
<manifest>
|
<manifest>
|
||||||
<addDefaultImplementationEntries>true</addDefaultImplementationEntries>
|
<addDefaultImplementationEntries>true</addDefaultImplementationEntries>
|
||||||
<addDefaultSpecificationEntries>true</addDefaultSpecificationEntries>
|
<addDefaultSpecificationEntries>true</addDefaultSpecificationEntries>
|
||||||
</manifest>
|
</manifest>
|
||||||
</archive>
|
</archive>
|
||||||
</configuration>
|
</configuration>
|
||||||
</plugin>
|
</plugin>
|
||||||
<plugin>
|
<plugin>
|
||||||
<groupId>org.apache.maven.plugins</groupId>
|
<groupId>org.apache.maven.plugins</groupId>
|
||||||
<artifactId>maven-compiler-plugin</artifactId>
|
<artifactId>maven-compiler-plugin</artifactId>
|
||||||
<version>3.7.0</version>
|
<version>3.7.0</version>
|
||||||
<configuration>
|
<configuration>
|
||||||
<target>1.8</target>
|
<target>1.8</target>
|
||||||
<source>1.8</source>
|
<source>1.8</source>
|
||||||
<encoding>UTF-8</encoding>
|
<encoding>UTF-8</encoding>
|
||||||
<useIncrementalCompilation>false</useIncrementalCompilation>
|
<useIncrementalCompilation>false</useIncrementalCompilation>
|
||||||
</configuration>
|
</configuration>
|
||||||
</plugin>
|
</plugin>
|
||||||
</plugins>
|
</plugins>
|
||||||
</build>
|
</build>
|
||||||
|
|
||||||
|
|
||||||
</project>
|
</project>
|
||||||
|
@ -1,237 +1,262 @@
|
|||||||
/*
|
/*
|
||||||
*
|
*
|
||||||
* Javascript-Expansion
|
* Javascript-Expansion
|
||||||
* Copyright (C) 2018 Ryan McCarthy
|
* Copyright (C) 2018 Ryan McCarthy
|
||||||
*
|
*
|
||||||
* This program is free software: you can redistribute it and/or modify
|
* 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
|
* it under the terms of the GNU General Public License as published by
|
||||||
* the Free Software Foundation, either version 3 of the License, or
|
* the Free Software Foundation, either version 3 of the License, or
|
||||||
* (at your option) any later version.
|
* (at your option) any later version.
|
||||||
*
|
*
|
||||||
* This program is distributed in the hope that it will be useful,
|
* This program is distributed in the hope that it will be useful,
|
||||||
* but WITHOUT ANY WARRANTY; without even the implied warranty of
|
* but WITHOUT ANY WARRANTY; without even the implied warranty of
|
||||||
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
||||||
* GNU General Public License for more details.
|
* GNU General Public License for more details.
|
||||||
*
|
*
|
||||||
* You should have received a copy of the GNU General Public License
|
* You should have received a copy of the GNU General Public License
|
||||||
* along with this program. If not, see <http://www.gnu.org/licenses/>.
|
* along with this program. If not, see <http://www.gnu.org/licenses/>.
|
||||||
*
|
*
|
||||||
*
|
*
|
||||||
*/
|
*/
|
||||||
package com.extendedclip.papi.expansion.javascript;
|
package com.extendedclip.papi.expansion.javascript;
|
||||||
|
|
||||||
import java.util.ArrayList;
|
import java.util.ArrayList;
|
||||||
import java.util.HashMap;
|
import java.util.HashMap;
|
||||||
import java.util.HashSet;
|
import java.util.HashSet;
|
||||||
import java.util.List;
|
import java.util.List;
|
||||||
import java.util.Map;
|
import java.util.Map;
|
||||||
import java.util.Set;
|
import java.util.Set;
|
||||||
|
|
||||||
import javax.script.ScriptEngine;
|
import javax.script.ScriptEngine;
|
||||||
import javax.script.ScriptEngineManager;
|
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.Cacheable;
|
||||||
import me.clip.placeholderapi.expansion.PlaceholderExpansion;
|
import me.clip.placeholderapi.expansion.Configurable;
|
||||||
import me.clip.placeholderapi.util.Msg;
|
import me.clip.placeholderapi.expansion.PlaceholderExpansion;
|
||||||
|
|
||||||
import org.bukkit.entity.Player;
|
import org.bukkit.ChatColor;
|
||||||
import org.bukkit.event.EventHandler;
|
import org.bukkit.OfflinePlayer;
|
||||||
import org.bukkit.event.Listener;
|
import org.bukkit.entity.Player;
|
||||||
import org.bukkit.event.player.PlayerCommandPreprocessEvent;
|
import org.bukkit.event.EventHandler;
|
||||||
|
import org.bukkit.event.Listener;
|
||||||
public class JavascriptExpansion extends PlaceholderExpansion implements Cacheable, Configurable, Listener {
|
import org.bukkit.event.player.PlayerCommandPreprocessEvent;
|
||||||
|
|
||||||
private ScriptEngine globalEngine = null;
|
public class JavascriptExpansion extends PlaceholderExpansion implements Cacheable, Configurable, Listener {
|
||||||
|
|
||||||
private JavascriptPlaceholdersConfig config;
|
private ScriptEngine globalEngine = null;
|
||||||
|
|
||||||
private final Set<JavascriptPlaceholder> scripts = new HashSet<>();
|
private JavascriptPlaceholdersConfig config;
|
||||||
|
|
||||||
private final String VERSION = getClass().getPackage().getImplementationVersion();
|
private final Set<JavascriptPlaceholder> scripts = new HashSet<>();
|
||||||
|
|
||||||
private static JavascriptExpansion instance;
|
private final String VERSION = getClass().getPackage().getImplementationVersion();
|
||||||
|
|
||||||
public JavascriptExpansion() {
|
private static JavascriptExpansion instance;
|
||||||
instance = this;
|
|
||||||
}
|
public JavascriptExpansion() {
|
||||||
|
instance = this;
|
||||||
/*
|
}
|
||||||
* I am just testing the waters here because there is no command system for expansions...
|
|
||||||
*/
|
private boolean debug = false;
|
||||||
@EventHandler
|
|
||||||
public void onCmdExecute(PlayerCommandPreprocessEvent event) {
|
/*
|
||||||
|
* I am just testing the waters here because there is no command system for expansions...
|
||||||
String msg = event.getMessage();
|
*/
|
||||||
|
@EventHandler
|
||||||
if (!msg.startsWith("/papijsp")) {
|
public void onCmdExecute(PlayerCommandPreprocessEvent event) {
|
||||||
return;
|
|
||||||
}
|
String msg = event.getMessage();
|
||||||
|
|
||||||
if (!event.getPlayer().hasPermission("placeholderapi.admin")) {
|
if (!msg.startsWith("/papijsp")) {
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
|
|
||||||
event.setCancelled(true);
|
if (!event.getPlayer().hasPermission("placeholderapi.admin")) {
|
||||||
|
return;
|
||||||
Player p = event.getPlayer();
|
}
|
||||||
|
|
||||||
// default command
|
event.setCancelled(true);
|
||||||
if (!msg.contains(" ")) {
|
|
||||||
Msg.msg(p, "&7Javascript expansion v: &f" + getVersion());
|
Player p = event.getPlayer();
|
||||||
Msg.msg(p, "&7Created by: &f" + getAuthor());
|
|
||||||
Msg.msg(p, "&fWiki: &ahttps://github.com/PlaceholderAPI-Expansions/Javascript-Expansion/wiki");
|
// default command
|
||||||
Msg.msg(p, "&r");
|
if (!msg.contains(" ")) {
|
||||||
Msg.msg(p, "&7/papijsp reload &7- &fReload your javascripts without reloading PlaceholderAPI");
|
msg(p, "&7Javascript expansion v: &f" + getVersion());
|
||||||
Msg.msg(p, "&7/papijsp list &7- &fList loaded script identifiers.");
|
msg(p, "&7Created by: &f" + getAuthor());
|
||||||
return;
|
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");
|
||||||
if (msg.equals("/papijsp reload")) {
|
msg(p, "&7/papijsp list &7- &fList loaded script identifiers.");
|
||||||
Msg.msg(p, "&aReloading...");
|
return;
|
||||||
int l = this.reloadScripts();
|
}
|
||||||
Msg.msg(p, l + " &7script" + (l == 1 ? "" : "s")+ " loaded");
|
|
||||||
return;
|
if (msg.equals("/papijsp reload")) {
|
||||||
}
|
msg(p, "&aReloading...");
|
||||||
|
int l = this.reloadScripts();
|
||||||
if (msg.equals("/papijsp list")) {
|
msg(p, l + " &7script" + (l == 1 ? "" : "s")+ " loaded");
|
||||||
List<String> loaded = this.getLoadedIdentifiers();
|
return;
|
||||||
Msg.msg(p, loaded.size() + " &7script" + (loaded.size() == 1 ? "" : "s")+ " loaded");
|
}
|
||||||
Msg.msg(p, String.join(", ", loaded));
|
|
||||||
return;
|
if (msg.equals("/papijsp list")) {
|
||||||
}
|
List<String> loaded = this.getLoadedIdentifiers();
|
||||||
|
msg(p, loaded.size() + " &7script" + (loaded.size() == 1 ? "" : "s")+ " loaded");
|
||||||
event.getPlayer().sendMessage("&cIncorrect usage &7- &f/papijsp");
|
msg(p, String.join(", ", loaded));
|
||||||
}
|
return;
|
||||||
|
}
|
||||||
@Override
|
|
||||||
public String getAuthor() {
|
event.getPlayer().sendMessage("&cIncorrect usage &7- &f/papijsp");
|
||||||
return "clip";
|
}
|
||||||
}
|
|
||||||
|
public void msg(Player p, String text) {
|
||||||
@Override
|
p.sendMessage(ChatColor.translateAlternateColorCodes('&', text));
|
||||||
public String getIdentifier() {
|
}
|
||||||
return "javascript";
|
|
||||||
}
|
@Override
|
||||||
|
public String getAuthor() {
|
||||||
@Override
|
return "clip";
|
||||||
public String getPlugin() {
|
}
|
||||||
return null;
|
|
||||||
}
|
@Override
|
||||||
|
public String getIdentifier() {
|
||||||
@Override
|
return "javascript";
|
||||||
public String getVersion() {
|
}
|
||||||
return VERSION;
|
|
||||||
}
|
@Override
|
||||||
|
public String getPlugin() {
|
||||||
@Override
|
return null;
|
||||||
public boolean register() {
|
}
|
||||||
if (globalEngine == null) {
|
|
||||||
try {
|
@Override
|
||||||
globalEngine = new ScriptEngineManager().getEngineByName(getString("engine", "javascript"));
|
public String getVersion() {
|
||||||
} catch (NullPointerException ex) {
|
return VERSION;
|
||||||
getPlaceholderAPI().getLogger().warning("Javascript engine type was invalid! Defaulting to 'javascript'");
|
}
|
||||||
globalEngine = new ScriptEngineManager().getEngineByName("javascript");
|
|
||||||
}
|
@Override
|
||||||
}
|
public boolean register() {
|
||||||
|
if (globalEngine == null) {
|
||||||
config = new JavascriptPlaceholdersConfig(this);
|
try {
|
||||||
config.loadPlaceholders();
|
globalEngine = new ScriptEngineManager().getEngineByName(getString("engine", "nashorn"));
|
||||||
return super.register();
|
} catch (NullPointerException ex) {
|
||||||
}
|
getPlaceholderAPI().getLogger().warning("Javascript engine type was invalid! Defaulting to 'nashorn'");
|
||||||
|
globalEngine = new ScriptEngineManager().getEngineByName("nashorn");
|
||||||
@Override
|
}
|
||||||
public void clear() {
|
}
|
||||||
scripts.forEach(s -> {
|
debug = (boolean) get("debug", false);
|
||||||
s.saveData();
|
config = new JavascriptPlaceholdersConfig(this);
|
||||||
s.cleanup();
|
config.loadPlaceholders();
|
||||||
});
|
if (debug) {
|
||||||
scripts.clear();
|
System.out.println("Java version: " + System.getProperty("java.version"));
|
||||||
globalEngine = null;
|
|
||||||
instance = null;
|
ScriptEngineManager manager = new ScriptEngineManager();
|
||||||
}
|
List<ScriptEngineFactory> factories = manager.getEngineFactories();
|
||||||
|
System.out.println("displaying all script engine factories:");
|
||||||
@Override
|
for (ScriptEngineFactory factory : factories) {
|
||||||
public String onPlaceholderRequest(Player p, String identifier) {
|
System.out.println("Engine name: " + factory.getEngineName());
|
||||||
if (p == null) {
|
System.out.println("version: " + factory.getEngineVersion());
|
||||||
return "";
|
System.out.println("lang name: " + factory.getLanguageName());
|
||||||
}
|
System.out.println("lang version: " + factory.getLanguageVersion());
|
||||||
|
System.out.println("extensions: " + factory.getExtensions());
|
||||||
if (scripts.isEmpty()) {
|
System.out.println("mime types: " + factory.getMimeTypes());
|
||||||
return null;
|
System.out.println("names: " + factory.getNames());
|
||||||
}
|
}
|
||||||
|
}
|
||||||
for (JavascriptPlaceholder script : scripts) {
|
return super.register();
|
||||||
if (identifier.startsWith(script.getIdentifier() + "_")) {
|
}
|
||||||
identifier = identifier.replace(script.getIdentifier() + "_", "");
|
|
||||||
return !identifier.contains(",") ? script.evaluate(p, identifier) : script.evaluate(p, identifier.split(","));
|
@Override
|
||||||
} else if (identifier.equalsIgnoreCase(script.getIdentifier())) {
|
public void clear() {
|
||||||
return script.evaluate(p);
|
scripts.forEach(s -> {
|
||||||
}
|
s.saveData();
|
||||||
}
|
s.cleanup();
|
||||||
return null;
|
});
|
||||||
}
|
scripts.clear();
|
||||||
|
globalEngine = null;
|
||||||
public boolean addJSPlaceholder(JavascriptPlaceholder p) {
|
instance = null;
|
||||||
if (p == null) {
|
}
|
||||||
return false;
|
|
||||||
}
|
@Override
|
||||||
|
public String onRequest( OfflinePlayer p, String identifier) {
|
||||||
if (scripts.isEmpty()) {
|
if (p == null) {
|
||||||
scripts.add(p);
|
return "";
|
||||||
return true;
|
}
|
||||||
}
|
|
||||||
|
if (scripts.isEmpty()) {
|
||||||
if (scripts.stream().filter(s -> s.getIdentifier().equalsIgnoreCase(p.getIdentifier())).findFirst().orElse(null) != null) {
|
return null;
|
||||||
return false;
|
}
|
||||||
}
|
|
||||||
|
for (JavascriptPlaceholder script : scripts) {
|
||||||
scripts.add(p);
|
if (identifier.startsWith(script.getIdentifier() + "_")) {
|
||||||
return true;
|
identifier = identifier.replace(script.getIdentifier() + "_", "");
|
||||||
}
|
return !identifier.contains(",") ? script.evaluate(p, identifier) : script.evaluate(p, identifier.split(","));
|
||||||
|
} else if (identifier.equalsIgnoreCase(script.getIdentifier())) {
|
||||||
public Set<JavascriptPlaceholder> getJSPlaceholders() {
|
return script.evaluate(p);
|
||||||
return scripts;
|
}
|
||||||
}
|
}
|
||||||
|
return null;
|
||||||
public List<String> getLoadedIdentifiers() {
|
}
|
||||||
List<String> l = new ArrayList<>();
|
|
||||||
scripts.forEach(s -> {
|
public boolean addJSPlaceholder(JavascriptPlaceholder p) {
|
||||||
l.add(s.getIdentifier());
|
if (p == null) {
|
||||||
});
|
return false;
|
||||||
return l;
|
}
|
||||||
}
|
|
||||||
|
if (scripts.isEmpty()) {
|
||||||
public JavascriptPlaceholder getJSPlaceholder(String identifier) {
|
scripts.add(p);
|
||||||
return scripts.stream().filter(s -> s.getIdentifier().equalsIgnoreCase(identifier)).findFirst().orElse(null);
|
return true;
|
||||||
}
|
}
|
||||||
|
|
||||||
public int getAmountLoaded() {
|
if (scripts.stream().filter(s -> s.getIdentifier().equalsIgnoreCase(p.getIdentifier())).findFirst().orElse(null) != null) {
|
||||||
return scripts.size();
|
return false;
|
||||||
}
|
}
|
||||||
|
|
||||||
public ScriptEngine getGlobalEngine() {
|
scripts.add(p);
|
||||||
return globalEngine;
|
return true;
|
||||||
}
|
}
|
||||||
|
|
||||||
@Override
|
public Set<JavascriptPlaceholder> getJSPlaceholders() {
|
||||||
public Map<String, Object> getDefaults() {
|
return scripts;
|
||||||
Map<String, Object> def = new HashMap<String, Object>();
|
}
|
||||||
def.put("engine", "javascript");
|
|
||||||
return def;
|
public List<String> getLoadedIdentifiers() {
|
||||||
}
|
List<String> l = new ArrayList<>();
|
||||||
|
scripts.forEach(s -> {
|
||||||
private int reloadScripts() {
|
l.add(s.getIdentifier());
|
||||||
scripts.forEach(s -> {
|
});
|
||||||
s.saveData();
|
return l;
|
||||||
s.cleanup();
|
}
|
||||||
});
|
|
||||||
scripts.clear();
|
public JavascriptPlaceholder getJSPlaceholder(String identifier) {
|
||||||
config.reload();
|
return scripts.stream().filter(s -> s.getIdentifier().equalsIgnoreCase(identifier)).findFirst().orElse(null);
|
||||||
return config.loadPlaceholders();
|
}
|
||||||
}
|
|
||||||
|
public int getAmountLoaded() {
|
||||||
public static JavascriptExpansion getInstance() {
|
return scripts.size();
|
||||||
return instance;
|
}
|
||||||
}
|
|
||||||
}
|
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
|
* Javascript-Expansion
|
||||||
* Copyright (C) 2018 Ryan McCarthy
|
* Copyright (C) 2018 Ryan McCarthy
|
||||||
*
|
*
|
||||||
* This program is free software: you can redistribute it and/or modify
|
* 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
|
* it under the terms of the GNU General Public License as published by
|
||||||
* the Free Software Foundation, either version 3 of the License, or
|
* the Free Software Foundation, either version 3 of the License, or
|
||||||
* (at your option) any later version.
|
* (at your option) any later version.
|
||||||
*
|
*
|
||||||
* This program is distributed in the hope that it will be useful,
|
* This program is distributed in the hope that it will be useful,
|
||||||
* but WITHOUT ANY WARRANTY; without even the implied warranty of
|
* but WITHOUT ANY WARRANTY; without even the implied warranty of
|
||||||
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
||||||
* GNU General Public License for more details.
|
* GNU General Public License for more details.
|
||||||
*
|
*
|
||||||
* You should have received a copy of the GNU General Public License
|
* You should have received a copy of the GNU General Public License
|
||||||
* along with this program. If not, see <http://www.gnu.org/licenses/>.
|
* along with this program. If not, see <http://www.gnu.org/licenses/>.
|
||||||
*
|
*
|
||||||
*
|
*
|
||||||
*/
|
*/
|
||||||
package com.extendedclip.papi.expansion.javascript;
|
package com.extendedclip.papi.expansion.javascript;
|
||||||
|
|
||||||
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 javax.script.ScriptEngine;
|
||||||
import javax.script.ScriptEngine;
|
import javax.script.ScriptException;
|
||||||
import javax.script.ScriptException;
|
import me.clip.placeholderapi.PlaceholderAPI;
|
||||||
|
import me.clip.placeholderapi.PlaceholderAPIPlugin;
|
||||||
import org.apache.commons.lang.Validate;
|
import org.apache.commons.lang.Validate;
|
||||||
import org.bukkit.Bukkit;
|
import org.bukkit.Bukkit;
|
||||||
import org.bukkit.configuration.InvalidConfigurationException;
|
import org.bukkit.OfflinePlayer;
|
||||||
import org.bukkit.configuration.file.FileConfiguration;
|
import org.bukkit.configuration.InvalidConfigurationException;
|
||||||
import org.bukkit.configuration.file.YamlConfiguration;
|
import org.bukkit.configuration.file.FileConfiguration;
|
||||||
import org.bukkit.entity.Player;
|
import org.bukkit.configuration.file.YamlConfiguration;
|
||||||
|
|
||||||
import me.clip.placeholderapi.PlaceholderAPI;
|
public class JavascriptPlaceholder {
|
||||||
import me.clip.placeholderapi.PlaceholderAPIPlugin;
|
|
||||||
|
private ScriptEngine engine = null;
|
||||||
public class JavascriptPlaceholder {
|
private String identifier;
|
||||||
|
private String script;
|
||||||
private ScriptEngine engine = null;
|
private ScriptData data = null;
|
||||||
|
private File dataFile;
|
||||||
private String identifier;
|
private FileConfiguration cfg;
|
||||||
|
|
||||||
private String script;
|
private final String FILEDIR = PlaceholderAPIPlugin.getInstance().getDataFolder() + File.separator + "javascripts"+ File.separator + "javascript_data";
|
||||||
|
|
||||||
private ScriptData data = null;
|
public JavascriptPlaceholder(ScriptEngine engine, String identifier, String script) {
|
||||||
|
Validate.notNull(engine, "ScriptEngine can not be null");
|
||||||
private File dataFile;
|
Validate.notNull(identifier, "Identifier can not be null");
|
||||||
|
Validate.notNull(script, "script can not be null");
|
||||||
private FileConfiguration cfg;
|
this.engine = engine;
|
||||||
|
this.identifier = identifier;
|
||||||
private final String FILEDIR = PlaceholderAPIPlugin.getInstance().getDataFolder() + File.separator + "javascripts" + File.separator + "javascript_data";
|
this.script = script;
|
||||||
|
File dir = new File(FILEDIR);
|
||||||
public JavascriptPlaceholder(ScriptEngine engine, String identifier, String script) {
|
|
||||||
Validate.notNull(engine, "ScriptEngine can not be null");
|
try {
|
||||||
Validate.notNull(identifier, "Identifier can not be null");
|
if (!dir.exists()) {
|
||||||
Validate.notNull(script, "script can not be null");
|
dir.mkdirs();
|
||||||
this.engine = engine;
|
}
|
||||||
this.identifier = identifier;
|
} catch (SecurityException e) {
|
||||||
this.script = script;
|
e.printStackTrace();
|
||||||
File dir = new File(FILEDIR);
|
}
|
||||||
|
data = new ScriptData();
|
||||||
try {
|
dataFile = new File(FILEDIR, identifier + "_data.yml");
|
||||||
if (!dir.exists()) {
|
engine.put("Data", data);
|
||||||
dir.mkdirs();
|
engine.put("BukkitServer", Bukkit.getServer());
|
||||||
}
|
engine.put("Expansion", JavascriptExpansion.getInstance());
|
||||||
} catch (SecurityException e) {
|
engine.put("Placeholder", this);
|
||||||
e.printStackTrace();
|
}
|
||||||
}
|
|
||||||
data = new ScriptData();
|
public String getIdentifier() {
|
||||||
dataFile = new File(FILEDIR, identifier + "_data.yml");
|
return identifier;
|
||||||
engine.put("Data", data);
|
}
|
||||||
engine.put("BukkitServer", Bukkit.getServer());
|
|
||||||
engine.put("Expansion", JavascriptExpansion.getInstance());
|
public String getScript() {
|
||||||
engine.put("Placeholder", this);
|
return script;
|
||||||
}
|
}
|
||||||
|
|
||||||
public String getIdentifier() {
|
public String evaluate(OfflinePlayer p, String... args) {
|
||||||
return identifier;
|
String exp = PlaceholderAPI.setPlaceholders(p, script);
|
||||||
}
|
|
||||||
|
try {
|
||||||
public String getScript() {
|
String[] c = null;
|
||||||
return script;
|
|
||||||
}
|
if (args != null && args.length > 0) {
|
||||||
|
for (int i = 0; i < args.length; i++) {
|
||||||
public String evaluate(Player p, String... args) {
|
if (args[i] == null || args[i].isEmpty()) {
|
||||||
String exp = PlaceholderAPI.setPlaceholders(p, script);
|
continue;
|
||||||
|
}
|
||||||
try {
|
|
||||||
String[] c = null;
|
String s = PlaceholderAPI.setBracketPlaceholders(p, args[i]);
|
||||||
|
|
||||||
if (args != null && args.length > 0) {
|
if (c == null) {
|
||||||
for (int i = 0 ; i < args.length ; i++) {
|
c = new String[args.length];
|
||||||
if (args[i] == null || args[i].isEmpty()) {
|
}
|
||||||
continue;
|
|
||||||
}
|
c[i] = s;
|
||||||
|
}
|
||||||
String s = PlaceholderAPI.setBracketPlaceholders(p, args[i]);
|
}
|
||||||
|
|
||||||
if (c == null) {
|
if (c == null) {
|
||||||
c = new String[args.length];
|
c = new String[]{};
|
||||||
}
|
}
|
||||||
|
|
||||||
c[i] = s;
|
engine.put("args", c);
|
||||||
}
|
engine.put("BukkitPlayer", p != null && p.isOnline() ? p.getPlayer() : null);
|
||||||
}
|
engine.put("OfflinePlayer", p);
|
||||||
|
Object result = engine.eval(exp);
|
||||||
if (c == null) {
|
return result != null ? PlaceholderAPI.setBracketPlaceholders(p, result.toString()) : "";
|
||||||
c = new String[]{};
|
} catch (ScriptException ex) {
|
||||||
}
|
ex.printStackTrace();
|
||||||
|
}
|
||||||
engine.put("args", c);
|
return "Script error";
|
||||||
engine.put("BukkitPlayer", p);
|
}
|
||||||
Object result = engine.eval(exp);
|
|
||||||
return result != null ? PlaceholderAPI.setBracketPlaceholders(p, result.toString()) : "";
|
public ScriptData getData() {
|
||||||
|
// this should never be null but just in case setData(null) is called
|
||||||
} catch (ScriptException ex) {
|
if (data == null) {
|
||||||
ex.printStackTrace();
|
data = new ScriptData();
|
||||||
}
|
}
|
||||||
return "Script error";
|
return data;
|
||||||
}
|
}
|
||||||
|
|
||||||
public ScriptData getData() {
|
public void setData(ScriptData data) {
|
||||||
// this should never be null but just in case setData(null) is called
|
this.data = data;
|
||||||
if (data == null) {
|
}
|
||||||
data = new ScriptData();
|
|
||||||
}
|
public boolean loadData() {
|
||||||
return data;
|
|
||||||
}
|
cfg = new YamlConfiguration();
|
||||||
|
|
||||||
public void setData(ScriptData data) {
|
if (!dataFile.exists()) {
|
||||||
this.data = data;
|
return false;
|
||||||
}
|
}
|
||||||
|
try {
|
||||||
public boolean loadData() {
|
cfg.load(dataFile);
|
||||||
|
} catch (IOException e) {
|
||||||
cfg = new YamlConfiguration();
|
e.printStackTrace();
|
||||||
|
return false;
|
||||||
if (!dataFile.exists()) {
|
} catch (InvalidConfigurationException e) {
|
||||||
return false;
|
e.printStackTrace();
|
||||||
}
|
return false;
|
||||||
try {
|
}
|
||||||
cfg.load(dataFile);
|
|
||||||
} catch (IOException e) {
|
final Set<String> keys = cfg.getKeys(true);
|
||||||
e.printStackTrace();
|
|
||||||
return false;
|
if (keys == null || keys.isEmpty()) {
|
||||||
} catch (InvalidConfigurationException e) {
|
return false;
|
||||||
e.printStackTrace();
|
}
|
||||||
return false;
|
|
||||||
}
|
if (data == null) {
|
||||||
|
data = new ScriptData();
|
||||||
final Set<String> keys = cfg.getKeys(true);
|
} else {
|
||||||
|
data.clear();
|
||||||
if (keys == null || keys.isEmpty()) {
|
}
|
||||||
return false;
|
|
||||||
}
|
keys.stream().forEach(k -> {
|
||||||
|
data.set(k, cfg.get(k));
|
||||||
if (data == null) {
|
});
|
||||||
data = new ScriptData();
|
|
||||||
} else {
|
if (!data.isEmpty()) {
|
||||||
data.clear();
|
this.setData(data);
|
||||||
}
|
return true;
|
||||||
|
}
|
||||||
keys.stream().forEach(k -> {
|
return false;
|
||||||
data.set(k, cfg.get(k));
|
}
|
||||||
});
|
|
||||||
|
public boolean saveData() {
|
||||||
if (!data.isEmpty()) {
|
if (data == null || data.isEmpty()) {
|
||||||
this.setData(data);
|
return false;
|
||||||
return true;
|
}
|
||||||
}
|
|
||||||
return false;
|
if (cfg == null) {
|
||||||
}
|
return false;
|
||||||
|
}
|
||||||
public boolean saveData() {
|
|
||||||
if (data == null || data.isEmpty()) {
|
data.getData().entrySet().forEach(e -> {
|
||||||
return false;
|
cfg.set(e.getKey(), e.getValue());
|
||||||
}
|
});
|
||||||
|
|
||||||
if (cfg == null) {
|
try {
|
||||||
return false;
|
cfg.save(dataFile);
|
||||||
}
|
return true;
|
||||||
|
} catch (IOException e) {
|
||||||
data.getData().entrySet().forEach(e -> {
|
return false;
|
||||||
cfg.set(e.getKey(), e.getValue());
|
}
|
||||||
});
|
}
|
||||||
|
|
||||||
try {
|
public void cleanup() {
|
||||||
cfg.save(dataFile);
|
if (this.data != null) {
|
||||||
return true;
|
this.data.clear();
|
||||||
} catch (IOException e) {
|
this.data = null;
|
||||||
return false;
|
}
|
||||||
}
|
this.cfg = null;
|
||||||
}
|
}
|
||||||
|
}
|
||||||
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