Implement SimpleCommand

This commit is contained in:
Him188 2020-06-28 10:44:55 +08:00
parent 01fd1b3b6e
commit 6bfe5929e8
3 changed files with 26 additions and 9 deletions

View File

@ -71,7 +71,7 @@ abstract class CompositeCommand @JvmOverloads constructor(
}
final override suspend fun CommandSender.onCommand(args: Array<out Any>) {
matchSubCommand(args)?.parseAndExecute(this, args) ?: kotlin.run {
matchSubCommand(args)?.parseAndExecute(this, args, true) ?: kotlin.run {
defaultSubCommand.onCommand(this, args)
}
}

View File

@ -20,6 +20,7 @@ package net.mamoe.mirai.console.command
import net.mamoe.mirai.console.command.description.CommandParserContext
import net.mamoe.mirai.console.command.description.CommandParserContextAware
import net.mamoe.mirai.console.command.description.EmptyCommandParserContext
import net.mamoe.mirai.console.command.description.plus
import net.mamoe.mirai.console.command.internal.AbstractReflectionCommand
import net.mamoe.mirai.console.command.internal.SimpleCommandSubCommandAnnotationResolver
@ -38,11 +39,18 @@ abstract class SimpleCommand @JvmOverloads constructor(
*/
protected annotation class Handler
final override val context: CommandParserContext
get() = TODO("Not yet implemented")
final override val context: CommandParserContext = CommandParserContext.Builtins + overrideContext
override fun checkSubCommand() {
super.checkSubCommand()
check(subCommands.size == 1) { "There can only be exactly one function annotated with Handler at this moment as overloading is not yet supported." }
}
@Deprecated("prohibited", level = DeprecationLevel.HIDDEN)
final override suspend fun CommandSender.onDefault(rawArgs: Array<out Any>) = error("shouldn't be reached")
final override suspend fun CommandSender.onCommand(args: Array<out Any>) {
subCommands.single().parseAndExecute(this, args, false)
}
final override val subCommandAnnotationResolver: SubCommandAnnotationResolver

View File

@ -52,7 +52,7 @@ internal abstract class AbstractReflectionCommand @JvmOverloads constructor(
@JvmField
@Suppress("PropertyName")
internal var _usage: String = "<command build failed>"
internal var _usage: String = "<not yet initialized>"
final override val usage: String // initialized by subCommand reflection
get() = _usage
@ -62,13 +62,17 @@ internal abstract class AbstractReflectionCommand @JvmOverloads constructor(
internal val defaultSubCommand: DefaultSubCommandDescriptor by lazy {
DefaultSubCommandDescriptor(
"",
CommandPermission.Default,
permission,
onCommand = block2 { sender: CommandSender, args: Array<out Any> ->
sender.onDefault(args)
}
)
}
internal open fun checkSubCommand() {
}
interface SubCommandAnnotationResolver {
fun hasAnnotation(function: KFunction<*>): Boolean
fun getSubCommandNames(function: KFunction<*>): Array<out String>
@ -86,7 +90,7 @@ internal abstract class AbstractReflectionCommand @JvmOverloads constructor(
createSubCommand(function, context)
}.toTypedArray().also {
_usage = it.firstOrNull()?.usage ?: description
}
}.also { checkSubCommand() }
}
internal val bakedCommandNameToSubDescriptorArray: Map<Array<String>, SubCommandDescriptor> by lazy {
@ -118,9 +122,14 @@ internal abstract class AbstractReflectionCommand @JvmOverloads constructor(
) {
internal suspend inline fun parseAndExecute(
sender: CommandSender,
argsWithSubCommandNameNotRemoved: Array<out Any>
argsWithSubCommandNameNotRemoved: Array<out Any>,
removeSubName: Boolean
) {
if (!onCommand(sender, parseArgs(sender, argsWithSubCommandNameNotRemoved, names.size))) {
if (!onCommand(
sender,
parseArgs(sender, argsWithSubCommandNameNotRemoved, if (removeSubName) names.size else 0)
)
) {
sender.sendMessage(usage)
}
}