Skip to content

Commit

Permalink
Revert "Resolve merge conflicts hopefully"
Browse files Browse the repository at this point in the history
This reverts commit dfd9341, reversing
changes made to 3578a8a.
  • Loading branch information
Matt-MX committed Jun 28, 2024
1 parent dfd9341 commit d32afc5
Show file tree
Hide file tree
Showing 77 changed files with 444 additions and 1,942 deletions.
1 change: 1 addition & 0 deletions .idea/gradle.xml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

4 changes: 0 additions & 4 deletions .idea/misc.xml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

15 changes: 0 additions & 15 deletions .idea/modules/plugin/ktgui.plugin.test.iml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 0 additions & 2 deletions api/.gradle/caches/paperweight/taskCache/reobfJar.log

This file was deleted.

26 changes: 0 additions & 26 deletions api/build.gradle.kts
Original file line number Diff line number Diff line change
Expand Up @@ -4,26 +4,11 @@ plugins {
`maven-publish`
}

<<<<<<< HEAD
dependencies {
compileOnly(libs.paper.api)
compileOnly(libs.placeholder.api)
implementation(libs.kotlin.reflect)
compileOnly(libs.kotlin.stdlib)
=======
val paper_version: String by rootProject

repositories {
mavenCentral()
}

dependencies {
// compileOnly(kotlin("reflect"))
shadow(implementation(kotlin("reflect"))!!)
// implementation("org.jetbrains.kotlin:kotlin-stdlib-jdk7:1.7.10")

paperweight.paperDevBundle(paper_version)
>>>>>>> fc760191aa5090e9dac6c3014739a12dc7fc5dfb
}

tasks.test {
Expand All @@ -34,13 +19,8 @@ version = rootProject.version

sourceSets["main"].resources.srcDir("src/resources/")

<<<<<<< HEAD
kotlin {
jvmToolchain(JavaVersion.VERSION_17.ordinal)
=======
tasks.withType<KotlinCompile> {
kotlinOptions.jvmTarget = "21"
>>>>>>> fc760191aa5090e9dac6c3014739a12dc7fc5dfb
}

tasks {
Expand All @@ -49,18 +29,12 @@ tasks {
}
shadowJar {
mergeServiceFiles()
<<<<<<< HEAD
// exclude {
//// it.path.startsWith("kotlin") && !it.path.contains("reactive")
// it.name.startsWith("kotlin")
// }
archiveBaseName.set("ktgui")
mergeServiceFiles()
=======
}
assemble {
dependsOn(reobfJar)
>>>>>>> fc760191aa5090e9dac6c3014739a12dc7fc5dfb
}
}

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

enum class ArgumentType {
REQUIRED_SINGLE,
OPTIONAL_SINGLE,
GREEDY
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
package com.mattmx.ktgui.commands.alpha

class CommandSender
Original file line number Diff line number Diff line change
@@ -0,0 +1,118 @@
package com.mattmx.ktgui.commands.alpha

import kotlin.properties.ReadOnlyProperty
import kotlin.reflect.KProperty

open class KtCommandBuilder<S : CommandSender>(val name: String) {
val aliases = arrayOf<String>()
val subcommands = arrayOf<KtCommandBuilder<*>>()
var expectedArguments = arrayOf<Argument<S, *, *>>()
private lateinit var permission: (CommandContext<S>) -> Boolean
private lateinit var runs: (CommandContext<S>) -> Unit

fun permission(block: CommandContext<S>.() -> Boolean) = apply { permission = block }
fun runs(block: CommandContext<S>.() -> Unit) = apply { runs = block }
open fun register() {

}

fun getSuggestions(context: CommandContext<S>): List<String> {
val currentArgument = getCurrentArgument(context)
val suggestions = currentArgument?.suggests?.invoke(context) ?: return emptyList()
val lastArgument = context.args.lastOrNull() ?: ""
return suggestions.filter { suggestion -> suggestion.startsWith(lastArgument, true) }.toList()
}

/**
* Used to find the current argument, presuming we are on the current sub-command
*
* @param args the current arguments of the command invocation
* @param sender command sender
* @return the current argument or null if it is invalid
*/
private fun getCurrentArgument(context: CommandContext<S>): Argument<S, *, *>? {
if (expectedArguments.isEmpty()) return null
// Greedy arguments will eat the rest of the arguments
if (expectedArguments.first().type == ArgumentType.GREEDY) return expectedArguments.first()

repeat(expectedArguments.size) { argIndex ->
val registered = expectedArguments.getOrNull(argIndex) ?: return null
val comparing = context.args.getOrNull(argIndex) ?: return null
// todo need to think about optional args!!! wtf do we do there
}

return null
}

/**
* Used to find the current sub-command
*
* @param args of the current command invocation
* @param sender command sender
* @return the current sub-command or null if it is invalid
*/
private fun getCurrentSubcommand(context: CommandContext<S>): KtCommandBuilder<*>? {
if (subcommands.isEmpty()) return null
val firstArg = context.args.getOrNull(0)
return subcommands.firstOrNull { it.name == firstArg || it.aliases.contains(firstArg) }
}

private lateinit var context: CommandContext<S>

operator fun <T, V> Argument<S, T, V>.provideDelegate(
thisRef: Any?,
property: KProperty<*>,
): ReadOnlyProperty<T, V> {
id = property.name

println("invoked eooeeo")

return ReadOnlyProperty { thisRef, property -> context.getValue(this) as V }
}

operator fun invoke(context: CommandContext<S>) {
// save values of args
this.context = context
val values = expectedArguments.map { it.id to it.getValue(context) }
runs.invoke(context.withValues(values))
}

/**
* Builds a usage for this command.
* You can create your own method with the [Configuration] class.
*
* @return a formatted string for usage of the command
*/
fun getUsage(showDescriptions: Boolean = false, maxArgumentOptionsDisplayed: Int = 5): String {
var builder = "$name "
if (subcommands.isNotEmpty())
builder += subcommands.joinToString("|") { subcommand -> subcommand.name }
else {
var end = ""
builder += expectedArguments.joinToString(" ") { arg ->
val suggestions = arg.getDefaultSuggestions()?.let {
if (it.size <= maxArgumentOptionsDisplayed) " = [" + it.joinToString("|") + "]"
else " = [...]"
} ?: ""

// Apply descriptions
if (showDescriptions && arg.description() != null) {
val extra = when (arg.type) {
ArgumentType.REQUIRED_SINGLE -> "(Required)"
ArgumentType.OPTIONAL_SINGLE -> "(Optional)"
else -> "(Sentence)"
}
end += "\n> ${arg.id} - ${arg.description()} $extra"
}

when (arg.type) {
ArgumentType.REQUIRED_SINGLE -> "<${arg.id}!$suggestions>"
ArgumentType.OPTIONAL_SINGLE -> "<${arg.id}?$suggestions>"
else -> "<${arg.id}...>"
}
}
builder += end
}
return builder
}
}
Original file line number Diff line number Diff line change
@@ -1,60 +1,46 @@
package com.mattmx.ktgui.commands.declarative

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) {
val arguments = arrayListOf<Argument<*>>()
val subcommands = arrayListOf<DeclarativeCommandBuilder>()
val subcommands = arrayListOf<DeclarativeCommandBuilder<*>>()

@JavaCompatibility
fun argument(argument: Argument<*>) = apply {
arguments.add(argument)
}

@JavaCompatibility
fun subcommand(vararg command: DeclarativeCommandBuilder) = apply {
fun subcommand(vararg command: DeclarativeCommandBuilder<*>) = apply {
subcommands.addAll(command)
}

inline infix fun <reified S : CommandSender> runs(noinline block: RunnableCommandContext<S>.() -> Unit) = build().runs(block)

fun build() = build(DeclarativeCommandBuilder(name))

fun <T : DeclarativeCommandBuilder> build(existing: T) = existing.apply {
inline fun <reified T : CommandSender> build() = DeclarativeCommandBuilder(name, T::class.java).apply {
this.expectedArguments += arguments
this.subcommands += this@ChainCommandBuilder.subcommands
}
}

inline operator fun ChainCommandBuilder.invoke(block: DeclarativeCommandBuilder.() -> Unit) =
build().apply(block)

val ChainCommandBuilder.command
get() = build()
inline operator fun <reified T : CommandSender> ChainCommandBuilder.invoke(block: DeclarativeCommandBuilder<T>.() -> Unit) =
build<T>().apply(block)

operator fun String.div(argument: Argument<*>) = ChainCommandBuilder(this).apply {
arguments.add(argument)
}

@JvmName("div1")
operator fun String.div(argument: List<Argument<*>>) = ChainCommandBuilder(this).apply {
arguments.add(MultiArgument("multi-argument", *argument.toTypedArray()))
}

operator fun String.div(s: String) = ChainCommandBuilder(this).apply {
subcommands.add(DeclarativeCommandBuilder(s))
arguments.addAll(argument)
}

operator fun String.div(subs: List<DeclarativeCommandBuilder>) = ChainCommandBuilder(this).apply {
operator fun String.div(subs: List<DeclarativeCommandBuilder<*>>) = ChainCommandBuilder(this).apply {
subcommands.addAll(subs)
}

operator fun DeclarativeCommandBuilder.plus(other: DeclarativeCommandBuilder) = listOf(this, other)
operator fun DeclarativeCommandBuilder<*>.plus(other: DeclarativeCommandBuilder<*>) = listOf(this, other)

operator fun ChainCommandBuilder.div(argument: Argument<*>) = this.apply {
arguments.add(argument)
Expand Down
Loading

0 comments on commit d32afc5

Please sign in to comment.