Skip to content

Commit

Permalink
multi-args #33
Browse files Browse the repository at this point in the history
  • Loading branch information
Matt-MX committed Jun 28, 2024
1 parent ad9870f commit 7e5690b
Show file tree
Hide file tree
Showing 4 changed files with 23 additions and 8 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,6 @@ import com.mattmx.ktgui.commands.declarative.arg.Argument
import com.mattmx.ktgui.commands.declarative.arg.impl.MultiArgument
import com.mattmx.ktgui.commands.declarative.invocation.RunnableCommandContext
import com.mattmx.ktgui.utils.JavaCompatibility
import org.bukkit.block.data.type.Chain
import org.bukkit.command.CommandSender

class ChainCommandBuilder(val name: String) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,10 @@ package com.mattmx.ktgui.commands.declarative.arg

import com.google.gson.JsonParser
import com.mattmx.ktgui.commands.declarative.arg.impl.*
import com.mattmx.ktgui.commands.declarative.div
import com.mattmx.ktgui.commands.declarative.invoke
import com.mattmx.ktgui.utils.not
import org.bukkit.entity.Player
import kotlin.math.min

class ArgumentProcessor(
Expand Down
Original file line number Diff line number Diff line change
@@ -1,14 +1,24 @@
package com.mattmx.ktgui.commands.declarative.arg.impl

import com.mattmx.ktgui.commands.declarative.DeclarativeCommandBuilder
import com.mattmx.ktgui.commands.declarative.arg.Argument
import com.mattmx.ktgui.commands.declarative.arg.consumer.ArgumentConsumer
import com.mattmx.ktgui.commands.declarative.arg.consumer.SingleArgumentConsumer
import com.mattmx.ktgui.commands.declarative.arg.suggests
import com.mattmx.ktgui.commands.declarative.invocation.BaseCommandContext

class MultiChoiceArgument<T : Any>(
name: String,
typeName: String,
consumer: ArgumentConsumer
) : Argument<T>(name, typeName, consumer) {

initialChoices: HashMap<String, T>
) : Argument<T>(name, "multi-choice", SingleArgumentConsumer()) {
private val choices = initialChoices

init {
suggests { choices.keys.toList() }
}

override fun getValueOfString(
cmd: DeclarativeCommandBuilder,
context: BaseCommandContext<*>,
stringValue: String?
) = choices[stringValue]
}
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,6 @@ package com.mattmx.ktgui.commands.declarative.arg.impl
import com.mattmx.ktgui.commands.declarative.arg.Argument
import com.mattmx.ktgui.commands.declarative.arg.consumer.GreedyArgumentConsumer
import com.mattmx.ktgui.commands.declarative.arg.consumer.SingleArgumentConsumer
import net.kyori.adventure.bossbar.BossBar.Flag
import kotlin.properties.ReadOnlyProperty
import kotlin.reflect.KProperty

Expand Down Expand Up @@ -57,4 +56,7 @@ inline fun <reified E : Enum<E>> enumArgument(type: String = E::class.java.simpl
fun flag() = delegateArgument(FlagArgument(DELEGATED_ARG_NAME))

inline fun <reified T : Any> optionArgument(type: String = T::class.java.simpleName) =
delegateArgument(OptionArgument<T>(DELEGATED_ARG_NAME, type, SingleArgumentConsumer()))
delegateArgument(OptionArgument<T>(DELEGATED_ARG_NAME, type, SingleArgumentConsumer()))

fun <T : Any> multiChoiceArgument(vararg choices: Pair<String, T>) =
delegateArgument(MultiChoiceArgument(DELEGATED_ARG_NAME, hashMapOf(*choices)))

0 comments on commit 7e5690b

Please sign in to comment.