From e9a4f2f35198ee253b500dbe93ecbbe715477e9c Mon Sep 17 00:00:00 2001 From: MiaoWoo Date: Tue, 22 Sep 2020 15:29:12 +0800 Subject: [PATCH] refactor: chat & command tabComplete Signed-off-by: MiaoWoo --- packages/api/src/chat.ts | 9 +++++ packages/api/src/command.ts | 3 ++ packages/api/src/interfaces/server/index.ts | 40 ++++++++------------- packages/bukkit/src/chat.ts | 7 ++-- packages/bukkit/src/command.ts | 27 +++++++------- packages/bukkit/src/server.ts | 12 ------- packages/bungee/src/server.ts | 27 +++++++------- packages/sponge/src/chat.ts | 22 ++++++++++++ packages/spring/src/command.ts | 3 ++ packages/spring/src/server.ts | 10 +----- 10 files changed, 85 insertions(+), 75 deletions(-) create mode 100644 packages/sponge/src/chat.ts diff --git a/packages/api/src/chat.ts b/packages/api/src/chat.ts index e11019b8..806ab96e 100644 --- a/packages/api/src/chat.ts +++ b/packages/api/src/chat.ts @@ -3,6 +3,15 @@ import { injectable } from '@ccms/container' export namespace chat { @injectable() export abstract class Chat { + /** + * sendJsonChat + * @param sender reciver + * @param json json + * @param type chat Type 0: chat 1: system 2: actionBar + */ + sendJson(sender: any, json: string | object, type = 0) { + throw new Error("Method not implemented.") + } sendMessage(sender: any, message: string) { throw new Error("Method not implemented.") } diff --git a/packages/api/src/command.ts b/packages/api/src/command.ts index c25be909..aa257e6a 100644 --- a/packages/api/src/command.ts +++ b/packages/api/src/command.ts @@ -24,6 +24,9 @@ export namespace command { this.onTabComplete(plugin, cmd, exec.tab) } } + public tabComplete(sender: any, input: string, index?: number): string[] { + throw new Error("Method not implemented.") + } /** * 取消命令注册 * @param plugin 插件 diff --git a/packages/api/src/interfaces/server/index.ts b/packages/api/src/interfaces/server/index.ts index 7bc52dbe..d0a816ce 100644 --- a/packages/api/src/interfaces/server/index.ts +++ b/packages/api/src/interfaces/server/index.ts @@ -4,6 +4,8 @@ import { injectable, inject } from '@ccms/container' import { NativePluginManager } from './native_plugin' import { constants } from '../../constants' +export { NativePluginManager } from './native_plugin' + export namespace server { /** * Runtime ServerType @@ -13,10 +15,6 @@ export namespace server { * Runtime Console */ export const Console = Symbol("Console") - /** - * MiaoScript Server - */ - export const Server = Symbol("Server") /** * Runtime Server Instance */ @@ -24,21 +22,19 @@ export namespace server { /** * MiaoScript Server */ - export interface Server { - getVersion(): string - getPlayer(name: string): any - getOnlinePlayers(): any[] - getConsoleSender(): any - getService(service: string): any - dispatchCommand(sender: string | any, command: string): boolean - dispatchConsoleCommand(command: string): boolean - getPluginsFolder(): string - getNativePluginManager(): NativePluginManager - getDedicatedServer?(): any - getNettyPipeline(): any - getRootLogger(): any - sendJson(sender: string | any, json: object | string): void - tabComplete?(sender: string | any, input: string, index?: number): string[] + export abstract class Server { + abstract getVersion(): string + abstract getPlayer(name: string): any + abstract getOnlinePlayers(): any[] + abstract getConsoleSender(): any + abstract getService(service: string): any + abstract dispatchCommand(sender: string | any, command: string): boolean + abstract dispatchConsoleCommand(command: string): boolean + abstract getPluginsFolder(): string + abstract getNativePluginManager(): NativePluginManager + abstract getDedicatedServer?(): any + abstract getNettyPipeline(): any + abstract getRootLogger(): any } @injectable() export class ServerChecker { @@ -102,12 +98,6 @@ export namespace server { getRootLogger() { throw new Error("Method not implemented.") } - sendJson(sender: any, json: string | object): void { - throw new Error("Method not implemented.") - } - tabComplete?(sender: any, input: string, index?: number): string[] { - throw new Error("Method not implemented.") - } protected reflect() { try { let consoleServer = this.getDedicatedServer() diff --git a/packages/bukkit/src/chat.ts b/packages/bukkit/src/chat.ts index 8323ec2e..71a97dc8 100644 --- a/packages/bukkit/src/chat.ts +++ b/packages/bukkit/src/chat.ts @@ -4,11 +4,14 @@ import bukkitChat from './enhance/chat' @provideSingleton(chat.Chat) export class BukkitChat extends chat.Chat { + sendJson(sender: any, json: string | object, type = 0) { + bukkitChat.send(sender, typeof json === "string" ? json : JSON.stringify(json), type) + } sendMessage(sender: any, message: string) { - bukkitChat.send(sender, JSON.stringify({ text: message }), 0) + this.sendJson(sender, { text: message }, 0) } sendActionBar(sender: any, message: string) { - bukkitChat.send(sender, JSON.stringify({ text: message }), 2) + this.sendJson(sender, { text: message }, 2) } sendTitle(sender: any, title: string, subtitle: string = '', fadeIn: number = 20, time: number = 100, fadeOut: number = 20) { sender.sendTitle(title, subtitle, fadeIn, time, fadeOut) diff --git a/packages/bukkit/src/command.ts b/packages/bukkit/src/command.ts index 6b23cc8b..7a6d018f 100644 --- a/packages/bukkit/src/command.ts +++ b/packages/bukkit/src/command.ts @@ -4,44 +4,47 @@ import { command, plugin } from '@ccms/api' import * as reflect from '@ccms/common/dist/reflect' import { provideSingleton, postConstruct, inject } from '@ccms/container' -let Bukkit = org.bukkit.Bukkit; -let TabCompleter = Java.type('org.bukkit.command.TabCompleter'); -let PluginCommand = Java.type('org.bukkit.command.PluginCommand'); -let CommandExecutor = Java.type('org.bukkit.command.CommandExecutor'); +let Bukkit = org.bukkit.Bukkit +let TabCompleter = Java.type('org.bukkit.command.TabCompleter') +let PluginCommand = Java.type('org.bukkit.command.PluginCommand') +let CommandExecutor = Java.type('org.bukkit.command.CommandExecutor') @provideSingleton(command.Command) export class BukkitCommand extends command.Command { @inject(plugin.PluginInstance) private pluginInstance: any - private commandMap: any; + private commandMap: any @postConstruct() init() { - this.commandMap = reflect.on(Bukkit.getPluginManager()).get('commandMap').get(); + this.commandMap = reflect.on(Bukkit.getPluginManager()).get('commandMap').get() } create(plugin: any, command: string) { var cmd = this.commandMap.getCommand(command) if (cmd && cmd instanceof PluginCommand) { return cmd }; - cmd = reflect.on(PluginCommand).create(command, this.pluginInstance).get(); - this.commandMap.register(plugin.description.name, cmd); - return cmd; + cmd = reflect.on(PluginCommand).create(command, this.pluginInstance).get() + this.commandMap.register(plugin.description.name, cmd) + return cmd } remove(plugin: any, command: string) { var cmd = this.commandMap.getCommand(command) if (cmd && cmd instanceof PluginCommand) { - cmd.unregister(this.commandMap); + cmd.unregister(this.commandMap) } } + tabComplete(sender: any, input: string, index?: number): string[] { + return Java.from(this.commandMap.tabComplete(sender, input)) + } onCommand(plugin: any, command: any, executor: Function) { // 必须指定需要实现的接口类型 否则MOD服会报错 command.setExecutor(new CommandExecutor({ onCommand: super.setExecutor(plugin, command, executor) - })); + })) } onTabComplete(plugin: any, command: any, tabCompleter: Function) { // 必须指定需要实现的接口类型 否则MOD服会报错 command.setTabCompleter(new TabCompleter({ onTabComplete: super.setTabCompleter(plugin, command, tabCompleter) - })); + })) } } diff --git a/packages/bukkit/src/server.ts b/packages/bukkit/src/server.ts index fbc049fc..2ca58056 100644 --- a/packages/bukkit/src/server.ts +++ b/packages/bukkit/src/server.ts @@ -54,16 +54,4 @@ export class BukkitServer extends server.ReflectServer { getRootLogger() { return this.rootLogger } - sendJson(sender: string | any, json: object | string): void { - if (typeof sender === "string") { - sender = this.getPlayer(sender) - } - let result = chat.json(sender, typeof json == "string" ? json : JSON.stringify(json)) - if (result !== false) { - this.dispatchConsoleCommand(result) - } - } - tabComplete?(sender: any, input: string, index?: number): string[] { - throw new Error("Method not implemented.") - } } diff --git a/packages/bungee/src/server.ts b/packages/bungee/src/server.ts index a11cd786..87acac73 100644 --- a/packages/bungee/src/server.ts +++ b/packages/bungee/src/server.ts @@ -3,29 +3,29 @@ 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(); +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; + private pluginsFolder: string + private pipeline: any + private rootLogger: any @inject(task.TaskManager) private task: task.TaskManager constructor() { - this.pluginsFolder = Bungee.getPluginsFolder().getCanonicalPath(); + this.pluginsFolder = Bungee.getPluginsFolder().getCanonicalPath() } @postConstruct() initialize() { - let count = 0; + 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(); + wait.cancel() } catch (ex) { count++ if (count > 50) { @@ -44,7 +44,7 @@ export class BungeeServer implements server.Server { } getPlayer(name: string) { - return Bungee.getPlayer(name); + return Bungee.getPlayer(name) } getVersion(): string { return Bungee.getVersion() @@ -56,7 +56,7 @@ export class BungeeServer implements server.Server { return Bungee.getConsole() } getService(service: string) { - throw new Error("Method not implemented."); + throw new Error("Method not implemented.") } dispatchCommand(sender: string | any, command: string): boolean { if (typeof sender === 'string') { @@ -68,18 +68,15 @@ export class BungeeServer implements server.Server { return Bungee.getPluginManager().dispatchCommand(Bungee.getConsole(), command) } getPluginsFolder(): string { - return this.pluginsFolder; + return this.pluginsFolder } getNativePluginManager() { return Bungee.getPluginManager() as any } getNettyPipeline() { - return this.pipeline; + return this.pipeline } getRootLogger() { - return this.rootLogger; - } - sendJson(sender: string | any, json: string): void { - throw new Error("Method not implemented."); + return this.rootLogger } } diff --git a/packages/sponge/src/chat.ts b/packages/sponge/src/chat.ts new file mode 100644 index 00000000..e4c5d7e4 --- /dev/null +++ b/packages/sponge/src/chat.ts @@ -0,0 +1,22 @@ +import { chat } from '@ccms/api' +import { provideSingleton } from '@ccms/container' + +const Sponge = org.spongepowered.api.Sponge +const ChatTypes = org.spongepowered.api.text.chat.ChatTypes +const TextSerializers = org.spongepowered.api.text.serializer.TextSerializers + +@provideSingleton(chat.Chat) +export class SpongeChat extends chat.Chat { + sendJson(sender: any, json: string | object) { + sender.sendMessage(TextSerializers.JSON.deserialize(typeof json === "string" ? json : JSON.stringify(json))) + } + sendMessage(sender: any, message: string) { + sender.sendMessage(TextSerializers.FORMATTING_CODE.deserialize(message)) + } + sendActionBar(sender: any, message: string) { + sender.sendMessage(ChatTypes.ACTION_BAR, TextSerializers.FORMATTING_CODE.deserialize(message)) + } + sendTitle(sender: any, title: string, subtitle: string = '', fadeIn: number = 20, time: number = 100, fadeOut: number = 20) { + sender.sendTitle(title, subtitle, fadeIn, time, fadeOut) + } +} diff --git a/packages/spring/src/command.ts b/packages/spring/src/command.ts index c565faf7..34812956 100644 --- a/packages/spring/src/command.ts +++ b/packages/spring/src/command.ts @@ -13,6 +13,9 @@ export class SpringCommand extends command.Command { protected create(plugin: any, command: string) { return this.commandMap.register(plugin, command) } + public tabComplete(sender: any, input: string, index?: number): string[] { + return this.commandMap.tabComplate(sender, input, index) + } protected remove(plugin: any, command: string) { this.commandMap.unregister(plugin, command) } diff --git a/packages/spring/src/server.ts b/packages/spring/src/server.ts index 1560ad06..1c903cba 100644 --- a/packages/spring/src/server.ts +++ b/packages/spring/src/server.ts @@ -1,6 +1,6 @@ import { server } from '@ccms/api' import { provideSingleton, inject } from '@ccms/container' -import { NativePluginManager } from '@ccms/api/dist/interfaces/server/native_plugin' +import { NativePluginManager } from '@ccms/api' import { CommandMap } from './internal/command' @provideSingleton(server.Server) @@ -8,8 +8,6 @@ export class SpringServer implements server.Server { @inject(CommandMap) private commandMap: CommandMap - constructor() { - } getVersion(): string { return "SpringFramework" } @@ -47,10 +45,4 @@ export class SpringServer implements server.Server { getRootLogger() { return Packages.org.slf4j.LoggerFactory.getLogger("root") || global.logger } - sendJson(sender: any, json: string | object): void { - throw new Error("Method not implemented.") - } - tabComplete(sender: any, input: string, index?: number) { - return this.commandMap.tabComplate(sender, input, index) - } }