From 6b241a455b58ca42fb6dca94f2126cb455f4692f Mon Sep 17 00:00:00 2001 From: Jakob K Date: Tue, 24 Aug 2021 03:02:13 +0200 Subject: [PATCH] Support reified argument types --- .../axay/kspigot/commands/ArgumentTypeUtils.kt | 17 +++++++++++++++++ .../axay/kspigot/commands/BrigardierWrapper.kt | 11 +++++++++++ 2 files changed, 28 insertions(+) create mode 100644 src/main/kotlin/net/axay/kspigot/commands/ArgumentTypeUtils.kt diff --git a/src/main/kotlin/net/axay/kspigot/commands/ArgumentTypeUtils.kt b/src/main/kotlin/net/axay/kspigot/commands/ArgumentTypeUtils.kt new file mode 100644 index 00000000..164ab472 --- /dev/null +++ b/src/main/kotlin/net/axay/kspigot/commands/ArgumentTypeUtils.kt @@ -0,0 +1,17 @@ +package net.axay.kspigot.commands + +import com.mojang.brigadier.arguments.* + +object ArgumentTypeUtils { + @Suppress("UNCHECKED_CAST") + inline fun fromReifiedType() = when (T::class) { + Boolean::class -> BoolArgumentType.bool() + Int::class -> IntegerArgumentType.integer() + Long::class -> LongArgumentType.longArg() + Float::class -> FloatArgumentType.floatArg() + Double::class -> DoubleArgumentType.doubleArg() + String::class -> StringArgumentType.string() + + else -> throw IllegalArgumentException("The specified type '${T::class.qualifiedName}' does not have corresponding default argument type") + } as ArgumentType +} diff --git a/src/main/kotlin/net/axay/kspigot/commands/BrigardierWrapper.kt b/src/main/kotlin/net/axay/kspigot/commands/BrigardierWrapper.kt index f43f6f74..32ee6988 100644 --- a/src/main/kotlin/net/axay/kspigot/commands/BrigardierWrapper.kt +++ b/src/main/kotlin/net/axay/kspigot/commands/BrigardierWrapper.kt @@ -64,6 +64,17 @@ inline fun ArgumentBuilder.argument( ): RequiredArgumentBuilder = RequiredArgumentBuilder.argument(name, type).apply(builder).also { then(it) } +/** + * Add an argument. + * + * @param name the name of the argument + */ +inline fun ArgumentBuilder.argument( + name: String, + builder: RequiredArgumentBuilder.() -> Unit, +): RequiredArgumentBuilder = + RequiredArgumentBuilder.argument(name, ArgumentTypeUtils.fromReifiedType()).apply(builder).also { then(it) } + private val argumentCoroutineScope = CoroutineScope(SupervisorJob() + Dispatchers.IO) /**