2020-01-15 08:43:00 +00:00
|
|
|
import { command, plugin } from "@ms/api";
|
|
|
|
import { inject, injectable } from "@ms/container";
|
|
|
|
import * as ref from '@ms/common/dist/reflect'
|
|
|
|
|
2020-02-09 14:07:00 +00:00
|
|
|
const Arrays = Java.type('java.util.Arrays')
|
|
|
|
const Command = Java.extend(Java.type('net.md_5.bungee.api.plugin.Command'), Java.type('net.md_5.bungee.api.plugin.TabExecutor'));
|
2020-01-15 08:43:00 +00:00
|
|
|
const createCommand = eval(`
|
2020-02-09 14:07:00 +00:00
|
|
|
function(cls, name, exec, tab){
|
|
|
|
return new cls(name) {
|
|
|
|
execute: exec,
|
|
|
|
onTabComplete: tab
|
|
|
|
}
|
2020-01-15 08:43:00 +00:00
|
|
|
}
|
|
|
|
`)
|
|
|
|
|
|
|
|
class SimpleCommand {
|
|
|
|
public callable: any;
|
|
|
|
private name: string;
|
|
|
|
private executor: Function;
|
2020-02-09 14:07:00 +00:00
|
|
|
private tabComplete: Function = () => [];
|
2020-01-15 08:43:00 +00:00
|
|
|
|
|
|
|
constructor(command: string) {
|
|
|
|
this.name = command;
|
2020-02-09 14:07:00 +00:00
|
|
|
this.callable = createCommand(Command, command,
|
|
|
|
(sender, args) => this.executor(sender, '', command, args),
|
|
|
|
(sender, args) => Arrays.asList(this.tabComplete(sender, '', command, args))
|
|
|
|
);
|
2020-01-15 08:43:00 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
setExecutor = (executor: Function) => this.executor = executor;
|
2020-02-09 14:07:00 +00:00
|
|
|
setTabComplete = (tabComplete: Function) => this.tabComplete = tabComplete;
|
2020-01-15 08:43:00 +00:00
|
|
|
toString = () => `Bungee SimpleCommand(${this.name})`
|
|
|
|
}
|
|
|
|
|
|
|
|
@injectable()
|
|
|
|
export class BungeeCommand extends command.Command {
|
|
|
|
@inject(plugin.PluginInstance)
|
|
|
|
private pluginInstance: any;
|
|
|
|
private pluginManager: net.md_5.bungee.api.plugin.PluginManager = base.getInstance().getProxy().getPluginManager();
|
|
|
|
private commandMapping: any[] = [];
|
|
|
|
|
|
|
|
create(plugin: any, command: string) {
|
|
|
|
let commandKey = this.getCommandKey(plugin, command);
|
|
|
|
let commandCallable = new SimpleCommand(command);
|
|
|
|
this.pluginManager.registerCommand(this.pluginInstance, commandCallable.callable)
|
|
|
|
this.commandMapping[commandKey] = commandCallable.callable;
|
|
|
|
return commandCallable;
|
|
|
|
}
|
|
|
|
remove(plugin: any, command: string) {
|
|
|
|
var commandKey = this.getCommandKey(plugin, command);
|
|
|
|
if (this.commandMapping[commandKey]) {
|
|
|
|
this.pluginManager.unregisterCommand(this.commandMapping[commandKey])
|
|
|
|
delete this.commandMapping[commandKey];
|
|
|
|
}
|
|
|
|
}
|
|
|
|
onCommand(plugin: any, command: any, executor: Function) {
|
|
|
|
command.setExecutor(super.setExecutor(plugin, command, executor));
|
|
|
|
}
|
|
|
|
onTabComplete(plugin: any, command: any, tabCompleter: Function) {
|
2020-02-09 14:07:00 +00:00
|
|
|
command.setTabComplete(super.setTabCompleter(plugin, command, tabCompleter))
|
2020-01-15 08:43:00 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
private getCommandKey(plugin: any, command: string) {
|
|
|
|
return plugin.description.name.toLowerCase() + ":" + command;
|
|
|
|
}
|
|
|
|
}
|