1- package me.zodd
1+ package me.zodd.dsl.command
22
33import me.zodd.annotations.ScriptDsl
44import net.kyori.adventure.text.Component
55import org.spongepowered.api.command.Command
66import org.spongepowered.api.command.CommandCause
77import org.spongepowered.api.command.CommandResult
8- import org.spongepowered.api.command.parameter.CommandContext
98import org.spongepowered.api.command.parameter.Parameter
109import org.spongepowered.api.command.parameter.managed.Flag
10+ import org.spongepowered.api.command.parameter.CommandContext as SpongeContext
1111import java.util.function.Predicate
1212
1313@ScriptDsl
14- class CommandManager : DslArgument , DslContext {
15-
16- operator fun invoke (initializer : CommandManager .() -> Unit ): List <DslCommand > {
17- this .initializer()
18- RegistrationHelper .registerCommand(CommandBuilder .builtCommands)
19- return CommandBuilder .builtCommands
20- }
21-
22- fun command (name : String , initializer : CommandBuilder .(name: String ) -> Unit ): DslCommand {
23- val builder = CommandBuilder ()
24- builder.aliases + = name
25- builder.initializer(name)
26- return builder.buildCommand()
27- }
28- }
29-
30- @ScriptDsl
31- class CommandBuilder : DslArgument , DslContext {
14+ class CommandBuilder : CommandArgument , CommandContext {
3215
3316 companion object {
3417 internal val builtCommands = mutableListOf<DslCommand >()
@@ -40,7 +23,7 @@ class CommandBuilder : DslArgument, DslContext {
4023 var terminal: Boolean = false
4124 var executionRequirement: Predicate <CommandCause > = Predicate { true }
4225
43- private var commandExecutor: CommandContext .() -> CommandResult =
26+ private var commandExecutor: SpongeContext .() -> CommandResult =
4427 { error(Component .text(" Command executor not registered" )) }
4528
4629 private var parameters = mutableListOf<Parameter >()
@@ -64,7 +47,7 @@ class CommandBuilder : DslArgument, DslContext {
6447 return command
6548 }
6649
67- fun executes (exec : CommandContext .() -> CommandResult ) {
50+ fun executes (exec : SpongeContext .() -> CommandResult ) {
6851 commandExecutor = exec
6952 }
7053
@@ -88,45 +71,4 @@ class CommandBuilder : DslArgument, DslContext {
8871
8972 return command
9073 }
91- }
92-
93-
94- sealed interface DslContext {
95- infix fun <T > CommandContext.requireOne (param : Parameter .Value <T >): T = this .requireOne(param)
96-
97- fun CommandContext.success (): CommandResult = CommandResult .success()
98-
99- infix fun CommandContext.error (errorMessage : Component ): CommandResult = CommandResult .error(errorMessage)
100-
101- infix fun CommandContext.hasFlag (flag : Flag ) = this .hasFlag(flag)
102- }
103-
104- sealed interface DslFlag {
105- infix fun String.buildFlag (permission : String ): Flag {
106- return asFlag(permission).build()
107- }
108-
109- infix fun String.asFlag (permission : String ): Flag .Builder {
110- return Flag .builder().aliases(this .split(" ," )).setPermission(permission)
111- }
112- }
113-
114- sealed interface DslParameter {
115- infix fun <T > String.withType (type : Parameter .Value .Builder <T >): Parameter .Value <T > {
116- return type.key(this ).build()
117- }
118-
119- infix fun <T > Parameter.Value.Builder<T>.keyedWith (key : String ): Parameter .Value <T > {
120- return this .key(key).build()
121- }
122- }
123-
124- interface DslArgument : DslParameter , DslFlag
125-
126- data class DslCommand (
127- val aliases : List <String >,
128- val command : Command .Parameterized ,
129- ) {
130- val baseAlias = aliases[0 ]
131- val remainingAliases = aliases.filterNot { it.contentEquals(baseAlias) }.toTypedArray()
13274}
0 commit comments