fix: 修复事件执行器错误 完善事件系统

merge/2/HEAD
coding 2018-01-04 16:54:00 +00:00
parent 10f5448259
commit 443a7d6b1d
4 changed files with 19 additions and 25 deletions

View File

@ -83,7 +83,7 @@ function EventHandlerDefault() {
throw new Error("当前服务器不支持事件系统!");
}
this.register = function register(eventCls, listener, priority, ignoreCancel) {
this.register = function register(eventCls, exec, priority, ignoreCancel) {
throw new Error("当前服务器不支持事件系统!");
}
@ -111,17 +111,25 @@ function EventHandlerDefault() {
priority = priority || 'NORMAL';
ignoreCancel = ignoreCancel || false;
// 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;
// 添加到缓存 用于关闭插件的时候关闭事件
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 = {
event: eventCls,
listener: listener,
off: function () {
this.unregister(eventCls, listener);
console.debug('插件 %s 注销事件 %s'.format(name, this.class2Name(eventCls)));
}
off: offExec
};
listenerMap[name].push(off);
// noinspection JSUnresolvedVariable

View File

@ -248,7 +248,7 @@ function init(path) {
};
function load() {
checkAndRun(arguments, 'init');
checkAndRun(arguments, 'load');
};
function enable() {

View File

@ -24,21 +24,14 @@ function isVaildEvent(clz) {
!Modifier.isAbstract(clz.getModifiers());
}
function register(eventCls, priority, ignoreCancel) {
function register(eventCls, exec, priority, ignoreCancel) {
var listener = new Listener({});
MServer.getPluginManager().registerEvent(
eventCls,
listener,
EventPriority[priority],
new EventExecutor({
execute: function execute(listener, event) {
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);
}
}
execute: exec
}),
this.plugin,
ignoreCancel);

View File

@ -35,16 +35,9 @@ function class2Name(clazz) {
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({
handle: function handle(event) {
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);
}
}
handle: exec
});
MServer.getEventManager().registerListener(this.plugin, eventCls, Order[priorityMap[priority]], listener);
return listener;