feat: 优化事件注册逻辑
This commit is contained in:
parent
035f887363
commit
15672ea461
@ -1,2 +1,144 @@
|
||||
/*global Java, base, module, exports, require*/
|
||||
module.exports = require('./msp.js').event;
|
||||
'use strict';
|
||||
/**
|
||||
* Bukkit 事件相关类
|
||||
*/
|
||||
/*global Java, base, module, exports, require, __FILE__*/
|
||||
|
||||
function EventHandlerDefault() {
|
||||
var Thread = Java.type("java.lang.Thread");
|
||||
|
||||
this.plugin = require('./server').plugin.self;
|
||||
this.mapEvent = [];
|
||||
this.listenerMap = [];
|
||||
this.baseEventDir = '';
|
||||
|
||||
/**
|
||||
* 扫描包 org.bukkit.event 下的所有事件
|
||||
* 映射简写名称 org.bukkit.event.player.PlayerLoginEvent => playerloginevent
|
||||
*/
|
||||
this.mapEventName = function mapEventName() {
|
||||
if (this.baseEventDir === "") {
|
||||
throw new Error("事件基础包名为空 无法进行事件映射!");
|
||||
}
|
||||
var count = 0;
|
||||
var dirs = Thread.currentThread().getContextClassLoader().getResources(this.baseEventDir);
|
||||
while (dirs.hasMoreElements()) {
|
||||
var url = dirs.nextElement();
|
||||
var protocol = url.protocol;
|
||||
if (protocol === "jar") {
|
||||
// noinspection JSUnresolvedVariable
|
||||
var jar = url.openConnection().jarFile;
|
||||
var entries = jar.entries();
|
||||
while (entries.hasMoreElements()) {
|
||||
var entry = entries.nextElement();
|
||||
var name = entry.name;
|
||||
// 以 org/bukkit/event 开头 并且以 .class 结尾
|
||||
if (name.startsWith(this.baseEventDir) && name.endsWith(".class")) {
|
||||
var i = name.replaceAll('/', '.');
|
||||
try {
|
||||
var clz = base.getClass(i.substring(0, i.length - 6));
|
||||
// 继承于 org.bukkit.event.Event 访问符为Public
|
||||
if (this.isVaildEvent(clz)) {
|
||||
// noinspection JSUnresolvedVariable
|
||||
var simpleName = this.class2Name(clz);
|
||||
console.debug("Mapping Event [%s] => %s".format(clz.name, simpleName));
|
||||
this.mapEvent[simpleName] = clz;
|
||||
count++;
|
||||
}
|
||||
} catch (ex) {
|
||||
//ignore already loaded class
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
return count;
|
||||
}
|
||||
|
||||
this.class2Name = function class2Name(clazz) {
|
||||
return clazz.simpleName.toLowerCase();
|
||||
}
|
||||
|
||||
this.name2Class = function name2Class(name, event) {
|
||||
var eventCls = this.mapEvent[event] || this.mapEvent[event.toLowerCase()] || this.mapEvent[event + 'Event'] || this.mapEvent[event.toLowerCase() + 'event'];
|
||||
if (!eventCls) {
|
||||
try {
|
||||
eventCls = base.getClass(eventCls);
|
||||
this.mapEvent[event] = eventCls;
|
||||
} catch (ex) {
|
||||
console.console("§6插件 §b%s §6注册事件 §c%s §6失败 §4事件未找到!".format(name, event));
|
||||
console.ex(new Error("插件 %s 注册事件 %s 失败 事件未找到!".format(name, event)))
|
||||
return;
|
||||
}
|
||||
}
|
||||
return eventCls;
|
||||
}
|
||||
|
||||
/**
|
||||
* 判断是否为一个有效的事件类
|
||||
* @param clz
|
||||
* @returns {*|boolean}
|
||||
*/
|
||||
this.isVaildEvent = function isVaildEvent(clz) {
|
||||
throw new Error("当前服务器不支持事件系统!");
|
||||
}
|
||||
|
||||
this.register = function register(eventCls, listener, priority, ignoreCancel) {
|
||||
throw new Error("当前服务器不支持事件系统!");
|
||||
}
|
||||
|
||||
this.unregister = function unregister(event, listener) {
|
||||
throw new Error("当前服务器不支持事件系统!");
|
||||
}
|
||||
|
||||
/**
|
||||
* 添加事件监听
|
||||
* @param jsp
|
||||
* @param event
|
||||
* @param exec {function}
|
||||
* @param priority [LOWEST,LOW,NORMAL,HIGH,HIGHEST,MONITOR]
|
||||
* @param ignoreCancel
|
||||
*/
|
||||
this.listen = function listen(jsp, event, exec, priority, ignoreCancel) {
|
||||
var name = jsp.description.name;
|
||||
if (ext.isNull(name)) throw new TypeError('插件名称为空 请检查传入参数!');
|
||||
var eventCls = this.name2Class(name, event);
|
||||
if (!eventCls) { return; }
|
||||
if (typeof priority === 'boolean') {
|
||||
ignoreCancel = priority;
|
||||
priority = 'NORMAL';
|
||||
}
|
||||
priority = priority || 'NORMAL';
|
||||
ignoreCancel = ignoreCancel || false;
|
||||
// noinspection JSUnusedGlobalSymbols
|
||||
var listener = this.register(eventCls, priority, ignoreCancel);
|
||||
var listenerMap = this.listenerMap;
|
||||
// 添加到缓存 用于关闭插件的时候关闭事件
|
||||
if (!listenerMap[name]) listenerMap[name] = [];
|
||||
var off = {
|
||||
event: eventCls,
|
||||
listener: listener,
|
||||
off: function () {
|
||||
this.unregister(eventCls, listener);
|
||||
console.debug('插件 %s 注销事件 %s'.format(name, this.class2Name(eventCls)));
|
||||
}
|
||||
};
|
||||
listenerMap[name].push(off);
|
||||
// noinspection JSUnresolvedVariable
|
||||
console.debug('插件 %s 注册事件 %s => %s'.format(name, eventCls.name.substring(eventCls.name.lastIndexOf(".") + 1), exec.name === '' ? '匿名方法' : exec.name));
|
||||
return off;
|
||||
}
|
||||
}
|
||||
var EventHandler = Object.assign({}, new EventHandlerDefault(), requireInternal('event'));
|
||||
// 映射事件名称
|
||||
console.info('%s 事件映射完毕 共计 %s 个事件!'.format(DetectServerType, EventHandler.mapEventName().toFixed(0)));
|
||||
module.exports = {
|
||||
on: EventHandler.listen.bind(EventHandler),
|
||||
disable: function (jsp) {
|
||||
var jspl = EventHandler.listenerMap[jsp.description.name];
|
||||
if (jspl) {
|
||||
jspl.forEach(function (t) t.off.bind(EventHandler)());
|
||||
delete EventHandler.listenerMap[jsp.description.name];
|
||||
}
|
||||
}
|
||||
};
|
@ -1,63 +1,15 @@
|
||||
'use strict';
|
||||
/**
|
||||
* Bukkit 事件相关类
|
||||
* Sponge 事件相关类
|
||||
*/
|
||||
/*global Java, base, module, exports, require, __FILE__*/
|
||||
var Thread = Java.type("java.lang.Thread");
|
||||
var Bukkit = Java.type("org.bukkit.Bukkit");
|
||||
var Listener = Java.type("org.bukkit.event.Listener");
|
||||
var Modifier = Java.type("java.lang.reflect.Modifier");
|
||||
var ref = require('reflect');
|
||||
var Event = Java.type("org.bukkit.event.Event");
|
||||
var Modifier = Java.type("java.lang.reflect.Modifier");
|
||||
var Listener = Java.type("org.bukkit.event.Listener");
|
||||
var EventPriority = Java.type("org.bukkit.event.EventPriority");
|
||||
var EventExecutor = Java.type("org.bukkit.plugin.EventExecutor");
|
||||
|
||||
var plugin = require('./server').plugin.self;
|
||||
|
||||
var ref = require('reflect');
|
||||
|
||||
var listenerMap = [];
|
||||
|
||||
/**
|
||||
* 扫描包 org.bukkit.event 下的所有事件
|
||||
* 映射简写名称 org.bukkit.event.player.PlayerLoginEvent => playerloginevent
|
||||
*/
|
||||
function mapEventName() {
|
||||
var eventPackageDir = "org/bukkit/event";
|
||||
var count = 0;
|
||||
var dirs = Thread.currentThread().getContextClassLoader().getResources(eventPackageDir);
|
||||
while (dirs.hasMoreElements()) {
|
||||
var url = dirs.nextElement();
|
||||
var protocol = url.protocol;
|
||||
if (protocol === "jar") {
|
||||
// noinspection JSUnresolvedVariable
|
||||
var jar = url.openConnection().jarFile;
|
||||
var entries = jar.entries();
|
||||
while (entries.hasMoreElements()) {
|
||||
var entry = entries.nextElement();
|
||||
var name = entry.name;
|
||||
// 以 org/bukkit/event 开头 并且以 .class 结尾
|
||||
if (name.startsWith(eventPackageDir) && name.endsWith(".class")) {
|
||||
var i = name.replaceAll('/', '.');
|
||||
try {
|
||||
var clz = base.getClass(i.substring(0, i.length - 6));
|
||||
// 继承于 org.bukkit.event.Event 访问符为Public
|
||||
if (isVaildEvent(clz)) {
|
||||
// noinspection JSUnresolvedVariable
|
||||
var simpleName = clz.simpleName.toLowerCase();
|
||||
console.debug("Mapping Event [%s] => %s".format(clz.name, simpleName));
|
||||
mapEvent[simpleName] = clz;
|
||||
count++;
|
||||
}
|
||||
} catch (ex) {
|
||||
//ignore already loaded class
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
return count;
|
||||
}
|
||||
|
||||
/**
|
||||
* 判断是否为一个有效的事件类
|
||||
* @param clz
|
||||
@ -72,44 +24,9 @@ function isVaildEvent(clz) {
|
||||
!Modifier.isAbstract(clz.getModifiers());
|
||||
}
|
||||
|
||||
/**
|
||||
* 添加事件监听
|
||||
* @param jsp
|
||||
* @param event
|
||||
* @param exec {function}
|
||||
* @param priority [LOWEST,LOW,NORMAL,HIGH,HIGHEST,MONITOR]
|
||||
* @param ignoreCancel
|
||||
*/
|
||||
function listen(jsp, event, exec, priority, ignoreCancel) {
|
||||
var name = jsp.description.name;
|
||||
if (ext.isNull(name)) throw new TypeError('插件名称为空 请检查传入参数!');
|
||||
var eventCls = mapEvent[event] || mapEvent[event.toLowerCase()] || mapEvent[event + 'Event'] || mapEvent[event.toLowerCase() + 'event'];
|
||||
if (!eventCls) {
|
||||
try {
|
||||
eventCls = base.getClass(eventCls);
|
||||
mapEvent[event] = eventCls;
|
||||
} catch (ex) {
|
||||
console.warn("事件 %s 未找到!".format(event));
|
||||
return;
|
||||
}
|
||||
}
|
||||
if (typeof priority === 'boolean') {
|
||||
ignoreCancel = priority;
|
||||
priority = 'NORMAL';
|
||||
}
|
||||
priority = priority || 'NORMAL';
|
||||
ignoreCancel = ignoreCancel || false;
|
||||
function register(eventCls, priority, ignoreCancel) {
|
||||
var listener = new Listener({});
|
||||
// noinspection JSUnusedGlobalSymbols
|
||||
/**
|
||||
* @param event Event type to register
|
||||
* @param listener Listener to register
|
||||
* @param priority Priority to register this event at
|
||||
* @param executor EventExecutor to register
|
||||
* @param plugin Plugin to register
|
||||
* @param ignoreCancel
|
||||
*/
|
||||
Bukkit.getPluginManager().registerEvent(
|
||||
MServer.getPluginManager().registerEvent(
|
||||
eventCls,
|
||||
listener,
|
||||
EventPriority[priority],
|
||||
@ -123,36 +40,18 @@ function listen(jsp, event, exec, priority, ignoreCancel) {
|
||||
}
|
||||
}
|
||||
}),
|
||||
plugin,
|
||||
this.plugin,
|
||||
ignoreCancel);
|
||||
// 添加到缓存 用于关闭插件的时候关闭事件
|
||||
if (!listenerMap[name]) listenerMap[name] = [];
|
||||
var listeners = listenerMap[name];
|
||||
var off = {
|
||||
event: eventCls,
|
||||
listener: listener,
|
||||
off: function () {
|
||||
ref.on(this.event).call('getHandlerList').get().unregister(this.listener);
|
||||
console.debug('插件 %s 注销事件 %s'.format(name, this.event.simpleName));
|
||||
}
|
||||
};
|
||||
listeners.push(off);
|
||||
// noinspection JSUnresolvedVariable
|
||||
console.debug('插件 %s 注册事件 %s => %s'.format(name, eventCls.simpleName, exec.name === '' ? '匿名方法' : exec.name));
|
||||
return off;
|
||||
return listener;
|
||||
}
|
||||
|
||||
var mapEvent = [];
|
||||
// 映射事件名称
|
||||
console.info('Sponge 事件映射完毕 共计 %s 个事件!'.format(mapEventName().toFixed(0)));
|
||||
function unregister(event, listener) {
|
||||
ref.on(event).call('getHandlerList').get().unregister(listener);
|
||||
}
|
||||
|
||||
module.exports = {
|
||||
on: listen,
|
||||
disable: function (jsp) {
|
||||
var jspl = listenerMap[jsp.description.name];
|
||||
if (jspl) {
|
||||
jspl.forEach(function (t) t.off());
|
||||
delete listenerMap[jsp.description.name];
|
||||
}
|
||||
}
|
||||
exports = module.exports = {
|
||||
baseEventDir: 'org/bukkit/event',
|
||||
isVaildEvent: isVaildEvent,
|
||||
register: register,
|
||||
unregister: unregister
|
||||
};
|
@ -1,61 +1,20 @@
|
||||
'use strict';
|
||||
/**
|
||||
* Bukkit 事件相关类
|
||||
* Sponge 事件相关类
|
||||
*/
|
||||
/*global Java, base, module, exports, require, __FILE__*/
|
||||
var Sponge = MServer;
|
||||
var Thread = Java.type("java.lang.Thread");
|
||||
var EventListener = Java.type("org.spongepowered.api.event.EventListener");
|
||||
var Modifier = Java.type("java.lang.reflect.Modifier");
|
||||
var Event = Java.type("org.spongepowered.api.event.Event");
|
||||
var Order = Java.type("org.spongepowered.api.event.Order");
|
||||
var Event = Java.type("org.spongepowered.api.event.Event");
|
||||
var EventListener = Java.type("org.spongepowered.api.event.EventListener");
|
||||
|
||||
var plugin = require('./server').plugin.self;
|
||||
|
||||
var ref = require('reflect');
|
||||
|
||||
var listenerMap = [];
|
||||
|
||||
/**
|
||||
* 扫描包 org.spongepowered.api.event 下的所有事件
|
||||
* 映射简写名称 org.spongepowered.api.event.game.state.GameInitializationEvent => gameinitializationevent
|
||||
*/
|
||||
function mapEventName() {
|
||||
var eventPackageDir = "org/spongepowered/api/event";
|
||||
var count = 0;
|
||||
var dirs = Thread.currentThread().getContextClassLoader().getResources(eventPackageDir);
|
||||
while (dirs.hasMoreElements()) {
|
||||
var url = dirs.nextElement();
|
||||
var protocol = url.protocol;
|
||||
if (protocol === "jar") {
|
||||
// noinspection JSUnresolvedVariable
|
||||
var jar = url.openConnection().jarFile;
|
||||
var entries = jar.entries();
|
||||
while (entries.hasMoreElements()) {
|
||||
var entry = entries.nextElement();
|
||||
var name = entry.name;
|
||||
// 以 org/spongepowered/api/event 开头 并且以 .class 结尾
|
||||
if (name.startsWith(eventPackageDir) && name.endsWith(".class")) {
|
||||
var i = name.replaceAll('/', '.');
|
||||
try {
|
||||
var clz = base.getClass(i.substring(0, i.length - 6));
|
||||
// 继承于 org.spongepowered.api.event.Event 访问符为 Public 并且不是抽象类
|
||||
if (isVaildEvent(clz)) {
|
||||
// noinspection JSUnresolvedVariable
|
||||
var clzName = clz.name;
|
||||
var simpleName = clzName.substring(clzName.lastIndexOf(".") + 1).replace(/\$/g, '.').toLowerCase();
|
||||
console.debug("Mapping Event [%s] => %s".format(clz.name, simpleName));
|
||||
mapEvent[simpleName] = clz;
|
||||
count++;
|
||||
}
|
||||
} catch (ex) {
|
||||
//ignore already loaded class
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
return count;
|
||||
var priorityMap = {
|
||||
'LOWEST': 'PRE',
|
||||
'LOW': 'FIRST',
|
||||
'NORMAL': 'DEFAULT',
|
||||
'HIGH': 'LATE',
|
||||
'HIGHEST': 'LAST',
|
||||
'MONITOR': 'POST'
|
||||
}
|
||||
|
||||
/**
|
||||
@ -72,24 +31,11 @@ function isVaildEvent(clz) {
|
||||
Modifier.isAbstract(clz.getModifiers());
|
||||
}
|
||||
|
||||
/**
|
||||
* 添加事件监听
|
||||
* @param jsp
|
||||
* @param event
|
||||
* @param exec {function}
|
||||
* @param priority [PRE,AFTER_PRE,FIRST,EARLY,DEFAULT,LATE,LAST,BEFORE_POST,POST]
|
||||
* @param ignoreCancel
|
||||
*/
|
||||
function listen(jsp, event, exec, priority, ignoreCancel) {
|
||||
var name = jsp.description.name;
|
||||
if (ext.isNull(name)) throw new TypeError('插件名称为空 请检查传入参数!');
|
||||
var eventCls = name2Class(event);
|
||||
if (typeof priority === 'boolean') {
|
||||
ignoreCancel = priority
|
||||
priority = 'DEFAULT'
|
||||
}
|
||||
priority = priority || 'DEFAULT';
|
||||
ignoreCancel = ignoreCancel || false;
|
||||
function class2Name(clazz) {
|
||||
return clazz.name.substring(clazz.name.lastIndexOf(".") + 1).replace(/\$/g, '.').toLowerCase();
|
||||
}
|
||||
|
||||
function register(eventCls, priority, ignoreCancel) {
|
||||
var listener = new EventListener({
|
||||
handle: function handle(event) {
|
||||
try {
|
||||
@ -100,50 +46,17 @@ function listen(jsp, event, exec, priority, ignoreCancel) {
|
||||
}
|
||||
}
|
||||
});
|
||||
Sponge.getEventManager().registerListener(plugin, eventCls, Order[priority], listener)
|
||||
// 添加到缓存 用于关闭插件的时候关闭事件
|
||||
if (!listenerMap[name]) listenerMap[name] = [];
|
||||
var listeners = listenerMap[name];
|
||||
var off = {
|
||||
event: eventCls,
|
||||
listener: listener,
|
||||
off: function () {
|
||||
Sponge.getEventManager().unregisterListeners(this.listener);
|
||||
console.debug('插件 %s 注销事件 %s'.format(name, this.event.simpleName));
|
||||
}
|
||||
};
|
||||
listeners.push(off);
|
||||
// noinspection JSUnresolvedVariable
|
||||
console.debug('插件 %s 注册事件 %s => %s'.format(name, eventCls.name.substring(eventCls.name.lastIndexOf(".") + 1), exec.name === '' ? '匿名方法' : exec.name));
|
||||
return off;
|
||||
MServer.getEventManager().registerListener(this.plugin, eventCls, Order[priorityMap[priority]], listener);
|
||||
return listener;
|
||||
}
|
||||
|
||||
function name2Class(event) {
|
||||
var eventCls = mapEvent[event] || mapEvent[event.toLowerCase()] || mapEvent[event + 'Event'] || mapEvent[event.toLowerCase() + 'event'];
|
||||
if (!eventCls) {
|
||||
try {
|
||||
eventCls = base.getClass(eventCls);
|
||||
mapEvent[event] = eventCls;
|
||||
} catch (ex) {
|
||||
console.console("§6插件 §b%s §6注册事件 §c%s §6失败 §4事件未找到!".format(name, event));
|
||||
console.ex(new Error("插件 %s 注册事件 %s 失败 事件未找到!".format(name, event)))
|
||||
return;
|
||||
}
|
||||
}
|
||||
return eventCls;
|
||||
function unregister(event, listener) {
|
||||
MServer.getEventManager().unregisterListeners(listener);
|
||||
}
|
||||
|
||||
var mapEvent = [];
|
||||
// 映射事件名称
|
||||
console.info('Sponge 事件映射完毕 共计 %s 个事件!'.format(mapEventName().toFixed(0)));
|
||||
|
||||
module.exports = {
|
||||
on: listen,
|
||||
disable: function (jsp) {
|
||||
var jspl = listenerMap[jsp.description.name];
|
||||
if (jspl) {
|
||||
jspl.forEach(function (t) t.off());
|
||||
delete listenerMap[jsp.description.name];
|
||||
}
|
||||
}
|
||||
exports = module.exports = {
|
||||
baseEventDir: 'org/spongepowered/api/event',
|
||||
isVaildEvent: isVaildEvent,
|
||||
class2Name: class2Name,
|
||||
register: register,
|
||||
unregister: unregister
|
||||
};
|
Loading…
Reference in New Issue
Block a user