Skip to content

Commit

Permalink
changing declarative arg processing behaviour
Browse files Browse the repository at this point in the history
  • Loading branch information
Matt-MX committed Jul 2, 2024
1 parent ac293c9 commit 2db9e3c
Show file tree
Hide file tree
Showing 25 changed files with 73 additions and 86 deletions.
2 changes: 1 addition & 1 deletion api/.gradle/caches/paperweight/taskCache/reobfJar.log
Original file line number Diff line number Diff line change
@@ -1,2 +1,2 @@
Command: C:\Program Files\Java\jdk-17\bin\java.exe -Xmx1G -classpath C:\Users\Mangr\.gradle\caches\modules-2\files-2.1\net.fabricmc\tiny-remapper\0.10.1\c293b2384ae12af74f407fa3aaa553bba4ac6763\tiny-remapper-0.10.1-fat.jar net.fabricmc.tinyremapper.Main D:\PC\Projects\KtBukkitGui\api\build\libs\ktgui-2.4.1-dev-all.jar D:\PC\Projects\KtBukkitGui\api\build\libs\api-2.4.1.jar C:\Users\Mangr\.gradle\caches\paperweight-userdev\ff775525efc29c3503a07d1006e63e5695a742b7505cf63e157d49d32419c69f\module\io.papermc.paper\dev-bundle\1.20.4-R0.1-SNAPSHOT\paperweight\setupCache\extractDevBundle.dir\data\mojang+yarn-spigot-reobf.tiny mojang+yarn spigot C:\Users\Mangr\.gradle\caches\paperweight-userdev\ff775525efc29c3503a07d1006e63e5695a742b7505cf63e157d49d32419c69f\module\io.papermc.paper\dev-bundle\1.20.4-R0.1-SNAPSHOT\paperweight\setupCache\applyMojangMappedPaperclipPatch.jar --threads=1
Finished after 3414.54 ms.
Finished after 2886.57 ms.
Original file line number Diff line number Diff line change
Expand Up @@ -4,9 +4,6 @@ import com.mattmx.ktgui.GuiManager
import com.mattmx.ktgui.commands.declarative.arg.Argument
import com.mattmx.ktgui.commands.declarative.arg.ArgumentContext
import com.mattmx.ktgui.commands.declarative.arg.ArgumentProcessor
import com.mattmx.ktgui.commands.declarative.arg.consumer.GreedyArgumentConsumer
import com.mattmx.ktgui.commands.declarative.arg.consumer.SingleArgumentConsumer
import com.mattmx.ktgui.commands.declarative.arg.consumers.ArgumentConsumer
import com.mattmx.ktgui.commands.declarative.arg.impl.FlagArgument
import com.mattmx.ktgui.commands.declarative.arg.impl.OptionArgument
import com.mattmx.ktgui.commands.declarative.arg.impl.OptionSyntax
Expand Down Expand Up @@ -312,6 +309,7 @@ open class DeclarativeCommandBuilder(
// var providedArgumentIndex = 0

val argumentProcessor = ArgumentProcessor(this, context.rawArgs)
println(context.rawArgs)
for ((index, arg) in expectedArguments.withIndex()) {
if (argumentProcessor.done()) {
if (arg.isRequired()) {
Expand Down Expand Up @@ -342,7 +340,7 @@ open class DeclarativeCommandBuilder(
val result = arg.consumer.consume(processorClone)
val actualValue = arg.getValueOfString(this, context, result.stringValue)

if (arg.isOptional() || (result.isEmpty() && actualValue != null)) {
if (actualValue != null || arg.isOptional()) {
argumentValues[arg.name()] = arg.createContext(result.stringValue, actualValue)
argumentProcessor.pointer = processorClone.pointer
argumentProcessor.optionsAndFlagsValues = processorClone.optionsAndFlagsValues
Expand All @@ -355,6 +353,7 @@ open class DeclarativeCommandBuilder(
}

invalid.ifPresent { it.invoke(invalidArgumentContext) }
return
}
}

Expand Down
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
package com.mattmx.ktgui.commands.declarative.arg

import com.mattmx.ktgui.commands.declarative.DeclarativeCommandBuilder
import com.mattmx.ktgui.commands.declarative.arg.consumers.ArgumentConsumer
import com.mattmx.ktgui.commands.declarative.arg.impl.OptionArgument
import com.mattmx.ktgui.commands.declarative.invocation.BaseCommandContext
import com.mattmx.ktgui.commands.declarative.invocation.InvalidArgContext
import com.mattmx.ktgui.commands.declarative.invocation.RunnableCommandContext
Expand Down Expand Up @@ -117,6 +117,8 @@ open class Argument<T : Any>(
} else null
}

fun optionArgument() = OptionArgument(this)

@JavaCompatibility
fun getContext(context: RunnableCommandContext<*>) = context.getArgumentContext<T>(name());

Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,4 @@
package com.mattmx.ktgui.commands.declarative.arg.consumers

import com.mattmx.ktgui.commands.declarative.arg.ArgumentProcessor
package com.mattmx.ktgui.commands.declarative.arg

fun interface ArgumentConsumer {

Expand All @@ -12,6 +10,8 @@ fun interface ArgumentConsumer {
) {
fun isEmpty() = stringValue == null

override fun toString() = "Result('$stringValue', [${consumed.joinToString(", ")}])"

companion object {
private val EMPTY = Result(null, emptyList())
fun empty() = EMPTY
Expand All @@ -30,9 +30,9 @@ fun interface ArgumentConsumer {

@JvmStatic
infix fun untilFalse(predicate: (ArgumentProcessor, String) -> Boolean) = ArgumentConsumer { processor ->
var current = processor.next()
val startIndex = processor.pointer
var fullString = current ?: ""
var current: String? = ""
val startIndex = processor.pointer + 1
var fullString = ""

while (current != null) {
current = processor.next()
Expand All @@ -41,7 +41,8 @@ fun interface ArgumentConsumer {
return@ArgumentConsumer Result.empty()
}

fullString += " $current"
fullString += "$current "
fullString = fullString.trim()

if (!predicate(processor, fullString)) {
return@ArgumentConsumer Result(fullString, (startIndex..processor.pointer).toList())
Expand All @@ -55,9 +56,9 @@ fun interface ArgumentConsumer {

@JvmStatic
infix fun untilFalsePartial(predicate: (ArgumentProcessor, String) -> Boolean) = ArgumentConsumer { processor ->
var current = processor.next()
val startIndex = processor.pointer
var fullString = current ?: ""
var current: String? = null
val startIndex = processor.pointer + 1
var fullString = ""

while (current != null) {
current = processor.next()
Expand All @@ -66,7 +67,8 @@ fun interface ArgumentConsumer {
return@ArgumentConsumer Result(fullString, (startIndex..processor.pointer).toList())
}

fullString += " $current"
fullString += "$current "
fullString = fullString.trim()

if (!predicate(processor, current)) {
return@ArgumentConsumer Result(fullString, (startIndex..processor.pointer).toList())
Expand All @@ -79,9 +81,7 @@ fun interface ArgumentConsumer {
infix fun untilPartial(predicate: (ArgumentProcessor, String) -> Boolean) = untilFalsePartial { p, s -> !predicate(p, s) }

@JvmStatic
fun remaining() = untilPartial { processor, _ ->
processor.pointer >= processor.args.size
}
fun remaining() = untilFalse { processor, _ -> !processor.done() }

@JvmStatic
infix fun variable(amount: Int): ArgumentConsumer {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ class ArgumentProcessor(
val command: DeclarativeCommandBuilder,
val args: List<String>
) {
var pointer = 0
var pointer = -1
var optionsAndFlagsValues = hashMapOf<String, Any>()

fun peek(i: Int) = args.getOrNull(pointer + i)
Expand Down Expand Up @@ -36,7 +36,7 @@ class ArgumentProcessor(
}

fun reset() {
this.pointer = 0
this.pointer = -1
this.optionsAndFlagsValues.clear()
}

Expand All @@ -45,5 +45,5 @@ class ArgumentProcessor(
this.pointer = this@ArgumentProcessor.pointer
}

fun done() = pointer >= args.size
fun done() = pointer >= args.size - 1
}

This file was deleted.

This file was deleted.

This file was deleted.

This file was deleted.

Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
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.suggests
import com.mattmx.ktgui.commands.declarative.invocation.BaseCommandContext

class BooleanArgument(
name: String,
typeName: String
) : Argument<Boolean>(name, typeName) {

init {
suggests { listOf("true", "false") }
}

override fun getValueOfString(
cmd: DeclarativeCommandBuilder,
context: BaseCommandContext<*>,
stringValue: String?
): Boolean? {
return stringValue?.toBooleanStrictOrNull()
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -2,15 +2,14 @@ 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.ArgumentProcessor
import com.mattmx.ktgui.commands.declarative.arg.consumers.ArgumentConsumer
import com.mattmx.ktgui.commands.declarative.arg.ArgumentConsumer
import com.mattmx.ktgui.commands.declarative.invocation.BaseCommandContext

class DoubleArgument(
name: String,
typeName: String
) : Argument<Double>(name, typeName) {
var min: Double = Double.MIN_VALUE
var min: Double = -999999999999.0
var max: Double = Double.MAX_VALUE

init {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ 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.consumers.ArgumentConsumer
import com.mattmx.ktgui.commands.declarative.arg.ArgumentConsumer
import com.mattmx.ktgui.commands.declarative.invocation.BaseCommandContext
import java.util.*

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,7 @@ 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.SingleArgumentConsumer
import com.mattmx.ktgui.commands.declarative.arg.consumers.ArgumentConsumer
import com.mattmx.ktgui.commands.declarative.arg.ArgumentConsumer
import com.mattmx.ktgui.commands.declarative.invocation.BaseCommandContext

class IntArgument(
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,7 @@ 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.SingleArgumentConsumer
import com.mattmx.ktgui.commands.declarative.arg.consumers.ArgumentConsumer
import com.mattmx.ktgui.commands.declarative.arg.ArgumentConsumer
import com.mattmx.ktgui.commands.declarative.invocation.BaseCommandContext

class LongArgument(
Expand Down
Original file line number Diff line number Diff line change
@@ -1,7 +1,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.ArgumentConsumer

class MultiArgument(
name: String,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,7 @@ 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.SingleArgumentConsumer
import com.mattmx.ktgui.commands.declarative.arg.consumers.ArgumentConsumer
import com.mattmx.ktgui.commands.declarative.arg.ArgumentConsumer
import com.mattmx.ktgui.commands.declarative.arg.suggests
import com.mattmx.ktgui.commands.declarative.invocation.BaseCommandContext

Expand All @@ -14,7 +13,12 @@ class MultiChoiceArgument<T : Any>(
private val choices = initialChoices

init {
this.consumes(ArgumentConsumer.until { argumentProcessor, s -> choices.containsKey(s) })
this.consumes(
ArgumentConsumer.until { argumentProcessor, s ->
println("processing op '$s'")
choices.containsKey(s)
}
)
suggests { choices.keys.toList() }
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,6 @@ 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.SingleArgumentConsumer
import com.mattmx.ktgui.commands.declarative.invocation.BaseCommandContext
import org.bukkit.Bukkit
import org.bukkit.entity.Player
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,7 @@ 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.VariableArgumentConsumer
import com.mattmx.ktgui.commands.declarative.arg.consumers.ArgumentConsumer
import com.mattmx.ktgui.commands.declarative.arg.ArgumentConsumer
import com.mattmx.ktgui.commands.declarative.invocation.BaseCommandContext
import org.bukkit.Location
import org.bukkit.entity.Entity
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,6 @@ 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.invocation.BaseCommandContext
import java.util.*

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ 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.consumers.ArgumentConsumer
import com.mattmx.ktgui.commands.declarative.arg.ArgumentConsumer
import com.mattmx.ktgui.commands.declarative.invocation.BaseCommandContext

class StringArgument(
Expand All @@ -11,7 +11,6 @@ class StringArgument(
) : Argument<String>(name, type) {
var min: Int = 0
var max: Int = Int.MAX_VALUE
lateinit var regex: Regex
val allowed = arrayListOf<Regex>()
val disallow = arrayListOf<Regex>()

Expand Down
Original file line number Diff line number Diff line change
@@ -1,9 +1,7 @@
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 com.mattmx.ktgui.commands.declarative.arg.consumers.ArgumentConsumer
import com.mattmx.ktgui.commands.declarative.arg.ArgumentConsumer
import kotlin.properties.ReadOnlyProperty
import kotlin.reflect.KProperty

Expand All @@ -24,7 +22,10 @@ fun stringArgument(type: String = "string") =
delegateArgument(StringArgument(DELEGATED_ARG_NAME, type))

fun greedyStringArgument(type: String = "string") =
delegateArgument(StringArgument(DELEGATED_ARG_NAME, type).apply { this.consumes(ArgumentConsumer.remaining()) })
delegateArgument(StringArgument(DELEGATED_ARG_NAME, type) greedy true)

fun booleanArgument(type: String = "boolean") =
delegateArgument(BooleanArgument(DELEGATED_ARG_NAME, type))

fun intArgument(type: String = "int") =
delegateArgument(IntArgument(DELEGATED_ARG_NAME, type))
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,7 @@ class RunnableCommandContext<T : CommandSender>(

operator fun <S : Any> Argument<S>.invoke(): S {
val ctx = context
return ctx.getOrNull()!!
return ctx.getOrNull() ?: error("The argument ${name()} wasn't provided in this invocation.")
}

}
2 changes: 1 addition & 1 deletion plugin/.gradle/caches/paperweight/taskCache/reobfJar.log
Original file line number Diff line number Diff line change
@@ -1,2 +1,2 @@
Command: C:\Program Files\Java\jdk-17\bin\java.exe -Xmx1G -classpath C:\Users\Mangr\.gradle\caches\modules-2\files-2.1\net.fabricmc\tiny-remapper\0.10.1\c293b2384ae12af74f407fa3aaa553bba4ac6763\tiny-remapper-0.10.1-fat.jar net.fabricmc.tinyremapper.Main D:\PC\Projects\KtBukkitGui\plugin\build\libs\ktgui-plugin-2.4.1-dev-all.jar D:\PC\Projects\KtBukkitGui\plugin\build\libs\plugin-unspecified.jar C:\Users\Mangr\.gradle\caches\paperweight-userdev\ff775525efc29c3503a07d1006e63e5695a742b7505cf63e157d49d32419c69f\module\io.papermc.paper\dev-bundle\1.20.4-R0.1-SNAPSHOT\paperweight\setupCache\extractDevBundle.dir\data\mojang+yarn-spigot-reobf.tiny mojang+yarn spigot C:\Users\Mangr\.gradle\caches\paperweight-userdev\ff775525efc29c3503a07d1006e63e5695a742b7505cf63e157d49d32419c69f\module\io.papermc.paper\dev-bundle\1.20.4-R0.1-SNAPSHOT\paperweight\setupCache\applyMojangMappedPaperclipPatch.jar --threads=1
Finished after 2176.44 ms.
Finished after 1904.84 ms.
Original file line number Diff line number Diff line change
Expand Up @@ -5,15 +5,11 @@
import com.mattmx.ktgui.commands.declarative.DeclarativeCommandBuilder;
import com.mattmx.ktgui.commands.declarative.arg.Argument;
import com.mattmx.ktgui.commands.declarative.arg.ArgumentContext;
import com.mattmx.ktgui.commands.declarative.arg.consumer.GreedyArgumentConsumer;
import com.mattmx.ktgui.commands.declarative.arg.consumers.ArgumentConsumer;
import com.mattmx.ktgui.commands.declarative.arg.impl.OnlinePlayerArgument;
import com.mattmx.ktgui.commands.declarative.arg.impl.StringArgument;
import net.kyori.adventure.text.Component;
import org.bukkit.command.CommandSender;
import org.bukkit.entity.Player;

import static com.mattmx.ktgui.commands.declarative.DeclarativeCommandBuilderKt.command;
import static com.mattmx.ktgui.utils.ColorKt.component;

public class JavaUpdateCommandExample {
Expand Down
Loading

0 comments on commit 2db9e3c

Please sign in to comment.