fix: 修复事件执行器错误 完善事件系统
This commit is contained in:
parent
10f5448259
commit
443a7d6b1d
@ -83,7 +83,7 @@ function EventHandlerDefault() {
|
|||||||
throw new Error("当前服务器不支持事件系统!");
|
throw new Error("当前服务器不支持事件系统!");
|
||||||
}
|
}
|
||||||
|
|
||||||
this.register = function register(eventCls, listener, priority, ignoreCancel) {
|
this.register = function register(eventCls, exec, priority, ignoreCancel) {
|
||||||
throw new Error("当前服务器不支持事件系统!");
|
throw new Error("当前服务器不支持事件系统!");
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -111,17 +111,25 @@ function EventHandlerDefault() {
|
|||||||
priority = priority || 'NORMAL';
|
priority = priority || 'NORMAL';
|
||||||
ignoreCancel = ignoreCancel || false;
|
ignoreCancel = ignoreCancel || false;
|
||||||
// noinspection JSUnusedGlobalSymbols
|
// noinspection JSUnusedGlobalSymbols
|
||||||
var listener = this.register(eventCls, priority, ignoreCancel);
|
var listener = this.register(eventCls, function execute() {
|
||||||
|
try {
|
||||||
|
exec(arguments[arguments.length -1 ]);
|
||||||
|
} catch (ex) {
|
||||||
|
console.console('§6插件 §b%s §6处理 §d%s §6事件时发生异常 §4%s'.format(name, this.class2Name(eventCls), ex));
|
||||||
|
console.ex(ex);
|
||||||
|
}
|
||||||
|
}.bind(this), priority, ignoreCancel);
|
||||||
var listenerMap = this.listenerMap;
|
var listenerMap = this.listenerMap;
|
||||||
// 添加到缓存 用于关闭插件的时候关闭事件
|
// 添加到缓存 用于关闭插件的时候关闭事件
|
||||||
if (!listenerMap[name]) listenerMap[name] = [];
|
if (!listenerMap[name]) listenerMap[name] = [];
|
||||||
|
var offExec = function () {
|
||||||
|
this.unregister(eventCls, listener);
|
||||||
|
console.debug('插件 %s 注销事件 %s'.format(name, this.class2Name(eventCls)));
|
||||||
|
}.bind(this);
|
||||||
var off = {
|
var off = {
|
||||||
event: eventCls,
|
event: eventCls,
|
||||||
listener: listener,
|
listener: listener,
|
||||||
off: function () {
|
off: offExec
|
||||||
this.unregister(eventCls, listener);
|
|
||||||
console.debug('插件 %s 注销事件 %s'.format(name, this.class2Name(eventCls)));
|
|
||||||
}
|
|
||||||
};
|
};
|
||||||
listenerMap[name].push(off);
|
listenerMap[name].push(off);
|
||||||
// noinspection JSUnresolvedVariable
|
// noinspection JSUnresolvedVariable
|
||||||
|
@ -248,7 +248,7 @@ function init(path) {
|
|||||||
};
|
};
|
||||||
|
|
||||||
function load() {
|
function load() {
|
||||||
checkAndRun(arguments, 'init');
|
checkAndRun(arguments, 'load');
|
||||||
};
|
};
|
||||||
|
|
||||||
function enable() {
|
function enable() {
|
||||||
|
@ -24,21 +24,14 @@ function isVaildEvent(clz) {
|
|||||||
!Modifier.isAbstract(clz.getModifiers());
|
!Modifier.isAbstract(clz.getModifiers());
|
||||||
}
|
}
|
||||||
|
|
||||||
function register(eventCls, priority, ignoreCancel) {
|
function register(eventCls, exec, priority, ignoreCancel) {
|
||||||
var listener = new Listener({});
|
var listener = new Listener({});
|
||||||
MServer.getPluginManager().registerEvent(
|
MServer.getPluginManager().registerEvent(
|
||||||
eventCls,
|
eventCls,
|
||||||
listener,
|
listener,
|
||||||
EventPriority[priority],
|
EventPriority[priority],
|
||||||
new EventExecutor({
|
new EventExecutor({
|
||||||
execute: function execute(listener, event) {
|
execute: exec
|
||||||
try {
|
|
||||||
exec(event);
|
|
||||||
} catch (ex) {
|
|
||||||
console.console('§6插件 §b%s §6处理 §d%s §6事件时发生异常 §4%s'.format(name, event.class.simpleName, ex));
|
|
||||||
console.ex(ex);
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}),
|
}),
|
||||||
this.plugin,
|
this.plugin,
|
||||||
ignoreCancel);
|
ignoreCancel);
|
||||||
|
@ -35,16 +35,9 @@ function class2Name(clazz) {
|
|||||||
return clazz.name.substring(clazz.name.lastIndexOf(".") + 1).replace(/\$/g, '.').toLowerCase();
|
return clazz.name.substring(clazz.name.lastIndexOf(".") + 1).replace(/\$/g, '.').toLowerCase();
|
||||||
}
|
}
|
||||||
|
|
||||||
function register(eventCls, priority, ignoreCancel) {
|
function register(eventCls, exec, priority, ignoreCancel) {
|
||||||
var listener = new EventListener({
|
var listener = new EventListener({
|
||||||
handle: function handle(event) {
|
handle: exec
|
||||||
try {
|
|
||||||
exec(event);
|
|
||||||
} catch (ex) {
|
|
||||||
console.console('§6插件 §b%s §6处理 §d%s §6事件时发生异常 §4%s'.format(name, event.class.simpleName, ex));
|
|
||||||
console.ex(ex);
|
|
||||||
}
|
|
||||||
}
|
|
||||||
});
|
});
|
||||||
MServer.getEventManager().registerListener(this.plugin, eventCls, Order[priorityMap[priority]], listener);
|
MServer.getEventManager().registerListener(this.plugin, eventCls, Order[priorityMap[priority]], listener);
|
||||||
return listener;
|
return listener;
|
||||||
|
Loading…
Reference in New Issue
Block a user