2019-09-10 09:21:00 +00:00
|
|
|
import { injectable } from "@ms/container";
|
|
|
|
|
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 {
|
2019-09-10 09:21:00 +00:00
|
|
|
on(plugin: any, name: string, exec: { cmd: Function, tab?: Function }) {
|
2019-09-21 06:58:00 +00:00
|
|
|
var cmd = this.create(plugin, name);
|
2020-01-15 08:48:18 +00:00
|
|
|
console.debug(`[${plugin.description.name}] register command ${name}(${cmd})...`)
|
2019-09-07 04:23:15 +00:00
|
|
|
if (exec.cmd && typeof exec.cmd === "function") {
|
2019-09-21 06:58:00 +00:00
|
|
|
this.onCommand(plugin, cmd, exec.cmd);
|
2019-09-07 04:23:15 +00:00
|
|
|
} else {
|
|
|
|
throw Error("CommandExec Must be a function... Input: " + exec.cmd)
|
|
|
|
}
|
|
|
|
if (exec.tab && typeof exec.tab === "function") {
|
2019-09-21 06:58:00 +00:00
|
|
|
this.onTabComplete(plugin, cmd, exec.tab);
|
2019-09-07 04:23:15 +00:00
|
|
|
}
|
2019-09-19 10:59:00 +00:00
|
|
|
}
|
|
|
|
off(plugin: any, name: string) {
|
2020-01-15 08:48:18 +00:00
|
|
|
console.debug(`[${plugin.description.name}] unregister command ${name}...`)
|
2019-09-22 10:00:18 +00:00
|
|
|
this.remove(plugin, name);
|
2019-09-07 04:23:15 +00:00
|
|
|
}
|
|
|
|
/**
|
|
|
|
* Create Server Command Object
|
|
|
|
*/
|
2019-09-22 10:00:18 +00:00
|
|
|
protected abstract create(plugin: any, command: string);
|
|
|
|
protected abstract remove(plugin: any, command: string);
|
|
|
|
protected abstract onCommand(plugin: any, command: any, executor: Function);
|
|
|
|
protected abstract onTabComplete(plugin: any, command: any, tabCompleter: Function);
|
2019-09-21 06:58:00 +00:00
|
|
|
|
2019-09-22 10:00:18 +00:00
|
|
|
protected setExecutor(plugin: any, 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 {
|
|
|
|
return executor(sender, command, Java.from(args));
|
|
|
|
} catch (ex) {
|
|
|
|
console.console(`§6玩家 §a${sender.name} §6执行 §b${plugin.description.name} §6插件 §d${command} ${Java.from(args).join(' ')} §6命令时发生异常 §4${ex}`);
|
|
|
|
console.ex(ex);
|
|
|
|
console.sender(sender, [`§6执行 §b${plugin.description.name} §6插件 §d${command} ${Java.from(args).join(' ')} §6命令时发生异常`, ...console.stack(ex)])
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-09-22 10:00:18 +00:00
|
|
|
protected setTabCompleter(plugin: any, 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 {
|
|
|
|
var token = args[args.length - 1];
|
|
|
|
var complete = tabCompleter(sender, command, Java.from(args)) || [];
|
|
|
|
return this.copyPartialMatches(complete, token);
|
|
|
|
} catch (ex) {
|
|
|
|
console.console(`§6玩家 §a${sender.name} §6执行 §b${plugin.description.name} §6插件 §d${command} ${Java.from(args).join(' ')} §6补全时发生异常 §4${ex}`);
|
|
|
|
console.ex(ex);
|
|
|
|
console.sender(sender, [`§6执行 §b${plugin.description.name} §6插件 §d${command} ${Java.from(args).join(' ')} §6补全时发生异常 §4${ex}`, ...console.stack(ex)]);
|
|
|
|
return [];
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
protected copyPartialMatches(complete: string[], token: string, array: string[] = []): string[] {
|
|
|
|
if (!token) { return complete }
|
|
|
|
complete.forEach(function(e) {
|
|
|
|
if (typeof e === "string" && e.toLowerCase().startsWith(token.toLowerCase())) {
|
|
|
|
array.push(e)
|
|
|
|
}
|
|
|
|
});
|
|
|
|
return array
|
|
|
|
}
|
2019-09-07 04:23:15 +00:00
|
|
|
}
|
|
|
|
}
|