feat: add server module

Signed-off-by: MiaoWoo <admin@yumc.pw>
This commit is contained in:
2020-01-14 17:44:20 +08:00
parent 93ec202894
commit 093a3dda00
6 changed files with 172 additions and 4 deletions

View File

@ -5,6 +5,7 @@ import { DefaultContainer as container } from '@ms/container'
import { SpongeConsole } from './console';
import { SpongeEvent } from './event';
import { SpongeServer } from './server';
import { SpongeCommand } from './command';
import { SpongeTaskManager } from './task';
@ -16,7 +17,6 @@ container.bind(server.ServerType).toConstantValue(SpongeServerType);
container.bind(plugin.PluginInstance).toConstantValue(Sponge.getPluginManager().getPlugin('MiaoScript').orElse(null));
container.bind(event.Event).to(SpongeEvent).inSingletonScope();
container.bind(server.Server).to(SpongeServer).inSingletonScope();
container.bind(command.Command).to(SpongeCommand).inSingletonScope();
container.bind(task.TaskManager).to(SpongeTaskManager).inSingletonScope();
console.debug(`Detect Sponge Compatible set ServerType to ${SpongeServerType} ...`)

View File

@ -0,0 +1,39 @@
import { server } from '@ms/api'
import { injectable } from '@ms/container';
let Sponge = org.spongepowered.api.Sponge;
let TextSerializers = org.spongepowered.api.text.serializer.TextSerializers;
@injectable()
export class SpongeServer implements server.Server {
getPlayer(name: string) {
return Sponge.getServer().getPlayer(name).orElse(null)
}
getVersion(): string {
return `${Sponge.getPlatform().getImplementation().getName()} (${Sponge.getPlatform().getImplementation().getVersion()})`
}
getOnlinePlayers() {
return Sponge.getServer().getOnlinePlayers()
}
getConsoleSender() {
return Sponge.getServer().getConsole()
}
getService(service: string) {
return Sponge.getServiceManager().provide(base.getClass(service)).orElse(null)
}
dispatchCommand(sender: string | any, command: string): boolean {
if (typeof sender === 'string') {
sender = this.getPlayer(sender)
}
return Sponge.getCommandManager().process(sender, command).getQueryResult()
}
dispatchConsoleCommand(command: string): boolean {
return Sponge.getCommandManager().process(Sponge.getServer().getConsole(), command).getQueryResult()
}
sendJson(sender: string | any, json: string): void {
if (typeof sender === "string") {
sender = this.getPlayer(sender)
}
sender.sendMessage(TextSerializers.JSON.deserialize(json))
}
}