9 Star 38 Fork 56

Soldier__/VipSystemRecode

加入 Gitee
与超过 1200万 开发者一起发现、参与优秀开源项目,私有仓库也完全免费 :)
免费加入
文件
克隆/下载
ScriptManager.java 2.88 KB
一键复制 编辑 原始数据 按行查看 历史
Soldier__ 提交于 2021-06-10 18:00 +08:00 . add debug
package me.zhanshi123.vipsystem.script;
import com.google.common.base.Charsets;
import me.zhanshi123.vipsystem.Main;
import me.zhanshi123.vipsystem.custom.CustomFunction;
import org.bukkit.Bukkit;
import javax.script.*;
import java.io.BufferedReader;
import java.io.File;
import java.io.FileInputStream;
import java.io.InputStreamReader;
import java.util.HashMap;
import java.util.Map;
import java.util.function.Consumer;
public class ScriptManager {
private ScriptEngine nashorn;
private Bindings bindings;
private Map<File, CompiledScript> cache = new HashMap();
public CompiledScript getCompiledScript(File script) {
return cache.computeIfAbsent(script, file -> {
try {
Main.getInstance().debug(file.getAbsolutePath() + " 's compiled script not found in cache, compiling!");
return ((Compilable) nashorn).compile(new BufferedReader(new InputStreamReader(new FileInputStream(script), Charsets.UTF_8)));
} catch (Exception e) {
e.printStackTrace();
}
Main.getInstance().debug(file.getAbsolutePath() + " compile failed");
return null;
});
}
public ScriptManager() {
try {
Class.forName("org.openjdk.nashorn.api.scripting.NashornScriptEngineFactory");
nashorn = new ScriptEngineManager(Main.getInstance().getClass().getClassLoader()).getEngineByName("nashorn");
if (nashorn == null) {
Main.getInstance().getLogger().warning("Cannot load JavaScript engine, custom script disabled");
}
bindings = nashorn.createBindings();
bindings.put("helper", new ScriptHelper());
bindings.put("getPlayer", (Consumer<String>) Bukkit::getPlayer);
} catch (ClassNotFoundException e) {
Main.getInstance().getLogger().warning("Cannot load JavaScript engine, custom script disabled");
e.printStackTrace();
}
}
public ScriptEngine getNashorn() {
return nashorn;
}
public Object invokeCustomFunction(CustomFunction customFunction, String function, String[] args) {
CompiledScript compiledScript;
try {
Main.getInstance().debug("Invoke " + customFunction.getScript().getAbsolutePath()+ " function " + function + " with args (" + String.join(",", args) + ")");
/*
compiledScript = getCompiledScript(customFunction.getScript());
compiledScript.eval(bindings);
((Invocable) compiledScript.getEngine()).invokeFunction(function, args);
*/
nashorn.eval(new BufferedReader(new InputStreamReader(new FileInputStream(customFunction.getScript()), Charsets.UTF_8)), bindings);
return ((Invocable) nashorn).invokeFunction(function, args);
} catch (Exception e) {
e.printStackTrace();
}
return null;
}
}
Loading...
马建仓 AI 助手
尝试更多
代码解读
代码找茬
代码优化
Java
1
https://gitee.com/Soldier233/VipSystemRecode.git
git@gitee.com:Soldier233/VipSystemRecode.git
Soldier233
VipSystemRecode
VipSystemRecode
530473f93c5d0f94faa5c288e5befc654e4b3be0

搜索帮助