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() {
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();

View File

@ -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