From 5cd35d7fbe22b3f568abdedaee31162391f719d9 Mon Sep 17 00:00:00 2001 From: Him188 Date: Thu, 10 Sep 2020 10:49:36 +0800 Subject: [PATCH] Fix permission duplication --- .../main/kotlin/net/mamoe/mirai/console/command/Command.kt | 4 ++-- .../kotlin/net/mamoe/mirai/console/command/RawCommand.kt | 4 ++-- .../net/mamoe/mirai/console/command/java/JRawCommand.kt | 4 ++-- .../console/internal/command/CompositeCommandInternal.kt | 2 +- .../net/mamoe/mirai/console/internal/command/internal.kt | 5 +++-- .../kotlin/net/mamoe/mirai/console/permission/Permission.kt | 2 +- 6 files changed, 11 insertions(+), 10 deletions(-) diff --git a/backend/mirai-console/src/main/kotlin/net/mamoe/mirai/console/command/Command.kt b/backend/mirai-console/src/main/kotlin/net/mamoe/mirai/console/command/Command.kt index cf757ee70..2636ae6d8 100644 --- a/backend/mirai-console/src/main/kotlin/net/mamoe/mirai/console/command/Command.kt +++ b/backend/mirai-console/src/main/kotlin/net/mamoe/mirai/console/command/Command.kt @@ -15,7 +15,7 @@ import net.mamoe.kjbb.JvmBlockingBridge import net.mamoe.mirai.console.command.CommandManager.INSTANCE.executeCommand import net.mamoe.mirai.console.command.CommandManager.INSTANCE.register import net.mamoe.mirai.console.command.java.JCommand -import net.mamoe.mirai.console.internal.command.createCommandPermission +import net.mamoe.mirai.console.internal.command.createOrFindCommandPermission import net.mamoe.mirai.console.internal.command.isValidSubName import net.mamoe.mirai.console.permission.ExperimentalPermission import net.mamoe.mirai.console.permission.Permission @@ -118,5 +118,5 @@ public abstract class AbstractCommand }.toTypedArray() @OptIn(ExperimentalPermission::class) - public override val permission: Permission by lazy { createCommandPermission(parentPermission) } + public override val permission: Permission by lazy { createOrFindCommandPermission(parentPermission) } } \ No newline at end of file diff --git a/backend/mirai-console/src/main/kotlin/net/mamoe/mirai/console/command/RawCommand.kt b/backend/mirai-console/src/main/kotlin/net/mamoe/mirai/console/command/RawCommand.kt index 47ac57558..7961a7212 100644 --- a/backend/mirai-console/src/main/kotlin/net/mamoe/mirai/console/command/RawCommand.kt +++ b/backend/mirai-console/src/main/kotlin/net/mamoe/mirai/console/command/RawCommand.kt @@ -14,7 +14,7 @@ package net.mamoe.mirai.console.command import net.mamoe.mirai.console.command.CommandManager.INSTANCE.execute import net.mamoe.mirai.console.command.CommandManager.INSTANCE.executeCommand import net.mamoe.mirai.console.command.java.JRawCommand -import net.mamoe.mirai.console.internal.command.createCommandPermission +import net.mamoe.mirai.console.internal.command.createOrFindCommandPermission import net.mamoe.mirai.console.permission.ExperimentalPermission import net.mamoe.mirai.console.permission.Permission import net.mamoe.mirai.console.permission.PermissionId @@ -49,7 +49,7 @@ public abstract class RawCommand @OptIn(ExperimentalPermission::class) construct public override val prefixOptional: Boolean = false ) : Command { @OptIn(ExperimentalPermission::class) - public override val permission: Permission by lazy { createCommandPermission(parentPermission) } + public override val permission: Permission by lazy { createOrFindCommandPermission(parentPermission) } /** * 在指令被执行时调用. diff --git a/backend/mirai-console/src/main/kotlin/net/mamoe/mirai/console/command/java/JRawCommand.kt b/backend/mirai-console/src/main/kotlin/net/mamoe/mirai/console/command/java/JRawCommand.kt index 9074cc170..566403750 100644 --- a/backend/mirai-console/src/main/kotlin/net/mamoe/mirai/console/command/java/JRawCommand.kt +++ b/backend/mirai-console/src/main/kotlin/net/mamoe/mirai/console/command/java/JRawCommand.kt @@ -13,7 +13,7 @@ import kotlinx.coroutines.Dispatchers import kotlinx.coroutines.withContext import net.mamoe.mirai.console.command.* import net.mamoe.mirai.console.command.CommandManager.INSTANCE.execute -import net.mamoe.mirai.console.internal.command.createCommandPermission +import net.mamoe.mirai.console.internal.command.createOrFindCommandPermission import net.mamoe.mirai.console.permission.ExperimentalPermission import net.mamoe.mirai.console.permission.Permission import net.mamoe.mirai.console.permission.PermissionId @@ -67,7 +67,7 @@ public abstract class JRawCommand @OptIn(ExperimentalPermission::class) /** 指令权限 */ @ExperimentalPermission - public final override var permission: Permission = createCommandPermission(parentPermission) + public final override var permission: Permission = createOrFindCommandPermission(parentPermission) protected set /** 为 `true` 时表示 [指令前缀][CommandManager.commandPrefix] 可选 */ diff --git a/backend/mirai-console/src/main/kotlin/net/mamoe/mirai/console/internal/command/CompositeCommandInternal.kt b/backend/mirai-console/src/main/kotlin/net/mamoe/mirai/console/internal/command/CompositeCommandInternal.kt index b427d982e..e9789b556 100644 --- a/backend/mirai-console/src/main/kotlin/net/mamoe/mirai/console/internal/command/CompositeCommandInternal.kt +++ b/backend/mirai-console/src/main/kotlin/net/mamoe/mirai/console/internal/command/CompositeCommandInternal.kt @@ -79,7 +79,7 @@ internal abstract class AbstractReflectionCommand @OptIn(ExperimentalPermission: internal val defaultSubCommand: DefaultSubCommandDescriptor by lazy { DefaultSubCommandDescriptor( "", - createCommandPermission(parentPermission), + createOrFindCommandPermission(parentPermission), onCommand = { sender: CommandSender, args: MessageChain -> sender.onDefault(args) } diff --git a/backend/mirai-console/src/main/kotlin/net/mamoe/mirai/console/internal/command/internal.kt b/backend/mirai-console/src/main/kotlin/net/mamoe/mirai/console/internal/command/internal.kt index 1958f7c61..5f16bf9ee 100644 --- a/backend/mirai-console/src/main/kotlin/net/mamoe/mirai/console/internal/command/internal.kt +++ b/backend/mirai-console/src/main/kotlin/net/mamoe/mirai/console/internal/command/internal.kt @@ -143,8 +143,9 @@ internal fun Group.fuzzySearchMember( } @OptIn(ExperimentalPermission::class) -internal fun Command.createCommandPermission(parent: PermissionId): Permission { - return PermissionService.INSTANCE.register(owner.permissionId(primaryName), description, parent) +internal fun Command.createOrFindCommandPermission(parent: PermissionId): Permission { + val id = owner.permissionId(primaryName) + return PermissionService.INSTANCE[id] ?: PermissionService.INSTANCE.register(id, description, parent) } //// internal diff --git a/backend/mirai-console/src/main/kotlin/net/mamoe/mirai/console/permission/Permission.kt b/backend/mirai-console/src/main/kotlin/net/mamoe/mirai/console/permission/Permission.kt index 79ca00b77..a5d48a1ce 100644 --- a/backend/mirai-console/src/main/kotlin/net/mamoe/mirai/console/permission/Permission.kt +++ b/backend/mirai-console/src/main/kotlin/net/mamoe/mirai/console/permission/Permission.kt @@ -56,7 +56,7 @@ public fun Permission.parentsWithSelfSequence(): Sequence = */ @Serializable @ExperimentalPermission -public class PermissionImpl( +public data class PermissionImpl( override val id: PermissionId, override val description: String, override val parentId: PermissionId = RootPermission.id