Skip to content

Commit

Permalink
feat[onebot11]: extend brigadier's type system
Browse files Browse the repository at this point in the history
  • Loading branch information
RTAkland committed Jan 16, 2025
1 parent 2831d30 commit fb9bd3d
Show file tree
Hide file tree
Showing 6 changed files with 116 additions and 3 deletions.
2 changes: 1 addition & 1 deletion docs
2 changes: 1 addition & 1 deletion gradle.properties
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
kotlin.code.style=official
libVersion=2.6.4
libVersion=2.6.5

#systemProp.http.proxyHost=127.0.0.1
#systemProp.http.proxyPort=12334
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
/*
* Copyright © 2025 RTAkland
* Author: RTAkland
* Date: 2025/1/16
*/

@file:Suppress("unused")

package cn.rtast.rob.command.arguments

import com.mojang.brigadier.StringReader
import com.mojang.brigadier.arguments.ArgumentType
import com.mojang.brigadier.context.CommandContext

/**
* 定义一个任意类型输入的Brigadier类型
* 输入任意类型最后都会解析成字符串[String]
*/
class AnyStringTypeArgument: ArgumentType<Any> {
override fun parse(reader: StringReader): String {
return reader.readUnquotedString().toString()
}

companion object {
fun anyStringType(): AnyStringTypeArgument {
return AnyStringTypeArgument()
}

fun getAnyString(context: CommandContext<*>, name: String): String {
return context.getArgument(name, Any::class.java).toString()
}
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
/*
* Copyright © 2025 RTAkland
* Author: RTAkland
* Date: 2025/1/16
*/

@file:Suppress("unused")

package cn.rtast.rob.command.arguments

import com.mojang.brigadier.StringReader
import com.mojang.brigadier.arguments.ArgumentType
import com.mojang.brigadier.context.CommandContext
import com.mojang.brigadier.exceptions.CommandSyntaxException

/**
* [Char]类型的Brigadier输入参数
*/
class CharTypeArgument : ArgumentType<Char> {
@Throws(CommandSyntaxException::class)
override fun parse(reader: StringReader): Char {
return reader.readChar()
}

companion object {
fun chatType(): CharTypeArgument {
return CharTypeArgument()
}

fun getChar(context: CommandContext<*>, name: String): Char {
return context.getArgument(name, Char::class.java)
}
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
/*
* Copyright © 2025 RTAkland
* Author: RTAkland
* Date: 2025/1/16
*/


package cn.rtast.rob.command.arguments

import com.mojang.brigadier.StringReader
import com.mojang.brigadier.exceptions.CommandSyntaxException

/**
* 读取一个[Char]
*/
@Throws(CommandSyntaxException::class)
fun StringReader.readChar(): Char {
if (!canRead()) {
throw CommandSyntaxException.BUILT_IN_EXCEPTIONS.readerExpectedBool().createWithContext(this)
}
return read()
}
26 changes: 25 additions & 1 deletion ronebot-onebot-v11/src/test/kotlin/test/TestClient.kt
Original file line number Diff line number Diff line change
Expand Up @@ -7,10 +7,11 @@
package test

import cn.rtast.rob.ROneBotFactory
import cn.rtast.rob.command.arguments.AnyStringTypeArgument
import cn.rtast.rob.command.arguments.CharTypeArgument
import cn.rtast.rob.entity.GroupMessage
import cn.rtast.rob.entity.custom.ErrorEvent
import cn.rtast.rob.enums.QQFace
import cn.rtast.rob.onebot.MessageChain
import cn.rtast.rob.onebot.OneBotListener
import cn.rtast.rob.util.BaseCommand
import cn.rtast.rob.util.BrigadierCommand
Expand Down Expand Up @@ -56,6 +57,29 @@ class TestBrigadierCommand : BrigadierCommand() {
0
}
)
.then(
LiteralArgumentBuilder.literal<CommandSource>("ss")
.then(
RequiredArgumentBuilder.argument<CommandSource, Any>(
"any",
AnyStringTypeArgument.anyStringType()
)
.executes {
println(AnyStringTypeArgument.getAnyString(it, "any")::class.java)
0
}
)
).then(
LiteralArgumentBuilder.literal<CommandSource>("char")
.then(
RequiredArgumentBuilder.argument<CommandSource, Char>("char", CharTypeArgument.chatType())
.executes {
println(CharTypeArgument.getChar(it, "char")::class.java)
0
}
)
)
dispatcher.register(root)
dispatcher.register(LiteralArgumentBuilder.literal<CommandSource>("/test").redirect(root.build()))
}
}
Expand Down

0 comments on commit fb9bd3d

Please sign in to comment.