From c20e5564993f47d6d2ed515ed3ca1aebb601ea3c Mon Sep 17 00:00:00 2001 From: MattMX Date: Sat, 22 Jun 2024 14:45:56 +0100 Subject: [PATCH] impl basic design for #28 --- .idea/modules/plugin/ktgui.plugin.test.iml | 1 - .../declarative/ChainCommandBuilder.kt | 5 ++ .../com/mattmx/ktgui/papi/Placeholder.kt | 12 +++++ .../ktgui/papi/PlaceholderExpansionWrapper.kt | 54 +++++++++++++++++++ .../ktgui/papi/PlaceholderParseContext.kt | 13 +++++ .../main/kotlin/com/mattmx/ktgui/papi/dsl.kt | 13 +++++ .../main/kotlin/com/mattmx/ktgui/KotlinGui.kt | 12 +++++ 7 files changed, 109 insertions(+), 1 deletion(-) create mode 100644 api/src/main/kotlin/com/mattmx/ktgui/papi/Placeholder.kt create mode 100644 api/src/main/kotlin/com/mattmx/ktgui/papi/PlaceholderExpansionWrapper.kt create mode 100644 api/src/main/kotlin/com/mattmx/ktgui/papi/PlaceholderParseContext.kt create mode 100644 api/src/main/kotlin/com/mattmx/ktgui/papi/dsl.kt diff --git a/.idea/modules/plugin/ktgui.plugin.test.iml b/.idea/modules/plugin/ktgui.plugin.test.iml index b1f816c..c0b28ad 100644 --- a/.idea/modules/plugin/ktgui.plugin.test.iml +++ b/.idea/modules/plugin/ktgui.plugin.test.iml @@ -5,7 +5,6 @@ PAPER - MCP ADVENTURE 1 diff --git a/api/src/main/kotlin/com/mattmx/ktgui/commands/declarative/ChainCommandBuilder.kt b/api/src/main/kotlin/com/mattmx/ktgui/commands/declarative/ChainCommandBuilder.kt index f1b9d85..9ede97e 100644 --- a/api/src/main/kotlin/com/mattmx/ktgui/commands/declarative/ChainCommandBuilder.kt +++ b/api/src/main/kotlin/com/mattmx/ktgui/commands/declarative/ChainCommandBuilder.kt @@ -3,6 +3,7 @@ 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.utils.JavaCompatibility +import org.bukkit.block.data.type.Chain class ChainCommandBuilder(val name: String) { val arguments = arrayListOf>() @@ -41,6 +42,10 @@ operator fun String.div(argument: List>) = ChainCommandBuilder(this) arguments.add(MultiArgument("multi-argument", *argument.toTypedArray())) } +operator fun String.div(s: String) = ChainCommandBuilder(this).apply { + subcommands.add(DeclarativeCommandBuilder(s)) +} + operator fun String.div(subs: List) = ChainCommandBuilder(this).apply { subcommands.addAll(subs) } diff --git a/api/src/main/kotlin/com/mattmx/ktgui/papi/Placeholder.kt b/api/src/main/kotlin/com/mattmx/ktgui/papi/Placeholder.kt new file mode 100644 index 0000000..97d7d5a --- /dev/null +++ b/api/src/main/kotlin/com/mattmx/ktgui/papi/Placeholder.kt @@ -0,0 +1,12 @@ +package com.mattmx.ktgui.papi + +import com.mattmx.ktgui.commands.declarative.ChainCommandBuilder + +class Placeholder( + val match: ChainCommandBuilder, + val supplier: (PlaceholderParseContext) -> Any? +) { + var priority = 0 + + fun parse(context: PlaceholderParseContext) = supplier.invoke(context) +} \ No newline at end of file diff --git a/api/src/main/kotlin/com/mattmx/ktgui/papi/PlaceholderExpansionWrapper.kt b/api/src/main/kotlin/com/mattmx/ktgui/papi/PlaceholderExpansionWrapper.kt new file mode 100644 index 0000000..4c56186 --- /dev/null +++ b/api/src/main/kotlin/com/mattmx/ktgui/papi/PlaceholderExpansionWrapper.kt @@ -0,0 +1,54 @@ +package com.mattmx.ktgui.papi + +import me.clip.placeholderapi.expansion.PlaceholderExpansion +import org.bukkit.entity.Player +import org.bukkit.plugin.java.JavaPlugin + +class PlaceholderExpansionWrapper( + private val owner: JavaPlugin +) : PlaceholderExpansion() { + private val placeholders = arrayListOf() + var id = owner.name + private set + var author = owner.pluginMeta.authors.joinToString(", ") + private set + var version = owner.pluginMeta.version + private set + var splitArgs = { params: String -> params.split("_") } + private set + + override fun getIdentifier() = id + override fun getAuthor() = author + override fun getVersion() = version + override fun getPlaceholders() = placeholders.map { it.toString() }.toMutableList() + + infix fun id(id: String) = apply { + this.id = id + } + + infix fun author(author: String) = apply { + this.author = author + } + + infix fun version(version: String) = apply { + this.version = version + } + + infix fun splitArgs(splitArgs: (String) -> List) = apply { + this.splitArgs = splitArgs + } + + infix fun registerPlaceholder(placeholder: Placeholder) = placeholders.add(placeholder) + + override fun onPlaceholderRequest(player: Player?, params: String): String? { + val context = PlaceholderParseContext(player, splitArgs(params)) + for (placeholder in placeholders.sortedByDescending { it.priority }) { + val content = placeholder.parse(context) + + if (content != null) { + return content.toString() + } + } + return null + } +} \ No newline at end of file diff --git a/api/src/main/kotlin/com/mattmx/ktgui/papi/PlaceholderParseContext.kt b/api/src/main/kotlin/com/mattmx/ktgui/papi/PlaceholderParseContext.kt new file mode 100644 index 0000000..f00a4f9 --- /dev/null +++ b/api/src/main/kotlin/com/mattmx/ktgui/papi/PlaceholderParseContext.kt @@ -0,0 +1,13 @@ +package com.mattmx.ktgui.papi + +import com.mattmx.ktgui.commands.declarative.arg.Argument +import org.bukkit.entity.Player + +class PlaceholderParseContext( + val requestedBy: Player?, + val params: List +) { + + operator fun Argument.invoke(): T = TODO() + +} \ No newline at end of file diff --git a/api/src/main/kotlin/com/mattmx/ktgui/papi/dsl.kt b/api/src/main/kotlin/com/mattmx/ktgui/papi/dsl.kt new file mode 100644 index 0000000..db63fad --- /dev/null +++ b/api/src/main/kotlin/com/mattmx/ktgui/papi/dsl.kt @@ -0,0 +1,13 @@ +package com.mattmx.ktgui.papi + +import com.mattmx.ktgui.commands.declarative.ChainCommandBuilder +import org.bukkit.plugin.java.JavaPlugin + +inline fun JavaPlugin.placeholderExpansion(builder: PlaceholderExpansionWrapper.() -> Unit) = + PlaceholderExpansionWrapper(this).apply(builder).apply { register() } + +fun placeholder(string: String, supplier: PlaceholderParseContext.() -> Any?) = + Placeholder(ChainCommandBuilder(string), supplier) + +fun placeholder(chain: ChainCommandBuilder, supplier: PlaceholderParseContext.() -> Any?) = + Placeholder(chain, supplier) \ No newline at end of file diff --git a/plugin/src/main/kotlin/com/mattmx/ktgui/KotlinGui.kt b/plugin/src/main/kotlin/com/mattmx/ktgui/KotlinGui.kt index d8d5d52..5c991ea 100644 --- a/plugin/src/main/kotlin/com/mattmx/ktgui/KotlinGui.kt +++ b/plugin/src/main/kotlin/com/mattmx/ktgui/KotlinGui.kt @@ -11,6 +11,8 @@ import com.mattmx.ktgui.components.screen.GuiScreen import com.mattmx.ktgui.cooldown.ActionCoolDown import com.mattmx.ktgui.designer.GuiDesigner import com.mattmx.ktgui.examples.* +import com.mattmx.ktgui.papi.placeholder +import com.mattmx.ktgui.papi.placeholderExpansion import com.mattmx.ktgui.scheduling.sync import com.mattmx.ktgui.sound.playSound import com.mattmx.ktgui.sound.sound @@ -65,6 +67,16 @@ class KotlinGui : JavaPlugin() { ) GuiHookExample.registerListener(this) + placeholderExpansion { + + val player by playerArgument() + + placeholder("ping" / player) { player().ping } + placeholder("ping") { requestedBy?.ping } + placeholder("iscool" / player) { if (player().name == author) "this player's sick" else "nah not rly" } + + } id "ktgui" author "MattMX" + sync { val cachedDesigners = hashMapOf() rawCommand("ktgui") {