feat: 完善事件模块 新增重启引擎命令

This commit is contained in:
coding 2017-10-11 07:47:25 +00:00
parent afdc39b1de
commit 3c888c7da7
3 changed files with 24 additions and 25 deletions

View File

@ -30,9 +30,9 @@ public class MiaoScript extends JavaPlugin implements Executor {
@Override @Override
public void onEnable() { public void onEnable() {
new CommandSub("ms", this);
saveScript(); saveScript();
loadEngine(); loadEngine();
new CommandSub("ms", this);
} }
@Cmd @Cmd
@ -49,6 +49,14 @@ public class MiaoScript extends JavaPlugin implements Executor {
result(sender, engine.eval(new FileReader(new File(getDataFolder(), file)))); result(sender, engine.eval(new FileReader(new File(getDataFolder(), file))));
} }
@Cmd
@Help("重启脚本引擎")
@SneakyThrows
public void reload(CommandSender sender) {
engine.invokeFunction("disable");
engine.invokeFunction("boot", this);
}
private void result(CommandSender sender, Object result) { private void result(CommandSender sender, Object result) {
if (result == null) { if (result == null) {
Log.sender(sender, "§a运行成功! §c没有返回结果!"); Log.sender(sender, "§a运行成功! §c没有返回结果!");

View File

@ -32,8 +32,7 @@ function Reflect(obj) {
} }
}; };
this.method = function (name, param) { this.method = function (name, clazzs) {
var clazzs = types(param);
try { try {
return this.class.getMethod(name, clazzs); return this.class.getMethod(name, clazzs);
} catch (ex) { } catch (ex) {
@ -41,10 +40,10 @@ function Reflect(obj) {
} }
}; };
this.cacheMethod = function (name, param) { this.cacheMethod = function (name, clazzs) {
var mkey = this.class.name + '.' + name; var mkey = this.class.name + '.' + name;
if (!methodCache[mkey]) { if (!methodCache[mkey]) {
methodCache[mkey] = this.method(name, param); methodCache[mkey] = this.method(name, clazzs);
} }
return methodCache[mkey]; return methodCache[mkey];
}; };

View File

@ -126,26 +126,18 @@ function listen(jsp, event, exec, priority, ignoreCancel) {
ignoreCancel); ignoreCancel);
// 添加到缓存 用于关闭插件的时候关闭事件 // 添加到缓存 用于关闭插件的时候关闭事件
var listeners = jspListener[jsp.description.name]; var listeners = jspListener[jsp.description.name];
listeners.push(listener); var off = {
// noinspection JSUnresolvedVariable
log.d('注册事件 %s 方法 %s', eventCls.simpleName, exec.name === '' ? '匿名方法' : exec.name);
return {
event: eventCls, event: eventCls,
listener: listener listener: listener,
off: function(){
ref.on(this.event).call('getHandlerList').get().unregister(this.listener);
log.d('插件 %s 注销事件 %s', pname, this.event.simpleName);
} }
}
/**
* 取消事件监听
* @param listener 监听结果
*/
function unlisten(listener) {
if (!listener.event || !listener.listener) {
throw new IllegalStateException("非法的监听器对象 无法取消事件!");
} }
ref.on(listener.event).call('getHandlerList').get().unregister(listener.listener); listeners.push(off);
// noinspection JSUnresolvedVariable // noinspection JSUnresolvedVariable
log.d('注销事件 %s', listener.event.simpleName); log.d('插件 %s 注册事件 %s 方法 %s', pname, eventCls.simpleName, exec.name === '' ? '匿名方法' : exec.name);
return off;
} }
var mapEvent = []; var mapEvent = [];
@ -155,10 +147,10 @@ mapEventName();
module.exports = { module.exports = {
on: listen, on: listen,
off: unlisten,
disable: function (jsp) { disable: function (jsp) {
jspListener[jsp.description.name].forEach(function (t) { jspListener[jsp.description.name].forEach(function (t) {
ref.on(HandlerList).call('unregisterAll', t); t.off();
}) });
delete jspListener[jsp.description.name];
} }
}; };