2020-06-02 09:50:47 +00:00
|
|
|
import { provideSingleton } from "@ccms/container"
|
|
|
|
import { plugin } from "@ccms/api"
|
|
|
|
|
|
|
|
type CommandExec = (sender: any, _: any, command: string, args: string[]) => boolean
|
|
|
|
type TabCompleter = (sender: any, _: any, command: string, args: string[]) => string[]
|
|
|
|
type CommandStore = { [key: string]: SpringCommand }
|
|
|
|
|
|
|
|
@provideSingleton(CommandMap)
|
|
|
|
export class CommandMap {
|
|
|
|
private commands: CommandStore = {}
|
|
|
|
private pluginCommands: { [key: string]: CommandStore } = {}
|
|
|
|
|
|
|
|
register(plugin: plugin.Plugin, command: string) {
|
|
|
|
let springCommand = new SpringCommand(plugin, command)
|
|
|
|
this.commands[command] = springCommand
|
|
|
|
if (!this.pluginCommands[plugin.description.name]) { this.pluginCommands[plugin.description.name] = {} }
|
|
|
|
this.pluginCommands[plugin.description.name][command] = springCommand
|
|
|
|
return springCommand
|
|
|
|
}
|
|
|
|
|
|
|
|
unregister(plugin: plugin.Plugin, command: string) {
|
|
|
|
delete this.commands[command]
|
|
|
|
delete this.pluginCommands[plugin.description.name][command]
|
|
|
|
}
|
|
|
|
|
|
|
|
dispatch(sender: any, command: string, args: string[]): boolean {
|
|
|
|
if (command === "help") {
|
|
|
|
sender.sendMessage('§e--------- §rHelp: Index §e---------------------------')
|
|
|
|
sender.sendMessage('Use /help [n] to get page n of help.')
|
|
|
|
for (const cmdName of Object.getOwnPropertyNames(this.commands)) {
|
|
|
|
sender.sendMessage(`§6/${cmdName}: §rA command provided by plugin §b${this.commands[cmdName].plugin.description.name}§r.`)
|
|
|
|
}
|
|
|
|
return
|
|
|
|
}
|
|
|
|
let exists = this.commands[command]
|
|
|
|
if (exists) {
|
2020-06-20 08:39:03 +00:00
|
|
|
try {
|
|
|
|
return exists.executor(sender, '', command, Java.to(args))
|
|
|
|
} catch (error) {
|
|
|
|
console.ex(error)
|
|
|
|
}
|
|
|
|
return true
|
2020-06-02 09:50:47 +00:00
|
|
|
} else {
|
|
|
|
sender.sendMessage && sender.sendMessage(`Unknown command. Type "/help" for help.`)
|
|
|
|
return false
|
|
|
|
}
|
|
|
|
}
|
2020-06-20 08:39:03 +00:00
|
|
|
|
|
|
|
tabComplate(sender: any, input: string, index?: number): string[] {
|
2020-07-01 06:33:16 +00:00
|
|
|
if (index == 0) { return Object.keys(this.commands).concat('help') }
|
2020-06-20 08:39:03 +00:00
|
|
|
let [command, ...args] = input.split(' ')
|
|
|
|
let exists = this.commands[command]
|
|
|
|
if (exists && exists.tabCompleter) {
|
|
|
|
try {
|
|
|
|
if (args.length !== index) { args.push('') }
|
|
|
|
return exists.tabCompleter(sender, '', command, Java.to(args))
|
|
|
|
} catch (error) {
|
|
|
|
console.ex(error)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return []
|
|
|
|
}
|
2020-06-02 09:50:47 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
export class SpringCommand {
|
|
|
|
public plugin: plugin.Plugin
|
|
|
|
public name: string
|
|
|
|
public executor: CommandExec
|
|
|
|
public tabCompleter: TabCompleter
|
|
|
|
|
|
|
|
constructor(plugin: plugin.Plugin, command: string, description: string = '暂无描述!') {
|
|
|
|
this.plugin = plugin
|
|
|
|
this.name = command
|
|
|
|
}
|
|
|
|
|
|
|
|
setExecutor = (executor: CommandExec) => this.executor = executor
|
|
|
|
setTabCompleter = (tabCompleter: TabCompleter) => this.tabCompleter = tabCompleter
|
|
|
|
toString = () => `SpringCommand(${this.name})`
|
|
|
|
}
|