2020-05-02 16:14:47 +00:00
|
|
|
import i18n from '@ccms/i18n'
|
2020-05-30 02:43:33 +00:00
|
|
|
import { injectable } from "@ccms/container"
|
|
|
|
import { plugin } from './interfaces'
|
2019-09-10 09:21:00 +00:00
|
|
|
|
2019-09-07 04:23:15 +00:00
|
|
|
export namespace command {
|
2019-09-10 09:21:00 +00:00
|
|
|
@injectable()
|
2019-09-07 04:23:15 +00:00
|
|
|
export abstract class Command {
|
2020-03-24 09:59:18 +00:00
|
|
|
/**
|
|
|
|
* 注册插件命令
|
|
|
|
* @param plugin 插件
|
|
|
|
* @param name 命令
|
|
|
|
* @param exec 执行器
|
|
|
|
*/
|
2020-05-30 02:43:33 +00:00
|
|
|
on(plugin: plugin.Plugin, name: string, exec: { cmd: Function, tab?: Function }) {
|
|
|
|
var cmd = this.create(plugin, name)
|
|
|
|
if (!cmd) { throw Error("") }
|
2020-02-27 04:11:18 +00:00
|
|
|
console.debug(i18n.translate("ms.api.command.register", { plugin: plugin.description.name, name, cmd }))
|
2019-09-07 04:23:15 +00:00
|
|
|
if (exec.cmd && typeof exec.cmd === "function") {
|
2020-05-30 02:43:33 +00:00
|
|
|
this.onCommand(plugin, cmd, exec.cmd)
|
2019-09-07 04:23:15 +00:00
|
|
|
} else {
|
2020-02-27 04:11:18 +00:00
|
|
|
throw Error(i18n.translate("ms.api.command.register.input.error", { exec: exec.cmd }))
|
2019-09-07 04:23:15 +00:00
|
|
|
}
|
|
|
|
if (exec.tab && typeof exec.tab === "function") {
|
2020-05-30 02:43:33 +00:00
|
|
|
this.onTabComplete(plugin, cmd, exec.tab)
|
2019-09-07 04:23:15 +00:00
|
|
|
}
|
2019-09-19 10:59:00 +00:00
|
|
|
}
|
2020-03-24 09:59:18 +00:00
|
|
|
/**
|
|
|
|
* 取消命令注册
|
|
|
|
* @param plugin 插件
|
|
|
|
* @param name 命令
|
|
|
|
*/
|
2020-05-30 02:43:33 +00:00
|
|
|
off(plugin: plugin.Plugin, name: string) {
|
2020-02-27 04:11:18 +00:00
|
|
|
console.debug(i18n.translate("ms.api.command.unregister", { plugin: plugin.description.name, name }))
|
2020-05-30 02:43:33 +00:00
|
|
|
this.remove(plugin, name)
|
2019-09-07 04:23:15 +00:00
|
|
|
}
|
2020-03-24 09:59:18 +00:00
|
|
|
|
2020-05-30 02:43:33 +00:00
|
|
|
protected abstract create(plugin: plugin.Plugin, command: string): any
|
|
|
|
protected abstract remove(plugin: plugin.Plugin, command: string): void
|
|
|
|
protected abstract onCommand(plugin: plugin.Plugin, command: any, executor: Function)
|
|
|
|
protected abstract onTabComplete(plugin: plugin.Plugin, command: any, tabCompleter: Function)
|
|
|
|
protected setExecutor(plugin: plugin.Plugin, command: any, executor: Function) {
|
2020-01-15 08:48:18 +00:00
|
|
|
return (sender: any, _: any, command: string, args: string[]) => {
|
2019-09-21 06:58:00 +00:00
|
|
|
try {
|
2020-05-30 02:43:33 +00:00
|
|
|
return executor(sender, command, Java.from(args))
|
2019-09-21 06:58:00 +00:00
|
|
|
} catch (ex) {
|
2020-04-03 05:32:54 +00:00
|
|
|
console.i18n("ms.api.command.execute.error", { player: sender.name, plugin: plugin.description.name, command, args: Java.from(args).join(' '), ex })
|
2020-05-30 02:43:33 +00:00
|
|
|
console.ex(ex)
|
2020-04-03 05:32:54 +00:00
|
|
|
if (sender.name != 'CONSOLE') {
|
|
|
|
console.sender(sender, [i18n.translate("ms.api.command.execute.error", { player: sender.name, plugin: plugin.description.name, command, args: Java.from(args).join(' '), ex }), ...console.stack(ex)])
|
|
|
|
}
|
2020-05-30 02:43:33 +00:00
|
|
|
return true
|
2019-09-21 06:58:00 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2020-05-30 02:43:33 +00:00
|
|
|
protected setTabCompleter(plugin: plugin.Plugin, command: any, tabCompleter: Function) {
|
2020-01-15 08:48:18 +00:00
|
|
|
return (sender: any, _: any, command: string, args: string[]) => {
|
2019-09-21 06:58:00 +00:00
|
|
|
try {
|
2020-05-30 02:43:33 +00:00
|
|
|
var token = args[args.length - 1]
|
|
|
|
var complete = tabCompleter(sender, command, Java.from(args)) || []
|
|
|
|
return this.copyPartialMatches(complete, token)
|
2019-09-21 06:58:00 +00:00
|
|
|
} catch (ex) {
|
2020-02-28 16:44:11 +00:00
|
|
|
console.i18n("ms.api.command.tab.completer.error", { sender: sender.name, plugin: plugin.description.name, command, args: Java.from(args).join(' '), ex })
|
2020-05-30 02:43:33 +00:00
|
|
|
console.ex(ex)
|
|
|
|
console.sender(sender, [i18n.translate("ms.api.command.tab.completer.error", { sender: sender.name, plugin: plugin.description.name, command, args: Java.from(args).join(' '), ex }), ...console.stack(ex)])
|
|
|
|
return []
|
2019-09-21 06:58:00 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
protected copyPartialMatches(complete: string[], token: string, array: string[] = []): string[] {
|
|
|
|
if (!token) { return complete }
|
2020-03-24 09:59:18 +00:00
|
|
|
complete.forEach(function (e) {
|
2019-09-21 06:58:00 +00:00
|
|
|
if (typeof e === "string" && e.toLowerCase().startsWith(token.toLowerCase())) {
|
|
|
|
array.push(e)
|
|
|
|
}
|
2020-05-30 02:43:33 +00:00
|
|
|
})
|
2019-09-21 06:58:00 +00:00
|
|
|
return array
|
|
|
|
}
|
2019-09-07 04:23:15 +00:00
|
|
|
}
|
|
|
|
}
|