2019-09-21 07:04:47 +00:00
|
|
|
import { command, plugin } from "@ms/api";
|
|
|
|
import { inject, injectable } from "@ms/container";
|
|
|
|
|
|
|
|
let Sponge = Java.type('org.spongepowered.api.Sponge');
|
|
|
|
|
|
|
|
var CommandCallable = Java.type('org.spongepowered.api.command.CommandCallable');
|
|
|
|
var CommandResult = Java.type('org.spongepowered.api.command.CommandResult');
|
|
|
|
|
|
|
|
let Text = Java.type('org.spongepowered.api.text.Text');
|
|
|
|
|
|
|
|
var Optional = Java.type('java.util.Optional');
|
|
|
|
|
|
|
|
class SimpleCommandCallable {
|
|
|
|
public callable: any;
|
2020-01-15 08:48:18 +00:00
|
|
|
private name: string;
|
2019-09-21 07:04:47 +00:00
|
|
|
private executor: Function;
|
|
|
|
private tabCompleter: Function;
|
|
|
|
|
|
|
|
constructor(command: string, description: string = '暂无描述!') {
|
2020-01-15 08:48:18 +00:00
|
|
|
this.name = command;
|
2019-09-21 07:04:47 +00:00
|
|
|
this.callable = new CommandCallable({
|
|
|
|
//CommandResult process(CommandSource source, String arguments) throws CommandException;
|
|
|
|
process: (sender: any, args) => {
|
2019-11-04 12:21:31 +00:00
|
|
|
return this.executor(sender, '', command, Java.to(args.split(" ").filter(e => e))) ? CommandResult.success() : CommandResult.empty();
|
2019-09-21 07:04:47 +00:00
|
|
|
},
|
|
|
|
//List<String> getSuggestions(CommandSource source, String arguments, @Nullable Location<World> targetPosition) throws CommandException;
|
|
|
|
getSuggestions: (sender: any, args, target) => {
|
2019-11-04 12:21:31 +00:00
|
|
|
return this.tabCompleter(sender, '', command, Java.to(args.split(" ").filter(e => e)));
|
2019-09-21 07:04:47 +00:00
|
|
|
},
|
|
|
|
//boolean testPermission(CommandSource source);
|
|
|
|
testPermission: () => {
|
|
|
|
return true;
|
|
|
|
},
|
|
|
|
//Optional<Text> getShortDescription(CommandSource source);
|
|
|
|
getShortDescription: () => {
|
|
|
|
return Optional.of(Text.of(description));
|
|
|
|
},
|
|
|
|
//Optional<Text> getHelp(CommandSource source);
|
|
|
|
getHelp: () => {
|
|
|
|
return Optional.of(Text.of(""));
|
|
|
|
},
|
|
|
|
//Text getUsage(CommandSource source);
|
|
|
|
getUsage: () => {
|
|
|
|
return Text.of('');
|
|
|
|
}
|
|
|
|
})
|
|
|
|
}
|
|
|
|
|
|
|
|
setExecutor = (executor: Function) => this.executor = executor;
|
|
|
|
setTabCompleter = (tabCompleter: Function) => this.tabCompleter = tabCompleter;
|
2020-01-15 08:48:18 +00:00
|
|
|
toString = () => `Sponge SimpleCommandCallable(${this.name})`
|
2019-09-21 07:04:47 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
@injectable()
|
|
|
|
export class SpongeCommand extends command.Command {
|
|
|
|
@inject(plugin.PluginInstance)
|
|
|
|
private pluginInstance: any
|
2019-09-22 10:00:18 +00:00
|
|
|
private commandMapping: any[] = [];
|
2019-09-21 07:04:47 +00:00
|
|
|
|
|
|
|
create(plugin: any, command: string) {
|
2019-11-04 12:21:31 +00:00
|
|
|
let commandKey = this.getCommandKey(plugin, command);
|
|
|
|
let commandCallable = new SimpleCommandCallable(command);
|
|
|
|
this.commandMapping[commandKey] = Sponge.getCommandManager().register(this.pluginInstance, commandCallable.callable, command, commandKey).orElse(null);
|
|
|
|
return commandCallable;
|
2019-09-21 07:04:47 +00:00
|
|
|
}
|
2019-09-22 10:00:18 +00:00
|
|
|
remove(plugin: any, command: string) {
|
|
|
|
var commandKey = this.getCommandKey(plugin, command);
|
|
|
|
if (this.commandMapping[commandKey]) {
|
|
|
|
Sponge.getCommandManager().removeMapping(this.commandMapping[commandKey]);
|
2019-11-04 12:21:31 +00:00
|
|
|
delete this.commandMapping[commandKey];
|
2019-09-22 10:00:18 +00:00
|
|
|
}
|
|
|
|
}
|
2019-09-21 07:04:47 +00:00
|
|
|
onCommand(plugin: any, command: any, executor: Function) {
|
|
|
|
command.setExecutor(super.setExecutor(plugin, command, executor));
|
|
|
|
}
|
|
|
|
onTabComplete(plugin: any, command: any, tabCompleter: Function) {
|
|
|
|
command.setTabCompleter(super.setTabCompleter(plugin, command, tabCompleter));
|
|
|
|
}
|
2019-09-22 10:00:18 +00:00
|
|
|
|
|
|
|
private getCommandKey(plugin: any, command: string) {
|
|
|
|
return plugin.description.name.toLowerCase() + ":" + command;
|
|
|
|
}
|
|
|
|
}
|