Support reified argument types

This commit is contained in:
Jakob K
2021-08-24 03:02:13 +02:00
parent e5faaebd38
commit 6b241a455b
2 changed files with 28 additions and 0 deletions

View File

@@ -0,0 +1,17 @@
package net.axay.kspigot.commands
import com.mojang.brigadier.arguments.*
object ArgumentTypeUtils {
@Suppress("UNCHECKED_CAST")
inline fun <reified T> 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<T>
}

View File

@@ -64,6 +64,17 @@ inline fun <T> ArgumentBuilder<CommandListenerWrapper, *>.argument(
): RequiredArgumentBuilder<CommandListenerWrapper, T> =
RequiredArgumentBuilder.argument<CommandListenerWrapper, T>(name, type).apply(builder).also { then(it) }
/**
* Add an argument.
*
* @param name the name of the argument
*/
inline fun <reified T> ArgumentBuilder<CommandListenerWrapper, *>.argument(
name: String,
builder: RequiredArgumentBuilder<CommandListenerWrapper, T>.() -> Unit,
): RequiredArgumentBuilder<CommandListenerWrapper, T> =
RequiredArgumentBuilder.argument<CommandListenerWrapper, T>(name, ArgumentTypeUtils.fromReifiedType<T>()).apply(builder).also { then(it) }
private val argumentCoroutineScope = CoroutineScope(SupervisorJob() + Dispatchers.IO)
/**