diff --git a/common/src/main/kotlin/taboolib/common/platform/command/SimpleCommand.kt b/common/src/main/kotlin/taboolib/common/platform/command/SimpleCommand.kt index 6e249f66c..d5b7d2c4a 100644 --- a/common/src/main/kotlin/taboolib/common/platform/command/SimpleCommand.kt +++ b/common/src/main/kotlin/taboolib/common/platform/command/SimpleCommand.kt @@ -28,7 +28,8 @@ annotation class CommandBody( val aliases: Array = [], val optional: Boolean = false, val permission: String = "", - val permissionDefault: PermissionDefault = PermissionDefault.OP + val permissionDefault: PermissionDefault = PermissionDefault.OP, + val hidden: Boolean = false, ) fun mainCommand(func: CommandBase.() -> Unit): SimpleCommandMain { @@ -48,6 +49,7 @@ class SimpleCommandBody(val func: CommandComponent.() -> Unit = {}) { var optional = false var permission = "" var permissionDefault: PermissionDefault = PermissionDefault.OP + var hidden = false val children = ArrayList() override fun toString(): String { @@ -55,6 +57,7 @@ class SimpleCommandBody(val func: CommandComponent.() -> Unit = {}) { } } +@Suppress("DuplicatedCode") @Awake class SimpleCommandRegister : ClassVisitor(0) { @@ -76,6 +79,7 @@ class SimpleCommandRegister : ClassVisitor(0) { optional = annotation.property("optional", false) permission = annotation.property("permission", "") permissionDefault = annotation.enum("permissionDefault", PermissionDefault.OP) + hidden = annotation.property("hidden", false) } } else -> { @@ -85,6 +89,7 @@ class SimpleCommandRegister : ClassVisitor(0) { optional = annotation.property("optional", false) permission = annotation.property("permission", "") permissionDefault = annotation.enum("permissionDefault", PermissionDefault.OP) + hidden = annotation.property("hidden", false) // 向下搜索字段 ReflexClass.of(field.fieldType).structure.fields.forEach { children += loadBody(it, instance) ?: return@forEach @@ -120,7 +125,7 @@ class SimpleCommandRegister : ClassVisitor(0) { main[clazz.name]?.func?.invoke(this) body[clazz.name]?.forEach { body -> fun register(body: SimpleCommandBody, component: CommandComponent) { - component.literal(body.name, *body.aliases, optional = body.optional, permission = body.permission) { + component.literal(body.name, *body.aliases, optional = body.optional, permission = body.permission, hidden = body.hidden) { if (body.children.isEmpty()) { body.func(this) } else { diff --git a/common/src/main/kotlin/taboolib/common/platform/command/component/CommandBase.kt b/common/src/main/kotlin/taboolib/common/platform/command/component/CommandBase.kt index 5a009dbb1..5cea07d97 100644 --- a/common/src/main/kotlin/taboolib/common/platform/command/component/CommandBase.kt +++ b/common/src/main/kotlin/taboolib/common/platform/command/component/CommandBase.kt @@ -122,7 +122,7 @@ class CommandBase : CommandComponent(-1, false) { cur + 1 == context.realArgs.size -> { val suggest = component.findChildren(context).flatMap { when (it) { - is CommandComponentLiteral -> it.aliases.toList() + is CommandComponentLiteral -> if (it.hidden) emptyList() else it.aliases.toList() is CommandComponentDynamic -> it.commandSuggestion?.exec(context) ?: emptyList() else -> emptyList() } diff --git a/common/src/main/kotlin/taboolib/common/platform/command/component/CommandComponent.kt b/common/src/main/kotlin/taboolib/common/platform/command/component/CommandComponent.kt index 07ac77a52..cd0ae65fa 100644 --- a/common/src/main/kotlin/taboolib/common/platform/command/component/CommandComponent.kt +++ b/common/src/main/kotlin/taboolib/common/platform/command/component/CommandComponent.kt @@ -20,9 +20,10 @@ abstract class CommandComponent(val index: Int, var optional: Boolean, val permi vararg aliases: String, optional: Boolean = false, permission: String = "", + hidden: Boolean = false, literal: CommandComponentLiteral.() -> Unit = {} ): CommandComponentLiteral { - val component = CommandComponentLiteral(index + 1, arrayOf(*aliases), optional, permission).also(literal).also { it.parent = this } + val component = CommandComponentLiteral(arrayOf(*aliases), hidden, index + 1, optional, permission).also(literal).also { it.parent = this } // 如果当前节点已存在命令执行器 // 则自动视为可选节点 if (commandExecutor != null) { @@ -41,7 +42,7 @@ abstract class CommandComponent(val index: Int, var optional: Boolean, val permi permission: String = "", dynamic: CommandComponentDynamic.() -> Unit = {} ): CommandComponentDynamic { - val component = CommandComponentDynamic(index + 1, comment, optional, permission).also(dynamic).also { it.parent = this } + val component = CommandComponentDynamic(comment, index + 1, optional, permission).also(dynamic).also { it.parent = this } // 如果当前节点已存在命令执行器 // 则自动视为可选节点 if (commandExecutor != null) { diff --git a/common/src/main/kotlin/taboolib/common/platform/command/component/CommandComponentDynamic.kt b/common/src/main/kotlin/taboolib/common/platform/command/component/CommandComponentDynamic.kt index 83d636e25..80713d540 100644 --- a/common/src/main/kotlin/taboolib/common/platform/command/component/CommandComponentDynamic.kt +++ b/common/src/main/kotlin/taboolib/common/platform/command/component/CommandComponentDynamic.kt @@ -2,7 +2,7 @@ package taboolib.common.platform.command.component import taboolib.common.platform.command.CommandContext -class CommandComponentDynamic(index: Int, val comment: String, optional: Boolean, permission: String) : CommandComponent(index, optional, permission) { +class CommandComponentDynamic(val comment: String, index: Int, optional: Boolean, permission: String) : CommandComponent(index, optional, permission) { internal var commandRestrict: CommandRestrict<*>? = null internal var commandSuggestion: CommandSuggestion<*>? = null diff --git a/common/src/main/kotlin/taboolib/common/platform/command/component/CommandComponentLiteral.kt b/common/src/main/kotlin/taboolib/common/platform/command/component/CommandComponentLiteral.kt index 869e8a086..26298ff52 100644 --- a/common/src/main/kotlin/taboolib/common/platform/command/component/CommandComponentLiteral.kt +++ b/common/src/main/kotlin/taboolib/common/platform/command/component/CommandComponentLiteral.kt @@ -1,6 +1,12 @@ package taboolib.common.platform.command.component -class CommandComponentLiteral(index: Int, val aliases: Array, optional: Boolean, permission: String) : CommandComponent(index, optional, permission) { +class CommandComponentLiteral( + val aliases: Array, + val hidden: Boolean, + index: Int, + optional: Boolean, + permission: String +) : CommandComponent(index, optional, permission) { override fun toString(): String { return "CommandComponentLiteral(aliases=${aliases.contentToString()})" diff --git a/expansion/expansion-command-helper/src/main/kotlin/taboolib/expansion/CommandHelper.kt b/expansion/expansion-command-helper/src/main/kotlin/taboolib/expansion/CommandHelper.kt index b13a412a2..81e2687c5 100644 --- a/expansion/expansion-command-helper/src/main/kotlin/taboolib/expansion/CommandHelper.kt +++ b/expansion/expansion-command-helper/src/main/kotlin/taboolib/expansion/CommandHelper.kt @@ -11,9 +11,22 @@ fun CommandComponent.createHelper(checkPermissions: Boolean = true) { val command = context.command val builder = StringBuilder("§cUsage: /${command.name}") var newline = false + + fun check(children: List): List { + // 检查权限 + val filterChildren = if (checkPermissions) { + children.filter { sender.hasPermission(it.permission) } + } else { + children + } + // 过滤隐藏 + return filterChildren.filter { it !is CommandComponentLiteral || !it.hidden } + } + fun space(space: Int): String { return (1..space).joinToString("") { " " } } + fun print(compound: CommandComponent, index: Int, size: Int, offset: Int = 8, level: Int = 0, end: Boolean = false, optional: Boolean = false) { var option = optional var comment = 0 @@ -58,24 +71,20 @@ fun CommandComponent.createHelper(checkPermissions: Boolean = true) { if (level > 0) { comment += 1 } - compound.children.forEachIndexed { i, children -> + val checkedChildren = check(compound.children) + checkedChildren.forEachIndexed { i, children -> // 因 literal 产生新的行 if (newline) { - print(children, i, compound.children.size, offset, level + comment, end, option) + print(children, i, checkedChildren.size, offset, level + comment, end, option) } else { val length = if (offset == 8) command.name.length + 1 else comment + 1 - print(children, i, compound.children.size, offset + length, level, end, option) + print(children, i, checkedChildren.size, offset + length, level, end, option) } } } - // 检查权限 - val filterChildren = if (checkPermissions) { - context.commandCompound.children.filter { sender.hasPermission(it.permission) } - } else { - context.commandCompound.children - } - val size = filterChildren.size - filterChildren.forEachIndexed { index, children -> + val checkedChildren = check(context.commandCompound.children) + val size = checkedChildren.size + checkedChildren.forEachIndexed { index, children -> print(children, index, size, end = index + 1 == size) } builder.lines().forEach {