Skip to content

Commit

Permalink
[6.0.10][publish] update "hidden" flag
Browse files Browse the repository at this point in the history
  • Loading branch information
Bkm016 committed Apr 6, 2023
1 parent 3aacec1 commit 39a9d4a
Show file tree
Hide file tree
Showing 6 changed files with 39 additions and 18 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,8 @@ annotation class CommandBody(
val aliases: Array<String> = [],
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 {
Expand All @@ -48,13 +49,15 @@ class SimpleCommandBody(val func: CommandComponent.() -> Unit = {}) {
var optional = false
var permission = ""
var permissionDefault: PermissionDefault = PermissionDefault.OP
var hidden = false
val children = ArrayList<SimpleCommandBody>()

override fun toString(): String {
return "SimpleCommandBody(name='$name', children=$children)"
}
}

@Suppress("DuplicatedCode")
@Awake
class SimpleCommandRegister : ClassVisitor(0) {

Expand All @@ -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 -> {
Expand All @@ -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
Expand Down Expand Up @@ -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 {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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()
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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) {
Expand All @@ -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) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,12 @@
package taboolib.common.platform.command.component

class CommandComponentLiteral(index: Int, val aliases: Array<String>, optional: Boolean, permission: String) : CommandComponent(index, optional, permission) {
class CommandComponentLiteral(
val aliases: Array<String>,
val hidden: Boolean,
index: Int,
optional: Boolean,
permission: String
) : CommandComponent(index, optional, permission) {

override fun toString(): String {
return "CommandComponentLiteral(aliases=${aliases.contentToString()})"
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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<CommandComponent>): List<CommandComponent> {
// 检查权限
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
Expand Down Expand Up @@ -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 {
Expand Down

0 comments on commit 39a9d4a

Please sign in to comment.