feat: add channel support for bungee bukkit sponge

Signed-off-by: MiaoWoo <admin@yumc.pw>
This commit is contained in:
2020-02-24 18:36:08 +08:00
parent 8616c3fe22
commit c996fa6873
9 changed files with 153 additions and 4 deletions

View File

@@ -0,0 +1,38 @@
import { channel, plugin } from '@ms/api'
import { inject, injectable } from '@ms/container'
const Sponge = org.spongepowered.api.Sponge
const RawDataListener = Java.type("org.spongepowered.api.network.RawDataListener")
const ChannelRegistrar = Sponge.getChannelRegistrar()
const Consumer = Java.type("java.util.function.Consumer");
@injectable()
export class SpongeChannel extends channel.Channel {
@inject(plugin.PluginInstance)
private pluginInstance: any;
private channelMap = new Map<string, any>();
send(player: any, channel: string, data: any) {
if (!this.channelMap.has(channel)) { return }
this.channelMap.get(channel).sendTo(player, new Consumer({
accept: (channelBuf: any) => channelBuf.writeBytes(data)
}))
}
register(channel: string, listener: channel.ChannelListener) {
if (!this.channelMap.has(channel)) {
this.channelMap.set(channel, ChannelRegistrar.getOrCreateRaw(this.pluginInstance, channel))
}
let innerListener = new RawDataListener({
handlePayload: (/* ChannelBuf */ data: any, /**RemoteConnection */ connection: any, /**Platform.Type */ side: any) => {
listener(data.readBytes(data.available()))
}
})
this.channelMap.get(channel).addListener(innerListener);
return innerListener;
}
unregister(channel: string, listener: any) {
if (!this.channelMap.has(channel)) { return }
this.channelMap.get(channel).removeListener(listener);
}
}