@@ -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();
 | 
				
			||||||
 
 | 
				
			|||||||
@@ -8,14 +8,13 @@ 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;
 | 
				
			||||||
 | 
					
 | 
				
			||||||
/**
 | 
					/**
 | 
				
			||||||
 * Created with IntelliJ IDEA
 | 
					 * Created with IntelliJ IDEA
 | 
				
			||||||
 *
 | 
					 *
 | 
				
			||||||
 * @author 喵♂呜
 | 
					 * @author 喵♂呜
 | 
				
			||||||
 *         Created on 2017/10/25 21:01.
 | 
					 * Created on 2017/10/25 21:01.
 | 
				
			||||||
 */
 | 
					 */
 | 
				
			||||||
public class ScriptEngine {
 | 
					public class ScriptEngine {
 | 
				
			||||||
    private String root;
 | 
					    private String root;
 | 
				
			||||||
@@ -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
 | 
				
			||||||
 
 | 
				
			|||||||
		Reference in New Issue
	
	Block a user