Skip to content

Commit

Permalink
chore: improvements to the panel api
Browse files Browse the repository at this point in the history
  • Loading branch information
TheFruxz committed Nov 19, 2023
1 parent 0d9c1c7 commit 09f2af5
Show file tree
Hide file tree
Showing 5 changed files with 31 additions and 10 deletions.
1 change: 0 additions & 1 deletion build.gradle.kts
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,6 @@ group = "dev.fruxz"
repositories {

mavenCentral()
mavenLocal()

maven("https://jitpack.io") {
name = "JitPack"
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ import dev.fruxz.sparkle.framework.ux.inventory.item.itemLike
import kotlinx.coroutines.Deferred
import net.kyori.adventure.text.Component
import net.kyori.adventure.text.ComponentLike
import org.bukkit.event.inventory.InventoryClickEvent
import org.bukkit.inventory.ItemStack
import java.util.*
import kotlin.coroutines.CoroutineContext
Expand Down Expand Up @@ -37,24 +38,38 @@ data class MutablePanel(

public override val clickActions = MutableListMap<Int, ClickAction>()

// set

operator fun set(slot: Int, item: ItemLike) =
content.set(slot, item)

operator fun set(slot: Int, itemStack: ItemStack) =
this.set(slot = slot, item = itemStack.itemLike)

operator fun set(slot: Int, item: Deferred<ItemLike>) =
// set deferred

fun deferred(slot: Int, item: Deferred<ItemLike>) =
lazyContent.set(slot, item)

operator fun set(slot: Int, builder: suspend () -> ItemLike) =
fun deferred(slot: Int, builder: suspend () -> ItemLike) =
lazyContent.set(slot, asAsync { builder() })

// actions
operator fun set(slot: Int, item: Deferred<ItemLike>) =
deferred(slot, item)

operator fun set(slot: Int, builder: suspend () -> ItemLike) =
deferred(slot, asAsync { builder() })

// click actions

@SparkleDSL
fun onClick(slot: Int, action: ClickAction) =
clickActions.addEntry(slot, action)

@SparkleDSL
fun onClick(slot: Int, process: suspend (InventoryClickEvent) -> Unit) =
onClick(slot, ClickAction(process))

@SparkleDSL
operator fun set(slot: Int, action: ClickAction) =
onClick(slot, action)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -95,4 +95,4 @@ open class Panel(
updateContext = updateContext,
)

}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
package dev.fruxz.sparkle.framework.ux.panel

fun buildPanel(process: MutablePanel.() -> Unit) : Panel =
MutablePanel().apply(process).toPanel()
13 changes: 8 additions & 5 deletions src/main/kotlin/dev/fruxz/sparkle/server/LocalSparklePlugin.kt
Original file line number Diff line number Diff line change
Expand Up @@ -14,9 +14,8 @@ import dev.fruxz.sparkle.framework.system.pluginsFolder
import dev.fruxz.sparkle.framework.util.json.serializer.*
import dev.fruxz.sparkle.framework.ux.inventory.item.Item
import dev.fruxz.sparkle.framework.ux.inventory.item.item
import dev.fruxz.sparkle.framework.ux.panel.MutablePanel
import dev.fruxz.sparkle.framework.ux.panel.Panel
import dev.fruxz.sparkle.framework.ux.panel.PanelListener
import dev.fruxz.sparkle.framework.ux.panel.buildPanel
import dev.fruxz.sparkle.server.command.SparkleCommand
import dev.fruxz.sparkle.server.component.demo.DemoListener
import dev.fruxz.sparkle.server.component.events.DamageListener
Expand Down Expand Up @@ -58,15 +57,19 @@ class LocalSparklePlugin : SparklePlugin({
onEnable {
println("Hey! Sparkle ${this.pluginMeta.version} is online! Running Kotlin $kotlinVersion")

val panel = MutablePanel().apply {
val panel = buildPanel {
label = "Test-Panel".asStyledComponent

content += 4 to Material.STONE_BUTTON.item

onClick(4, Panel.ClickAction { e ->
onClick(4) { e ->
repeat(2_000) { 1.0 / 2 }
e.player.sendMessage("Clicked!")
})
}

deferred(2) {
Material.ACACIA_BUTTON.item
}

this[2] = {
delay(15.seconds)
Expand Down

0 comments on commit 09f2af5

Please sign in to comment.