From 7367bb167d1470084258bb6d56bb0a19f42d03e9 Mon Sep 17 00:00:00 2001 From: MattMX Date: Mon, 15 Jul 2024 12:22:11 +0100 Subject: [PATCH] added root arg placeholders --- .../caches/paperweight/taskCache/reobfJar.log | 2 +- .../com/mattmx/ktgui/papi/Placeholder.kt | 47 +++++++++++++- .../ktgui/papi/PlaceholderExpansionWrapper.kt | 23 ++++++- .../main/kotlin/com/mattmx/ktgui/papi/dsl.kt | 5 +- .../JavaPlaceholderExpansionExample.java | 27 ++++++++ .../main/kotlin/com/mattmx/ktgui/KotlinGui.kt | 63 ------------------- 6 files changed, 98 insertions(+), 69 deletions(-) create mode 100644 plugin/src/main/java/com/mattmx/ktgui/examples/JavaPlaceholderExpansionExample.java diff --git a/api/.gradle/caches/paperweight/taskCache/reobfJar.log b/api/.gradle/caches/paperweight/taskCache/reobfJar.log index 6794786..c8fcbfa 100644 --- a/api/.gradle/caches/paperweight/taskCache/reobfJar.log +++ b/api/.gradle/caches/paperweight/taskCache/reobfJar.log @@ -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.2-alpha-dev-all.jar D:\PC\Projects\KtBukkitGui\api\build\libs\api-2.4.2-alpha.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 2991.53 ms. +Finished after 2923.07 ms. diff --git a/api/src/main/kotlin/com/mattmx/ktgui/papi/Placeholder.kt b/api/src/main/kotlin/com/mattmx/ktgui/papi/Placeholder.kt index b428ffb..d5a5994 100644 --- a/api/src/main/kotlin/com/mattmx/ktgui/papi/Placeholder.kt +++ b/api/src/main/kotlin/com/mattmx/ktgui/papi/Placeholder.kt @@ -1,9 +1,11 @@ package com.mattmx.ktgui.papi import com.mattmx.ktgui.commands.declarative.ChainCommandBuilder +import com.mattmx.ktgui.utils.JavaCompatibility import java.util.* class Placeholder( + val owner: PlaceholderExpansionWrapper, val match: ChainCommandBuilder, val supplier: (PlaceholderParseContext) -> Any? ) { @@ -18,9 +20,52 @@ class Placeholder( } override fun toString() = - "%${match.name}_${ + "%${owner.id}_${if (match.name == EMPTY_PLACEHOLDER) "" else "${match.name}_"}${ match.arguments.joinToString("_") { arg -> "<${arg.name()}${if (arg.isOptional()) "?" else ""}:${arg.type()}>" } }%${if (description.isPresent) " - ${description.get()}" else ""}" + + @JavaCompatibility + class Builder { + var match = Optional.empty() + private set + var supplier = Optional.empty<(PlaceholderParseContext) -> Any?>() + private set + var description = Optional.empty() + private set + var priority = 0 + private set + + infix fun matches(builder: ChainCommandBuilder) = apply { + this.match = Optional.of(builder) + } + + infix fun supplier(supplier: (PlaceholderParseContext) -> Any?) = apply { + this.supplier = Optional.of(supplier) + } + + infix fun description(description: String?) = apply { + this.description = Optional.ofNullable(description) + } + + infix fun priority(priority: Int) = apply { + this.priority = priority + } + + fun build(owner: PlaceholderExpansionWrapper) = + Placeholder(owner, match.get(), supplier.get()) + .description(description.orElse(null)) + .apply { priority = this@Builder.priority } + } + + companion object { + const val EMPTY_PLACEHOLDER = "ROOT" + + @JvmStatic + fun builder() = Builder() + + @JvmStatic + fun emptyCommandBuilder() = ChainCommandBuilder(EMPTY_PLACEHOLDER) + } } \ 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 index bb2b487..d76ac89 100644 --- a/api/src/main/kotlin/com/mattmx/ktgui/papi/PlaceholderExpansionWrapper.kt +++ b/api/src/main/kotlin/com/mattmx/ktgui/papi/PlaceholderExpansionWrapper.kt @@ -1,9 +1,13 @@ package com.mattmx.ktgui.papi +import com.mattmx.ktgui.commands.declarative.ChainCommandBuilder 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.ArgumentProcessor +import com.mattmx.ktgui.commands.declarative.div import com.mattmx.ktgui.commands.declarative.invocation.StorageCommandContext +import com.mattmx.ktgui.utils.JavaCompatibility import me.clip.placeholderapi.expansion.PlaceholderExpansion import org.bukkit.entity.Player import org.bukkit.plugin.java.JavaPlugin @@ -59,6 +63,21 @@ class PlaceholderExpansionWrapper( infix fun registerPlaceholder(placeholder: Placeholder) = placeholders.add(placeholder) + @JavaCompatibility + infix fun withPlaceholder(placeholder: Placeholder) = apply { + placeholders.add(placeholder) + } + + @JavaCompatibility + infix fun withPlaceholder(builder: Placeholder.Builder) = apply { + placeholders.add(builder.build(this)) + } + + operator fun Argument<*>.div(other: Argument<*>) = + ChainCommandBuilder(Placeholder.EMPTY_PLACEHOLDER) + .div(this) + .div(other) + override fun onPlaceholderRequest(player: Player?, params: String): String? { val paramsSplit = splitArgs(params) @@ -68,9 +87,9 @@ class PlaceholderExpansionWrapper( else null for (placeholder in placeholders.sortedByDescending { it.priority }) { - // todo support no identifiers (root placeholder) val identifier = placeholder.match.name - if (paramsSplit.getOrNull(0) != identifier) continue + if (identifier != Placeholder.EMPTY_PLACEHOLDER && paramsSplit.getOrNull(0) != identifier) + continue val argumentParser = ArgumentProcessor( emptyCommand, diff --git a/api/src/main/kotlin/com/mattmx/ktgui/papi/dsl.kt b/api/src/main/kotlin/com/mattmx/ktgui/papi/dsl.kt index 0fb72c2..38a3456 100644 --- a/api/src/main/kotlin/com/mattmx/ktgui/papi/dsl.kt +++ b/api/src/main/kotlin/com/mattmx/ktgui/papi/dsl.kt @@ -1,6 +1,7 @@ package com.mattmx.ktgui.papi import com.mattmx.ktgui.commands.declarative.ChainCommandBuilder +import com.mattmx.ktgui.commands.declarative.arg.Argument import com.mattmx.ktgui.scheduling.sync import com.mattmx.ktgui.scheduling.syncDelayed import org.bukkit.plugin.java.JavaPlugin @@ -13,7 +14,7 @@ inline fun JavaPlugin.placeholderExpansion(builder: PlaceholderExpansionWrapper. } fun PlaceholderExpansionWrapper.placeholder(string: String, supplier: PlaceholderParseContext.() -> Any?) = - Placeholder(ChainCommandBuilder(string), supplier).apply { registerPlaceholder(this) } + Placeholder(this, ChainCommandBuilder(string), supplier).apply { registerPlaceholder(this) } fun PlaceholderExpansionWrapper.placeholder(chain: ChainCommandBuilder, supplier: PlaceholderParseContext.() -> Any?) = - Placeholder(chain, supplier).apply { registerPlaceholder(this) } \ No newline at end of file + Placeholder(this, chain, supplier).apply { registerPlaceholder(this) } \ No newline at end of file diff --git a/plugin/src/main/java/com/mattmx/ktgui/examples/JavaPlaceholderExpansionExample.java b/plugin/src/main/java/com/mattmx/ktgui/examples/JavaPlaceholderExpansionExample.java new file mode 100644 index 0000000..0c32fd0 --- /dev/null +++ b/plugin/src/main/java/com/mattmx/ktgui/examples/JavaPlaceholderExpansionExample.java @@ -0,0 +1,27 @@ +package com.mattmx.ktgui.examples; + +import com.mattmx.ktgui.commands.declarative.arg.impl.DoubleArgument; +import com.mattmx.ktgui.papi.Placeholder; +import com.mattmx.ktgui.papi.PlaceholderExpansionWrapper; +import org.bukkit.plugin.java.JavaPlugin; + +public class JavaPlaceholderExpansionExample { + public void test(JavaPlugin plugin) { + DoubleArgument a = new DoubleArgument("a", "double"); + DoubleArgument b = new DoubleArgument("b", "double"); + + new PlaceholderExpansionWrapper(plugin) + .author("MattMX") + .id("+") + .withPlaceholder( + Placeholder.builder() + .matches(Placeholder.emptyCommandBuilder().argument(a).argument(b)) + .supplier((context) -> + context.getContext(a).orElse(0.0) + context.getContext(b).orElse(0.0) + ) + ); + + // Usage: %+_1.0_2.0% -> 3.0 + // %+_1.4_-1.0% -> 0.4 + } +} diff --git a/plugin/src/main/kotlin/com/mattmx/ktgui/KotlinGui.kt b/plugin/src/main/kotlin/com/mattmx/ktgui/KotlinGui.kt index 4b9d24b..8db7c2b 100644 --- a/plugin/src/main/kotlin/com/mattmx/ktgui/KotlinGui.kt +++ b/plugin/src/main/kotlin/com/mattmx/ktgui/KotlinGui.kt @@ -77,69 +77,6 @@ class KotlinGui : JavaPlugin() { ) GuiHookExample.registerListener(this) - fun mapFont(alphabet: String) = alphabet - .mapIndexed { index, it -> Char('a'.code + index) to it } - .toMap(HashMap()) - - fun convertFont(original: String, fontMap: Map) = - String(original.map { c -> fontMap[c] ?: c }.toCharArray()) - - val smallFont = mapFont("ᴀʙᴄᴅᴇғɢʜɪᴊᴋʟᴍɴᴏᴘǫʀsᴛᴜᴠᴡxʏᴢ") - val balls = mapFont("ⓐⓑⓒⓓⓔⓕⓖⓗⓘⓙⓚⓛⓜⓝⓞⓟⓠⓡⓢⓣⓤⓥⓦⓧⓨⓩ") - val blackSquares = - mapFont("\uD83C\uDD70\uD83C\uDD71\uD83C\uDD72\uD83C\uDD73\uD83C\uDD74\uD83C\uDD75\uD83C\uDD76\uD83C\uDD77\uD83C\uDD78\uD83C\uDD79\uD83C\uDD7A\uD83C\uDD7B\uD83C\uDD7C\uD83C\uDD7D\uD83C\uDD7E\uD83C\uDD7F\uD83C\uDD80\uD83C\uDD81\uD83C\uDD82\uD83C\uDD83\uD83C\uDD84\uD83C\uDD85\uD83C\uDD86\uD83C\uDD87\uD83C\uDD88\uD83C\uDD89") - - val stringToConvert by greedyStringArgument() - val fontType by multiChoiceArgument<(String) -> String>( - "smalltext" to { convertFont(it, smallFont) }, - "balls" to { convertFont(it, balls) }, - "blacksquares" to { convertFont(it, blackSquares) } - ) - - val brandingType by multiChoiceArgument( - config.getConfigurationSection("branding") - ?.let { section -> - section.getKeys(false).associateWithTo(HashMap()) { k -> section.getString(k) ?: "null" } - } - ?: hashMapOf() - ) - - placeholderExpansion { - placeholder("font" / fontType / stringToConvert) { - fontType()(stringToConvert()) - } - - placeholder("font-ph" / fontType / stringToConvert) { - val formatted = "%${stringToConvert().replace(" ", "_")}%" - val string = PlaceholderAPI.setPlaceholders(requestedBy, formatted) - fontType()(string) - } - - placeholder("branding" / brandingType) { brandingType() } - val a by doubleArgument() - val op by multiChoiceArgument<(Double, Double) -> Double>( - "+" to { a, b -> a + b }, - "-" to { a, b -> a - b }, - "/" to { a, b -> a / b }, - "*" to { a, b -> a * b }, - ) - val b by doubleArgument() - - placeholder("math" / a / op / b) { - op()(a(), b()) - } - - } id "ktgui" author "MattMX" - - ("font" / fontType / stringToConvert).runs { - val text = fontType()(stringToConvert()) - reply( - text.component - .clickEvent(ClickEvent.suggestCommand(text)) - .hoverEvent(HoverEvent.showText(!"&aClick to copy")) - ) - } permission "ktgui.command.font" register this - sync { rawCommand("ktgui") { permission = "ktgui.command"