mirror of
https://github.com/PlaceholderAPI/Javascript-Expansion.git
synced 2025-05-23 18:42:44 +00:00
Scripts available on Github now fetched from repo master list
This commit is contained in:
parent
66f75c7ab5
commit
d32eb34d6d
@ -20,10 +20,9 @@
|
|||||||
*/
|
*/
|
||||||
package com.extendedclip.papi.expansion.javascript;
|
package com.extendedclip.papi.expansion.javascript;
|
||||||
|
|
||||||
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 java.lang.reflect.Field;
|
||||||
import java.util.ArrayList;
|
import java.util.ArrayList;
|
||||||
import java.util.Arrays;
|
|
||||||
import java.util.HashMap;
|
import java.util.HashMap;
|
||||||
import java.util.HashSet;
|
import java.util.HashSet;
|
||||||
import java.util.List;
|
import java.util.List;
|
||||||
@ -39,14 +38,11 @@ import me.clip.placeholderapi.expansion.Configurable;
|
|||||||
import me.clip.placeholderapi.expansion.PlaceholderExpansion;
|
import me.clip.placeholderapi.expansion.PlaceholderExpansion;
|
||||||
|
|
||||||
import org.bukkit.Bukkit;
|
import org.bukkit.Bukkit;
|
||||||
import org.bukkit.ChatColor;
|
|
||||||
import org.bukkit.OfflinePlayer;
|
import org.bukkit.OfflinePlayer;
|
||||||
import org.bukkit.entity.Player;
|
import org.bukkit.command.Command;
|
||||||
import org.bukkit.event.EventHandler;
|
import org.bukkit.command.CommandMap;
|
||||||
import org.bukkit.event.Listener;
|
|
||||||
import org.bukkit.event.player.PlayerCommandPreprocessEvent;
|
|
||||||
|
|
||||||
public class JavascriptExpansion extends PlaceholderExpansion implements Cacheable, Configurable, Listener {
|
public class JavascriptExpansion extends PlaceholderExpansion implements Cacheable, Configurable {
|
||||||
|
|
||||||
private ScriptEngine globalEngine = null;
|
private ScriptEngine globalEngine = null;
|
||||||
|
|
||||||
@ -54,123 +50,21 @@ public class JavascriptExpansion extends PlaceholderExpansion implements Cacheab
|
|||||||
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 = false;
|
private boolean debug;
|
||||||
private GithubScriptManager githubScripts = null;
|
private GithubScriptManager githubScripts;
|
||||||
|
private JavascriptExpansionCommands commands;
|
||||||
|
private CommandMap commandMap;
|
||||||
|
|
||||||
|
|
||||||
public JavascriptExpansion() {
|
public JavascriptExpansion() {
|
||||||
instance = this;
|
instance = this;
|
||||||
}
|
try {
|
||||||
|
final Field f = Bukkit.getServer().getClass().getDeclaredField("commandMap");
|
||||||
/*
|
f.setAccessible(true);
|
||||||
* I am just testing the waters here because there is no command system for expansions...
|
commandMap = (CommandMap) f.get(Bukkit.getServer());
|
||||||
*/
|
} catch (Exception e) {
|
||||||
@EventHandler
|
e.printStackTrace();
|
||||||
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.");
|
|
||||||
msg(p, "&7/papijsp git download <name> &7- &fDownload a script from the js expansion github.");
|
|
||||||
msg(p, "&7/papijsp git list &7- &fList available scripts in the js expansion github.");
|
|
||||||
msg(p, "&7/papijsp git info (name) &7- &fGet the description and url of a specific script.");
|
|
||||||
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;
|
|
||||||
}
|
|
||||||
|
|
||||||
if (msg.equals("/papijsp git list")) {
|
|
||||||
msg(p, GithubScript.values().length + " &7script"
|
|
||||||
+ (GithubScript.values().length == 1 ? "" : "s") + " available on Github.");
|
|
||||||
msg(p, String.join(", ", GithubScript.getAllScriptNames()));
|
|
||||||
return;
|
|
||||||
}
|
|
||||||
|
|
||||||
if (msg.startsWith("/papijsp git info ")) {
|
|
||||||
|
|
||||||
if (this.githubScripts == null) {
|
|
||||||
msg(p, "This feature is disabled in the PAPI config!");
|
|
||||||
return;
|
|
||||||
}
|
|
||||||
|
|
||||||
msg = msg.replace("/papijsp git info ", "");
|
|
||||||
|
|
||||||
GithubScript script = GithubScript.getScript(msg);
|
|
||||||
|
|
||||||
if (script == null) {
|
|
||||||
msg(p, "&cThe script &7" + msg + " &cdoes not exist!");
|
|
||||||
return;
|
|
||||||
}
|
|
||||||
|
|
||||||
msg(p, "&7Name: &f" + script.getName(),
|
|
||||||
"&7Version: &f" + script.getVersion(),
|
|
||||||
"&7Description: &f" + script.getDescription(),
|
|
||||||
"&7Url: &f" + script.getUrl());
|
|
||||||
|
|
||||||
return;
|
|
||||||
}
|
|
||||||
|
|
||||||
if (msg.startsWith("/papijsp git download ")) {
|
|
||||||
|
|
||||||
if (this.githubScripts == null) {
|
|
||||||
msg(p, "This feature is disabled in the PAPI config!");
|
|
||||||
return;
|
|
||||||
}
|
|
||||||
|
|
||||||
msg = msg.replace("/papijsp git download ", "");
|
|
||||||
|
|
||||||
GithubScript script = GithubScript.getScript(msg);
|
|
||||||
|
|
||||||
if (script == null) {
|
|
||||||
msg(p, "&cThe script &7" + msg + " &cdoes not exist!");
|
|
||||||
return;
|
|
||||||
}
|
|
||||||
|
|
||||||
Bukkit.getScheduler().runTaskAsynchronously(getPlaceholderAPI(), new Runnable() {
|
|
||||||
@Override
|
|
||||||
public void run() {
|
|
||||||
githubScripts.downloadScript(script);
|
|
||||||
}
|
|
||||||
});
|
|
||||||
msg(p, "&aDownload initiated... Check the scripts folder in a moment...");
|
|
||||||
return;
|
|
||||||
}
|
|
||||||
|
|
||||||
msg(p, "&cIncorrect usage &7- &f/papijsp");
|
|
||||||
}
|
|
||||||
|
|
||||||
public void msg(Player p, String... text) {
|
|
||||||
Arrays.stream(text).forEach(line -> p.sendMessage(ChatColor.translateAlternateColorCodes('&', line)));
|
|
||||||
}
|
}
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
@ -224,16 +118,22 @@ public class JavascriptExpansion extends PlaceholderExpansion implements Cacheab
|
|||||||
}
|
}
|
||||||
if ((Boolean) get("github_script_downloads", false)) {
|
if ((Boolean) get("github_script_downloads", false)) {
|
||||||
githubScripts = new GithubScriptManager(this);
|
githubScripts = new GithubScriptManager(this);
|
||||||
|
githubScripts.fetch();
|
||||||
}
|
}
|
||||||
|
|
||||||
|
registerCommand();
|
||||||
return super.register();
|
return super.register();
|
||||||
}
|
}
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
public void clear() {
|
public void clear() {
|
||||||
|
unregisterCommand();
|
||||||
scripts.forEach(s -> {
|
scripts.forEach(s -> {
|
||||||
s.saveData();
|
s.saveData();
|
||||||
s.cleanup();
|
s.cleanup();
|
||||||
});
|
});
|
||||||
|
githubScripts.clear();
|
||||||
|
githubScripts = null;
|
||||||
scripts.clear();
|
scripts.clear();
|
||||||
globalEngine = null;
|
globalEngine = null;
|
||||||
instance = null;
|
instance = null;
|
||||||
@ -315,7 +215,7 @@ public class JavascriptExpansion extends PlaceholderExpansion implements Cacheab
|
|||||||
return def;
|
return def;
|
||||||
}
|
}
|
||||||
|
|
||||||
private int reloadScripts() {
|
protected int reloadScripts() {
|
||||||
scripts.forEach(s -> {
|
scripts.forEach(s -> {
|
||||||
s.saveData();
|
s.saveData();
|
||||||
s.cleanup();
|
s.cleanup();
|
||||||
@ -328,4 +228,22 @@ public class JavascriptExpansion extends PlaceholderExpansion implements Cacheab
|
|||||||
public static JavascriptExpansion getInstance() {
|
public static JavascriptExpansion getInstance() {
|
||||||
return instance;
|
return instance;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
public GithubScriptManager getGithubScriptManager() {
|
||||||
|
return githubScripts;
|
||||||
|
}
|
||||||
|
|
||||||
|
private boolean unregisterCommand() {
|
||||||
|
if (commandMap == null || commands == null) return false;
|
||||||
|
Command c = commandMap.getCommand(commands.getName());
|
||||||
|
if (c == null) return false;
|
||||||
|
return c.unregister(commandMap);
|
||||||
|
}
|
||||||
|
|
||||||
|
private boolean registerCommand() {
|
||||||
|
if (commandMap == null) return false;
|
||||||
|
commands = new JavascriptExpansionCommands(this);
|
||||||
|
commandMap.register(commands.getName(), commands);
|
||||||
|
return commands.isRegistered();
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
@ -0,0 +1,128 @@
|
|||||||
|
package com.extendedclip.papi.expansion.javascript;
|
||||||
|
|
||||||
|
import com.extendedclip.papi.expansion.javascript.cloud.GithubScript;
|
||||||
|
import com.extendedclip.papi.expansion.javascript.cloud.GithubScriptManager;
|
||||||
|
import java.util.Arrays;
|
||||||
|
import java.util.List;
|
||||||
|
import java.util.stream.Collectors;
|
||||||
|
import org.bukkit.ChatColor;
|
||||||
|
import org.bukkit.command.Command;
|
||||||
|
import org.bukkit.command.CommandSender;
|
||||||
|
|
||||||
|
public class JavascriptExpansionCommands extends Command {
|
||||||
|
|
||||||
|
private JavascriptExpansion expansion;
|
||||||
|
|
||||||
|
public JavascriptExpansionCommands(JavascriptExpansion expansion) {
|
||||||
|
super("jsexpansion");
|
||||||
|
this.expansion = expansion;
|
||||||
|
this.setDescription("Javascript expansion commands");
|
||||||
|
this.setUsage("/jsexpansion <arg>");
|
||||||
|
this.setPermission("placeholderapi.js.admin");
|
||||||
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public boolean execute(CommandSender s, String label, String[] args) {
|
||||||
|
if (!s.hasPermission(this.getPermission())) {
|
||||||
|
msg(s, "&cYou don't have permission to do that!");
|
||||||
|
return true;
|
||||||
|
}
|
||||||
|
|
||||||
|
if (args.length == 0) {
|
||||||
|
msg(s, "&7Javascript expansion v: &f" + expansion.getVersion(),
|
||||||
|
"&7Created by: &f" + expansion.getAuthor(),
|
||||||
|
"&fWiki: &ahttps://github.com/PlaceholderAPI-Expansions/Javascript-Expansion/wiki",
|
||||||
|
"&r",
|
||||||
|
"&7/jsexpansion reload &7- &fReload your javascripts without reloading PlaceholderAPI",
|
||||||
|
"&7/jsexpansion list &7- &fList loaded script identifiers.");
|
||||||
|
if (expansion.getGithubScriptManager() != null) {
|
||||||
|
msg(s,
|
||||||
|
"&7/jsexpansion git download <name> &7- &fDownload a script from the js expansion github.");
|
||||||
|
msg(s, "&7/jsexpansion git list &7- &fList available scripts in the js expansion github.");
|
||||||
|
msg(s,
|
||||||
|
"&7/jsexpansion git info (name) &7- &fGet the description and url of a specific script.");
|
||||||
|
}
|
||||||
|
return true;
|
||||||
|
}
|
||||||
|
|
||||||
|
if (args[0].equalsIgnoreCase("reload")) {
|
||||||
|
msg(s, "&aJavascriptExpansion reloading...");
|
||||||
|
int l = expansion.reloadScripts();
|
||||||
|
msg(s, l + " &7script" + (l == 1 ? "" : "s") + " loaded");
|
||||||
|
return true;
|
||||||
|
}
|
||||||
|
|
||||||
|
if (args[0].equalsIgnoreCase("list")) {
|
||||||
|
List<String> loaded = expansion.getLoadedIdentifiers();
|
||||||
|
msg(s, loaded.size() + " &7script" + (loaded.size() == 1 ? "" : "s") + " loaded");
|
||||||
|
msg(s, String.join(", ", loaded));
|
||||||
|
return true;
|
||||||
|
}
|
||||||
|
|
||||||
|
if (args[0].equalsIgnoreCase("git")) {
|
||||||
|
if (expansion.getGithubScriptManager() == null) {
|
||||||
|
msg(s, "&8This feature is disabled in the PlaceholderAPI config.");
|
||||||
|
return true;
|
||||||
|
}
|
||||||
|
|
||||||
|
if (args.length < 2) {
|
||||||
|
msg(s, "&cIncorrect usage!");
|
||||||
|
return true;
|
||||||
|
}
|
||||||
|
|
||||||
|
GithubScriptManager manager = expansion.getGithubScriptManager();
|
||||||
|
|
||||||
|
if (args[1].equalsIgnoreCase("list")) {
|
||||||
|
msg(s, manager.getAvailableScripts().size() + " &escript"
|
||||||
|
+ (manager.getAvailableScripts().size() == 1 ? "" : "s") + " available on Github.");
|
||||||
|
msg(s, String.join(", ", manager.getAvailableScripts().stream()
|
||||||
|
.map(GithubScript::getName)
|
||||||
|
.collect(Collectors.toSet())));
|
||||||
|
return true;
|
||||||
|
}
|
||||||
|
|
||||||
|
if (args[1].equalsIgnoreCase("info")) {
|
||||||
|
if (args.length < 3) {
|
||||||
|
msg(s, "&4Incorrect usage! &f" + this.getName() + " git info <name>");
|
||||||
|
return true;
|
||||||
|
}
|
||||||
|
GithubScript script = manager.getScript(args[2]);
|
||||||
|
if (script == null) {
|
||||||
|
msg(s, "&4The script &f" + args[2] + " &4does not exist!");
|
||||||
|
return true;
|
||||||
|
}
|
||||||
|
msg(s, "&eName: &f" + script.getName(),
|
||||||
|
"&eVersion: &f" + script.getVersion(),
|
||||||
|
"&eDescription: &f" + script.getDescription(),
|
||||||
|
"&eAuthor: &f" + script.getAuthor(),
|
||||||
|
"&eUrl: &f" + script.getUrl());
|
||||||
|
return true;
|
||||||
|
}
|
||||||
|
if (args[1].equalsIgnoreCase("download")) {
|
||||||
|
if (args.length < 3) {
|
||||||
|
msg(s, "&4Incorrect usage! &f" + this.getName() + " git download <name>");
|
||||||
|
return true;
|
||||||
|
}
|
||||||
|
GithubScript script = manager.getScript(args[2]);
|
||||||
|
|
||||||
|
if (script == null) {
|
||||||
|
msg(s, "&4The script &f" + args[2] + " &4does not exist!");
|
||||||
|
return true;
|
||||||
|
}
|
||||||
|
|
||||||
|
manager.downloadScript(script);
|
||||||
|
msg(s, "&aDownload initiated... &eCheck the scripts folder in a moment...");
|
||||||
|
return true;
|
||||||
|
}
|
||||||
|
msg(s, "&4Incorrect usage! &f" + this.getName() + " &7for more help.");
|
||||||
|
return true;
|
||||||
|
}
|
||||||
|
|
||||||
|
return true;
|
||||||
|
}
|
||||||
|
|
||||||
|
public void msg(CommandSender s, String... text) {
|
||||||
|
Arrays.stream(text)
|
||||||
|
.forEach(line -> s.sendMessage(ChatColor.translateAlternateColorCodes('&', line)));
|
||||||
|
}
|
||||||
|
}
|
@ -36,6 +36,9 @@ import org.bukkit.configuration.file.YamlConfiguration;
|
|||||||
|
|
||||||
public class JavascriptPlaceholder {
|
public class JavascriptPlaceholder {
|
||||||
|
|
||||||
|
private final String FILEDIR =
|
||||||
|
PlaceholderAPIPlugin.getInstance().getDataFolder() + File.separator + "javascripts"
|
||||||
|
+ File.separator + "javascript_data";
|
||||||
private ScriptEngine engine = null;
|
private ScriptEngine engine = null;
|
||||||
private String identifier;
|
private String identifier;
|
||||||
private String script;
|
private String script;
|
||||||
@ -43,8 +46,6 @@ public class JavascriptPlaceholder {
|
|||||||
private File dataFile;
|
private File dataFile;
|
||||||
private FileConfiguration cfg;
|
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) {
|
public JavascriptPlaceholder(ScriptEngine engine, String identifier, String script) {
|
||||||
Validate.notNull(engine, "ScriptEngine can not be null");
|
Validate.notNull(engine, "ScriptEngine can not be null");
|
||||||
Validate.notNull(identifier, "Identifier can not be null");
|
Validate.notNull(identifier, "Identifier can not be null");
|
||||||
|
@ -25,198 +25,209 @@ import java.io.FileNotFoundException;
|
|||||||
import java.io.IOException;
|
import java.io.IOException;
|
||||||
import java.util.Scanner;
|
import java.util.Scanner;
|
||||||
import java.util.logging.Level;
|
import java.util.logging.Level;
|
||||||
|
|
||||||
import javax.script.ScriptEngine;
|
import javax.script.ScriptEngine;
|
||||||
import javax.script.ScriptEngineManager;
|
import javax.script.ScriptEngineManager;
|
||||||
|
|
||||||
import me.clip.placeholderapi.PlaceholderAPIPlugin;
|
import me.clip.placeholderapi.PlaceholderAPIPlugin;
|
||||||
|
|
||||||
import org.bukkit.configuration.file.FileConfiguration;
|
import org.bukkit.configuration.file.FileConfiguration;
|
||||||
import org.bukkit.configuration.file.YamlConfiguration;
|
import org.bukkit.configuration.file.YamlConfiguration;
|
||||||
|
|
||||||
public class JavascriptPlaceholdersConfig {
|
public class JavascriptPlaceholdersConfig {
|
||||||
|
|
||||||
private JavascriptExpansion ex;
|
private JavascriptExpansion ex;
|
||||||
|
|
||||||
private PlaceholderAPIPlugin plugin;
|
private PlaceholderAPIPlugin plugin;
|
||||||
|
|
||||||
private FileConfiguration config;
|
private FileConfiguration config;
|
||||||
|
|
||||||
private File file;
|
private File file;
|
||||||
|
|
||||||
public JavascriptPlaceholdersConfig(JavascriptExpansion ex) {
|
public JavascriptPlaceholdersConfig(JavascriptExpansion ex) {
|
||||||
this.ex = ex;
|
this.ex = ex;
|
||||||
plugin = ex.getPlaceholderAPI();
|
plugin = ex.getPlaceholderAPI();
|
||||||
reload();
|
reload();
|
||||||
}
|
}
|
||||||
|
|
||||||
public void reload() {
|
public void reload() {
|
||||||
if (file == null) {
|
if (file == null) {
|
||||||
file = new File(plugin.getDataFolder(), "javascript_placeholders.yml");
|
file = new File(plugin.getDataFolder(), "javascript_placeholders.yml");
|
||||||
}
|
}
|
||||||
|
|
||||||
config = YamlConfiguration.loadConfiguration(file);
|
config = YamlConfiguration.loadConfiguration(file);
|
||||||
config.options().header("Javascript Expansion: " + ex.getVersion()
|
config.options().header("Javascript Expansion: " + ex.getVersion()
|
||||||
+ "\nThis is the main configuration file for the Javascript Expansion."
|
+ "\nThis is the main configuration file for the Javascript Expansion."
|
||||||
+ "\n"
|
+ "\n"
|
||||||
+ "\nYou will define your javascript placeholders in this file."
|
+ "\nYou will define your javascript placeholders in this file."
|
||||||
+ "\n"
|
+ "\n"
|
||||||
+ "\nJavascript files must be located in the:"
|
+ "\nJavascript files must be located in the:"
|
||||||
+ "\n /plugins/placeholderapi/javascripts/ folder"
|
+ "\n /plugins/placeholderapi/javascripts/ folder"
|
||||||
+ "\n"
|
+ "\n"
|
||||||
+ "\nA detailed guide on how to create your own javascript placeholders"
|
+ "\nA detailed guide on how to create your own javascript placeholders"
|
||||||
+ "\ncan be found here:"
|
+ "\ncan be found here:"
|
||||||
+ "\nhttps://github.com/PlaceholderAPI-Expansions/Javascript-Expansion/wiki"
|
+ "\nhttps://github.com/PlaceholderAPI-Expansions/Javascript-Expansion/wiki"
|
||||||
+ "\n"
|
+ "\n"
|
||||||
+ "\nYour javascript placeholders will be identified by: %javascript_<identifier>%"
|
+ "\nYour javascript placeholders will be identified by: %javascript_<identifier>%"
|
||||||
+ "\n"
|
+ "\n"
|
||||||
+ "\nConfiguration format:"
|
+ "\nConfiguration format:"
|
||||||
+ "\n"
|
+ "\n"
|
||||||
+ "\n<identifier>:"
|
+ "\n<identifier>:"
|
||||||
+ "\n file: <name of file>.<file extension>"
|
+ "\n file: <name of file>.<file extension>"
|
||||||
+ "\n engine: (name of script engine)"
|
+ "\n engine: (name of script engine)"
|
||||||
+ "\n"
|
+ "\n"
|
||||||
+ "\n"
|
+ "\n"
|
||||||
+ "\nExample:"
|
+ "\nExample:"
|
||||||
+ "\n"
|
+ "\n"
|
||||||
+ "\n'my_placeholder':"
|
+ "\n'my_placeholder':"
|
||||||
+ "\n file: 'my_placeholder.js'"
|
+ "\n file: 'my_placeholder.js'"
|
||||||
+ "\n engine: 'nashorn'");
|
+ "\n engine: 'nashorn'");
|
||||||
|
|
||||||
if (config.getKeys(false) == null || config.getKeys(false).isEmpty()) {
|
if (config.getKeys(false) == null || config.getKeys(false).isEmpty()) {
|
||||||
config.set("example.file", "example.js");
|
config.set("example.file", "example.js");
|
||||||
config.set("example.engine", "nashorn");
|
config.set("example.engine", "nashorn");
|
||||||
}
|
}
|
||||||
save();
|
save();
|
||||||
}
|
}
|
||||||
|
|
||||||
public FileConfiguration load() {
|
public FileConfiguration load() {
|
||||||
if (config == null) {
|
if (config == null) {
|
||||||
reload();
|
reload();
|
||||||
}
|
}
|
||||||
return config;
|
return config;
|
||||||
}
|
}
|
||||||
|
|
||||||
public void save() {
|
public void save() {
|
||||||
if ((config == null) || (file == null)) {
|
if ((config == null) || (file == null)) {
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
|
|
||||||
try {
|
try {
|
||||||
load().save(file);
|
load().save(file);
|
||||||
} catch (IOException ex) {
|
} catch (IOException ex) {
|
||||||
plugin.getLogger().log(Level.SEVERE, "Could not save to " + file, ex);
|
plugin.getLogger().log(Level.SEVERE, "Could not save to " + file, ex);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
public int loadPlaceholders() {
|
public int loadPlaceholders() {
|
||||||
if (config == null || config.getKeys(false) == null || config.getKeys(false).isEmpty()) {
|
if (config == null || config.getKeys(false) == null || config.getKeys(false).isEmpty()) {
|
||||||
return 0;
|
return 0;
|
||||||
}
|
}
|
||||||
|
|
||||||
File dir = new File(plugin.getDataFolder() + File.separator + "javascripts");
|
File dir = new File(plugin.getDataFolder() + File.separator + "javascripts");
|
||||||
|
|
||||||
try {
|
try {
|
||||||
if (!dir.exists()) {
|
if (!dir.exists()) {
|
||||||
dir.mkdirs();
|
dir.mkdirs();
|
||||||
plugin.getLogger().info("Creating directory: plugins" + File.separator + "PlaceholderAPI" + File.separator + "javascripts");
|
plugin.getLogger().info(
|
||||||
} else {
|
"Creating directory: plugins" + File.separator + "PlaceholderAPI" + File.separator
|
||||||
}
|
+ "javascripts");
|
||||||
} catch (SecurityException e) {
|
} else {
|
||||||
plugin.getLogger().severe("Could not create directory: plugins" + File.separator + "PlaceholderAPI" + File.separator + "javascripts");
|
}
|
||||||
}
|
} catch (SecurityException e) {
|
||||||
|
plugin.getLogger().severe(
|
||||||
|
"Could not create directory: plugins" + File.separator + "PlaceholderAPI" + File.separator
|
||||||
|
+ "javascripts");
|
||||||
|
}
|
||||||
|
|
||||||
for (String identifier : config.getKeys(false)) {
|
for (String identifier : config.getKeys(false)) {
|
||||||
if (!config.contains(identifier + ".file") || config.getString(identifier + ".file", null) == null) {
|
if (!config.contains(identifier + ".file")
|
||||||
plugin.getLogger().warning("Javascript placeholder: " + identifier + " does not have a file specified");
|
|| config.getString(identifier + ".file", null) == null) {
|
||||||
continue;
|
plugin.getLogger()
|
||||||
}
|
.warning("Javascript placeholder: " + identifier + " does not have a file specified");
|
||||||
|
continue;
|
||||||
|
}
|
||||||
|
|
||||||
File scriptFile = new File(plugin.getDataFolder() + File.separator + "javascripts", config.getString(identifier + ".file"));
|
File scriptFile = new File(plugin.getDataFolder() + File.separator + "javascripts",
|
||||||
|
config.getString(identifier + ".file"));
|
||||||
|
|
||||||
if (!scriptFile.exists()) {
|
if (!scriptFile.exists()) {
|
||||||
plugin.getLogger().info(scriptFile.getName() + " does not exist. Creating file...");
|
plugin.getLogger().info(scriptFile.getName() + " does not exist. Creating file...");
|
||||||
|
|
||||||
try {
|
try {
|
||||||
scriptFile.createNewFile();
|
scriptFile.createNewFile();
|
||||||
plugin.getLogger().info(scriptFile.getName() + " created! Add your javascript to this file and use /placeholderapi reload to load it!");
|
plugin.getLogger().info(scriptFile.getName()
|
||||||
} catch (IOException e) {
|
+ " created! Add your javascript to this file and use /placeholderapi reload to load it!");
|
||||||
e.printStackTrace();
|
} catch (IOException e) {
|
||||||
}
|
e.printStackTrace();
|
||||||
continue;
|
}
|
||||||
}
|
continue;
|
||||||
|
}
|
||||||
|
|
||||||
String script = getContents(scriptFile);
|
String script = getContents(scriptFile);
|
||||||
|
|
||||||
if (script == null || script.isEmpty()) {
|
if (script == null || script.isEmpty()) {
|
||||||
plugin.getLogger().warning("File: " + scriptFile.getName() + " for javascript placeholder: " + identifier + " is empty");
|
plugin.getLogger().warning(
|
||||||
continue;
|
"File: " + scriptFile.getName() + " for javascript placeholder: " + identifier
|
||||||
}
|
+ " is empty");
|
||||||
|
continue;
|
||||||
|
}
|
||||||
|
|
||||||
ScriptEngine engine = null;
|
ScriptEngine engine = null;
|
||||||
|
|
||||||
if (!config.contains(identifier + ".engine")) {
|
if (!config.contains(identifier + ".engine")) {
|
||||||
engine = ex.getGlobalEngine();
|
engine = ex.getGlobalEngine();
|
||||||
} else {
|
} else {
|
||||||
try {
|
try {
|
||||||
engine = new ScriptEngineManager().getEngineByName(config.getString(identifier + ".engine", "nashorn"));
|
engine = new ScriptEngineManager()
|
||||||
} catch (NullPointerException e) {
|
.getEngineByName(config.getString(identifier + ".engine", "nashorn"));
|
||||||
plugin.getLogger().warning("ScriptEngine type for javascript placeholder: " + identifier + " is invalid! Defaulting to global");
|
} catch (NullPointerException e) {
|
||||||
engine = ex.getGlobalEngine();
|
plugin.getLogger().warning("ScriptEngine type for javascript placeholder: " + identifier
|
||||||
}
|
+ " is invalid! Defaulting to global");
|
||||||
}
|
engine = ex.getGlobalEngine();
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
if (engine == null) {
|
if (engine == null) {
|
||||||
plugin.getLogger().warning("Failed to set ScriptEngine for javascript placeholder: " + identifier);
|
plugin.getLogger()
|
||||||
continue;
|
.warning("Failed to set ScriptEngine for javascript placeholder: " + identifier);
|
||||||
}
|
continue;
|
||||||
|
}
|
||||||
|
|
||||||
JavascriptPlaceholder pl = new JavascriptPlaceholder(engine, identifier, script);
|
JavascriptPlaceholder pl = new JavascriptPlaceholder(engine, identifier, script);
|
||||||
|
|
||||||
boolean added = ex.addJSPlaceholder(pl);
|
boolean added = ex.addJSPlaceholder(pl);
|
||||||
|
|
||||||
if (added) {
|
if (added) {
|
||||||
|
|
||||||
if (pl.loadData()) {
|
if (pl.loadData()) {
|
||||||
plugin.getLogger().info("Loaded data for javascript placeholder: " + identifier);
|
plugin.getLogger().info("Loaded data for javascript placeholder: " + identifier);
|
||||||
}
|
}
|
||||||
plugin.getLogger().info("%javascript_" + identifier + "% has been loaded!");
|
plugin.getLogger().info("%javascript_" + identifier + "% has been loaded!");
|
||||||
} else {
|
} else {
|
||||||
plugin.getLogger().warning("Javascript placeholder %javascript_" + identifier + "% is a duplicate!");
|
plugin.getLogger()
|
||||||
}
|
.warning("Javascript placeholder %javascript_" + identifier + "% is a duplicate!");
|
||||||
}
|
}
|
||||||
return ex.getAmountLoaded();
|
}
|
||||||
}
|
return ex.getAmountLoaded();
|
||||||
|
}
|
||||||
|
|
||||||
private String getContents(File f) {
|
private String getContents(File f) {
|
||||||
StringBuilder sb = new StringBuilder();
|
StringBuilder sb = new StringBuilder();
|
||||||
|
|
||||||
try {
|
try {
|
||||||
Scanner scanner = new Scanner(f);
|
Scanner scanner = new Scanner(f);
|
||||||
|
|
||||||
while (scanner.hasNextLine()) {
|
while (scanner.hasNextLine()) {
|
||||||
String line = scanner.nextLine();
|
String line = scanner.nextLine();
|
||||||
|
|
||||||
if (line == null || line.isEmpty()) {
|
if (line == null || line.isEmpty()) {
|
||||||
continue;
|
continue;
|
||||||
}
|
}
|
||||||
|
|
||||||
line = line.trim();
|
line = line.trim();
|
||||||
|
|
||||||
/* temp fix for single line comments
|
/* temp fix for single line comments
|
||||||
* doesnt solve every case though..
|
* doesnt solve every case though..
|
||||||
* lines that start with code and may have a comment afterward still screw stuff up...
|
* lines that start with code and may have a comment afterward still screw stuff up...
|
||||||
*/
|
*/
|
||||||
if (line.startsWith("//")) {
|
if (line.startsWith("//")) {
|
||||||
continue;
|
continue;
|
||||||
}
|
}
|
||||||
sb.append(line + " ");
|
sb.append(line + " ");
|
||||||
}
|
}
|
||||||
scanner.close();
|
scanner.close();
|
||||||
} catch (FileNotFoundException e) {
|
} catch (FileNotFoundException e) {
|
||||||
e.printStackTrace();
|
e.printStackTrace();
|
||||||
return null;
|
return null;
|
||||||
}
|
}
|
||||||
return sb.toString();
|
return sb.toString();
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
@ -25,41 +25,41 @@ import java.util.Map;
|
|||||||
|
|
||||||
public class ScriptData {
|
public class ScriptData {
|
||||||
|
|
||||||
private Map<String, Object> map;
|
private Map<String, Object> map;
|
||||||
|
|
||||||
public ScriptData(Map<String, Object> data) {
|
public ScriptData(Map<String, Object> data) {
|
||||||
this.map = data;
|
this.map = data;
|
||||||
}
|
}
|
||||||
|
|
||||||
public ScriptData() {
|
public ScriptData() {
|
||||||
this.map = new HashMap<>();
|
this.map = new HashMap<>();
|
||||||
}
|
}
|
||||||
|
|
||||||
public Map<String, Object> getData() {
|
public Map<String, Object> getData() {
|
||||||
return map;
|
return map;
|
||||||
}
|
}
|
||||||
|
|
||||||
public void clear() {
|
public void clear() {
|
||||||
map.clear();
|
map.clear();
|
||||||
}
|
}
|
||||||
|
|
||||||
public boolean exists(String key) {
|
public boolean exists(String key) {
|
||||||
return map.containsKey(key) && map.get(key) != null;
|
return map.containsKey(key) && map.get(key) != null;
|
||||||
}
|
}
|
||||||
|
|
||||||
public Object get(String key) {
|
public Object get(String key) {
|
||||||
return map.get(key);
|
return map.get(key);
|
||||||
}
|
}
|
||||||
|
|
||||||
public void remove(String key) {
|
public void remove(String key) {
|
||||||
map.put(key, null);
|
map.put(key, null);
|
||||||
}
|
}
|
||||||
|
|
||||||
public void set(String key, Object value) {
|
public void set(String key, Object value) {
|
||||||
map.put(key, value);
|
map.put(key, value);
|
||||||
}
|
}
|
||||||
|
|
||||||
public boolean isEmpty() {
|
public boolean isEmpty() {
|
||||||
return map.isEmpty();
|
return map.isEmpty();
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
@ -1,20 +1,11 @@
|
|||||||
package com.extendedclip.papi.expansion.javascript.cloud;
|
package com.extendedclip.papi.expansion.javascript.cloud;
|
||||||
|
|
||||||
import java.util.Arrays;
|
|
||||||
import java.util.List;
|
|
||||||
import java.util.stream.Collectors;
|
|
||||||
|
|
||||||
public enum GithubScript {
|
public class GithubScript {
|
||||||
HAS_PERMISSION("has_permission",
|
|
||||||
"1.0.0",
|
|
||||||
"aBooDyy",
|
|
||||||
"Check if player have \"permission.test\" permission and send a custom message.",
|
|
||||||
"https://raw.githubusercontent.com/PlaceholderAPI/Javascript-Expansion/master/scripts/has_permission.js"),
|
|
||||||
;
|
|
||||||
|
|
||||||
private String name, version, author, description, url;
|
private String name, version, author, description, url;
|
||||||
|
|
||||||
private GithubScript(String name, String version, String author, String description, String url) {
|
public GithubScript(String name, String version, String author, String description, String url) {
|
||||||
this.name = name;
|
this.name = name;
|
||||||
this.version = version;
|
this.version = version;
|
||||||
this.author = author;
|
this.author = author;
|
||||||
@ -41,12 +32,4 @@ public enum GithubScript {
|
|||||||
public String getUrl() {
|
public String getUrl() {
|
||||||
return url;
|
return url;
|
||||||
}
|
}
|
||||||
|
|
||||||
public static List<String> getAllScriptNames() {
|
|
||||||
return Arrays.stream(values()).map(GithubScript::getName).collect(Collectors.toList());
|
|
||||||
}
|
|
||||||
|
|
||||||
public static GithubScript getScript(String name) {
|
|
||||||
return Arrays.stream(values()).filter(s -> { return s.getName().equalsIgnoreCase(name); } ).findFirst().orElse(null);
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
@ -1,6 +1,8 @@
|
|||||||
package com.extendedclip.papi.expansion.javascript.cloud;
|
package com.extendedclip.papi.expansion.javascript.cloud;
|
||||||
|
|
||||||
import com.extendedclip.papi.expansion.javascript.JavascriptExpansion;
|
import com.extendedclip.papi.expansion.javascript.JavascriptExpansion;
|
||||||
|
import com.google.gson.Gson;
|
||||||
|
import com.google.gson.reflect.TypeToken;
|
||||||
import java.io.BufferedReader;
|
import java.io.BufferedReader;
|
||||||
import java.io.File;
|
import java.io.File;
|
||||||
import java.io.FileNotFoundException;
|
import java.io.FileNotFoundException;
|
||||||
@ -10,34 +12,72 @@ import java.io.PrintStream;
|
|||||||
import java.net.URL;
|
import java.net.URL;
|
||||||
import java.util.ArrayList;
|
import java.util.ArrayList;
|
||||||
import java.util.List;
|
import java.util.List;
|
||||||
|
import org.bukkit.Bukkit;
|
||||||
|
|
||||||
public class GithubScriptManager {
|
public class GithubScriptManager {
|
||||||
|
|
||||||
private JavascriptExpansion expansion;
|
private JavascriptExpansion expansion;
|
||||||
private String path;
|
private String javascriptsFolder;
|
||||||
|
private List<GithubScript> availableScripts;
|
||||||
|
private final String MASTER_LIST_URL = "https://raw.githubusercontent.com/PlaceholderAPI/Javascript-Expansion/master/scripts/master_list.json";
|
||||||
|
private final Gson GSON = new Gson();
|
||||||
|
|
||||||
public GithubScriptManager(JavascriptExpansion expansion) {
|
public GithubScriptManager(JavascriptExpansion expansion) {
|
||||||
this.expansion = expansion;
|
this.expansion = expansion;
|
||||||
path = expansion.getPlaceholderAPI().getDataFolder()
|
javascriptsFolder = expansion.getPlaceholderAPI().getDataFolder()
|
||||||
+ File.separator
|
+ File.separator
|
||||||
+ "javascripts"
|
+ "javascripts"
|
||||||
+ File.separator;
|
+ File.separator;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
public void clear() {
|
||||||
|
availableScripts = null;
|
||||||
|
}
|
||||||
|
|
||||||
|
public void fetch() {
|
||||||
|
Bukkit.getScheduler().runTaskAsynchronously(expansion.getPlaceholderAPI(), new Runnable() {
|
||||||
|
@Override
|
||||||
|
public void run() {
|
||||||
|
String json = getContents(MASTER_LIST_URL);
|
||||||
|
|
||||||
|
if (json.isEmpty()) {
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
System.out.println(json);
|
||||||
|
availableScripts = GSON.fromJson(json, new TypeToken<ArrayList<GithubScript>>() {
|
||||||
|
}.getType());
|
||||||
|
}
|
||||||
|
});
|
||||||
|
}
|
||||||
|
|
||||||
public void downloadScript(GithubScript script) {
|
public void downloadScript(GithubScript script) {
|
||||||
List<String> contents = read(script.getUrl());
|
Bukkit.getScheduler().runTaskAsynchronously(expansion.getPlaceholderAPI(), new Runnable() {
|
||||||
if (contents == null || contents.isEmpty()) {
|
@Override
|
||||||
return;
|
public void run() {
|
||||||
}
|
List<String> contents = read(script.getUrl());
|
||||||
File f = new File(path, script.getName() + ".js");
|
if (contents == null || contents.isEmpty()) {
|
||||||
try (PrintStream out = new PrintStream(new FileOutputStream(f))) {
|
return;
|
||||||
contents.forEach(l -> out.println(l));
|
}
|
||||||
} catch (FileNotFoundException e) {
|
File f = new File(javascriptsFolder, script.getName() + ".js");
|
||||||
e.printStackTrace();
|
try (PrintStream out = new PrintStream(new FileOutputStream(f))) {
|
||||||
return;
|
contents.forEach(l -> out.println(l));
|
||||||
}
|
} catch (FileNotFoundException e) {
|
||||||
expansion.getConfig().load().set(script.getName() + ".file", script.getName() +".js");
|
e.printStackTrace();
|
||||||
expansion.getConfig().save();
|
return;
|
||||||
|
}
|
||||||
|
Bukkit.getScheduler().runTask(expansion.getPlaceholderAPI(), new Runnable() {
|
||||||
|
@Override
|
||||||
|
public void run() {
|
||||||
|
expansion.getConfig().load().set(script.getName() + ".file", script.getName() + ".js");
|
||||||
|
expansion.getConfig().save();
|
||||||
|
}
|
||||||
|
});
|
||||||
|
}
|
||||||
|
});
|
||||||
|
}
|
||||||
|
|
||||||
|
private String getContents(String url) {
|
||||||
|
return String.join("", read(url));
|
||||||
}
|
}
|
||||||
|
|
||||||
private List<String> read(String url) {
|
private List<String> read(String url) {
|
||||||
@ -57,4 +97,13 @@ public class GithubScriptManager {
|
|||||||
|
|
||||||
return lines;
|
return lines;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
public List<GithubScript> getAvailableScripts() {
|
||||||
|
return availableScripts;
|
||||||
|
}
|
||||||
|
|
||||||
|
public GithubScript getScript(String name) {
|
||||||
|
if (availableScripts == null) return null;
|
||||||
|
return availableScripts.stream().filter(s -> {return s.getName().equalsIgnoreCase(name);}).findFirst().orElse(null);
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
Loading…
x
Reference in New Issue
Block a user