diff --git a/cloud-kotlin/cloud-kotlin-extensions/src/main/kotlin/cloud/commandframework/kotlin/MutableCommandBuilder.kt b/cloud-kotlin/cloud-kotlin-extensions/src/main/kotlin/cloud/commandframework/kotlin/MutableCommandBuilder.kt index 1a1d8374..bbdeb5cd 100644 --- a/cloud-kotlin/cloud-kotlin-extensions/src/main/kotlin/cloud/commandframework/kotlin/MutableCommandBuilder.kt +++ b/cloud-kotlin/cloud-kotlin-extensions/src/main/kotlin/cloud/commandframework/kotlin/MutableCommandBuilder.kt @@ -39,11 +39,22 @@ import kotlin.reflect.KClass * A mutable [Command.Builder] wrapper, providing functions to assist in creating commands using the * Kotlin builder DSL style * + * @property commandBuilder the command builder the mutate + * @property commandManager the command manager which will own this command + * @constructor Create a new [MutableCommandBuilder] * @since 1.3.0 */ -public class MutableCommandBuilder { - private val commandManager: CommandManager - private var commandBuilder: Command.Builder +public class MutableCommandBuilder( + commandBuilder: Command.Builder, + private val commandManager: CommandManager, +) { + /** + * The command builder that is being mutated by this [MutableCommandBuilder] instance. + * + * This is public so that this can be returned to a command builder for interop with java apis. + */ + public var commandBuilder: Command.Builder = commandBuilder + private set /** * Create a new [MutableCommandBuilder] @@ -64,10 +75,7 @@ public class MutableCommandBuilder { description: Description = Description.empty(), aliases: Array = emptyArray(), commandManager: CommandManager - ) { - this.commandManager = commandManager - this.commandBuilder = commandManager.commandBuilder(name, description, *aliases) - } + ) : this(commandManager.commandBuilder(name, description, *aliases), commandManager) /** * Create a new [MutableCommandBuilder] @@ -83,10 +91,7 @@ public class MutableCommandBuilder { description: ArgumentDescription = ArgumentDescription.empty(), aliases: Array = emptyArray(), commandManager: CommandManager - ) { - this.commandManager = commandManager - this.commandBuilder = commandManager.commandBuilder(name, description, *aliases) - } + ) : this(commandManager.commandBuilder(name, description, *aliases), commandManager) /** * Create a new [MutableCommandBuilder] and invoke the provided receiver lambda on it @@ -133,11 +138,6 @@ public class MutableCommandBuilder { lambda(this) } - private constructor(commandManager: CommandManager, commandBuilder: Command.Builder) { - this.commandManager = commandManager - this.commandBuilder = commandBuilder - } - /** * Build a [Command] from the current state of this builder * @@ -184,7 +184,7 @@ public class MutableCommandBuilder { * @since 1.3.0 */ public fun copy(): MutableCommandBuilder = - MutableCommandBuilder(this.commandManager, this.commandBuilder) + MutableCommandBuilder(this.commandBuilder, this.commandManager) /** * Make a new copy of this [MutableCommandBuilder] and invoke the provided receiver lambda on it diff --git a/cloud-kotlin/cloud-kotlin-extensions/src/main/kotlin/cloud/commandframework/kotlin/extension/CommandBuildingExtensions.kt b/cloud-kotlin/cloud-kotlin-extensions/src/main/kotlin/cloud/commandframework/kotlin/extension/CommandBuildingExtensions.kt index 7d265050..57b5b19b 100644 --- a/cloud-kotlin/cloud-kotlin-extensions/src/main/kotlin/cloud/commandframework/kotlin/extension/CommandBuildingExtensions.kt +++ b/cloud-kotlin/cloud-kotlin-extensions/src/main/kotlin/cloud/commandframework/kotlin/extension/CommandBuildingExtensions.kt @@ -129,6 +129,28 @@ public fun CommandManager.command( public fun Command.Builder.senderType(type: KClass): Command.Builder = senderType(type.java) +/** + * Create a new [MutableCommandBuilder]. + * + * @param commandManager the command manager, which will own this command. + * @since 1.7.0 + */ +public fun Command.Builder.toMutable( + commandManager: CommandManager +): MutableCommandBuilder = MutableCommandBuilder(this, commandManager) + +/** + * Create a new [MutableCommandBuilder] and invoke the provided receiver lambda on it. + * + * @param commandManager the command manager, which will own this command. + * @param lambda receiver lambda, which will be invoked on the new builder. + * @since 1.7.0 + */ +public fun Command.Builder.mutate( + commandManager: CommandManager, + lambda: MutableCommandBuilder.() -> Unit +): MutableCommandBuilder = MutableCommandBuilder(this, commandManager).also(lambda) + /** * Get a [Description], defaulting to [Description.empty] *