From bca108bf7f3e9deacf75e0820db236dae8687f35 Mon Sep 17 00:00:00 2001 From: MattMXX Date: Wed, 22 Nov 2023 16:58:56 +0000 Subject: [PATCH] Fixing build issues --- api/build.gradle.kts | 2 +- .../ktgui/commands/declarative/Argument.kt | 3 +-- .../commands/declarative/CommandContext.kt | 25 ++++++++++++++++++- .../commands/declarative/KtCommandBuilder.kt | 19 +++++--------- .../ktgui/commands/declarative/tests.kt | 5 ++-- build.gradle.kts | 6 +---- plugin/build.gradle.kts | 20 ++++++++++----- 7 files changed, 50 insertions(+), 30 deletions(-) diff --git a/api/build.gradle.kts b/api/build.gradle.kts index 51970ac..84c4a51 100644 --- a/api/build.gradle.kts +++ b/api/build.gradle.kts @@ -6,7 +6,7 @@ plugins { } dependencies { - implementation(kotlin("reflect")) + compileOnly(kotlin("reflect")) } tasks.test { diff --git a/api/src/main/kotlin/com/mattmx/ktgui/commands/declarative/Argument.kt b/api/src/main/kotlin/com/mattmx/ktgui/commands/declarative/Argument.kt index 2232d6f..4ee79f7 100644 --- a/api/src/main/kotlin/com/mattmx/ktgui/commands/declarative/Argument.kt +++ b/api/src/main/kotlin/com/mattmx/ktgui/commands/declarative/Argument.kt @@ -27,7 +27,6 @@ open class Argument( } fun getValue(context: CommandContext): V { - println("getValue") - return getter(context) + return context.getValue(this) ?: getter(context) } } \ No newline at end of file diff --git a/api/src/main/kotlin/com/mattmx/ktgui/commands/declarative/CommandContext.kt b/api/src/main/kotlin/com/mattmx/ktgui/commands/declarative/CommandContext.kt index c565a9a..933dfa1 100644 --- a/api/src/main/kotlin/com/mattmx/ktgui/commands/declarative/CommandContext.kt +++ b/api/src/main/kotlin/com/mattmx/ktgui/commands/declarative/CommandContext.kt @@ -1,7 +1,30 @@ package com.mattmx.ktgui.commands.declarative +import kotlin.properties.ReadOnlyProperty +import kotlin.reflect.KProperty class CommandContext( val sender: S, val args: Array, -) \ No newline at end of file +) { + private lateinit var map: Map + + fun ref(name: String) = map[name] + + operator fun ReadOnlyProperty.getValue( + thisRef: T, + property: KProperty<*> + ) : V { + return map[property.name] as V + } + + fun getValue(argument: Argument) : V? { + return if (::map.isInitialized) map[argument.id] as V? else null + } + + fun clone() = CommandContext(sender, args) + + fun withValues(map: List>) = apply { + this.map = map.toMap() + } +} \ No newline at end of file diff --git a/api/src/main/kotlin/com/mattmx/ktgui/commands/declarative/KtCommandBuilder.kt b/api/src/main/kotlin/com/mattmx/ktgui/commands/declarative/KtCommandBuilder.kt index 6acf8ec..595aee5 100644 --- a/api/src/main/kotlin/com/mattmx/ktgui/commands/declarative/KtCommandBuilder.kt +++ b/api/src/main/kotlin/com/mattmx/ktgui/commands/declarative/KtCommandBuilder.kt @@ -58,12 +58,7 @@ open class KtCommandBuilder(val name: String) { return subcommands.firstOrNull { it.name == firstArg || it.aliases.contains(firstArg) } } - // something i saw on another command dsl, doesn't solve problem - lateinit var contextRef: ContextRef - - class ContextRef( - var context: CommandContext - ) + private lateinit var context: CommandContext operator fun Argument.provideDelegate( thisRef: Any?, @@ -71,16 +66,14 @@ open class KtCommandBuilder(val name: String) { ): ReadOnlyProperty { id = property.name - println("KtCommandBuilder#provideDelegate") - return ReadOnlyProperty { _, _ -> getter.invoke(contextRef.context) } + return ReadOnlyProperty { thisRef, property -> context.getValue(this) as V } } operator fun invoke(context: CommandContext) { - if (!::contextRef.isInitialized) - contextRef = ContextRef(context) - else this.contextRef.context = context - - runs.invoke(context) + // save values of args + this.context = context + val values = expectedArguments.map { it.id to it.getValue(context) } + runs.invoke(context.withValues(values)) } /** diff --git a/api/src/main/kotlin/com/mattmx/ktgui/commands/declarative/tests.kt b/api/src/main/kotlin/com/mattmx/ktgui/commands/declarative/tests.kt index 5fc6ce5..8707016 100644 --- a/api/src/main/kotlin/com/mattmx/ktgui/commands/declarative/tests.kt +++ b/api/src/main/kotlin/com/mattmx/ktgui/commands/declarative/tests.kt @@ -22,12 +22,13 @@ fun main() { runs { thread { Thread.sleep(1000) - println("[You -> $username]") + println(ref("username")) + println("[You -> $username] $message") } } } println(msg.getUsage(true)) - msg(CommandContext(CommandSender(), argsOf("MattMX"))) + msg(CommandContext(CommandSender(), argsOf("MattMX test"))) msg(CommandContext(CommandSender(), argsOf("GabbySimon"))) } \ No newline at end of file diff --git a/build.gradle.kts b/build.gradle.kts index ee6d381..da4fa61 100644 --- a/build.gradle.kts +++ b/build.gradle.kts @@ -1,14 +1,10 @@ plugins { kotlin("jvm") version "1.7.21" - id("org.ajoberstar.grgit") version "4.1.0" } -fun getCheckedOutGitCommitHash(): String = grgit.head().abbreviatedId - -val commitHash = getCheckedOutGitCommitHash() val version = "2.0" -rootProject.version = "$version-$commitHash" +rootProject.version = version subprojects { apply(plugin = "java") diff --git a/plugin/build.gradle.kts b/plugin/build.gradle.kts index 806cc5a..3bf4c55 100644 --- a/plugin/build.gradle.kts +++ b/plugin/build.gradle.kts @@ -2,8 +2,9 @@ import org.jetbrains.kotlin.gradle.tasks.KotlinCompile plugins { kotlin("jvm") - id("com.github.johnrengelman.shadow") version "7.0.0" + id("com.github.johnrengelman.shadow") version "7.1.2" `maven-publish` + id("org.ajoberstar.grgit") version "4.1.0" } repositories { @@ -14,8 +15,8 @@ repositories { } dependencies { - implementation(project(":api")) - implementation("co.pvphub:ProtocolLibDsl:-SNAPSHOT") + shadow(implementation(project(":api"))!!) + shadow(implementation("co.pvphub:ProtocolLibDsl:-SNAPSHOT")!!) compileOnly("com.comphenix.protocol:ProtocolLib:4.7.0") } @@ -29,9 +30,13 @@ tasks.withType { kotlinOptions.jvmTarget = "17" } +fun getCheckedOutGitCommitHash(): String = grgit.head().abbreviatedId + +val commitHash = getCheckedOutGitCommitHash() + tasks { withType { - val props = "version" to rootProject.version + val props = "version" to "${rootProject.version}-commit-$commitHash" inputs.properties(props) filteringCharset = "UTF-8" filesMatching("plugin.yml") { @@ -43,7 +48,10 @@ tasks { } } -val compile = tasks.withType { - archiveBaseName.set("ktgui-plugin-${rootProject.version}") +tasks.withType { + archiveBaseName.set("ktgui-plugin") + archiveClassifier.set("all") + archiveVersion.set(rootProject.version.toString()) + exclude("kotlin.*") mergeServiceFiles() } \ No newline at end of file