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 * handle plugin message
* @param data byte[] * @param data byte[]
*/ */
export type ChannelListener = (data: any) => void export type ChannelListener = (data: any, exts?: any) => void
@injectable() @injectable()
export abstract class Channel { export abstract class Channel {

View File

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

View File

@ -1,18 +1,25 @@
import { channel } from '@ms/api' import { channel, event } from '@ms/api'
import { provideSingleton } from '@ms/container' import { provideSingleton, inject } from '@ms/container'
const Bungee: net.md_5.bungee.api.ProxyServer = base.getInstance().getProxy() const Bungee: net.md_5.bungee.api.ProxyServer = base.getInstance().getProxy()
@provideSingleton(channel.Channel) @provideSingleton(channel.Channel)
export class BungeeChannel extends channel.Channel { export class BungeeChannel extends channel.Channel {
@inject(event.Event)
private eventManager: event.Event;
send(player: any, channel: string, data: any) { send(player: any, channel: string, data: any) {
throw new Error("Method not implemented."); throw new Error("Method not implemented.");
} }
register(channel: string, listener: channel.ChannelListener) { register(channel: string, listener: channel.ChannelListener) {
Bungee.registerChannel(channel); 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) { unregister(channel: string, listener: any) {
Bungee.unregisterChannel(channel); Bungee.unregisterChannel(channel);
listener.off();
} }
} }

View File

@ -3,7 +3,7 @@
/// <reference types="@ms/types/dist/typings/bungee" /> /// <reference types="@ms/types/dist/typings/bungee" />
import { server, plugin as pluginApi, channel } from '@ms/api' 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 { plugin, interfaces, cmd, listener, tab, config } from '@ms/plugin'
import Tellraw from '@ms/common/dist/tellraw' import Tellraw from '@ms/common/dist/tellraw'
@ -56,10 +56,10 @@ class MiaoMessage {
export class MiaoChat extends interfaces.Plugin { export class MiaoChat extends interfaces.Plugin {
@inject(server.Server) @inject(server.Server)
private Server: server.Server private Server: server.Server
@inject(server.ServerType)
private ServerType: string
@inject(channel.Channel) @inject(channel.Channel)
private Channel: channel.Channel @optional() private Channel: channel.Channel
private channelOff: { off: () => void };
@config() @config()
private config = { private config = {
@ -169,12 +169,10 @@ export class MiaoChat extends interfaces.Plugin {
return string; return string;
} }
} }
this.Channel.listen(this, MiaoMessage.CHANNEL, (data) => {
this.sendChatAll(MiaoMessage.decode(data).json)
})
} }
disable() { disable() {
this.channelOff?.off()
} }
bukkitenable() { bukkitenable() {
@ -186,9 +184,9 @@ export class MiaoChat extends interfaces.Plugin {
} catch (ex) { } catch (ex) {
this.logger.console("§cCan't found me.clip.placeholderapi.PlaceholderAPI variable will not be replaced! Err: " + ex) this.logger.console("§cCan't found me.clip.placeholderapi.PlaceholderAPI variable will not be replaced! Err: " + ex)
} }
} this.channelOff = this.Channel?.listen(this, MiaoMessage.CHANNEL, (data) => {
this.sendChatAll(MiaoMessage.decode(data).json)
bukkitdisable() { })
} }
spongeenable() { spongeenable() {
@ -207,15 +205,25 @@ export class MiaoChat extends interfaces.Plugin {
} catch (ex) { } catch (ex) {
this.logger.console("§cCan't found me.rojo8399.placeholderapi.PlaceholderService variable will not be replaced! Err: " + ex) this.logger.console("§cCan't found me.rojo8399.placeholderapi.PlaceholderService variable will not be replaced! Err: " + ex)
} }
} this.channelOff = this.Channel?.listen(this, MiaoMessage.CHANNEL, (data) => {
this.sendChatAll(MiaoMessage.decode(data).json)
spongedisable() { })
} }
bungeeenable() { bungeeenable() {
} 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()
bungeedisable() { 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"] }) @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[]) { initFormat(chatFormats: any[]) {
chatFormats.forEach(chatFormat => { chatFormats.forEach(chatFormat => {
var chat_format_str = chatFormat.format; var chat_format_str = chatFormat.format;
@ -308,7 +300,7 @@ export class MiaoChat extends interfaces.Plugin {
chat_format.format_list.forEach((format) => { chat_format.format_list.forEach((format) => {
var style = this.styleFormats[format]; var style = this.styleFormats[format];
if (style) { if (style) {
tr.then(this.replace(player, style.text)); tr.then(this.replace(player, style.text.replace(/&(\w)/g, '§$1')));
if (style.hover) { if (style.hover) {
tr.tip(this.replace(player, style.hover.join('\n'))); 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() let json = tr.then(this.replace(player, plain)).json()
this.sendChatAll(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) { sendChatAll(json: string) {

View File

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