feat: 调整Event载入提示 完善Bukkit的聊天发送
This commit is contained in:
parent
39d9c67c75
commit
8df9ad9ec7
@ -12,6 +12,8 @@ function EventHandlerDefault() {
|
|||||||
this.listenerMap = [];
|
this.listenerMap = [];
|
||||||
this.baseEventDir = '';
|
this.baseEventDir = '';
|
||||||
|
|
||||||
|
var self = this;
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* 扫描包 org.bukkit.event 下的所有事件
|
* 扫描包 org.bukkit.event 下的所有事件
|
||||||
* 映射简写名称 org.bukkit.event.player.PlayerLoginEvent => playerloginevent
|
* 映射简写名称 org.bukkit.event.player.PlayerLoginEvent => playerloginevent
|
||||||
@ -40,8 +42,8 @@ function EventHandlerDefault() {
|
|||||||
// 继承于 org.bukkit.event.Event 访问符为Public
|
// 继承于 org.bukkit.event.Event 访问符为Public
|
||||||
if (this.isVaildEvent(clz)) {
|
if (this.isVaildEvent(clz)) {
|
||||||
// noinspection JSUnresolvedVariable
|
// noinspection JSUnresolvedVariable
|
||||||
var simpleName = this.class2Name(clz);
|
var simpleName = this.class2Name(clz).toLowerCase();
|
||||||
console.debug("Mapping Event [%s] => %s".format(clz.name, simpleName));
|
console.debug("Mapping Event [%s] => %s".format(clz.canonicalName, simpleName));
|
||||||
this.mapEvent[simpleName] = clz;
|
this.mapEvent[simpleName] = clz;
|
||||||
count++;
|
count++;
|
||||||
}
|
}
|
||||||
@ -56,11 +58,11 @@ function EventHandlerDefault() {
|
|||||||
}
|
}
|
||||||
|
|
||||||
this.class2Name = function class2Name(clazz) {
|
this.class2Name = function class2Name(clazz) {
|
||||||
return clazz.simpleName.toLowerCase();
|
return clazz.simpleName;
|
||||||
}
|
}
|
||||||
|
|
||||||
this.name2Class = function name2Class(name, event) {
|
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) {
|
if (!eventCls) {
|
||||||
try {
|
try {
|
||||||
eventCls = base.getClass(eventCls);
|
eventCls = base.getClass(eventCls);
|
||||||
@ -128,7 +130,7 @@ function EventHandlerDefault() {
|
|||||||
if (!listenerMap[name]) listenerMap[name] = [];
|
if (!listenerMap[name]) listenerMap[name] = [];
|
||||||
var offExec = function () {
|
var offExec = function () {
|
||||||
this.unregister(eventCls, listener);
|
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);
|
}.bind(this);
|
||||||
var off = {
|
var off = {
|
||||||
event: eventCls,
|
event: eventCls,
|
||||||
@ -137,7 +139,7 @@ function EventHandlerDefault() {
|
|||||||
};
|
};
|
||||||
listenerMap[name].push(off);
|
listenerMap[name].push(off);
|
||||||
// noinspection JSUnresolvedVariable
|
// 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;
|
return off;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
@ -147,9 +149,9 @@ console.info('%s 事件映射完毕 共计 %s 个事件!'.format(DetectServerTyp
|
|||||||
module.exports = {
|
module.exports = {
|
||||||
on: EventHandler.listen.bind(EventHandler),
|
on: EventHandler.listen.bind(EventHandler),
|
||||||
disable: function (jsp) {
|
disable: function (jsp) {
|
||||||
var jspl = EventHandler.listenerMap[jsp.description.name];
|
var eventCache = EventHandler.listenerMap[jsp.description.name];
|
||||||
if (jspl) {
|
if (eventCache) {
|
||||||
jspl.forEach(function (t) t.off.call(EventHandler));
|
eventCache.forEach(function (t) t.off.call(EventHandler));
|
||||||
delete EventHandler.listenerMap[jsp.description.name];
|
delete EventHandler.listenerMap[jsp.description.name];
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
@ -1,8 +1,52 @@
|
|||||||
/*global Java, base, module, exports, require*/
|
/*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 = {
|
exports = module.exports = {
|
||||||
json: json
|
json: json
|
||||||
};
|
};
|
@ -19,6 +19,12 @@ exports.nmsVersion = Bukkit.server.class.name.split('.')[3];
|
|||||||
exports.nmsCls = function (name) {
|
exports.nmsCls = function (name) {
|
||||||
return Java.type(['net.minecraft.server', exports.nmsVersion, name].join('.'));
|
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('.'));
|
||||||
|
};
|
||||||
/**
|
/**
|
||||||
* 获取玩家
|
* 获取玩家
|
||||||
*/
|
*/
|
||||||
|
@ -32,7 +32,7 @@ function isVaildEvent(clz) {
|
|||||||
}
|
}
|
||||||
|
|
||||||
function class2Name(clazz) {
|
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) {
|
function register(eventCls, exec, priority, ignoreCancel) {
|
||||||
|
@ -24,7 +24,7 @@ exports.plugin = {
|
|||||||
* @returns {*}
|
* @returns {*}
|
||||||
*/
|
*/
|
||||||
get: function (name) {
|
get: function (name) {
|
||||||
return PluginManager.getPlugin(name);
|
return PluginManager.getPlugin(name).orElse(undefined);
|
||||||
},
|
},
|
||||||
/**
|
/**
|
||||||
* 载入插件 并且返回结果
|
* 载入插件 并且返回结果
|
||||||
|
Loading…
Reference in New Issue
Block a user