2020-05-30 02:43:33 +00:00
|
|
|
import { command, plugin } from "@ccms/api"
|
|
|
|
import { inject, provideSingleton } from "@ccms/container"
|
2019-09-21 07:04:47 +00:00
|
|
|
|
2020-05-30 02:43:33 +00:00
|
|
|
let Sponge = Java.type('org.spongepowered.api.Sponge')
|
|
|
|
let Text = Java.type('org.spongepowered.api.text.Text')
|
|
|
|
var CommandCallable = Java.type('org.spongepowered.api.command.CommandCallable')
|
|
|
|
var CommandResult = Java.type('org.spongepowered.api.command.CommandResult')
|
|
|
|
var Optional = Java.type('java.util.Optional')
|
2019-09-21 07:04:47 +00:00
|
|
|
|
2020-02-26 02:15:33 +00:00
|
|
|
@provideSingleton(command.Command)
|
|
|
|
export class SpongeCommand extends command.Command {
|
|
|
|
@inject(plugin.PluginInstance)
|
|
|
|
private pluginInstance: any
|
2020-05-30 02:43:33 +00:00
|
|
|
private commandMapping: any[] = []
|
2019-09-21 07:04:47 +00:00
|
|
|
|
2020-02-26 02:15:33 +00:00
|
|
|
create(plugin: any, command: string) {
|
2020-05-30 02:43:33 +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
|
2020-02-26 02:15:33 +00:00
|
|
|
}
|
|
|
|
remove(plugin: any, command: string) {
|
2020-05-30 02:43:33 +00:00
|
|
|
var commandKey = this.getCommandKey(plugin, command)
|
2020-02-26 02:15:33 +00:00
|
|
|
if (this.commandMapping[commandKey]) {
|
2020-05-30 02:43:33 +00:00
|
|
|
Sponge.getCommandManager().removeMapping(this.commandMapping[commandKey])
|
|
|
|
delete this.commandMapping[commandKey]
|
2020-02-26 02:15:33 +00:00
|
|
|
}
|
|
|
|
}
|
2020-05-30 02:43:33 +00:00
|
|
|
onCommand(plugin: plugin.Plugin, command: any, executor: Function) {
|
|
|
|
command.setExecutor(super.setExecutor(plugin, command, executor))
|
2020-02-26 02:15:33 +00:00
|
|
|
}
|
2020-05-30 02:43:33 +00:00
|
|
|
onTabComplete(plugin: plugin.Plugin, command: any, tabCompleter: Function) {
|
|
|
|
command.setTabCompleter(super.setTabCompleter(plugin, command, tabCompleter))
|
2020-02-26 02:15:33 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
private getCommandKey(plugin: any, command: string) {
|
2020-05-30 02:43:33 +00:00
|
|
|
return plugin.description.name.toLowerCase() + ":" + command
|
2020-02-26 02:15:33 +00:00
|
|
|
}
|
|
|
|
}
|
2019-09-21 07:04:47 +00:00
|
|
|
|
|
|
|
class SimpleCommandCallable {
|
2020-05-30 02:43:33 +00:00
|
|
|
public callable: any
|
|
|
|
private name: string
|
|
|
|
private executor: Function
|
|
|
|
private tabCompleter: Function
|
2019-09-21 07:04:47 +00:00
|
|
|
|
|
|
|
constructor(command: string, description: string = '暂无描述!') {
|
2020-05-30 02:43:33 +00:00
|
|
|
this.name = command
|
2019-09-21 07:04:47 +00:00
|
|
|
this.callable = new CommandCallable({
|
2020-05-30 02:43:33 +00:00
|
|
|
//CommandResult process(CommandSource source, String arguments) throws CommandException
|
2019-09-21 07:04:47 +00:00
|
|
|
process: (sender: any, args) => {
|
2020-09-24 02:33:16 +00:00
|
|
|
return this.executor(sender, '', command, Java.to(args.split(" "))) ? CommandResult.success() : CommandResult.empty()
|
2019-09-21 07:04:47 +00:00
|
|
|
},
|
2020-05-30 02:43:33 +00:00
|
|
|
//List<String> getSuggestions(CommandSource source, String arguments, @Nullable Location<World> targetPosition) throws CommandException
|
2019-09-21 07:04:47 +00:00
|
|
|
getSuggestions: (sender: any, args, target) => {
|
2020-09-24 02:33:16 +00:00
|
|
|
return this.tabCompleter(sender, '', command, Java.to(args.split(" ")))
|
2019-09-21 07:04:47 +00:00
|
|
|
},
|
2020-05-30 02:43:33 +00:00
|
|
|
//boolean testPermission(CommandSource source)
|
2019-09-21 07:04:47 +00:00
|
|
|
testPermission: () => {
|
2020-05-30 02:43:33 +00:00
|
|
|
return true
|
2019-09-21 07:04:47 +00:00
|
|
|
},
|
2020-05-30 02:43:33 +00:00
|
|
|
//Optional<Text> getShortDescription(CommandSource source)
|
2019-09-21 07:04:47 +00:00
|
|
|
getShortDescription: () => {
|
2020-05-30 02:43:33 +00:00
|
|
|
return Optional.of(Text.of(description))
|
2019-09-21 07:04:47 +00:00
|
|
|
},
|
2020-05-30 02:43:33 +00:00
|
|
|
//Optional<Text> getHelp(CommandSource source)
|
2019-09-21 07:04:47 +00:00
|
|
|
getHelp: () => {
|
2020-05-30 02:43:33 +00:00
|
|
|
return Optional.of(Text.of(""))
|
2019-09-21 07:04:47 +00:00
|
|
|
},
|
2020-05-30 02:43:33 +00:00
|
|
|
//Text getUsage(CommandSource source)
|
2019-09-21 07:04:47 +00:00
|
|
|
getUsage: () => {
|
2020-05-30 02:43:33 +00:00
|
|
|
return Text.of('')
|
2019-09-21 07:04:47 +00:00
|
|
|
}
|
|
|
|
})
|
|
|
|
}
|
|
|
|
|
2020-05-30 02:43:33 +00:00
|
|
|
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})`
|
2020-02-26 02:15:33 +00:00
|
|
|
}
|