feat: channel add ext data

Signed-off-by: MiaoWoo <admin@yumc.pw>
This commit is contained in:
MiaoWoo 2020-02-27 18:14:17 +08:00
parent 0b416adfd2
commit e7d44af48c
5 changed files with 40 additions and 41 deletions

View File

@ -5,7 +5,7 @@ export namespace channel {
* handle plugin message
* @param data byte[]
*/
export type ChannelListener = (data: any) => void
export type ChannelListener = (data: any, exts?: any) => void
@injectable()
export abstract class Channel {

View File

@ -15,8 +15,8 @@ export class BukkitChannel extends channel.Channel {
}
register(channel: string, listener: channel.ChannelListener) {
Messenger.registerIncomingPluginChannel(this.pluginInstance, channel, new PluginMessageListener({
onPluginMessageReceived: (/**String */ var1, /**Player */ var2, /**byte[] */var3) => {
listener(var3)
onPluginMessageReceived: (/**String */ channel, /**Player */ player, /**byte[] */data) => {
listener(data, { channel, player, data })
}
}));
Messenger.registerOutgoingPluginChannel(this.pluginInstance, channel);

View File

@ -1,18 +1,25 @@
import { channel } from '@ms/api'
import { provideSingleton } from '@ms/container'
import { channel, event } from '@ms/api'
import { provideSingleton, inject } from '@ms/container'
const Bungee: net.md_5.bungee.api.ProxyServer = base.getInstance().getProxy()
@provideSingleton(channel.Channel)
export class BungeeChannel extends channel.Channel {
@inject(event.Event)
private eventManager: event.Event;
send(player: any, channel: string, data: any) {
throw new Error("Method not implemented.");
}
register(channel: string, listener: channel.ChannelListener) {
Bungee.registerChannel(channel);
console.console('§6[§eWARN§6] §eBungeeCord Channel only registerChannel you need self hanler PluginMessageEvent!')
// console.console('§6[§eWARN§6] §eMiaoScript channel in BungeeCord only register. you need self hanler PluginMessageEvent!')
return this.eventManager.listen({ description: { name: channel } }, "PluginMessageEvent", (event: net.md_5.bungee.api.event.PluginMessageEvent) => {
listener(event.getData(), event)
})
}
unregister(channel: string, listener: any) {
Bungee.unregisterChannel(channel);
listener.off();
}
}

View File

@ -3,7 +3,7 @@
/// <reference types="@ms/types/dist/typings/bungee" />
import { server, plugin as pluginApi, channel } from '@ms/api'
import { inject } from '@ms/container';
import { inject, optional } from '@ms/container';
import { plugin, interfaces, cmd, listener, tab, config } from '@ms/plugin'
import Tellraw from '@ms/common/dist/tellraw'
@ -56,10 +56,10 @@ class MiaoMessage {
export class MiaoChat extends interfaces.Plugin {
@inject(server.Server)
private Server: server.Server
@inject(server.ServerType)
private ServerType: string
@inject(channel.Channel)
private Channel: channel.Channel
@optional() private Channel: channel.Channel
private channelOff: { off: () => void };
@config()
private config = {
@ -169,12 +169,10 @@ export class MiaoChat extends interfaces.Plugin {
return string;
}
}
this.Channel.listen(this, MiaoMessage.CHANNEL, (data) => {
this.sendChatAll(MiaoMessage.decode(data).json)
})
}
disable() {
this.channelOff?.off()
}
bukkitenable() {
@ -186,9 +184,9 @@ export class MiaoChat extends interfaces.Plugin {
} catch (ex) {
this.logger.console("§cCan't found me.clip.placeholderapi.PlaceholderAPI variable will not be replaced! Err: " + ex)
}
}
bukkitdisable() {
this.channelOff = this.Channel?.listen(this, MiaoMessage.CHANNEL, (data) => {
this.sendChatAll(MiaoMessage.decode(data).json)
})
}
spongeenable() {
@ -207,15 +205,25 @@ export class MiaoChat extends interfaces.Plugin {
} catch (ex) {
this.logger.console("§cCan't found me.rojo8399.placeholderapi.PlaceholderService variable will not be replaced! Err: " + ex)
}
}
spongedisable() {
this.channelOff = this.Channel?.listen(this, MiaoMessage.CHANNEL, (data) => {
this.sendChatAll(MiaoMessage.decode(data).json)
})
}
bungeeenable() {
}
bungeedisable() {
this.channelOff = this.Channel?.listen(this, MiaoMessage.CHANNEL, (data, event: net.md_5.bungee.api.event.PluginMessageEvent) => {
let bungee: net.md_5.bungee.api.ProxyServer = base.getInstance().getProxy()
if (event.getTag() == MiaoMessage.CHANNEL) {
let origin = event.getSender().getAddress();
bungee.getServers().forEach(new BiConsumer({
accept: (s, server) => {
if (server.getAddress() != origin && server.getPlayers().size() > 0) {
server.sendData(event.getTag(), event.getData())
}
}
}))
}
})
}
@cmd({ servers: ["bungee"] })
@ -257,22 +265,6 @@ export class MiaoChat extends interfaces.Plugin {
});
}
@listener({ servers: ['bungee'] })
PluginMessageEvent(e: any) {
let bungee: net.md_5.bungee.api.ProxyServer = base.getInstance().getProxy()
let event = e as net.md_5.bungee.api.event.PluginMessageEvent
if (event.getTag() == MiaoMessage.CHANNEL) {
let origin = event.getSender().getAddress();
bungee.getServers().forEach(new BiConsumer({
accept: (s, server) => {
if (server.getAddress() != origin && server.getPlayers().size() > 0) {
server.sendData(event.getTag(), event.getData())
}
}
}))
}
}
initFormat(chatFormats: any[]) {
chatFormats.forEach(chatFormat => {
var chat_format_str = chatFormat.format;
@ -308,7 +300,7 @@ export class MiaoChat extends interfaces.Plugin {
chat_format.format_list.forEach((format) => {
var style = this.styleFormats[format];
if (style) {
tr.then(this.replace(player, style.text));
tr.then(this.replace(player, style.text.replace(/&(\w)/g, '§$1')));
if (style.hover) {
tr.tip(this.replace(player, style.hover.join('\n')));
}
@ -333,7 +325,7 @@ export class MiaoChat extends interfaces.Plugin {
});
let json = tr.then(this.replace(player, plain)).json()
this.sendChatAll(json)
this.Channel.send(player, MiaoMessage.CHANNEL, MiaoMessage.encode(json))
this.Channel?.send(player, MiaoMessage.CHANNEL, MiaoMessage.encode(json))
}
sendChatAll(json: string) {

View File

@ -25,7 +25,7 @@ export class SpongeChannel extends channel.Channel {
}
let innerListener = new RawDataListener({
handlePayload: (/* ChannelBuf */ data: any, /**RemoteConnection */ connection: any, /**Platform.Type */ side: any) => {
listener(data.readBytes(data.available()))
listener(data.readBytes(data.available()), { data, connection, side })
}
})
this.channelMap.get(channel).addListener(innerListener);