feat: 使用插件的类加载器

Signed-off-by: 502647092 <admin@yumc.pw>
This commit is contained in:
502647092 2017-11-03 02:04:22 +08:00
parent 1b330c4886
commit abe5569c4a
2 changed files with 10 additions and 12 deletions

View File

@ -10,7 +10,6 @@ import org.bukkit.plugin.java.JavaPlugin;
import lombok.SneakyThrows; import lombok.SneakyThrows;
import lombok.val; import lombok.val;
import pw.yumc.YumCore.bukkit.Log; import pw.yumc.YumCore.bukkit.Log;
import pw.yumc.YumCore.bukkit.P;
import pw.yumc.YumCore.commands.CommandSub; import pw.yumc.YumCore.commands.CommandSub;
import pw.yumc.YumCore.commands.annotation.Cmd; import pw.yumc.YumCore.commands.annotation.Cmd;
import pw.yumc.YumCore.commands.annotation.Help; import pw.yumc.YumCore.commands.annotation.Help;
@ -30,6 +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());
} }
@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(); engine.enableEngine(getClassLoader());
Log.sender(sender, "§bMiaoScript §eEngine §a重启完成!"); Log.sender(sender, "§bMiaoScript §eEngine §a重启完成!");
} }

View File

@ -1,16 +1,11 @@
package pw.yumc.MiaoScript; package pw.yumc.MiaoScript;
import java.lang.Thread;
import java.io.InputStreamReader; 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.Files;
import java.nio.file.Path; import java.nio.file.Path;
import java.nio.file.Paths;
import javax.script.ScriptEngineManager; import javax.script.ScriptEngineManager;
import javax.script.ScriptException;
import lombok.SneakyThrows; import lombok.SneakyThrows;
import pw.yumc.YumCore.engine.MiaoScriptEngine; import pw.yumc.YumCore.engine.MiaoScriptEngine;
@ -29,11 +24,14 @@ public class ScriptEngine {
public ScriptEngine(String root, Object logger) { public ScriptEngine(String root, Object logger) {
this.root = root; this.root = root;
this.logger = logger; this.logger = logger;
enableEngine(); }
public void enableEngine() {
enableEngine(Thread.currentThread().getContextClassLoader());
} }
@SneakyThrows @SneakyThrows
public void enableEngine() { public void enableEngine(ClassLoader loader) {
ScriptEngineManager manager = new ScriptEngineManager(null); ScriptEngineManager manager = new ScriptEngineManager(null);
this.engine = new MiaoScriptEngine(manager, "nashorn"); this.engine = new MiaoScriptEngine(manager, "nashorn");
this.engine.put("base", new Base()); this.engine.put("base", new Base());
@ -42,7 +40,7 @@ public class ScriptEngine {
if (Files.exists(bios)) { if (Files.exists(bios)) {
this.engine.eval("load('" + bios.toFile().getCanonicalPath() + "')"); this.engine.eval("load('" + bios.toFile().getCanonicalPath() + "')");
} else { } 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); engine.invokeFunction("boot", root, logger);
} }