mirror of
https://github.com/PlaceholderAPI/Javascript-Expansion.git
synced 2025-05-23 10:39:04 +00:00
Update to 1.6.1
This commit is contained in:
parent
8b957eb62f
commit
83003446a9
@ -1,164 +1,168 @@
|
|||||||
/*
|
/*
|
||||||
*
|
*
|
||||||
* Javascript-Expansion
|
* Javascript-Expansion
|
||||||
* Copyright (C) 2020 Ryan McCarthy
|
* Copyright (C) 2020 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 com.extendedclip.papi.expansion.javascript.cloud.GithubScript;
|
import com.extendedclip.papi.expansion.javascript.cloud.GithubScript;
|
||||||
import com.extendedclip.papi.expansion.javascript.command.*;
|
import com.extendedclip.papi.expansion.javascript.command.*;
|
||||||
import org.bukkit.command.Command;
|
import org.bukkit.command.Command;
|
||||||
import org.bukkit.command.CommandSender;
|
import org.bukkit.command.CommandSender;
|
||||||
import org.bukkit.util.StringUtil;
|
import org.bukkit.util.StringUtil;
|
||||||
import org.jetbrains.annotations.NotNull;
|
import org.jetbrains.annotations.NotNull;
|
||||||
|
|
||||||
import java.util.ArrayList;
|
import java.util.ArrayList;
|
||||||
import java.util.Arrays;
|
import java.util.Arrays;
|
||||||
import java.util.Collections;
|
import java.util.Collections;
|
||||||
import java.util.List;
|
import java.util.List;
|
||||||
import java.util.stream.Collectors;
|
import java.util.stream.Collectors;
|
||||||
|
|
||||||
public class JavascriptExpansionCommands extends Command {
|
public class JavascriptExpansionCommands extends Command {
|
||||||
|
|
||||||
private final JavascriptExpansion expansion;
|
private final JavascriptExpansion expansion;
|
||||||
private final String PERMISSION = "placeholderapi.js.admin";
|
private final String PERMISSION = "placeholderapi.js.admin";
|
||||||
private final String command;
|
private final String command;
|
||||||
private List<ICommand> subCommands;
|
private List<ICommand> subCommands;
|
||||||
|
|
||||||
public JavascriptExpansionCommands(JavascriptExpansion expansion) {
|
public JavascriptExpansionCommands(JavascriptExpansion expansion) {
|
||||||
super("jsexpansion");
|
super("jsexpansion");
|
||||||
command = getName();
|
command = getName();
|
||||||
this.expansion = expansion;
|
this.expansion = expansion;
|
||||||
this.setDescription("Javascript expansion commands");
|
this.setDescription("Javascript expansion commands");
|
||||||
this.setUsage("/" + command + " <args>");
|
this.setUsage("/" + command + " <args>");
|
||||||
this.setAliases(new ArrayList<>(Arrays.asList("javascriptexpansion", "jsexp")));
|
this.setAliases(new ArrayList<>(Arrays.asList("javascriptexpansion", "jsexp")));
|
||||||
this.setPermission(PERMISSION);
|
this.setPermission(PERMISSION);
|
||||||
initCommands();
|
initCommands();
|
||||||
}
|
}
|
||||||
|
|
||||||
public void initCommands() {
|
public void initCommands() {
|
||||||
if (subCommands != null) {
|
if (subCommands != null) {
|
||||||
subCommands.clear();
|
subCommands.clear();
|
||||||
}
|
}
|
||||||
subCommands = new ArrayList<>(Arrays.asList(
|
subCommands = new ArrayList<>(Arrays.asList(
|
||||||
new GitCommand(expansion),
|
new GitCommand(expansion),
|
||||||
new ListCommand(expansion),
|
new ListCommand(expansion),
|
||||||
new ParseCommand(expansion),
|
new ParseCommand(expansion),
|
||||||
new ReloadCommand(expansion),
|
new ReloadCommand(expansion),
|
||||||
new DebugCommand(expansion))
|
new DebugCommand(expansion))
|
||||||
);
|
);
|
||||||
}
|
}
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
public boolean execute(CommandSender sender, @NotNull String label, String[] args) {
|
public boolean execute(CommandSender sender, @NotNull String label, String[] args) {
|
||||||
|
|
||||||
if (!sender.hasPermission(PERMISSION)) {
|
if (!sender.hasPermission(PERMISSION)) {
|
||||||
ExpansionUtils.sendMsg(sender, "&cYou don't have permission to do that!");
|
ExpansionUtils.sendMsg(sender, "&cYou don't have permission to do that!");
|
||||||
return true;
|
return true;
|
||||||
}
|
}
|
||||||
|
|
||||||
if (args.length == 0) {
|
if (args.length == 0) {
|
||||||
sendHelp(sender);
|
sendHelp(sender);
|
||||||
return true;
|
return true;
|
||||||
}
|
}
|
||||||
|
|
||||||
ICommand command = null;
|
ICommand command = null;
|
||||||
for (ICommand icmd : subCommands) {
|
for (ICommand icmd : subCommands) {
|
||||||
if (icmd.getAlias().equalsIgnoreCase(args[0])) {
|
if (icmd.getAlias().equalsIgnoreCase(args[0])) {
|
||||||
command = icmd;
|
command = icmd;
|
||||||
command.command = getName();
|
command.command = getName();
|
||||||
break;
|
break;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
if (command == null) {
|
if (command == null) {
|
||||||
ExpansionUtils.sendMsg(sender, "&cInvalid expansion sub-command! Type&f /" + getName() + " &cfor help");
|
ExpansionUtils.sendMsg(sender, "&cInvalid expansion sub-command! Type&f /" + getName() + " &cfor help");
|
||||||
return true;
|
return true;
|
||||||
}
|
}
|
||||||
|
|
||||||
command.execute(sender, sliceFirstArr(args));
|
command.execute(sender, sliceFirstArr(args));
|
||||||
|
|
||||||
return true;
|
return true;
|
||||||
}
|
}
|
||||||
|
|
||||||
//TODO: This thing here has to be organized thoroughly later...
|
//TODO: This thing here has to be organized thoroughly later...
|
||||||
@Override
|
@Override
|
||||||
public List<String> tabComplete(CommandSender sender, @NotNull String alias, String[] args) throws IllegalArgumentException {
|
public List<String> tabComplete(CommandSender sender, @NotNull String alias, String[] args) throws IllegalArgumentException {
|
||||||
if (!sender.hasPermission(PERMISSION)) {
|
if (!sender.hasPermission(PERMISSION)) {
|
||||||
return Collections.emptyList();
|
return Collections.emptyList();
|
||||||
}
|
}
|
||||||
|
|
||||||
final List<String> commands = new ArrayList<>(Arrays.asList("list", "parse", "reload"));
|
final List<String> commands = new ArrayList<>(Arrays.asList("list", "parse", "reload"));
|
||||||
final List<String> completion = new ArrayList<>();
|
final List<String> completion = new ArrayList<>();
|
||||||
|
|
||||||
if (expansion.getGithubScriptManager() != null) {
|
if (expansion.getGithubScriptManager() != null) {
|
||||||
commands.add(0, "git");
|
commands.add(0, "git");
|
||||||
}
|
}
|
||||||
|
|
||||||
if (args.length == 1) {
|
if (args.length == 1) {
|
||||||
return StringUtil.copyPartialMatches(args[0], commands, completion);
|
return StringUtil.copyPartialMatches(args[0], commands, completion);
|
||||||
}
|
}
|
||||||
|
|
||||||
if (args[0].equalsIgnoreCase("git")) {
|
if (args[0].equalsIgnoreCase("git")) {
|
||||||
if (expansion.getGithubScriptManager() == null) {
|
if (expansion.getGithubScriptManager() == null) {
|
||||||
return Collections.emptyList();
|
return Collections.emptyList();
|
||||||
}
|
}
|
||||||
|
|
||||||
if (args.length == 2) {
|
if (args.length == 2) {
|
||||||
return StringUtil.copyPartialMatches(args[1], Arrays.asList("download", "enable", "info", "list", "refresh"), completion);
|
return StringUtil.copyPartialMatches(args[1], Arrays.asList("download", "enable", "info", "list", "refresh"), completion);
|
||||||
}
|
}
|
||||||
|
|
||||||
if (args.length == 3 && args[1].equalsIgnoreCase("download")) {
|
if (args.length == 3 && args[1].equalsIgnoreCase("download")) {
|
||||||
if (expansion.getGithubScriptManager().getAvailableScripts() == null) {
|
if (expansion.getGithubScriptManager().getAvailableScripts() == null) {
|
||||||
return Collections.emptyList();
|
return Collections.emptyList();
|
||||||
}
|
}
|
||||||
|
|
||||||
return StringUtil.copyPartialMatches(args[2], expansion.getGithubScriptManager().getAvailableScripts().stream().map(GithubScript::getName).collect(Collectors.toList()), completion);
|
return StringUtil.copyPartialMatches(args[2], expansion.getGithubScriptManager().getAvailableScripts().stream().map(GithubScript::getName).collect(Collectors.toList()), completion);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
return Collections.emptyList();
|
return Collections.emptyList();
|
||||||
}
|
}
|
||||||
|
|
||||||
private void sendHelp(CommandSender sender) {
|
private void sendHelp(CommandSender sender) {
|
||||||
ExpansionUtils.sendMsg(sender,
|
ExpansionUtils.sendMsg(sender,
|
||||||
"&eJavascript expansion &7v: &f" + expansion.getVersion(),
|
"&eJavascript expansion &7v: &f" + expansion.getVersion(),
|
||||||
"&eCreated by: &f" + expansion.getAuthor(),
|
"&eCreated by: &f" + expansion.getAuthor(),
|
||||||
"&eWiki: &fhttps://github.com/PlaceholderAPI/Javascript-Expansion/wiki",
|
"&eWiki: &fhttps://github.com/PlaceholderAPI/Javascript-Expansion/wiki",
|
||||||
"&r",
|
"&r",
|
||||||
"&e/" + command + " reload &7- &fReload your javascripts without reloading PlaceholderAPI.",
|
"&e/" + command + " reload &7- &fReload your javascripts without reloading PlaceholderAPI.",
|
||||||
"&e/" + command + " list &7- &fList loaded script identifiers.",
|
"&e/" + command + " list &7- &fList loaded script identifiers.",
|
||||||
"&e/" + command + " parse [me/player] [code] &7- &fTest JavaScript code in chat.",
|
"&e/" + command + " parse [me/player] [code] &7- &fTest JavaScript code in chat.",
|
||||||
"&e/" + command + " debug [savedata/loaddata] [identifier] &7- &fTest JavaScript code in chat."
|
"&e/" + command + " debug [savedata/loaddata] [identifier] &7- &fTest JavaScript code in chat."
|
||||||
);
|
);
|
||||||
|
|
||||||
if (expansion.getGithubScriptManager() != null) {
|
if (expansion.getGithubScriptManager() != null) {
|
||||||
ExpansionUtils.sendMsg(sender,
|
ExpansionUtils.sendMsg(sender,
|
||||||
"&e/" + command + " git refresh &7- &fRefresh available Github scripts",
|
"&e/" + command + " git refresh &7- &fRefresh available Github scripts",
|
||||||
"&e/" + command + " git download [name] &7- &fDownload a script from the js expansion github.",
|
"&e/" + command + " git download [name] &7- &fDownload a script from the js expansion github.",
|
||||||
"&e/" + command + " git list &7- &fList available scripts in the js expansion github.",
|
"&e/" + command + " git list &7- &fList available scripts in the js expansion github.",
|
||||||
"&e/" + command + " git info [name] &7- &fGet the description and url of a specific script."
|
"&e/" + command + " git info [name] &7- &fGet the description and url of a specific script."
|
||||||
);
|
);
|
||||||
}
|
} else {
|
||||||
}
|
ExpansionUtils.sendMsg(sender,
|
||||||
|
"&e/" + command + " git &7- &fGithub command &7(please enable in config)"
|
||||||
public String[] sliceFirstArr(String[] args) {
|
);
|
||||||
return Arrays.stream(args).skip(1).toArray(String[]::new);
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
}
|
public String[] sliceFirstArr(String[] args) {
|
||||||
|
return Arrays.stream(args).skip(1).toArray(String[]::new);
|
||||||
|
}
|
||||||
|
|
||||||
|
}
|
||||||
|
@ -1,212 +1,212 @@
|
|||||||
/*
|
/*
|
||||||
*
|
*
|
||||||
* Javascript-Expansion
|
* Javascript-Expansion
|
||||||
* Copyright (C) 2020 Ryan McCarthy
|
* Copyright (C) 2020 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 me.clip.placeholderapi.PlaceholderAPI;
|
import me.clip.placeholderapi.PlaceholderAPI;
|
||||||
import me.clip.placeholderapi.PlaceholderAPIPlugin;
|
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.OfflinePlayer;
|
import org.bukkit.OfflinePlayer;
|
||||||
import org.bukkit.configuration.InvalidConfigurationException;
|
import org.bukkit.configuration.InvalidConfigurationException;
|
||||||
import org.bukkit.configuration.file.YamlConfiguration;
|
import org.bukkit.configuration.file.YamlConfiguration;
|
||||||
|
|
||||||
import javax.script.ScriptEngine;
|
import javax.script.ScriptEngine;
|
||||||
import javax.script.ScriptException;
|
import javax.script.ScriptException;
|
||||||
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 java.util.regex.Matcher;
|
import java.util.regex.Matcher;
|
||||||
import java.util.regex.Pattern;
|
import java.util.regex.Pattern;
|
||||||
|
|
||||||
public class JavascriptPlaceholder {
|
public class JavascriptPlaceholder {
|
||||||
|
|
||||||
private final ScriptEngine engine;
|
private final ScriptEngine engine;
|
||||||
private final String identifier;
|
private final String identifier;
|
||||||
private final String script;
|
private final String script;
|
||||||
private ScriptData scriptData;
|
private ScriptData scriptData;
|
||||||
private final File dataFile;
|
private final File dataFile;
|
||||||
private YamlConfiguration yaml;
|
private YamlConfiguration yaml;
|
||||||
private final Pattern pattern;
|
private final Pattern pattern;
|
||||||
|
|
||||||
@SuppressWarnings("ResultOfMethodCallIgnored")
|
@SuppressWarnings("ResultOfMethodCallIgnored")
|
||||||
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");
|
||||||
Validate.notNull(script, "Script can not be null");
|
Validate.notNull(script, "Script can not be null");
|
||||||
|
|
||||||
String dir = PlaceholderAPIPlugin.getInstance().getDataFolder() + "/javascripts/javascript_data";
|
String dir = PlaceholderAPIPlugin.getInstance().getDataFolder() + "/javascripts/javascript_data";
|
||||||
this.engine = engine;
|
this.engine = engine;
|
||||||
this.identifier = identifier;
|
this.identifier = identifier;
|
||||||
this.script = script;
|
this.script = script;
|
||||||
final File directory = new File(dir);
|
final File directory = new File(dir);
|
||||||
|
|
||||||
if (!directory.exists()) {
|
if (!directory.exists()) {
|
||||||
directory.mkdirs();
|
directory.mkdirs();
|
||||||
}
|
}
|
||||||
|
|
||||||
pattern = Pattern.compile("//.*|/\\*[\\S\\s]*?\\*/|%([^%]+)%");
|
pattern = Pattern.compile("//.*|/\\*[\\S\\s]*?\\*/|%([^%]+)%");
|
||||||
scriptData = new ScriptData();
|
scriptData = new ScriptData();
|
||||||
dataFile = new File(directory, identifier + "_data.yml");
|
dataFile = new File(directory, identifier + "_data.yml");
|
||||||
engine.put("Data", scriptData);
|
engine.put("Data", scriptData);
|
||||||
engine.put("DataVar", scriptData.getData());
|
engine.put("DataVar", scriptData.getData());
|
||||||
engine.put("BukkitServer", Bukkit.getServer());
|
engine.put("BukkitServer", Bukkit.getServer());
|
||||||
engine.put("Expansion", JavascriptExpansion.getInstance());
|
engine.put("Expansion", JavascriptExpansion.getInstance());
|
||||||
engine.put("Placeholder", this);
|
engine.put("Placeholder", this);
|
||||||
engine.put("PlaceholderAPI", PlaceholderAPI.class);
|
engine.put("PlaceholderAPI", PlaceholderAPI.class);
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
||||||
public String getIdentifier() {
|
public String getIdentifier() {
|
||||||
return identifier;
|
return identifier;
|
||||||
}
|
}
|
||||||
|
|
||||||
public String evaluate(OfflinePlayer player, String... args) {
|
public String evaluate(OfflinePlayer player, String... args) {
|
||||||
|
|
||||||
// A checker to deny all placeholders inside comment codes
|
// A checker to deny all placeholders inside comment codes
|
||||||
Matcher matcher = pattern.matcher(script);
|
Matcher matcher = pattern.matcher(script);
|
||||||
StringBuffer buffer = new StringBuffer();
|
StringBuffer buffer = new StringBuffer();
|
||||||
|
|
||||||
while (matcher.find()) {
|
while (matcher.find()) {
|
||||||
String matched = matcher.group(0);
|
String matched = matcher.group(0);
|
||||||
if (!matched.startsWith("%") || matched.startsWith("/*") || matched.startsWith("//")) continue;
|
if (!matched.startsWith("%") || matched.startsWith("/*") || matched.startsWith("//")) continue;
|
||||||
|
|
||||||
matcher.appendReplacement(buffer, PlaceholderAPI.setPlaceholders(player, matched));
|
matcher.appendReplacement(buffer, PlaceholderAPI.setPlaceholders(player, matched));
|
||||||
}
|
}
|
||||||
|
|
||||||
matcher.appendTail(buffer);
|
matcher.appendTail(buffer);
|
||||||
String exp = buffer.toString();
|
String exp = buffer.toString();
|
||||||
|
|
||||||
try {
|
try {
|
||||||
String[] arguments = null;
|
String[] arguments = null;
|
||||||
|
|
||||||
if (args != null && args.length > 0) {
|
if (args != null && args.length > 0) {
|
||||||
arguments = new String[args.length];
|
arguments = new String[args.length];
|
||||||
|
|
||||||
for (int i = 0; i < args.length; i++) {
|
for (int i = 0; i < args.length; i++) {
|
||||||
if (args[i] == null || args[i].isEmpty()) {
|
if (args[i] == null || args[i].isEmpty()) {
|
||||||
continue;
|
continue;
|
||||||
}
|
}
|
||||||
arguments[i] = PlaceholderAPI.setBracketPlaceholders(player, args[i]);
|
arguments[i] = PlaceholderAPI.setBracketPlaceholders(player, args[i]);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
if (arguments == null) {
|
if (arguments == null) {
|
||||||
arguments = new String[]{};
|
arguments = new String[]{};
|
||||||
}
|
}
|
||||||
|
|
||||||
engine.put("args", arguments);
|
engine.put("args", arguments);
|
||||||
|
|
||||||
if (player != null && player.isOnline()) {
|
if (player != null && player.isOnline()) {
|
||||||
engine.put("BukkitPlayer", player.getPlayer());
|
engine.put("BukkitPlayer", player.getPlayer());
|
||||||
engine.put("Player", player.getPlayer());
|
engine.put("Player", player.getPlayer());
|
||||||
}
|
}
|
||||||
|
|
||||||
engine.put("OfflinePlayer", player);
|
engine.put("OfflinePlayer", player);
|
||||||
Object result = engine.eval(exp);
|
Object result = engine.eval(exp);
|
||||||
return result != null ? PlaceholderAPI.setBracketPlaceholders(player, result.toString()) : "";
|
return result != null ? PlaceholderAPI.setBracketPlaceholders(player, result.toString()) : "";
|
||||||
|
|
||||||
} catch (ScriptException ex) {
|
} catch (ScriptException ex) {
|
||||||
ExpansionUtils.errorLog("An error occurred while executing the script '" + identifier + "':\n\t" + ex.getMessage(), null);
|
ExpansionUtils.errorLog("An error occurred while executing the script '" + identifier + "':\n\t" + ex.getMessage(), null);
|
||||||
} catch (ArrayIndexOutOfBoundsException ex) {
|
} catch (ArrayIndexOutOfBoundsException ex) {
|
||||||
ExpansionUtils.errorLog("Argument out of bound while executing script '" + identifier + "':\n\t" + ex.getMessage(), null);
|
ExpansionUtils.errorLog("Argument out of bound while executing script '" + identifier + "':\n\t" + ex.getMessage(), null);
|
||||||
}
|
}
|
||||||
return "Script error (check console)";
|
return "Script error (check console)";
|
||||||
}
|
}
|
||||||
|
|
||||||
public String getScript() {
|
public String getScript() {
|
||||||
return script;
|
return script;
|
||||||
}
|
}
|
||||||
|
|
||||||
public ScriptData getData() {
|
public ScriptData getData() {
|
||||||
if (scriptData == null) {
|
if (scriptData == null) {
|
||||||
scriptData = new ScriptData();
|
scriptData = new ScriptData();
|
||||||
}
|
}
|
||||||
return scriptData;
|
return scriptData;
|
||||||
}
|
}
|
||||||
|
|
||||||
public void setData(ScriptData data) {
|
public void setData(ScriptData data) {
|
||||||
this.scriptData = data;
|
this.scriptData = data;
|
||||||
}
|
}
|
||||||
|
|
||||||
@SuppressWarnings("ResultOfMethodCallIgnored")
|
@SuppressWarnings("ResultOfMethodCallIgnored")
|
||||||
public boolean loadData() {
|
public boolean loadData() {
|
||||||
yaml = new YamlConfiguration();
|
yaml = new YamlConfiguration();
|
||||||
dataFile.getParentFile().mkdirs();
|
dataFile.getParentFile().mkdirs();
|
||||||
|
|
||||||
if (!dataFile.exists()) {
|
if (!dataFile.exists()) {
|
||||||
try {
|
try {
|
||||||
dataFile.createNewFile();
|
dataFile.createNewFile();
|
||||||
} catch (IOException e) {
|
} catch (IOException e) {
|
||||||
ExpansionUtils.errorLog("An error occurred while creating data file for " + getIdentifier(), e);
|
ExpansionUtils.errorLog("An error occurred while creating data file for " + getIdentifier(), e);
|
||||||
return false;
|
return false;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
try {
|
try {
|
||||||
yaml.load(dataFile);
|
yaml.load(dataFile);
|
||||||
} catch (IOException | InvalidConfigurationException e) {
|
} catch (IOException | InvalidConfigurationException e) {
|
||||||
ExpansionUtils.errorLog("An error occurred while loading for " + getIdentifier(), e);
|
ExpansionUtils.errorLog("An error occurred while loading for " + getIdentifier(), e);
|
||||||
return false;
|
return false;
|
||||||
}
|
}
|
||||||
|
|
||||||
final Set<String> keys = yaml.getKeys(true);
|
final Set<String> keys = yaml.getKeys(true);
|
||||||
|
|
||||||
if (keys.size() == 0) {
|
if (keys.size() == 0) {
|
||||||
return false;
|
return false;
|
||||||
}
|
}
|
||||||
|
|
||||||
if (scriptData == null)
|
if (scriptData == null)
|
||||||
scriptData = new ScriptData();
|
scriptData = new ScriptData();
|
||||||
else scriptData.clear();
|
else scriptData.clear();
|
||||||
|
|
||||||
keys.forEach(key -> scriptData.set(key, ExpansionUtils.ymlToJavaObj(yaml.get(key))));
|
keys.forEach(key -> scriptData.set(key, ExpansionUtils.ymlToJavaObj(yaml.get(key))));
|
||||||
|
|
||||||
if (!scriptData.isEmpty()) {
|
if (!scriptData.isEmpty()) {
|
||||||
this.setData(scriptData);
|
setData(scriptData);
|
||||||
return true;
|
return true;
|
||||||
}
|
}
|
||||||
return false;
|
return false;
|
||||||
}
|
}
|
||||||
|
|
||||||
public void saveData() {
|
public void saveData() {
|
||||||
if (scriptData == null || scriptData.isEmpty() || yaml == null) {
|
if (scriptData == null || scriptData.isEmpty() || yaml == null) {
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
|
|
||||||
// Function for merging JSON.
|
// Function for merging JSON.
|
||||||
// TODO: This will be removed along with Nashorn in a later future
|
// TODO: This will be removed along with Nashorn in a later future
|
||||||
scriptData.getData().forEach((key, value) -> yaml.set(key, ExpansionUtils.jsonToJava(value)));
|
scriptData.getData().forEach((key, value) -> yaml.set(key, ExpansionUtils.jsonToJava(value)));
|
||||||
|
|
||||||
try {
|
try {
|
||||||
yaml.save(dataFile);
|
yaml.save(dataFile);
|
||||||
} catch (IOException e) {
|
} catch (IOException e) {
|
||||||
ExpansionUtils.errorLog(ExpansionUtils.PREFIX + "An error occurred while saving data for " + getIdentifier(), e);
|
ExpansionUtils.errorLog(ExpansionUtils.PREFIX + "An error occurred while saving data for " + getIdentifier(), e);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
public void cleanup() {
|
public void cleanup() {
|
||||||
if (this.scriptData != null) {
|
if (this.scriptData != null) {
|
||||||
this.scriptData.clear();
|
this.scriptData.clear();
|
||||||
this.scriptData = null;
|
this.scriptData = null;
|
||||||
}
|
}
|
||||||
this.yaml = null;
|
this.yaml = null;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
@ -1,199 +1,213 @@
|
|||||||
/*
|
/*
|
||||||
*
|
*
|
||||||
* Javascript-Expansion
|
* Javascript-Expansion
|
||||||
* Copyright (C) 2020 Ryan McCarthy
|
* Copyright (C) 2020 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 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;
|
||||||
|
|
||||||
import javax.script.ScriptEngine;
|
import javax.script.ScriptEngine;
|
||||||
import javax.script.ScriptEngineManager;
|
import javax.script.ScriptEngineManager;
|
||||||
import java.io.File;
|
import java.io.File;
|
||||||
import java.io.IOException;
|
import java.io.IOException;
|
||||||
import java.nio.file.Files;
|
import java.nio.file.Files;
|
||||||
import java.util.List;
|
import java.util.List;
|
||||||
|
|
||||||
public class JavascriptPlaceholdersConfig {
|
public class JavascriptPlaceholdersConfig {
|
||||||
|
|
||||||
private final JavascriptExpansion ex;
|
private final JavascriptExpansion ex;
|
||||||
private final PlaceholderAPIPlugin plugin;
|
private final 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).isEmpty()) {
|
if (config.getKeys(false).isEmpty()) {
|
||||||
config.set("example.file", "example.js");
|
config.set("example.file", "example.js");
|
||||||
config.set("example.engine", ExpansionUtils.DEFAULT_ENGINE);
|
config.set("example.engine", ExpansionUtils.DEFAULT_ENGINE);
|
||||||
}
|
}
|
||||||
|
|
||||||
save();
|
save();
|
||||||
}
|
}
|
||||||
|
|
||||||
public FileConfiguration load() {
|
public FileConfiguration load() {
|
||||||
if (config == null) reload();
|
if (config == null) 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) {
|
||||||
ExpansionUtils.warnLog("Could not save to " + file, ex);
|
ExpansionUtils.warnLog("Could not save to " + file, ex);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
@SuppressWarnings("ResultOfMethodCallIgnored")
|
@SuppressWarnings("ResultOfMethodCallIgnored")
|
||||||
public int loadPlaceholders() {
|
public int loadPlaceholders() {
|
||||||
if (config == null || config.getKeys(false).isEmpty()) {
|
if (config == null || config.getKeys(false).isEmpty()) {
|
||||||
return 0;
|
return 0;
|
||||||
}
|
}
|
||||||
|
|
||||||
final File directory = new File(plugin.getDataFolder(), "javascripts");
|
final File directory = new File(plugin.getDataFolder(), "javascripts");
|
||||||
|
|
||||||
try {
|
try {
|
||||||
if (!directory.exists()) {
|
if (!directory.exists()) {
|
||||||
directory.mkdirs();
|
directory.mkdirs();
|
||||||
ExpansionUtils.infoLog("Creating directory: " + directory.getPath());
|
ExpansionUtils.infoLog("Creating directory: " + directory.getPath());
|
||||||
}
|
}
|
||||||
} catch (SecurityException e) {
|
} catch (SecurityException e) {
|
||||||
ExpansionUtils.errorLog("Could not create directory: " + directory.getPath(), e);
|
ExpansionUtils.errorLog("Could not create directory: " + directory.getPath(), e);
|
||||||
}
|
}
|
||||||
|
|
||||||
for (String identifier : config.getKeys(false)) {
|
for (String identifier : config.getKeys(false)) {
|
||||||
final String fileName = config.getString(identifier + ".file");
|
final String fileName = config.getString(identifier + ".file");
|
||||||
if (!config.contains(identifier + ".file") || fileName == null) {
|
if (!config.contains(identifier + ".file") || fileName == null) {
|
||||||
ExpansionUtils.warnLog("Javascript placeholder: " + identifier + " does not have a file specified", null);
|
ExpansionUtils.warnLog("Javascript placeholder: " + identifier + " does not have a file specified", null);
|
||||||
continue;
|
continue;
|
||||||
}
|
}
|
||||||
|
|
||||||
final File scriptFile = new File(plugin.getDataFolder() + "/javascripts", fileName);
|
final File scriptFile = new File(plugin.getDataFolder() + "/javascripts", fileName);
|
||||||
|
|
||||||
if (!scriptFile.exists()) {
|
if (!scriptFile.exists()) {
|
||||||
ExpansionUtils.infoLog(scriptFile.getName() + " does not exist. Creating one for you...");
|
ExpansionUtils.infoLog(scriptFile.getName() + " does not exist. Creating one for you...");
|
||||||
|
|
||||||
try {
|
try {
|
||||||
scriptFile.createNewFile();
|
scriptFile.createNewFile();
|
||||||
ExpansionUtils.infoLog(scriptFile.getName() + " created! Add your javascript to this file and use '/jsexpansion reload' to load it!");
|
ExpansionUtils.infoLog(scriptFile.getName() + " created! Add your javascript to this file and use '/jsexpansion reload' to load it!");
|
||||||
} catch (IOException e) {
|
} catch (IOException e) {
|
||||||
ExpansionUtils.errorLog("An error occurred while creating " + scriptFile.getName(), e);
|
ExpansionUtils.errorLog("An error occurred while creating " + scriptFile.getName(), e);
|
||||||
}
|
}
|
||||||
|
|
||||||
continue;
|
continue;
|
||||||
}
|
}
|
||||||
|
|
||||||
final String script = getContents(scriptFile);
|
final String script = getContents(scriptFile);
|
||||||
|
|
||||||
if (script == null || script.isEmpty()) {
|
if (script == null || script.isEmpty()) {
|
||||||
ExpansionUtils.warnLog("File: " + scriptFile.getName() + " for Javascript placeholder: " + identifier + " is empty", null);
|
ExpansionUtils.warnLog("File: " + scriptFile.getName() + " for Javascript placeholder: " + identifier + " is empty", null);
|
||||||
continue;
|
continue;
|
||||||
}
|
}
|
||||||
|
|
||||||
ScriptEngine engine;
|
boolean debug = (boolean) ex.get("debug", false);
|
||||||
if (!config.contains(identifier + ".engine")) {
|
int errScriptEngine = 0;
|
||||||
engine = ex.getGlobalEngine();
|
|
||||||
ExpansionUtils.warnLog("ScriptEngine type for javascript placeholder " + identifier + " isn't initialized! Defaulting to global", null);
|
ScriptEngine engine;
|
||||||
} else {
|
if (!config.contains(identifier + ".engine")) {
|
||||||
try {
|
engine = ex.getGlobalEngine();
|
||||||
engine = new ScriptEngineManager(null).getEngineByName(config.getString(identifier + ".engine", "nashorn"));
|
if (debug) {
|
||||||
} catch (NullPointerException e) {
|
ExpansionUtils.warnLog("ScriptEngine type for javascript placeholder: " + identifier + " is empty! Defaulting to global", null);
|
||||||
ExpansionUtils.warnLog("ScriptEngine type for javascript placeholder: " + identifier + " is invalid! Defaulting to global", null);
|
} else {
|
||||||
engine = ex.getGlobalEngine();
|
errScriptEngine++;
|
||||||
}
|
}
|
||||||
}
|
} else {
|
||||||
|
try {
|
||||||
if (engine == null) {
|
engine = new ScriptEngineManager(null).getEngineByName(config.getString(identifier + ".engine", "nashorn"));
|
||||||
ExpansionUtils.warnLog("Failed to set ScriptEngine for javascript placeholder: " + identifier, null);
|
} catch (NullPointerException e) {
|
||||||
continue;
|
if (debug) {
|
||||||
}
|
ExpansionUtils.warnLog("ScriptEngine type for javascript placeholder: " + identifier + " is invalid! Defaulting to global", null);
|
||||||
|
} else {
|
||||||
final JavascriptPlaceholder placeholder = new JavascriptPlaceholder(engine, identifier, script);
|
errScriptEngine++;
|
||||||
final boolean added = ex.addJSPlaceholder(placeholder);
|
}
|
||||||
|
engine = ex.getGlobalEngine();
|
||||||
if (added) {
|
}
|
||||||
if (placeholder.loadData()) {
|
}
|
||||||
ExpansionUtils.infoLog("Data for placeholder &b" + identifier + "&r has been loaded");
|
|
||||||
}
|
if (errScriptEngine > 0) {
|
||||||
|
ExpansionUtils.warnLog("ScriptEngine type for " + errScriptEngine + " javascript placeholder" + ExpansionUtils.plural(errScriptEngine) +
|
||||||
ExpansionUtils.infoLog("Placeholder &b%javascript_" + identifier + "%&r has been loaded");
|
" failed! Defaulting all to global. More information by enabling debug mode", null);
|
||||||
} else {
|
}
|
||||||
ExpansionUtils.warnLog("Javascript placeholder %javascript_" + identifier + "% is duplicated!", null);
|
|
||||||
}
|
if (engine == null) {
|
||||||
}
|
ExpansionUtils.warnLog("Failed to set ScriptEngine for javascript placeholder: " + identifier, null);
|
||||||
return ex.getAmountLoaded();
|
continue;
|
||||||
}
|
}
|
||||||
|
|
||||||
private String getContents(File file) {
|
final JavascriptPlaceholder placeholder = new JavascriptPlaceholder(engine, identifier, script);
|
||||||
final StringBuilder sb = new StringBuilder();
|
final boolean added = ex.addJSPlaceholder(placeholder);
|
||||||
|
|
||||||
try {
|
if (added) {
|
||||||
List<String> lines = Files.readAllLines(file.toPath());
|
if (placeholder.loadData()) {
|
||||||
lines.forEach((line) -> sb.append(line).append("\n"));
|
ExpansionUtils.infoLog("Data for placeholder &b" + identifier + "&r has been loaded");
|
||||||
} catch (IOException e) {
|
}
|
||||||
return null;
|
|
||||||
}
|
ExpansionUtils.infoLog("Placeholder &b%javascript_" + identifier + "%&r has been loaded");
|
||||||
|
} else {
|
||||||
// This thing is just in case, who needs it now..
|
ExpansionUtils.warnLog("Javascript placeholder %javascript_" + identifier + "% is duplicated!", null);
|
||||||
// return sb.toString().replaceAll("//.*|/\\*(?:[^/*|*/]|\\\\.|\\n\\*)*\\*/", "");
|
}
|
||||||
return sb.toString();
|
}
|
||||||
}
|
return ex.getAmountLoaded();
|
||||||
}
|
}
|
||||||
|
|
||||||
|
private String getContents(File file) {
|
||||||
|
final StringBuilder sb = new StringBuilder();
|
||||||
|
|
||||||
|
try {
|
||||||
|
List<String> lines = Files.readAllLines(file.toPath());
|
||||||
|
lines.forEach((line) -> sb.append(line).append("\n"));
|
||||||
|
} catch (IOException e) {
|
||||||
|
return null;
|
||||||
|
}
|
||||||
|
|
||||||
|
return sb.toString();
|
||||||
|
}
|
||||||
|
}
|
||||||
|
Loading…
x
Reference in New Issue
Block a user