Skip to content

Commit

Permalink
feat(commands): Command aliases
Browse files Browse the repository at this point in the history
fix(commands): Accidentally mark arguments as nullable after suggests call
feat(commands): Add Args class for simplicity
  • Loading branch information
0ffz committed Aug 24, 2024
1 parent 3c11fa6 commit ecc9da6
Show file tree
Hide file tree
Showing 3 changed files with 60 additions and 5 deletions.
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
package com.mineinabyss.idofront.commands.brigadier

import com.mojang.brigadier.arguments.*
import io.papermc.paper.command.brigadier.argument.ArgumentTypes

object Args {
fun word() = StringArgumentType.word()
fun string() = StringArgumentType.string()
fun greedyString() = StringArgumentType.greedyString()

fun bool() = BoolArgumentType.bool()

fun double(min: Double = Double.MIN_VALUE, max: Double = Double.MAX_VALUE) =
DoubleArgumentType.doubleArg(min, max)

fun float(min: Float = Float.MIN_VALUE, max: Float = Float.MAX_VALUE) =
FloatArgumentType.floatArg(min, max)

fun integer(min: Int = Int.MIN_VALUE, max: Int = Int.MAX_VALUE) =
IntegerArgumentType.integer(min, max)


fun long(min: Long = Long.MIN_VALUE, max: Long = Long.MAX_VALUE) =
LongArgumentType.longArg(min, max)
}

typealias ArgsMinecraft = ArgumentTypes
Original file line number Diff line number Diff line change
Expand Up @@ -3,13 +3,14 @@ package com.mineinabyss.idofront.commands.brigadier
import com.github.shynixn.mccoroutine.bukkit.asyncDispatcher
import com.mineinabyss.idofront.commands.execution.CommandExecutionFailedException
import com.mineinabyss.idofront.textcomponents.miniMsg
import com.mojang.brigadier.arguments.ArgumentType
import com.mojang.brigadier.arguments.*
import com.mojang.brigadier.builder.ArgumentBuilder
import com.mojang.brigadier.builder.LiteralArgumentBuilder
import com.mojang.brigadier.suggestion.SuggestionProvider
import com.mojang.brigadier.tree.LiteralCommandNode
import io.papermc.paper.command.brigadier.CommandSourceStack
import io.papermc.paper.command.brigadier.Commands
import io.papermc.paper.command.brigadier.argument.ArgumentTypes
import kotlinx.coroutines.CoroutineScope
import kotlinx.coroutines.async
import kotlinx.coroutines.future.asCompletableFuture
Expand Down Expand Up @@ -39,7 +40,7 @@ open class IdoCommand(
return IdoArgument(property.name)
}

operator fun <T> IdoArgumentBuilder<T>.provideDelegate(thisRef: Any?, property: KProperty<*>): IdoArgument<T?> {
operator fun <T> IdoArgumentBuilder<T>.provideDelegate(thisRef: Any?, property: KProperty<*>): IdoArgument<T> {
add(RenderStep.Builder(Commands.argument(property.name, type).apply {
if (this@provideDelegate.suggestions != null)
suggests { context, builder ->
Expand All @@ -57,11 +58,22 @@ open class IdoCommand(
add(RenderStep.Command(IdoCommand(Commands.literal(this), this, plugin).apply(init)))
}

inline operator fun List<String>.invoke(init: IdoCommand.() -> Unit) {
forEach { it.invoke { init() } }
}

operator fun String.div(other: String) = listOf(this, other)
operator fun List<String>.div(other: String) = this + other

/** Specifies a predicate for the command to execute further, may be calculated more than once. */
inline fun requires(crossinline init: CommandSourceStack.() -> Boolean) = edit {
requires { init(it) }
}

fun requiresPermission(permission: String) = requires {
sender.hasPermission("$permission.*") || sender.hasPermission(permission)
}

/** Specifies an end node for the command that runs something, only one executes block can run per command execution. */
inline fun executes(crossinline run: IdoCommandContext.() -> Unit) = edit {
executes { context ->
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -9,14 +9,30 @@ data class IdoSuggestionsContext(
val context: CommandContext<CommandSourceStack>,
val suggestions: SuggestionsBuilder,
) {
/** The argument currently being typed*/
val argument get() = suggestions.remaining

/** The full input string */
val input get() = suggestions.input

/** Add a suggestion. */
fun suggest(name: String) {
suggestions.suggest(name)
}

/** Add a list of suggestions. */
fun suggest(list: List<String>) {
list.forEach { suggest(it) }
}

/** Add a suggestion, filtering it as the user types. */
fun suggestFiltering(name: String) {
if (name.startsWith(suggestions.remaining, ignoreCase = true))
suggestions.suggest(name)
if (name.startsWith(argument, ignoreCase = true))
suggest(name)
}

/** Add a list of suggestions, filtering them as the user types. */
fun suggest(list: List<String>) {
fun suggestFiltering(list: List<String>) {
list.forEach { suggestFiltering(it) }
}
}

0 comments on commit ecc9da6

Please sign in to comment.