Support reified argument types
This commit is contained in:
@@ -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>
|
||||
}
|
@@ -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)
|
||||
|
||||
/**
|
||||
|
Reference in New Issue
Block a user