-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat[onebot11]: extend brigadier's type system
- Loading branch information
Showing
6 changed files
with
116 additions
and
3 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
33 changes: 33 additions & 0 deletions
33
ronebot-common/src/main/kotlin/cn/rtast/rob/command/arguments/AnyStringTypeArgument.kt
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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() | ||
} | ||
} | ||
} |
34 changes: 34 additions & 0 deletions
34
ronebot-common/src/main/kotlin/cn/rtast/rob/command/arguments/CharTypeArgument.kt
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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) | ||
} | ||
} | ||
} |
22 changes: 22 additions & 0 deletions
22
ronebot-common/src/main/kotlin/cn/rtast/rob/command/arguments/StringReader.kt
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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() | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters