ms/packages/bungee/src/server.ts
MiaoWoo 5ed61829e1 feat: update typescript version
Signed-off-by: MiaoWoo <admin@yumc.pw>
2021-12-26 01:21:06 +08:00

89 lines
2.7 KiB
TypeScript

import { server, task } from '@ccms/api'
import { provideSingleton, inject, postConstruct } from '@ccms/container'
import * as reflect from '@ccms/common/dist/reflect'
let Bungee: net.md_5.bungee.api.ProxyServer = base.getInstance().getProxy()
@provideSingleton(server.Server)
export class BungeeServer implements server.Server {
private pluginsFolder: string
private pipeline: any
private rootLogger: any
@inject(task.TaskManager)
private task: task.TaskManager
constructor() {
this.pluginsFolder = Bungee.getPluginsFolder().getCanonicalPath()
}
@postConstruct()
initialize() {
let count = 0
let wait = this.task.create(() => {
try {
// @ts-ignore
this.pipeline = reflect.on(base.getInstance().getProxy()).get('listeners').get().toArray()[0].pipeline()
wait.cancel()
} catch (ex: any) {
count++
if (count > 50) {
console.error('Reflect BungeeCord netty channel pipeline error time > 50times. Err: ' + ex)
wait.cancel()
} else {
console.warn('Wait BungeeCord start ready to get netty channel pipeline. Err: ' + ex)
}
}
}).later(10).timer(20).submit()
try {
this.rootLogger = Bungee.getLogger()
} catch (error: any) {
console.error("Can't found rootLogger!")
}
}
getPlayer(name: string) {
return Bungee.getPlayer(name)
}
getVersion(): string {
return Bungee.getVersion()
}
getOnlinePlayers() {
return Bungee.getPlayers()
}
getConsoleSender() {
return Bungee.getConsole()
}
getService(service: string) {
throw new Error("Method not implemented.")
}
broadcast(message: string, permission: string) {
return Bungee.broadcast(message)
}
broadcastMessage(message: string) {
return Bungee.broadcast(message)
}
dispatchCommand(sender: string | any, command: string): boolean {
if (typeof sender === 'string') {
sender = this.getPlayer(sender)
}
return Bungee.getPluginManager().dispatchCommand(sender, command)
}
dispatchConsoleCommand(command: string): boolean {
return Bungee.getPluginManager().dispatchCommand(Bungee.getConsole(), command)
}
getPluginsFolder(): string {
return this.pluginsFolder
}
getNativePluginManager() {
return Bungee.getPluginManager() as any
}
getNettyPipeline() {
return this.pipeline
}
getRootLogger() {
return this.rootLogger
}
}