feat: add channel support for bungee bukkit sponge
Signed-off-by: MiaoWoo <admin@yumc.pw>
This commit is contained in:
38
packages/sponge/src/channel.ts
Normal file
38
packages/sponge/src/channel.ts
Normal 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);
|
||||
}
|
||||
}
|
@ -1,12 +1,13 @@
|
||||
/// <reference types="@ms/types/dist/typings/sponge" />
|
||||
|
||||
import { server, command, event, task } from '@ms/api'
|
||||
import { server, command, event, channel, task } from '@ms/api'
|
||||
import { Container } from '@ms/container'
|
||||
|
||||
import { SpongeConsole } from './console';
|
||||
import { SpongeEvent } from './event';
|
||||
import { SpongeServer } from './server';
|
||||
import { SpongeCommand } from './command';
|
||||
import { SpongeChannel } from './channel';
|
||||
import { SpongeTaskManager } from './task';
|
||||
|
||||
export default function SpongeImpl(container: Container) {
|
||||
@ -14,5 +15,6 @@ export default function SpongeImpl(container: Container) {
|
||||
container.bind(event.Event).to(SpongeEvent).inSingletonScope();
|
||||
container.bind(server.Server).to(SpongeServer).inSingletonScope();
|
||||
container.bind(command.Command).to(SpongeCommand).inSingletonScope();
|
||||
container.bind(channel.Channel).to(SpongeChannel).inSingletonScope();
|
||||
container.bind(task.TaskManager).to(SpongeTaskManager).inSingletonScope();
|
||||
}
|
||||
|
Reference in New Issue
Block a user