2020-02-24 10:36:08 +00:00
|
|
|
import { channel, plugin } from '@ms/api'
|
2020-02-26 02:15:33 +00:00
|
|
|
import { inject, provideSingleton } from '@ms/container'
|
2020-02-24 10:36:08 +00:00
|
|
|
|
|
|
|
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");
|
|
|
|
|
2020-02-26 02:15:33 +00:00
|
|
|
@provideSingleton(channel.Channel)
|
2020-02-24 10:36:08 +00:00
|
|
|
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);
|
|
|
|
}
|
|
|
|
}
|