core: Use the ArgumentDescription interface for descriptions

This allows minecraft-extras to provide an implementation that
uses Adventure chat components to represent the description.
This commit is contained in:
Zach Levis 2021-01-11 23:08:24 -08:00 committed by Alexander Söderberg
parent fa16fc8ef2
commit b38c725dc5
19 changed files with 903 additions and 94 deletions

View file

@ -23,6 +23,7 @@
//
package cloud.commandframework.kotlin
import cloud.commandframework.ArgumentDescription
import cloud.commandframework.Command
import cloud.commandframework.CommandManager
import cloud.commandframework.Description
@ -52,6 +53,8 @@ public class MutableCommandBuilder<C : Any> {
* @param commandManager the command manager which will own this command
* @since 1.3.0
*/
@Suppress("DEPRECATION")
@Deprecated(message = "ArgumentDescription should be used over Description", level = DeprecationLevel.HIDDEN)
public constructor(
name: String,
description: Description = Description.empty(),
@ -62,6 +65,25 @@ public class MutableCommandBuilder<C : Any> {
this.commandBuilder = commandManager.commandBuilder(name, description, *aliases)
}
/**
* Create a new [MutableCommandBuilder]
*
* @param name name for the root command node
* @param description description for the root command node
* @param aliases aliases for the root command node
* @param commandManager the command manager which will own this command
* @since 1.4.0
*/
public constructor(
name: String,
description: ArgumentDescription = ArgumentDescription.empty(),
aliases: Array<String> = emptyArray(),
commandManager: CommandManager<C>
) {
this.commandManager = commandManager
this.commandBuilder = commandManager.commandBuilder(name, description, *aliases)
}
/**
* Create a new [MutableCommandBuilder] and invoke the provided receiver lambda on it
*
@ -72,6 +94,8 @@ public class MutableCommandBuilder<C : Any> {
* @param lambda receiver lambda which will be invoked on the new builder
* @since 1.3.0
*/
@Suppress("DEPRECATION")
@Deprecated(message = "ArgumentDescription should be used over Description", level = DeprecationLevel.HIDDEN)
public constructor(
name: String,
description: Description = Description.empty(),
@ -82,6 +106,26 @@ public class MutableCommandBuilder<C : Any> {
lambda(this)
}
/**
* Create a new [MutableCommandBuilder] and invoke the provided receiver lambda on it
*
* @param name name for the root command node
* @param description description for the root command node
* @param aliases aliases for the root command node
* @param commandManager the command manager which will own this command
* @param lambda receiver lambda which will be invoked on the new builder
* @since 1.4.0
*/
public constructor(
name: String,
description: ArgumentDescription = ArgumentDescription.empty(),
aliases: Array<String> = emptyArray(),
commandManager: CommandManager<C>,
lambda: MutableCommandBuilder<C>.() -> Unit
) : this(name, description, aliases, commandManager) {
lambda(this)
}
private constructor(
commandManager: CommandManager<C>,
commandBuilder: Command.Builder<C>
@ -165,6 +209,8 @@ public class MutableCommandBuilder<C : Any> {
* @return a copy of this mutable builder
* @since 1.3.0
*/
@Suppress("DEPRECATION")
@Deprecated(message = "ArgumentDescription should be used over Description", level = DeprecationLevel.HIDDEN)
public fun copy(
literal: String,
description: Description,
@ -175,6 +221,25 @@ public class MutableCommandBuilder<C : Any> {
lambda(this)
}
/**
* Make a new copy of this [MutableCommandBuilder], append a literal, and invoke the provided receiver lambda on it
*
* @param literal name for the literal
* @param description description for the literal
* @param lambda receiver lambda which will be invoked on the new builder
* @return a copy of this mutable builder
* @since 1.4.0
*/
public fun copy(
literal: String,
description: ArgumentDescription,
lambda: MutableCommandBuilder<C>.() -> Unit
): MutableCommandBuilder<C> =
copy().apply {
literal(literal, description)
lambda(this)
}
/**
* Make a new copy of this [MutableCommandBuilder], append a literal, and invoke the provided receiver lambda on it
*
@ -247,6 +312,8 @@ public class MutableCommandBuilder<C : Any> {
* @see [CommandManager.command]
* @since 1.3.0
*/
@Suppress("DEPRECATION")
@Deprecated(message = "ArgumentDescription should be used over Description", level = DeprecationLevel.HIDDEN)
public fun registerCopy(
literal: String,
description: Description,
@ -254,6 +321,25 @@ public class MutableCommandBuilder<C : Any> {
): MutableCommandBuilder<C> =
copy(literal, description, lambda).register()
/**
* Create a new copy of this mutable builder, append a literal, act on it with a receiver lambda, and then register it with
* the owning
* command manager
*
* @param literal name for the literal
* @param description description for the literal
* @param lambda receiver lambda which will be invoked on the new builder
* @return the new mutable builder
* @see [CommandManager.command]
* @since 1.4.0
*/
public fun registerCopy(
literal: String,
description: ArgumentDescription,
lambda: MutableCommandBuilder<C>.() -> Unit
): MutableCommandBuilder<C> =
copy(literal, description, lambda).register()
/**
* Set the value for a certain [CommandMeta.Key] in the command meta storage for this builder
*
@ -414,12 +500,28 @@ public class MutableCommandBuilder<C : Any> {
* @return this mutable builder
* @since 1.3.0
*/
@Suppress("DEPRECATION")
@Deprecated(message = "ArgumentDescription should be used over Description", level = DeprecationLevel.HIDDEN)
public fun argument(
argument: CommandArgument<C, *>,
description: Description = Description.empty()
): MutableCommandBuilder<C> =
mutate { it.argument(argument, description) }
/**
* Add a new argument to this command
*
* @param argument argument to add
* @param description description of the argument
* @return this mutable builder
* @since 1.4.0
*/
public fun argument(
argument: CommandArgument<C, *>,
description: ArgumentDescription = ArgumentDescription.empty()
): MutableCommandBuilder<C> =
mutate { it.argument(argument, description) }
/**
* Add a new argument to this command
*
@ -428,12 +530,28 @@ public class MutableCommandBuilder<C : Any> {
* @return this mutable builder
* @since 1.3.0
*/
@Suppress("DEPRECATION")
@Deprecated(message = "ArgumentDescription should be used over Description", level = DeprecationLevel.HIDDEN)
public fun argument(
argument: CommandArgument.Builder<C, *>,
description: Description = Description.empty()
): MutableCommandBuilder<C> =
mutate { it.argument(argument, description) }
/**
* Add a new argument to this command
*
* @param argument argument to add
* @param description description of the argument
* @return this mutable builder
* @since 1.4.0
*/
public fun argument(
argument: CommandArgument.Builder<C, *>,
description: ArgumentDescription = ArgumentDescription.empty()
): MutableCommandBuilder<C> =
mutate { it.argument(argument, description) }
/**
* Add a new argument to this command
*
@ -442,12 +560,28 @@ public class MutableCommandBuilder<C : Any> {
* @return this mutable builder
* @since 1.3.0
*/
@Suppress("DEPRECATION")
@Deprecated(message = "ArgumentDescription should be used over Description", level = DeprecationLevel.HIDDEN)
public fun argument(
description: Description = Description.empty(),
argumentSupplier: () -> CommandArgument<C, *>
): MutableCommandBuilder<C> =
mutate { it.argument(argumentSupplier(), description) }
/**
* Add a new argument to this command
*
* @param description description of the argument
* @param argumentSupplier supplier of the argument to add
* @return this mutable builder
* @since 1.4.0
*/
public fun argument(
description: ArgumentDescription = ArgumentDescription.empty(),
argumentSupplier: () -> CommandArgument<C, *>
): MutableCommandBuilder<C> =
mutate { it.argument(argumentSupplier(), description) }
/**
* Add a new literal argument to this command
*
@ -457,6 +591,8 @@ public class MutableCommandBuilder<C : Any> {
* @return this mutable builder
* @since 1.3.0
*/
@Suppress("DEPRECATION")
@Deprecated(message = "ArgumentDescription should be used over Description", level = DeprecationLevel.HIDDEN)
public fun literal(
name: String,
description: Description = Description.empty(),
@ -464,6 +600,22 @@ public class MutableCommandBuilder<C : Any> {
): MutableCommandBuilder<C> =
mutate { it.literal(name, description, *aliases) }
/**
* Add a new literal argument to this command
*
* @param name main argument name
* @param description literal description
* @param aliases argument aliases
* @return this mutable builder
* @since 1.4.0
*/
public fun literal(
name: String,
description: ArgumentDescription = ArgumentDescription.empty(),
vararg aliases: String
): MutableCommandBuilder<C> =
mutate { it.literal(name, description, *aliases) }
/**
* Set the [CommandExecutionHandler] for this builder
*
@ -489,7 +641,7 @@ public class MutableCommandBuilder<C : Any> {
public fun flag(
name: String,
aliases: Array<String> = emptyArray(),
description: Description = Description.empty(),
description: ArgumentDescription = ArgumentDescription.empty(),
argumentSupplier: () -> CommandArgument<C, *>
): MutableCommandBuilder<C> = mutate {
it.flag(
@ -514,7 +666,7 @@ public class MutableCommandBuilder<C : Any> {
public fun flag(
name: String,
aliases: Array<String> = emptyArray(),
description: Description = Description.empty(),
description: ArgumentDescription = ArgumentDescription.empty(),
argument: CommandArgument<C, *>
): MutableCommandBuilder<C> = mutate {
it.flag(
@ -539,7 +691,7 @@ public class MutableCommandBuilder<C : Any> {
public fun flag(
name: String,
aliases: Array<String> = emptyArray(),
description: Description = Description.empty(),
description: ArgumentDescription = ArgumentDescription.empty(),
argumentBuilder: CommandArgument.Builder<C, *>
): MutableCommandBuilder<C> = mutate {
it.flag(
@ -563,7 +715,7 @@ public class MutableCommandBuilder<C : Any> {
public fun flag(
name: String,
aliases: Array<String> = emptyArray(),
description: Description = Description.empty(),
description: ArgumentDescription = ArgumentDescription.empty(),
): MutableCommandBuilder<C> = mutate {
it.flag(
this.commandManager.flagBuilder(name)

View file

@ -23,6 +23,7 @@
//
package cloud.commandframework.kotlin.extension
import cloud.commandframework.ArgumentDescription
import cloud.commandframework.Command
import cloud.commandframework.CommandManager
import cloud.commandframework.Description
@ -38,6 +39,8 @@ import kotlin.reflect.KClass
* @param lambda receiver lambda which will be invoked on the new builder
* @since 1.3.0
*/
@Suppress("DEPRECATION")
@Deprecated(message = "ArgumentDescription should be used over Description", level = DeprecationLevel.HIDDEN)
public fun <C : Any> CommandManager<C>.commandBuilder(
name: String,
description: Description = Description.empty(),
@ -46,6 +49,23 @@ public fun <C : Any> CommandManager<C>.commandBuilder(
): MutableCommandBuilder<C> =
MutableCommandBuilder(name, description, aliases, this, lambda)
/**
* Create a new [MutableCommandBuilder] and invoke the provided receiver lambda on it
*
* @param name name for the root command node
* @param description description for the root command node
* @param aliases aliases for the root command node
* @param lambda receiver lambda which will be invoked on the new builder
* @since 1.4.0
*/
public fun <C : Any> CommandManager<C>.commandBuilder(
name: String,
description: ArgumentDescription = ArgumentDescription.empty(),
aliases: Array<String> = emptyArray(),
lambda: MutableCommandBuilder<C>.() -> Unit
): MutableCommandBuilder<C> =
MutableCommandBuilder(name, description, aliases, this, lambda)
/**
* Create a new [MutableCommandBuilder] which will invoke the provided receiver lambda, and then register itself with the
* owning [CommandManager]
@ -56,6 +76,8 @@ public fun <C : Any> CommandManager<C>.commandBuilder(
* @param lambda receiver lambda which will be invoked on the new builder
* @since 1.3.0
*/
@Suppress("DEPRECATION")
@Deprecated(message = "ArgumentDescription should be used over Description", level = DeprecationLevel.HIDDEN)
public fun <C : Any> CommandManager<C>.buildAndRegister(
name: String,
description: Description = Description.empty(),
@ -64,6 +86,24 @@ public fun <C : Any> CommandManager<C>.buildAndRegister(
): MutableCommandBuilder<C> =
commandBuilder(name, description, aliases, lambda).register()
/**
* Create a new [MutableCommandBuilder] which will invoke the provided receiver lambda, and then register itself with the
* owning [CommandManager]
*
* @param name name for the root command node
* @param description description for the root command node
* @param aliases aliases for the root command node
* @param lambda receiver lambda which will be invoked on the new builder
* @since 1.4.0
*/
public fun <C : Any> CommandManager<C>.buildAndRegister(
name: String,
description: ArgumentDescription = ArgumentDescription.empty(),
aliases: Array<String> = emptyArray(),
lambda: MutableCommandBuilder<C>.() -> Unit
): MutableCommandBuilder<C> =
commandBuilder(name, description, aliases, lambda).register()
/**
* Build the provided [MutableCommandBuilder]s into [Command]s, and then register them with the command manager
*
@ -98,7 +138,24 @@ public fun <C : Any> Command.Builder<C>.senderType(type: KClass<out C>): Command
* @return the description
* @since 1.3.0
*/
@Suppress("DEPRECATION")
@Deprecated(
message = "Use interface variant that allows for rich text",
replaceWith = ReplaceWith("argumentDescription(description)")
)
public fun description(
description: String = ""
): Description =
if (description.isEmpty()) Description.empty() else Description.of(description)
/**
* Get a [ArgumentDescription], defaulting to [ArgumentDescription.empty]
*
* @param description description string
* @return the description
* @since 1.4.0
*/
public fun argumentDescription(
description: String = ""
): ArgumentDescription =
if (description.isEmpty()) ArgumentDescription.empty() else ArgumentDescription.of(description)

View file

@ -27,10 +27,7 @@ import cloud.commandframework.CommandManager
import cloud.commandframework.arguments.standard.StringArgument
import cloud.commandframework.execution.CommandExecutionCoordinator
import cloud.commandframework.internal.CommandRegistrationHandler
import cloud.commandframework.kotlin.extension.buildAndRegister
import cloud.commandframework.kotlin.extension.command
import cloud.commandframework.kotlin.extension.commandBuilder
import cloud.commandframework.kotlin.extension.description
import cloud.commandframework.kotlin.extension.*
import cloud.commandframework.meta.CommandMeta
import cloud.commandframework.meta.SimpleCommandMeta
import org.junit.jupiter.api.Assertions
@ -48,7 +45,7 @@ class CommandBuildingDSLTest {
senderType<SpecificCommandSender>()
literal("dsl")
argument(description("An amazing command argument")) {
argument(argumentDescription("An amazing command argument")) {
StringArgument.of("moment")
}
handler {