refactor: chat & command tabComplete

Signed-off-by: MiaoWoo <admin@yumc.pw>
This commit is contained in:
MiaoWoo 2020-09-22 15:29:12 +08:00
parent 68a996f830
commit af85703bd9
10 changed files with 85 additions and 75 deletions

View File

@ -3,6 +3,15 @@ import { injectable } from '@ccms/container'
export namespace chat { export namespace chat {
@injectable() @injectable()
export abstract class Chat { 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) { sendMessage(sender: any, message: string) {
throw new Error("Method not implemented.") throw new Error("Method not implemented.")
} }

View File

@ -24,6 +24,9 @@ export namespace command {
this.onTabComplete(plugin, cmd, exec.tab) this.onTabComplete(plugin, cmd, exec.tab)
} }
} }
public tabComplete(sender: any, input: string, index?: number): string[] {
throw new Error("Method not implemented.")
}
/** /**
* *
* @param plugin * @param plugin

View File

@ -4,6 +4,8 @@ import { injectable, inject } from '@ccms/container'
import { NativePluginManager } from './native_plugin' import { NativePluginManager } from './native_plugin'
import { constants } from '../../constants' import { constants } from '../../constants'
export { NativePluginManager } from './native_plugin'
export namespace server { export namespace server {
/** /**
* Runtime ServerType * Runtime ServerType
@ -13,10 +15,6 @@ export namespace server {
* Runtime Console * Runtime Console
*/ */
export const Console = Symbol("Console") export const Console = Symbol("Console")
/**
* MiaoScript Server
*/
export const Server = Symbol("Server")
/** /**
* Runtime Server Instance * Runtime Server Instance
*/ */
@ -24,21 +22,19 @@ export namespace server {
/** /**
* MiaoScript Server * MiaoScript Server
*/ */
export interface Server { export abstract class Server {
getVersion(): string abstract getVersion(): string
getPlayer(name: string): any abstract getPlayer(name: string): any
getOnlinePlayers(): any[] abstract getOnlinePlayers(): any[]
getConsoleSender(): any abstract getConsoleSender(): any
getService(service: string): any abstract getService(service: string): any
dispatchCommand(sender: string | any, command: string): boolean abstract dispatchCommand(sender: string | any, command: string): boolean
dispatchConsoleCommand(command: string): boolean abstract dispatchConsoleCommand(command: string): boolean
getPluginsFolder(): string abstract getPluginsFolder(): string
getNativePluginManager(): NativePluginManager abstract getNativePluginManager(): NativePluginManager
getDedicatedServer?(): any abstract getDedicatedServer?(): any
getNettyPipeline(): any abstract getNettyPipeline(): any
getRootLogger(): any abstract getRootLogger(): any
sendJson(sender: string | any, json: object | string): void
tabComplete?(sender: string | any, input: string, index?: number): string[]
} }
@injectable() @injectable()
export class ServerChecker { export class ServerChecker {
@ -102,12 +98,6 @@ export namespace server {
getRootLogger() { getRootLogger() {
throw new Error("Method not implemented.") 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() { protected reflect() {
try { try {
let consoleServer = this.getDedicatedServer() let consoleServer = this.getDedicatedServer()

View File

@ -4,11 +4,14 @@ import bukkitChat from './enhance/chat'
@provideSingleton(chat.Chat) @provideSingleton(chat.Chat)
export class BukkitChat extends 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) { sendMessage(sender: any, message: string) {
bukkitChat.send(sender, JSON.stringify({ text: message }), 0) this.sendJson(sender, { text: message }, 0)
} }
sendActionBar(sender: any, message: string) { 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) { sendTitle(sender: any, title: string, subtitle: string = '', fadeIn: number = 20, time: number = 100, fadeOut: number = 20) {
sender.sendTitle(title, subtitle, fadeIn, time, fadeOut) sender.sendTitle(title, subtitle, fadeIn, time, fadeOut)

View File

@ -4,44 +4,47 @@ import { command, plugin } from '@ccms/api'
import * as reflect from '@ccms/common/dist/reflect' import * as reflect from '@ccms/common/dist/reflect'
import { provideSingleton, postConstruct, inject } from '@ccms/container' import { provideSingleton, postConstruct, inject } from '@ccms/container'
let Bukkit = org.bukkit.Bukkit; let Bukkit = org.bukkit.Bukkit
let TabCompleter = Java.type('org.bukkit.command.TabCompleter'); let TabCompleter = Java.type('org.bukkit.command.TabCompleter')
let PluginCommand = Java.type('org.bukkit.command.PluginCommand'); let PluginCommand = Java.type('org.bukkit.command.PluginCommand')
let CommandExecutor = Java.type('org.bukkit.command.CommandExecutor'); let CommandExecutor = Java.type('org.bukkit.command.CommandExecutor')
@provideSingleton(command.Command) @provideSingleton(command.Command)
export class BukkitCommand extends command.Command { export class BukkitCommand extends command.Command {
@inject(plugin.PluginInstance) @inject(plugin.PluginInstance)
private pluginInstance: any private pluginInstance: any
private commandMap: any; private commandMap: any
@postConstruct() @postConstruct()
init() { init() {
this.commandMap = reflect.on(Bukkit.getPluginManager()).get('commandMap').get(); this.commandMap = reflect.on(Bukkit.getPluginManager()).get('commandMap').get()
} }
create(plugin: any, command: string) { create(plugin: any, command: string) {
var cmd = this.commandMap.getCommand(command) var cmd = this.commandMap.getCommand(command)
if (cmd && cmd instanceof PluginCommand) { return cmd }; if (cmd && cmd instanceof PluginCommand) { return cmd };
cmd = reflect.on(PluginCommand).create(command, this.pluginInstance).get(); cmd = reflect.on(PluginCommand).create(command, this.pluginInstance).get()
this.commandMap.register(plugin.description.name, cmd); this.commandMap.register(plugin.description.name, cmd)
return cmd; return cmd
} }
remove(plugin: any, command: string) { remove(plugin: any, command: string) {
var cmd = this.commandMap.getCommand(command) var cmd = this.commandMap.getCommand(command)
if (cmd && cmd instanceof PluginCommand) { 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) { onCommand(plugin: any, command: any, executor: Function) {
// 必须指定需要实现的接口类型 否则MOD服会报错 // 必须指定需要实现的接口类型 否则MOD服会报错
command.setExecutor(new CommandExecutor({ command.setExecutor(new CommandExecutor({
onCommand: super.setExecutor(plugin, command, executor) onCommand: super.setExecutor(plugin, command, executor)
})); }))
} }
onTabComplete(plugin: any, command: any, tabCompleter: Function) { onTabComplete(plugin: any, command: any, tabCompleter: Function) {
// 必须指定需要实现的接口类型 否则MOD服会报错 // 必须指定需要实现的接口类型 否则MOD服会报错
command.setTabCompleter(new TabCompleter({ command.setTabCompleter(new TabCompleter({
onTabComplete: super.setTabCompleter(plugin, command, tabCompleter) onTabComplete: super.setTabCompleter(plugin, command, tabCompleter)
})); }))
} }
} }

View File

@ -54,16 +54,4 @@ export class BukkitServer extends server.ReflectServer {
getRootLogger() { getRootLogger() {
return this.rootLogger 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.")
}
} }

View File

@ -3,29 +3,29 @@ import { provideSingleton, inject, postConstruct } from '@ccms/container'
import * as reflect from '@ccms/common/dist/reflect' 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) @provideSingleton(server.Server)
export class BungeeServer implements server.Server { export class BungeeServer implements server.Server {
private pluginsFolder: string; private pluginsFolder: string
private pipeline: any; private pipeline: any
private rootLogger: any; private rootLogger: any
@inject(task.TaskManager) @inject(task.TaskManager)
private task: task.TaskManager private task: task.TaskManager
constructor() { constructor() {
this.pluginsFolder = Bungee.getPluginsFolder().getCanonicalPath(); this.pluginsFolder = Bungee.getPluginsFolder().getCanonicalPath()
} }
@postConstruct() @postConstruct()
initialize() { initialize() {
let count = 0; let count = 0
let wait = this.task.create(() => { let wait = this.task.create(() => {
try { try {
// @ts-ignore // @ts-ignore
this.pipeline = reflect.on(base.getInstance().getProxy()).get('listeners').get().toArray()[0].pipeline() this.pipeline = reflect.on(base.getInstance().getProxy()).get('listeners').get().toArray()[0].pipeline()
wait.cancel(); wait.cancel()
} catch (ex) { } catch (ex) {
count++ count++
if (count > 50) { if (count > 50) {
@ -44,7 +44,7 @@ export class BungeeServer implements server.Server {
} }
getPlayer(name: string) { getPlayer(name: string) {
return Bungee.getPlayer(name); return Bungee.getPlayer(name)
} }
getVersion(): string { getVersion(): string {
return Bungee.getVersion() return Bungee.getVersion()
@ -56,7 +56,7 @@ export class BungeeServer implements server.Server {
return Bungee.getConsole() return Bungee.getConsole()
} }
getService(service: string) { getService(service: string) {
throw new Error("Method not implemented."); throw new Error("Method not implemented.")
} }
dispatchCommand(sender: string | any, command: string): boolean { dispatchCommand(sender: string | any, command: string): boolean {
if (typeof sender === 'string') { if (typeof sender === 'string') {
@ -68,18 +68,15 @@ export class BungeeServer implements server.Server {
return Bungee.getPluginManager().dispatchCommand(Bungee.getConsole(), command) return Bungee.getPluginManager().dispatchCommand(Bungee.getConsole(), command)
} }
getPluginsFolder(): string { getPluginsFolder(): string {
return this.pluginsFolder; return this.pluginsFolder
} }
getNativePluginManager() { getNativePluginManager() {
return Bungee.getPluginManager() as any return Bungee.getPluginManager() as any
} }
getNettyPipeline() { getNettyPipeline() {
return this.pipeline; return this.pipeline
} }
getRootLogger() { getRootLogger() {
return this.rootLogger; return this.rootLogger
}
sendJson(sender: string | any, json: string): void {
throw new Error("Method not implemented.");
} }
} }

View File

@ -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)
}
}

View File

@ -13,6 +13,9 @@ export class SpringCommand extends command.Command {
protected create(plugin: any, command: string) { protected create(plugin: any, command: string) {
return this.commandMap.register(plugin, command) 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) { protected remove(plugin: any, command: string) {
this.commandMap.unregister(plugin, command) this.commandMap.unregister(plugin, command)
} }

View File

@ -1,6 +1,6 @@
import { server } from '@ccms/api' import { server } from '@ccms/api'
import { provideSingleton, inject } from '@ccms/container' 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' import { CommandMap } from './internal/command'
@provideSingleton(server.Server) @provideSingleton(server.Server)
@ -8,8 +8,6 @@ export class SpringServer implements server.Server {
@inject(CommandMap) @inject(CommandMap)
private commandMap: CommandMap private commandMap: CommandMap
constructor() {
}
getVersion(): string { getVersion(): string {
return "SpringFramework" return "SpringFramework"
} }
@ -47,10 +45,4 @@ export class SpringServer implements server.Server {
getRootLogger() { getRootLogger() {
return Packages.org.slf4j.LoggerFactory.getLogger("root") || global.logger 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)
}
} }