Fix permission duplication

This commit is contained in:
Him188 2020-09-10 10:49:36 +08:00
parent 8dd302073b
commit 5cd35d7fbe
6 changed files with 11 additions and 10 deletions

View File

@ -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) }
}

View File

@ -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) }
/**
* 在指令被执行时调用.

View File

@ -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] 可选 */

View File

@ -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)
}

View File

@ -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

View File

@ -56,7 +56,7 @@ public fun Permission.parentsWithSelfSequence(): Sequence<Permission> =
*/
@Serializable
@ExperimentalPermission
public class PermissionImpl(
public data class PermissionImpl(
override val id: PermissionId,
override val description: String,
override val parentId: PermissionId = RootPermission.id