fix: 修复在MODS服下未加载js引擎的问题

Signed-off-by: 502647092 <admin@yumc.pw>
This commit is contained in:
502647092 2017-11-11 18:04:39 +08:00
parent bf8201e9d2
commit c307fc1ae3
2 changed files with 11 additions and 12 deletions

View File

@ -29,7 +29,7 @@ public class MiaoScript extends JavaPlugin implements Executor {
public void onEnable() { public void onEnable() {
new CommandSub("ms", this); new CommandSub("ms", this);
engine = new ScriptEngine(getDataFolder().getCanonicalPath(), getLogger()); engine = new ScriptEngine(getDataFolder().getCanonicalPath(), getLogger());
engine.enableEngine(getClassLoader()); enableEngine();
} }
@Cmd @Cmd
@ -60,7 +60,7 @@ public class MiaoScript extends JavaPlugin implements Executor {
} catch (Exception ex) { } catch (Exception ex) {
Log.d("Error reload", ex); Log.d("Error reload", ex);
} }
engine.enableEngine(getClassLoader()); enableEngine();
Log.sender(sender, "§bMiaoScript §eEngine §a重启完成!"); 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 @Override
public void onDisable() { public void onDisable() {
engine.disableEngine(); engine.disableEngine();

View File

@ -8,7 +8,6 @@ import java.nio.file.Paths;
import javax.script.ScriptEngineManager; import javax.script.ScriptEngineManager;
import lombok.SneakyThrows; import lombok.SneakyThrows;
import lombok.val;
import pw.yumc.YumCore.engine.MiaoScriptEngine; import pw.yumc.YumCore.engine.MiaoScriptEngine;
/** /**
@ -27,14 +26,8 @@ public class ScriptEngine {
this.logger = logger; this.logger = logger;
} }
public void enableEngine() {
enableEngine(Thread.currentThread().getContextClassLoader());
}
@SneakyThrows @SneakyThrows
public void enableEngine(ClassLoader loader) { public void enableEngine() {
val origin = Thread.currentThread().getContextClassLoader();
Thread.currentThread().setContextClassLoader(loader);
ScriptEngineManager manager = new ScriptEngineManager(); ScriptEngineManager manager = new ScriptEngineManager();
this.engine = new MiaoScriptEngine(manager, "nashorn"); this.engine = new MiaoScriptEngine(manager, "nashorn");
this.engine.put("base", new Base()); this.engine.put("base", new Base());
@ -46,7 +39,6 @@ public class ScriptEngine {
this.engine.eval(new InputStreamReader(Thread.currentThread().getContextClassLoader().getResourceAsStream("bios.js"))); this.engine.eval(new InputStreamReader(Thread.currentThread().getContextClassLoader().getResourceAsStream("bios.js")));
} }
engine.invokeFunction("boot", root, logger); engine.invokeFunction("boot", root, logger);
Thread.currentThread().setContextClassLoader(origin);
} }
@SneakyThrows @SneakyThrows