From f40dd189f205ca0c5170e127a17e3cee05765124 Mon Sep 17 00:00:00 2001 From: Him188 Date: Fri, 18 Sep 2020 23:26:16 +0800 Subject: [PATCH] Fix commands --- .../mirai/console/command/CommandManager.kt | 2 +- .../mirai/console/command/java/JCommand.kt | 5 ++- .../console/command/java/JCompositeCommand.kt | 2 +- .../console/command/java/JSimpleCommand.kt | 1 + .../internal/command/CommandManagerImpl.kt | 36 +++++++++++-------- .../mirai/console/command/TestCommand.kt | 2 +- 6 files changed, 27 insertions(+), 21 deletions(-) diff --git a/backend/mirai-console/src/main/kotlin/net/mamoe/mirai/console/command/CommandManager.kt b/backend/mirai-console/src/main/kotlin/net/mamoe/mirai/console/command/CommandManager.kt index 81e1d6132..b2c058d78 100644 --- a/backend/mirai-console/src/main/kotlin/net/mamoe/mirai/console/command/CommandManager.kt +++ b/backend/mirai-console/src/main/kotlin/net/mamoe/mirai/console/command/CommandManager.kt @@ -157,7 +157,7 @@ public interface CommandManager { public companion object INSTANCE : CommandManager by CommandManagerImpl { // TODO: 2020/8/20 https://youtrack.jetbrains.com/issue/KT-41191 - override val CommandOwner.registeredCommands: List get() = CommandManagerImpl.run { registeredCommands } + override val CommandOwner.registeredCommands: List get() = CommandManagerImpl.run { this@registeredCommands.registeredCommands } override fun CommandOwner.unregisterAllCommands(): Unit = CommandManagerImpl.run { unregisterAllCommands() } override fun Command.register(override: Boolean): Boolean = CommandManagerImpl.run { register(override) } override fun Command.findDuplicate(): Command? = CommandManagerImpl.run { findDuplicate() } diff --git a/backend/mirai-console/src/main/kotlin/net/mamoe/mirai/console/command/java/JCommand.kt b/backend/mirai-console/src/main/kotlin/net/mamoe/mirai/console/command/java/JCommand.kt index 38d3d2c0f..1fbf748c0 100644 --- a/backend/mirai-console/src/main/kotlin/net/mamoe/mirai/console/command/java/JCommand.kt +++ b/backend/mirai-console/src/main/kotlin/net/mamoe/mirai/console/command/java/JCommand.kt @@ -16,7 +16,6 @@ import net.mamoe.mirai.console.command.CommandManager import net.mamoe.mirai.console.command.CommandManager.INSTANCE.executeCommand import net.mamoe.mirai.console.command.CommandSender import net.mamoe.mirai.message.data.MessageChain -import net.mamoe.mirai.message.data.SingleMessage /** * 为 Java 用户添加协程帮助的 [Command]. @@ -33,9 +32,9 @@ public interface JCommand : Command { /** * 在指令被执行时调用. * - * @param args 指令参数. 数组元素类型可能是 [SingleMessage] 或 [String]. 且已经以 ' ' 分割. + * @param args 精确的指令参数. [MessageChain] 每个元素代表一个精确的参数. * * @see CommandManager.executeCommand 查看更多信息 */ - public fun onCommand(sender: CommandSender, args: MessageChain) // overrides bridge + public fun onCommand(sender: CommandSender, args: MessageChain) // overrides blocking bridge } \ No newline at end of file diff --git a/backend/mirai-console/src/main/kotlin/net/mamoe/mirai/console/command/java/JCompositeCommand.kt b/backend/mirai-console/src/main/kotlin/net/mamoe/mirai/console/command/java/JCompositeCommand.kt index fdb5aee47..09bc507e6 100644 --- a/backend/mirai-console/src/main/kotlin/net/mamoe/mirai/console/command/java/JCompositeCommand.kt +++ b/backend/mirai-console/src/main/kotlin/net/mamoe/mirai/console/command/java/JCompositeCommand.kt @@ -24,7 +24,7 @@ import net.mamoe.mirai.console.permission.Permission * public final class MyCompositeCommand extends CompositeCommand { * public static final MyCompositeCommand INSTANCE = new MyCompositeCommand(); * - * public MyCompositeCommand() { + * private MyCompositeCommand() { * super(MyPluginMain.INSTANCE, "manage") // "manage" 是主指令名 * } * diff --git a/backend/mirai-console/src/main/kotlin/net/mamoe/mirai/console/command/java/JSimpleCommand.kt b/backend/mirai-console/src/main/kotlin/net/mamoe/mirai/console/command/java/JSimpleCommand.kt index b136af3be..e7a3eb3fd 100644 --- a/backend/mirai-console/src/main/kotlin/net/mamoe/mirai/console/command/java/JSimpleCommand.kt +++ b/backend/mirai-console/src/main/kotlin/net/mamoe/mirai/console/command/java/JSimpleCommand.kt @@ -20,6 +20,7 @@ import net.mamoe.mirai.console.permission.Permission * Java 实现: * ```java * public final class MySimpleCommand extends JSimpleCommand { + * public static final MySimpleCommand INSTANCE = new MySimpleCommand(); * private MySimpleCommand() { * super(MyPlugin.INSTANCE, "tell") * // 可选设置如下属性 diff --git a/backend/mirai-console/src/main/kotlin/net/mamoe/mirai/console/internal/command/CommandManagerImpl.kt b/backend/mirai-console/src/main/kotlin/net/mamoe/mirai/console/internal/command/CommandManagerImpl.kt index 2fb30f9b3..eb34fb34a 100644 --- a/backend/mirai-console/src/main/kotlin/net/mamoe/mirai/console/internal/command/CommandManagerImpl.kt +++ b/backend/mirai-console/src/main/kotlin/net/mamoe/mirai/console/internal/command/CommandManagerImpl.kt @@ -14,6 +14,7 @@ import kotlinx.coroutines.CoroutineExceptionHandler import kotlinx.coroutines.CoroutineScope import net.mamoe.mirai.console.MiraiConsole import net.mamoe.mirai.console.command.* +import net.mamoe.mirai.console.command.Command.Companion.allNames import net.mamoe.mirai.console.command.CommandSender.Companion.toCommandSender import net.mamoe.mirai.event.Listener import net.mamoe.mirai.event.subscribeAlways @@ -30,8 +31,9 @@ internal object CommandManagerImpl : CommandManager, CoroutineScope by Coroutine MiraiConsole.createLogger("command") } + @Suppress("ObjectPropertyName") @JvmField - internal val registeredCommands: MutableList = mutableListOf() + internal val _registeredCommands: MutableList = mutableListOf() @JvmField internal val requiredPrefixCommandMap: MutableMap = mutableMapOf() @@ -88,8 +90,8 @@ internal object CommandManagerImpl : CommandManager, CoroutineScope by Coroutine ///// IMPL - override val CommandOwner.registeredCommands: List get() = CommandManagerImpl.registeredCommands.filter { it.owner == this } - override val allRegisteredCommands: List get() = registeredCommands.toList() // copy + override val CommandOwner.registeredCommands: List get() = _registeredCommands.filter { it.owner == this } + override val allRegisteredCommands: List get() = _registeredCommands.toList() // copy override val commandPrefix: String get() = "/" override fun CommandOwner.unregisterAllCommands() { for (registeredCommand in registeredCommands) { @@ -99,24 +101,28 @@ internal object CommandManagerImpl : CommandManager, CoroutineScope by Coroutine override fun Command.register(override: Boolean): Boolean { if (this is CompositeCommand) this.subCommands // init lazy - this.permission // init lazy - this.secondaryNames // init lazy - this.description // init lazy - this.usage // init lazy + kotlin.runCatching { + this.permission // init lazy + this.secondaryNames // init lazy + this.description // init lazy + this.usage // init lazy + }.onFailure { + throw IllegalStateException("Failed to init command ${this@register}.", it) + } modifyLock.withLock { if (!override) { if (findDuplicate() != null) return false } - registeredCommands.add(this@register) + _registeredCommands.add(this@register) if (this.prefixOptional) { - for (name in this.secondaryNames) { + for (name in this.allNames) { val lowerCaseName = name.toLowerCase() optionalPrefixCommandMap[lowerCaseName] = this requiredPrefixCommandMap[lowerCaseName] = this } } else { - for (name in this.secondaryNames) { + for (name in this.allNames) { val lowerCaseName = name.toLowerCase() optionalPrefixCommandMap.remove(lowerCaseName) // ensure resolution consistency requiredPrefixCommandMap[lowerCaseName] = this @@ -127,21 +133,21 @@ internal object CommandManagerImpl : CommandManager, CoroutineScope by Coroutine } override fun Command.findDuplicate(): Command? = - registeredCommands.firstOrNull { it.secondaryNames intersectsIgnoringCase this.secondaryNames } + _registeredCommands.firstOrNull { it.allNames intersectsIgnoringCase this.allNames } override fun Command.unregister(): Boolean = modifyLock.withLock { if (this.prefixOptional) { - this.secondaryNames.forEach { + this.allNames.forEach { optionalPrefixCommandMap.remove(it) } } - this.secondaryNames.forEach { + this.allNames.forEach { requiredPrefixCommandMap.remove(it) } - registeredCommands.remove(this) + _registeredCommands.remove(this) } - override fun Command.isRegistered(): Boolean = this in registeredCommands + override fun Command.isRegistered(): Boolean = this in _registeredCommands override suspend fun Command.execute( sender: CommandSender, diff --git a/backend/mirai-console/src/test/kotlin/net/mamoe/mirai/console/command/TestCommand.kt b/backend/mirai-console/src/test/kotlin/net/mamoe/mirai/console/command/TestCommand.kt index 3db1b0a52..122475bed 100644 --- a/backend/mirai-console/src/test/kotlin/net/mamoe/mirai/console/command/TestCommand.kt +++ b/backend/mirai-console/src/test/kotlin/net/mamoe/mirai/console/command/TestCommand.kt @@ -79,7 +79,7 @@ internal class TestCommand { assertEquals(1, ConsoleCommandOwner.registeredCommands.size) - assertEquals(1, CommandManagerImpl.registeredCommands.size) + assertEquals(1, CommandManagerImpl._registeredCommands.size) assertEquals(2, CommandManagerImpl.requiredPrefixCommandMap.size) } finally { TestCompositeCommand.unregister()