1
0
mirror of https://e.coding.net/circlecloud/YumCore.git synced 2024-12-26 07:18:52 +00:00

feat: 添加统一的脚本管理器

Signed-off-by: 502647092 <admin@yumc.pw>
This commit is contained in:
502647092 2017-04-17 20:48:19 +08:00
parent d8c35355fa
commit 94f134ddf9

View File

@ -1,14 +1,11 @@
package pw.yumc.YumCore.engine; package pw.yumc.YumCore.engine;
import java.io.Reader; import java.io.Reader;
import java.util.HashMap;
import javax.script.Bindings; import javax.script.*;
import javax.script.Invocable;
import javax.script.ScriptContext; import pw.yumc.YumCore.bukkit.P;
import javax.script.ScriptEngine;
import javax.script.ScriptEngineFactory;
import javax.script.ScriptEngineManager;
import javax.script.ScriptException;
/** /**
* 喵式脚本引擎 * 喵式脚本引擎
@ -18,30 +15,43 @@ import javax.script.ScriptException;
*/ */
public class MiaoScriptEngine implements ScriptEngine, Invocable { public class MiaoScriptEngine implements ScriptEngine, Invocable {
private static MiaoScriptEngine DEFAULT; private static MiaoScriptEngine DEFAULT;
private static ScriptEngineManager manager;
private ScriptEngine engine; private ScriptEngine engine;
static {
manager = new ScriptEngineManager(P.getPlugin().getClass().getClassLoader());
}
public static void setBindings(Bindings bindings) {
manager.setBindings(bindings);
}
public static Bindings getBindings() {
return manager.getBindings();
}
public MiaoScriptEngine() { public MiaoScriptEngine() {
this("js"); this("js");
} }
public MiaoScriptEngine(final String engineType) { public MiaoScriptEngine(final String engineType) {
try { try {
engine = new ScriptEngineManager().getEngineByName(engineType); engine = manager.getEngineByName(engineType);
} catch (final NullPointerException ex) { } catch (final NullPointerException ex) {
engine = new ScriptEngineManager().getEngineByName("javascript"); engine = manager.getEngineByName("javascript");
} }
} }
public static MiaoScriptEngine getDefault() { public static MiaoScriptEngine getDefault() {
if (DEFAULT == null) { if (DEFAULT == null) {
DEFAULT = new MiaoScriptEngine("javascript"); DEFAULT = new MiaoScriptEngine();
} }
return DEFAULT; return DEFAULT;
} }
@Override @Override
public Bindings createBindings() { public Bindings createBindings() {
return engine.createBindings(); return new SimpleBindings(new HashMap<>(engine.getBindings(ScriptContext.GLOBAL_SCOPE)));
} }
@Override @Override