From fb9bd3d0e576f4692d049dde9d08527540c92a05 Mon Sep 17 00:00:00 2001 From: RTAkland Date: Thu, 16 Jan 2025 14:05:31 +0800 Subject: [PATCH] feat[onebot11]: extend brigadier's type system --- docs | 2 +- gradle.properties | 2 +- .../arguments/AnyStringTypeArgument.kt | 33 ++++++++++++++++++ .../rob/command/arguments/CharTypeArgument.kt | 34 +++++++++++++++++++ .../rob/command/arguments/StringReader.kt | 22 ++++++++++++ .../src/test/kotlin/test/TestClient.kt | 26 +++++++++++++- 6 files changed, 116 insertions(+), 3 deletions(-) create mode 100644 ronebot-common/src/main/kotlin/cn/rtast/rob/command/arguments/AnyStringTypeArgument.kt create mode 100644 ronebot-common/src/main/kotlin/cn/rtast/rob/command/arguments/CharTypeArgument.kt create mode 100644 ronebot-common/src/main/kotlin/cn/rtast/rob/command/arguments/StringReader.kt diff --git a/docs b/docs index b2ddc70..70b370e 160000 --- a/docs +++ b/docs @@ -1 +1 @@ -Subproject commit b2ddc7044d516e4b4791e146139071f55df791e8 +Subproject commit 70b370ee19b6c360f41a62c43834d90e13bbef8c diff --git a/gradle.properties b/gradle.properties index de32e84..cc59a78 100644 --- a/gradle.properties +++ b/gradle.properties @@ -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 diff --git a/ronebot-common/src/main/kotlin/cn/rtast/rob/command/arguments/AnyStringTypeArgument.kt b/ronebot-common/src/main/kotlin/cn/rtast/rob/command/arguments/AnyStringTypeArgument.kt new file mode 100644 index 0000000..481c1b5 --- /dev/null +++ b/ronebot-common/src/main/kotlin/cn/rtast/rob/command/arguments/AnyStringTypeArgument.kt @@ -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 { + 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() + } + } +} \ No newline at end of file diff --git a/ronebot-common/src/main/kotlin/cn/rtast/rob/command/arguments/CharTypeArgument.kt b/ronebot-common/src/main/kotlin/cn/rtast/rob/command/arguments/CharTypeArgument.kt new file mode 100644 index 0000000..ec26610 --- /dev/null +++ b/ronebot-common/src/main/kotlin/cn/rtast/rob/command/arguments/CharTypeArgument.kt @@ -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 { + @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) + } + } +} \ No newline at end of file diff --git a/ronebot-common/src/main/kotlin/cn/rtast/rob/command/arguments/StringReader.kt b/ronebot-common/src/main/kotlin/cn/rtast/rob/command/arguments/StringReader.kt new file mode 100644 index 0000000..0d48e87 --- /dev/null +++ b/ronebot-common/src/main/kotlin/cn/rtast/rob/command/arguments/StringReader.kt @@ -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() +} \ No newline at end of file diff --git a/ronebot-onebot-v11/src/test/kotlin/test/TestClient.kt b/ronebot-onebot-v11/src/test/kotlin/test/TestClient.kt index fe4e3d5..50d7cb7 100644 --- a/ronebot-onebot-v11/src/test/kotlin/test/TestClient.kt +++ b/ronebot-onebot-v11/src/test/kotlin/test/TestClient.kt @@ -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 @@ -56,6 +57,29 @@ class TestBrigadierCommand : BrigadierCommand() { 0 } ) + .then( + LiteralArgumentBuilder.literal("ss") + .then( + RequiredArgumentBuilder.argument( + "any", + AnyStringTypeArgument.anyStringType() + ) + .executes { + println(AnyStringTypeArgument.getAnyString(it, "any")::class.java) + 0 + } + ) + ).then( + LiteralArgumentBuilder.literal("char") + .then( + RequiredArgumentBuilder.argument("char", CharTypeArgument.chatType()) + .executes { + println(CharTypeArgument.getChar(it, "char")::class.java) + 0 + } + ) + ) + dispatcher.register(root) dispatcher.register(LiteralArgumentBuilder.literal("/test").redirect(root.build())) } }