From 634dbdb16d895d60fb68ab7b5c562f8d76d2fda9 Mon Sep 17 00:00:00 2001 From: MiaoWoo Date: Tue, 10 Sep 2019 17:21:44 +0800 Subject: [PATCH] feat: complate sponge console Signed-off-by: MiaoWoo --- packages/sponge/src/console.ts | 29 +++++++++++++++++++++++++++++ packages/sponge/src/index.ts | 14 +++++++++++++- 2 files changed, 42 insertions(+), 1 deletion(-) create mode 100644 packages/sponge/src/console.ts diff --git a/packages/sponge/src/console.ts b/packages/sponge/src/console.ts new file mode 100644 index 00000000..2ce32ebb --- /dev/null +++ b/packages/sponge/src/console.ts @@ -0,0 +1,29 @@ +import '@ms/nashorn' +import { MiaoScriptConsole } from '@ms/api' + +let Sponge = Java.type('org.spongepowered.api.Sponge'); +var Text = Java.type('org.spongepowered.api.text.Text'); +let CommandSource = Java.type('org.spongepowered.api.command.CommandSource'); + +export class SpongeConsole extends MiaoScriptConsole { + sender(sender, ...args) { + if (!(sender instanceof CommandSource)) { + this.error('第一个参数未实现 org.spongepowered.api.command.CommandSource 无法发送消息!') + return + } + if (args[0].toString() === '[object Array]') { + args[0].forEach(line => sender.sendMessage(Text.of(this.prefix + line))) + } else { + sender.sendMessage(Text.of(this.prefix + args.join(' '))); + } + } + console(...args): void { + this.sender(Sponge.server.console, args.join(' ')); + } + warn(...args) { + this.logger.warn(this.name + args.join(' ')); + } + error(...args) { + this.logger.error(this.name + args.join(' ')); + } +} diff --git a/packages/sponge/src/index.ts b/packages/sponge/src/index.ts index 56004c9f..c9bd8533 100644 --- a/packages/sponge/src/index.ts +++ b/packages/sponge/src/index.ts @@ -1 +1,13 @@ -export default {} \ No newline at end of file +import { server, plugin } from '@ms/api' +import { DefaultContainer as container } from '@ms/container' + +import { SpongeConsole } from './console' + +let SpongeServerType = 'sponge'; +let Sponge = Java.type("org.spongepowered.api.Sponge"); + +console.debug(`Detect Sponge Compatible set ServerType to ${SpongeServerType} ...`) + +container.bind(server.Console).toConstantValue(SpongeConsole); +container.bind(server.ServerType).toConstantValue(SpongeServerType); +container.bind(plugin.PluginInstance).toConstantValue(Sponge.pluginManager.getPlugin('MiaoScript').orElse(null));