feat: 完善事件模块 新增重启引擎命令
This commit is contained in:
parent
afdc39b1de
commit
3c888c7da7
@ -30,9 +30,9 @@ public class MiaoScript extends JavaPlugin implements Executor {
|
||||
|
||||
@Override
|
||||
public void onEnable() {
|
||||
new CommandSub("ms", this);
|
||||
saveScript();
|
||||
loadEngine();
|
||||
new CommandSub("ms", this);
|
||||
}
|
||||
|
||||
@Cmd
|
||||
@ -49,6 +49,14 @@ public class MiaoScript extends JavaPlugin implements Executor {
|
||||
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) {
|
||||
if (result == null) {
|
||||
Log.sender(sender, "§a运行成功! §c没有返回结果!");
|
||||
|
@ -32,8 +32,7 @@ function Reflect(obj) {
|
||||
}
|
||||
};
|
||||
|
||||
this.method = function (name, param) {
|
||||
var clazzs = types(param);
|
||||
this.method = function (name, clazzs) {
|
||||
try {
|
||||
return this.class.getMethod(name, clazzs);
|
||||
} 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;
|
||||
if (!methodCache[mkey]) {
|
||||
methodCache[mkey] = this.method(name, param);
|
||||
methodCache[mkey] = this.method(name, clazzs);
|
||||
}
|
||||
return methodCache[mkey];
|
||||
};
|
||||
|
@ -126,26 +126,18 @@ function listen(jsp, event, exec, priority, ignoreCancel) {
|
||||
ignoreCancel);
|
||||
// 添加到缓存 用于关闭插件的时候关闭事件
|
||||
var listeners = jspListener[jsp.description.name];
|
||||
listeners.push(listener);
|
||||
// noinspection JSUnresolvedVariable
|
||||
log.d('注册事件 %s 方法 %s', eventCls.simpleName, exec.name === '' ? '匿名方法' : exec.name);
|
||||
return {
|
||||
var off = {
|
||||
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
|
||||
log.d('注销事件 %s', listener.event.simpleName);
|
||||
log.d('插件 %s 注册事件 %s 方法 %s', pname, eventCls.simpleName, exec.name === '' ? '匿名方法' : exec.name);
|
||||
return off;
|
||||
}
|
||||
|
||||
var mapEvent = [];
|
||||
@ -155,10 +147,10 @@ mapEventName();
|
||||
|
||||
module.exports = {
|
||||
on: listen,
|
||||
off: unlisten,
|
||||
disable: function (jsp) {
|
||||
jspListener[jsp.description.name].forEach(function (t) {
|
||||
ref.on(HandlerList).call('unregisterAll', t);
|
||||
})
|
||||
t.off();
|
||||
});
|
||||
delete jspListener[jsp.description.name];
|
||||
}
|
||||
};
|
Loading…
Reference in New Issue
Block a user