Skip to content

Commit

Permalink
impl docs
Browse files Browse the repository at this point in the history
  • Loading branch information
Matt-MX committed May 20, 2024
1 parent 31d57df commit d828615
Show file tree
Hide file tree
Showing 40 changed files with 557 additions and 402 deletions.
32 changes: 0 additions & 32 deletions api/src/main/kotlin/com/mattmx/ktgui/commands/alpha/Argument.kt

This file was deleted.

This file was deleted.

This file was deleted.

This file was deleted.

This file was deleted.

34 changes: 0 additions & 34 deletions api/src/main/kotlin/com/mattmx/ktgui/commands/alpha/tests.kt

This file was deleted.

Original file line number Diff line number Diff line change
@@ -1,9 +1,8 @@
package com.mattmx.ktgui.commands.declarative

import com.mattmx.ktgui.commands.declarative.arg.Argument
import com.mattmx.ktgui.commands.declarative.arg.MultiArgument
import com.mattmx.ktgui.commands.declarative.arg.impl.MultiArgument
import com.mattmx.ktgui.utils.JavaCompatibility
import org.bukkit.command.CommandSender

class ChainCommandBuilder(val name: String) {
val arguments = arrayListOf<Argument<*>>()
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -9,12 +9,15 @@ import com.mattmx.ktgui.commands.declarative.invocation.*
import com.mattmx.ktgui.commands.declarative.syntax.*
import com.mattmx.ktgui.commands.suggestions.CommandSuggestion
import com.mattmx.ktgui.commands.usage.CommandUsageOptions
import com.mattmx.ktgui.cooldown.ActionCoolDown
import com.mattmx.ktgui.utils.JavaCompatibility
import com.mattmx.ktgui.utils.not
import com.mattmx.ktgui.utils.pretty
import org.bukkit.Bukkit
import org.bukkit.command.CommandSender
import org.bukkit.permissions.Permission
import org.bukkit.permissions.PermissionDefault
import java.time.Duration
import java.util.*
import java.util.function.Consumer

Expand All @@ -26,6 +29,10 @@ open class DeclarativeCommandBuilder(
var subcommands = setOf<DeclarativeCommandBuilder>()
var expectedArguments = arrayOf<Argument<*>>()
var localArgumentSuggestions = hashMapOf<String, CommandSuggestion<*>>()
var coolDown = Optional.empty<ActionCoolDown<CommandSender>>()
private set
var coolDownCallback = Optional.empty<(StorageCommandContext<*>) -> Unit>()
private set
var buildAutomaticPermissions = Optional.empty<String>()
private set
var permission: Optional<(StorageCommandContext<*>) -> Boolean> = Optional.empty()
Expand All @@ -46,6 +53,11 @@ open class DeclarativeCommandBuilder(
var unknownCommand: Optional<(StorageCommandContext<*>) -> Unit> = Optional.empty()
private set

fun cooldown(duration: Duration, block: (StorageCommandContext<*>.() -> Unit)? = null) = apply {
this.coolDown = Optional.of(ActionCoolDown(duration))
this.coolDownCallback = Optional.ofNullable(block)
}

infix fun permission(block: StorageCommandContext<*>.() -> Boolean) = apply {
this.permission = Optional.of(block)
}
Expand All @@ -62,8 +74,8 @@ open class DeclarativeCommandBuilder(
this.unknownCommand = Optional.of(block)
}

infix fun buildAutomaticPermissions(root: String) = apply {
this.buildAutomaticPermissions = Optional.of(root)
infix fun buildAutomaticPermissions(root: String?) = apply {
this.buildAutomaticPermissions = Optional.ofNullable(root)
}

infix fun incorrectExecutor(block: (StorageCommandContext<*>.() -> Unit)?) = apply {
Expand All @@ -86,6 +98,22 @@ open class DeclarativeCommandBuilder(
this.runs[senderClass] = block as (RunnableCommandContext<*>) -> Unit
}

inline fun <reified T : CommandSender> runs(
vararg argsProvided: Argument<*>,
noinline block: RunnableCommandContext<T>.() -> Unit
) = apply {
runs(T::class.javaObjectType, *argsProvided, block = block)
}

fun <T : CommandSender> runs(
senderClass: Class<T>,
vararg argsProvided: Argument<*>,
block: RunnableCommandContext<T>.() -> Unit
) = apply {
TODO("Not yet implemented, use runs without argsProvided")
this.runs[senderClass] = block as (RunnableCommandContext<*>) -> Unit
}

@JavaCompatibility
fun <T : CommandSender> runs(senderClass: Class<T>, block: Consumer<RunnableCommandContext<T>>) = apply {
runs(senderClass) {
Expand Down Expand Up @@ -167,10 +195,20 @@ open class DeclarativeCommandBuilder(
.map { listOf(it.name) + it.aliases }
val cmdsList = cmds.flatten()

val list = context.rawArgs.toMutableList()
var suggestedArgs: List<String>? = null
for (arg in expectedArguments) {
val consumed = arg.consumer.consume(list)
list.removeAll(consumed)

if (list.isEmpty()) {
// This is the last argument
suggestedArgs = getSuggestions(arg)?.getLastArgSuggestion(context)
}
}

val arg = expectedArguments.getOrNull(context.rawArgs.size - 1)
val suggestedArgs = getSuggestions(arg)?.getLastArgSuggestion(context)
// val arg = expectedArguments.getOrNull(context.rawArgs.size - 1)
// val suggestedArgs = getSuggestions(arg)?.getLastArgSuggestion(context)

return if (suggestedArgs != null) {
cmdsList + suggestedArgs
Expand Down Expand Up @@ -223,6 +261,17 @@ open class DeclarativeCommandBuilder(
return
}

if (coolDown.isPresent && !coolDown.get().test(context.sender)) {
coolDownCallback.ifPresentOrElse({ it.invoke(context) }) {
context.reply(
!"&cPlease wait ${
coolDown.get().timeRemaining(context.sender).pretty()
} before running the command again."
)
}
return
}

val argumentValues = hashMapOf<String, ArgumentContext<*>>()
var expectedArgumentIndex = 0
var providedArgumentIndex = 0
Expand All @@ -242,7 +291,8 @@ open class DeclarativeCommandBuilder(
val expectedArg = expectedArguments.getOrNull(expectedArgumentIndex)
if (expectedArg != null) {
// Get full argument string using consumer
val consumed = expectedArg.consumer.consume(context.rawArgs.subList(providedArgumentIndex, context.rawArgs.size))
val consumed =
expectedArg.consumer.consume(context.rawArgs.subList(providedArgumentIndex, context.rawArgs.size))
providedArgumentIndex += consumed.size

// If the value is empty and IS required
Expand Down Expand Up @@ -324,7 +374,8 @@ open class DeclarativeCommandBuilder(
val executor =
runs.entries.firstOrNull { (clazz, _) ->
clazz.isAssignableFrom(context.sender.javaClass)
} ?: return context.reply(!"&cThis command can only be ran by ${runs.values.joinToString("/") { "${it.javaClass.simpleName}s" }}.")
}
?: return context.reply(!"&cThis command can only be ran by ${runs.values.joinToString("/") { "${it.javaClass.simpleName}s" }}.")

executor.value.invoke(runnableContext)
}
Expand Down Expand Up @@ -412,4 +463,7 @@ inline operator fun String.invoke(block: DeclarativeCommandBuilder.() -> Unit) =
DeclarativeCommandBuilder(this).apply(block)

inline fun command(name: String, block: DeclarativeCommandBuilder.() -> Unit) =
DeclarativeCommandBuilder(name).apply(block)
DeclarativeCommandBuilder(name).apply(block)

@JavaCompatibility
fun command(name: String) = DeclarativeCommandBuilder(name)
Loading

0 comments on commit d828615

Please sign in to comment.