From abe5569c4affbae566432e3e5961795605f0f330 Mon Sep 17 00:00:00 2001 From: 502647092 Date: Fri, 3 Nov 2017 02:04:22 +0800 Subject: [PATCH] =?UTF-8?q?feat:=20=E4=BD=BF=E7=94=A8=E6=8F=92=E4=BB=B6?= =?UTF-8?q?=E7=9A=84=E7=B1=BB=E5=8A=A0=E8=BD=BD=E5=99=A8?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Signed-off-by: 502647092 --- .../java/pw/yumc/MiaoScript/MiaoScript.java | 4 ++-- .../java/pw/yumc/MiaoScript/ScriptEngine.java | 18 ++++++++---------- 2 files changed, 10 insertions(+), 12 deletions(-) diff --git a/src/main/java/pw/yumc/MiaoScript/MiaoScript.java b/src/main/java/pw/yumc/MiaoScript/MiaoScript.java index 3e304c9..8988e5b 100644 --- a/src/main/java/pw/yumc/MiaoScript/MiaoScript.java +++ b/src/main/java/pw/yumc/MiaoScript/MiaoScript.java @@ -10,7 +10,6 @@ import org.bukkit.plugin.java.JavaPlugin; import lombok.SneakyThrows; import lombok.val; import pw.yumc.YumCore.bukkit.Log; -import pw.yumc.YumCore.bukkit.P; import pw.yumc.YumCore.commands.CommandSub; import pw.yumc.YumCore.commands.annotation.Cmd; import pw.yumc.YumCore.commands.annotation.Help; @@ -30,6 +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()); } @Cmd @@ -60,7 +60,7 @@ public class MiaoScript extends JavaPlugin implements Executor { } catch (Exception ex) { Log.d("Error reload", ex); } - engine.enableEngine(); + engine.enableEngine(getClassLoader()); Log.sender(sender, "§bMiaoScript §eEngine §a重启完成!"); } diff --git a/src/main/java/pw/yumc/MiaoScript/ScriptEngine.java b/src/main/java/pw/yumc/MiaoScript/ScriptEngine.java index f859f58..d83a1a4 100644 --- a/src/main/java/pw/yumc/MiaoScript/ScriptEngine.java +++ b/src/main/java/pw/yumc/MiaoScript/ScriptEngine.java @@ -1,16 +1,11 @@ package pw.yumc.MiaoScript; -import java.lang.Thread; import java.io.InputStreamReader; -import java.util.logging.Level; -import java.util.logging.Logger; - -import java.nio.file.Paths; import java.nio.file.Files; import java.nio.file.Path; +import java.nio.file.Paths; import javax.script.ScriptEngineManager; -import javax.script.ScriptException; import lombok.SneakyThrows; import pw.yumc.YumCore.engine.MiaoScriptEngine; @@ -19,7 +14,7 @@ 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; @@ -29,11 +24,14 @@ public class ScriptEngine { public ScriptEngine(String root, Object logger) { this.root = root; this.logger = logger; - enableEngine(); + } + + public void enableEngine() { + enableEngine(Thread.currentThread().getContextClassLoader()); } @SneakyThrows - public void enableEngine() { + public void enableEngine(ClassLoader loader) { ScriptEngineManager manager = new ScriptEngineManager(null); this.engine = new MiaoScriptEngine(manager, "nashorn"); this.engine.put("base", new Base()); @@ -42,7 +40,7 @@ public class ScriptEngine { if (Files.exists(bios)) { this.engine.eval("load('" + bios.toFile().getCanonicalPath() + "')"); } else { - this.engine.eval(new InputStreamReader(Thread.currentThread().getContextClassLoader().getResourceAsStream("bios.js"))); + this.engine.eval(new InputStreamReader(loader.getResourceAsStream("bios.js"))); } engine.invokeFunction("boot", root, logger); }