From 94f134ddf9a33804c64d8474f6c442abd37feedb Mon Sep 17 00:00:00 2001 From: 502647092 Date: Mon, 17 Apr 2017 20:48:19 +0800 Subject: [PATCH] =?UTF-8?q?feat:=20=E6=B7=BB=E5=8A=A0=E7=BB=9F=E4=B8=80?= =?UTF-8?q?=E7=9A=84=E8=84=9A=E6=9C=AC=E7=AE=A1=E7=90=86=E5=99=A8?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Signed-off-by: 502647092 --- .../yumc/YumCore/engine/MiaoScriptEngine.java | 32 ++++++++++++------- 1 file changed, 21 insertions(+), 11 deletions(-) diff --git a/src/main/java/pw/yumc/YumCore/engine/MiaoScriptEngine.java b/src/main/java/pw/yumc/YumCore/engine/MiaoScriptEngine.java index c3cb023..b6c8307 100644 --- a/src/main/java/pw/yumc/YumCore/engine/MiaoScriptEngine.java +++ b/src/main/java/pw/yumc/YumCore/engine/MiaoScriptEngine.java @@ -1,14 +1,11 @@ package pw.yumc.YumCore.engine; import java.io.Reader; +import java.util.HashMap; -import javax.script.Bindings; -import javax.script.Invocable; -import javax.script.ScriptContext; -import javax.script.ScriptEngine; -import javax.script.ScriptEngineFactory; -import javax.script.ScriptEngineManager; -import javax.script.ScriptException; +import javax.script.*; + +import pw.yumc.YumCore.bukkit.P; /** * 喵式脚本引擎 @@ -18,30 +15,43 @@ import javax.script.ScriptException; */ public class MiaoScriptEngine implements ScriptEngine, Invocable { private static MiaoScriptEngine DEFAULT; + private static ScriptEngineManager manager; 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() { this("js"); } public MiaoScriptEngine(final String engineType) { try { - engine = new ScriptEngineManager().getEngineByName(engineType); + engine = manager.getEngineByName(engineType); } catch (final NullPointerException ex) { - engine = new ScriptEngineManager().getEngineByName("javascript"); + engine = manager.getEngineByName("javascript"); } } public static MiaoScriptEngine getDefault() { if (DEFAULT == null) { - DEFAULT = new MiaoScriptEngine("javascript"); + DEFAULT = new MiaoScriptEngine(); } return DEFAULT; } @Override public Bindings createBindings() { - return engine.createBindings(); + return new SimpleBindings(new HashMap<>(engine.getBindings(ScriptContext.GLOBAL_SCOPE))); } @Override