feat: 更新事件注册处理

This commit is contained in:
coding 2018-01-02 18:00:39 +00:00
parent 21808e4266
commit b189732b80
2 changed files with 15 additions and 7 deletions

View File

@ -77,7 +77,7 @@ function isVaildEvent(clz) {
* @param jsp * @param jsp
* @param event * @param event
* @param exec {function} * @param exec {function}
* @param priority * @param priority [LOWEST,LOW,NORMAL,HIGH,HIGHEST,MONITOR]
* @param ignoreCancel * @param ignoreCancel
*/ */
function listen(jsp, event, exec, priority, ignoreCancel) { function listen(jsp, event, exec, priority, ignoreCancel) {
@ -94,7 +94,8 @@ function listen(jsp, event, exec, priority, ignoreCancel) {
} }
} }
if (typeof priority === 'boolean') { if (typeof priority === 'boolean') {
ignoreCancel = priority ignoreCancel = priority;
priority = 'NORMAL';
} }
priority = priority || 'NORMAL'; priority = priority || 'NORMAL';
ignoreCancel = ignoreCancel || false; ignoreCancel = ignoreCancel || false;
@ -113,7 +114,7 @@ function listen(jsp, event, exec, priority, ignoreCancel) {
listener, listener,
EventPriority[priority], EventPriority[priority],
new EventExecutor({ new EventExecutor({
execute: function (listener, event) { execute: function execute(listener, event) {
try { try {
exec(event); exec(event);
} catch (ex) { } catch (ex) {

View File

@ -8,6 +8,7 @@ var Thread = Java.type("java.lang.Thread");
var EventListener = Java.type("org.spongepowered.api.event.EventListener"); var EventListener = Java.type("org.spongepowered.api.event.EventListener");
var Modifier = Java.type("java.lang.reflect.Modifier"); var Modifier = Java.type("java.lang.reflect.Modifier");
var Event = Java.type("org.spongepowered.api.event.Event"); var Event = Java.type("org.spongepowered.api.event.Event");
var Order = Java.type("org.spongepowered.api.event.Order");
var plugin = require('./server').plugin.self; var plugin = require('./server').plugin.self;
@ -76,7 +77,7 @@ function isVaildEvent(clz) {
* @param jsp * @param jsp
* @param event * @param event
* @param exec {function} * @param exec {function}
* @param priority * @param priority [PRE,AFTER_PRE,FIRST,EARLY,DEFAULT,LATE,LAST,BEFORE_POST,POST]
* @param ignoreCancel * @param ignoreCancel
*/ */
function listen(jsp, event, exec, priority, ignoreCancel) { function listen(jsp, event, exec, priority, ignoreCancel) {
@ -85,15 +86,21 @@ function listen(jsp, event, exec, priority, ignoreCancel) {
var eventCls = name2Class(event); var eventCls = name2Class(event);
if (typeof priority === 'boolean') { if (typeof priority === 'boolean') {
ignoreCancel = priority ignoreCancel = priority
priority = 'DEFAULT'
} }
priority = priority || 'NORMAL'; priority = priority || 'DEFAULT';
ignoreCancel = ignoreCancel || false; ignoreCancel = ignoreCancel || false;
var listener = new EventListener({ var listener = new EventListener({
handle: function handle(event) { handle: function handle(event) {
try {
exec(event); exec(event);
} catch (ex) {
console.console('§6插件 §b%s §6处理 §d%s §6事件时发生异常 §4%s'.format(name, event.class.simpleName, ex));
console.ex(ex);
}
} }
}); });
Sponge.getEventManager().registerListener(plugin, eventCls, listener) Sponge.getEventManager().registerListener(plugin, eventCls, Order[priority], listener)
// 添加到缓存 用于关闭插件的时候关闭事件 // 添加到缓存 用于关闭插件的时候关闭事件
if (!listenerMap[name]) listenerMap[name] = []; if (!listenerMap[name]) listenerMap[name] = [];
var listeners = listenerMap[name]; var listeners = listenerMap[name];