Skip to content

Commit

Permalink
impl basic design for #28
Browse files Browse the repository at this point in the history
  • Loading branch information
Matt-MX committed Jun 22, 2024
1 parent c7357dc commit c20e556
Show file tree
Hide file tree
Showing 7 changed files with 109 additions and 1 deletion.
1 change: 0 additions & 1 deletion .idea/modules/plugin/ktgui.plugin.test.iml

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

Original file line number Diff line number Diff line change
Expand Up @@ -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<Argument<*>>()
Expand Down Expand Up @@ -41,6 +42,10 @@ operator fun String.div(argument: List<Argument<*>>) = 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<DeclarativeCommandBuilder>) = ChainCommandBuilder(this).apply {
subcommands.addAll(subs)
}
Expand Down
12 changes: 12 additions & 0 deletions api/src/main/kotlin/com/mattmx/ktgui/papi/Placeholder.kt
Original file line number Diff line number Diff line change
@@ -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)
}
Original file line number Diff line number Diff line change
@@ -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<Placeholder>()
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<String>) = 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
}
}
Original file line number Diff line number Diff line change
@@ -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<String>
) {

operator fun <T : Any> Argument<T>.invoke(): T = TODO()

}
13 changes: 13 additions & 0 deletions api/src/main/kotlin/com/mattmx/ktgui/papi/dsl.kt
Original file line number Diff line number Diff line change
@@ -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)
12 changes: 12 additions & 0 deletions plugin/src/main/kotlin/com/mattmx/ktgui/KotlinGui.kt
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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<String, GuiDesigner>()
rawCommand("ktgui") {
Expand Down

0 comments on commit c20e556

Please sign in to comment.