Skip to content

Commit

Permalink
added root arg placeholders
Browse files Browse the repository at this point in the history
  • Loading branch information
Matt-MX committed Jul 15, 2024
1 parent a92232b commit 7367bb1
Show file tree
Hide file tree
Showing 6 changed files with 98 additions and 69 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.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.
47 changes: 46 additions & 1 deletion api/src/main/kotlin/com/mattmx/ktgui/papi/Placeholder.kt
Original file line number Diff line number Diff line change
@@ -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?
) {
Expand All @@ -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<ChainCommandBuilder>()
private set
var supplier = Optional.empty<(PlaceholderParseContext) -> Any?>()
private set
var description = Optional.empty<String>()
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)
}
}
Original file line number Diff line number Diff line change
@@ -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
Expand Down Expand Up @@ -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)

Expand All @@ -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,
Expand Down
5 changes: 3 additions & 2 deletions api/src/main/kotlin/com/mattmx/ktgui/papi/dsl.kt
Original file line number Diff line number Diff line change
@@ -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
Expand All @@ -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) }
Placeholder(this, chain, supplier).apply { registerPlaceholder(this) }
Original file line number Diff line number Diff line change
@@ -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
}
}
63 changes: 0 additions & 63 deletions plugin/src/main/kotlin/com/mattmx/ktgui/KotlinGui.kt
Original file line number Diff line number Diff line change
Expand Up @@ -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<Char, Char>) =
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<CommandSender> {
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"
Expand Down

0 comments on commit 7367bb1

Please sign in to comment.