From c307fc1ae37c0d37b9055f566d3edccdead9399b Mon Sep 17 00:00:00 2001 From: 502647092 Date: Sat, 11 Nov 2017 18:04:39 +0800 Subject: [PATCH] =?UTF-8?q?fix:=20=E4=BF=AE=E5=A4=8D=E5=9C=A8MODS=E6=9C=8D?= =?UTF-8?q?=E4=B8=8B=E6=9C=AA=E5=8A=A0=E8=BD=BDjs=E5=BC=95=E6=93=8E?= =?UTF-8?q?=E7=9A=84=E9=97=AE=E9=A2=98?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Signed-off-by: 502647092 --- src/main/java/pw/yumc/MiaoScript/MiaoScript.java | 11 +++++++++-- src/main/java/pw/yumc/MiaoScript/ScriptEngine.java | 12 ++---------- 2 files changed, 11 insertions(+), 12 deletions(-) diff --git a/src/main/java/pw/yumc/MiaoScript/MiaoScript.java b/src/main/java/pw/yumc/MiaoScript/MiaoScript.java index 8988e5b..c7bbae9 100644 --- a/src/main/java/pw/yumc/MiaoScript/MiaoScript.java +++ b/src/main/java/pw/yumc/MiaoScript/MiaoScript.java @@ -29,7 +29,7 @@ public class MiaoScript extends JavaPlugin implements Executor { public void onEnable() { new CommandSub("ms", this); engine = new ScriptEngine(getDataFolder().getCanonicalPath(), getLogger()); - engine.enableEngine(getClassLoader()); + enableEngine(); } @Cmd @@ -60,7 +60,7 @@ public class MiaoScript extends JavaPlugin implements Executor { } catch (Exception ex) { Log.d("Error reload", ex); } - engine.enableEngine(getClassLoader()); + enableEngine(); Log.sender(sender, "§bMiaoScript §eEngine §a重启完成!"); } @@ -72,6 +72,13 @@ public class MiaoScript extends JavaPlugin implements Executor { } } + private void enableEngine() { + val origin = Thread.currentThread().getContextClassLoader(); + Thread.currentThread().setContextClassLoader(getClassLoader()); + engine.enableEngine(); + Thread.currentThread().setContextClassLoader(origin); + } + @Override public void onDisable() { engine.disableEngine(); diff --git a/src/main/java/pw/yumc/MiaoScript/ScriptEngine.java b/src/main/java/pw/yumc/MiaoScript/ScriptEngine.java index 6469186..15973ab 100644 --- a/src/main/java/pw/yumc/MiaoScript/ScriptEngine.java +++ b/src/main/java/pw/yumc/MiaoScript/ScriptEngine.java @@ -8,14 +8,13 @@ import java.nio.file.Paths; import javax.script.ScriptEngineManager; import lombok.SneakyThrows; -import lombok.val; import pw.yumc.YumCore.engine.MiaoScriptEngine; /** * Created with IntelliJ IDEA * * @author 喵♂呜 - * Created on 2017/10/25 21:01. + * Created on 2017/10/25 21:01. */ public class ScriptEngine { private String root; @@ -27,14 +26,8 @@ public class ScriptEngine { this.logger = logger; } - public void enableEngine() { - enableEngine(Thread.currentThread().getContextClassLoader()); - } - @SneakyThrows - public void enableEngine(ClassLoader loader) { - val origin = Thread.currentThread().getContextClassLoader(); - Thread.currentThread().setContextClassLoader(loader); + public void enableEngine() { ScriptEngineManager manager = new ScriptEngineManager(); this.engine = new MiaoScriptEngine(manager, "nashorn"); this.engine.put("base", new Base()); @@ -46,7 +39,6 @@ public class ScriptEngine { this.engine.eval(new InputStreamReader(Thread.currentThread().getContextClassLoader().getResourceAsStream("bios.js"))); } engine.invokeFunction("boot", root, logger); - Thread.currentThread().setContextClassLoader(origin); } @SneakyThrows