feat: 调整Event载入提示 完善Bukkit的聊天发送

This commit is contained in:
coding 2018-05-14 16:42:08 +00:00
parent 39d9c67c75
commit 8df9ad9ec7
5 changed files with 65 additions and 13 deletions

View File

@ -12,6 +12,8 @@ function EventHandlerDefault() {
this.listenerMap = [];
this.baseEventDir = '';
var self = this;
/**
* 扫描包 org.bukkit.event 下的所有事件
* 映射简写名称 org.bukkit.event.player.PlayerLoginEvent => playerloginevent
@ -40,8 +42,8 @@ function EventHandlerDefault() {
// 继承于 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));
var simpleName = this.class2Name(clz).toLowerCase();
console.debug("Mapping Event [%s] => %s".format(clz.canonicalName, simpleName));
this.mapEvent[simpleName] = clz;
count++;
}
@ -56,11 +58,11 @@ function EventHandlerDefault() {
}
this.class2Name = function class2Name(clazz) {
return clazz.simpleName.toLowerCase();
return clazz.simpleName;
}
this.name2Class = function name2Class(name, event) {
var eventCls = this.mapEvent[event] || this.mapEvent[event.toLowerCase()] || this.mapEvent[event + 'Event'] || this.mapEvent[event.toLowerCase() + 'event'];
var eventCls = this.mapEvent[event.toLowerCase()] || this.mapEvent[event.toLowerCase() + 'event'];
if (!eventCls) {
try {
eventCls = base.getClass(eventCls);
@ -128,7 +130,7 @@ function EventHandlerDefault() {
if (!listenerMap[name]) listenerMap[name] = [];
var offExec = function () {
this.unregister(eventCls, listener);
console.debug('插件 %s 注销事件 %s'.format(name, eventCls.name.substring(eventCls.name.lastIndexOf(".") + 1)));
console.debug('插件 %s 注销事件 %s'.format(name, this.class2Name(eventCls)));
}.bind(this);
var off = {
event: eventCls,
@ -137,7 +139,7 @@ function EventHandlerDefault() {
};
listenerMap[name].push(off);
// noinspection JSUnresolvedVariable
console.debug('插件 %s 注册事件 %s => %s'.format(name, eventCls.name.substring(eventCls.name.lastIndexOf(".") + 1), exec.name === '' ? '匿名方法' : exec.name));
console.debug('插件 %s 注册事件 %s => %s'.format(name, this.class2Name(eventCls), exec.name === '' ? '匿名方法' : exec.name));
return off;
}
}
@ -147,9 +149,9 @@ console.info('%s 事件映射完毕 共计 %s 个事件!'.format(DetectServerTyp
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.call(EventHandler));
var eventCache = EventHandler.listenerMap[jsp.description.name];
if (eventCache) {
eventCache.forEach(function (t) t.off.call(EventHandler));
delete EventHandler.listenerMap[jsp.description.name];
}
}

View File

@ -1,8 +1,52 @@
/*global Java, base, module, exports, require*/
function json(sender, json) {
var bukkit = require('api/server');
var nmsChatSerializerClass;
var nmsIChatBaseComponentClass;
var packetTypeClass;
var nmsChatMessageTypeClass;
var chatMessageTypes;
var String = Java.type('java.lang.String');
function init () {
nmsChatSerializerClass = bukkit.nmsCls(bukkit.nmsVersion.split("_")[1] > 7 ? "IChatBaseComponent$ChatSerializer" : "ChatSerializer");
nmsIChatBaseComponentClass = bukkit.nmsCls('IChatBaseComponent');
packetTypeClass = bukkit.nmsCls("PacketPlayOutChat");
var packetTypeConstructor;
Java.from(packetTypeClass.class.constructors).forEach(function (c) {
if (c.parameterTypes.length == 2) { packetTypeConstructor = c };
})
nmsChatMessageTypeClass = packetTypeConstructor.parameterTypes[1];
if (nmsChatMessageTypeClass.isEnum()) {
chatMessageTypes = nmsChatMessageTypeClass.getEnumConstants();
} else {
switch (nmsChatMessageTypeClass.name) {
case "int":
nmsChatMessageTypeClass = java.lang.Integer;
case "byte":
nmsChatMessageTypeClass = java.lang.Byte;
}
}
}
function json(sender, json) {
send(sender, json, 0);
}
function send(sender, json, type) {
var serialized = nmsChatSerializerClass.a(json)
var typeObj = chatMessageTypes == null ? nmsChatMessageTypeClass.valueOf(String.valueOf(type)) : chatMessageTypes[type];
sendPacket(sender, new packetTypeClass(serialized, typeObj))
}
function sendPacket(player, p) {
player.handle.playerConnection.sendPacket(p);
}
init();
exports = module.exports = {
json: json
};

View File

@ -19,6 +19,12 @@ exports.nmsVersion = Bukkit.server.class.name.split('.')[3];
exports.nmsCls = function (name) {
return Java.type(['net.minecraft.server', exports.nmsVersion, name].join('.'));
};
/**
* 获取OBC类
*/
exports.obcCls = function (name) {
return Java.type(['org.bukkit.craftbukkit', exports.nmsVersion, name].join('.'));
};
/**
* 获取玩家
*/

View File

@ -32,7 +32,7 @@ function isVaildEvent(clz) {
}
function class2Name(clazz) {
return clazz.name.substring(clazz.name.lastIndexOf(".") + 1).replace(/\$/g, '.').toLowerCase();
return clazz.canonicalName.substring(clazz.name.lastIndexOf(".") + 1);
}
function register(eventCls, exec, priority, ignoreCancel) {

View File

@ -24,7 +24,7 @@ exports.plugin = {
* @returns {*}
*/
get: function (name) {
return PluginManager.getPlugin(name);
return PluginManager.getPlugin(name).orElse(undefined);
},
/**
* 载入插件 并且返回结果