Skip to content

Commit

Permalink
feature: working panel flags
Browse files Browse the repository at this point in the history
  • Loading branch information
TheFruxz committed Nov 17, 2023
1 parent cf98fdd commit 8c30c31
Show file tree
Hide file tree
Showing 3 changed files with 50 additions and 22 deletions.
18 changes: 10 additions & 8 deletions src/main/kotlin/dev/fruxz/sparkle/framework/ux/panel/PanelFlag.kt
Original file line number Diff line number Diff line change
Expand Up @@ -2,27 +2,29 @@ package dev.fruxz.sparkle.framework.ux.panel

enum class PanelFlag {

NO_GRAB,
NO_DRAG,
NO_MOVE,
NO_SWAP,
NO_OPEN,
NO_CLICK,
NO_REFRESH,
NO_CLOSE,
NO_OPEN,

/**
* Instead of processing each panel action one by one, process them all at once in an async context.
*/
PARALLEL_ACTION_PROCESSING,

/**
* If actions should be possible to track, even if not in the inventory provided by the panel. (e.g. player inventory)
* If actions should be possible to track, even if not in the inventory provided by the panel (e.g., player inventory)
*/
DETECT_OUTSIDE_CLICKS,

/**
* If a slot is clicked with an action, it will not automatically cancel the event, to perform the action.
*/
DETECT_OUTSIDE_CLICKS;
ACTION_NO_CANCEL;

companion object {

val defaultProtection = setOf(NO_GRAB, NO_DRAG, NO_MOVE, NO_SWAP)
val defaultProtection = setOf(NO_MOVE, NO_CLICK)

}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,16 +4,34 @@ import dev.fruxz.ascend.extension.future.awaitAll
import dev.fruxz.ascend.extension.objects.takeIfInstance
import dev.fruxz.sparkle.framework.coroutine.task.doAsync
import dev.fruxz.sparkle.framework.system.debugLog
import io.papermc.paper.event.inventory.PaperInventoryMoveItemEvent
import kotlinx.coroutines.runBlocking
import org.bukkit.event.EventHandler
import org.bukkit.event.Listener
import org.bukkit.event.inventory.InventoryClickEvent
import org.bukkit.event.inventory.InventoryOpenEvent
import kotlin.time.measureTime

class PanelListener : Listener {

@EventHandler
fun onClick(event: InventoryClickEvent) {
fun onInventoryOpen(event: InventoryOpenEvent) {
val panel = event.inventory.holder?.takeIfInstance<PanelHolder>()?.panel ?: return

if (PanelFlag.NO_OPEN in panel.flags) event.isCancelled = true
}

@EventHandler
fun onItemMove(event: PaperInventoryMoveItemEvent) {
val source = event.source.holder?.takeIfInstance<PanelHolder>()?.panel
val destination = event.destination.holder?.takeIfInstance<PanelHolder>()?.panel
val flags = source?.flags.orEmpty() + destination?.flags.orEmpty()

if (PanelFlag.NO_MOVE in flags) event.isCancelled = true
}

@EventHandler
fun onInventoryClick(event: InventoryClickEvent) {
val holder = event.inventory.holder?.takeIfInstance<PanelHolder>() ?: return
val panel = holder.panel
val clicks = panel.clickActions[event.rawSlot]
Expand All @@ -22,25 +40,31 @@ class PanelListener : Listener {

debugLog { "Click on slot ${event.rawSlot} in panel ${panel.identifier()} - ${clicks?.size ?: 0} click-actions assigned to this slot" }

if (clicks.isNullOrEmpty()) return
if (PanelFlag.NO_CLICK in panel.flags) event.isCancelled = true

event.isCancelled = true
when {
!clicks.isNullOrEmpty() -> {

measureTime {
runBlocking {
when {
PanelFlag.PARALLEL_ACTION_PROCESSING in panel.flags -> { // TODO experimental
clicks.map {
doAsync { _ ->
it.action(event)
event.isCancelled = !event.isCancelled && PanelFlag.ACTION_NO_CANCEL !in panel.flags

measureTime {
runBlocking {
when {
PanelFlag.PARALLEL_ACTION_PROCESSING in panel.flags -> { // TODO experimental
clicks.map {
doAsync { _ ->
it.action(event)
}
}.awaitAll()
}
}.awaitAll()
else -> clicks.forEach { it.action(event) }
}
}
else -> clicks.forEach { it.action(event) }
}.also { time ->
debugLog { "Click on slot ${event.rawSlot} in panel ${panel.identifier()} => took $time! (avg. ${time / clicks.size} per action)" }
}

}
}.also { time ->
debugLog { "Click on slot ${event.rawSlot} in panel ${panel.identifier()} => took $time! (avg. ${time / clicks.size} per action)" }
}

}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@ 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.PanelFlag
import dev.fruxz.sparkle.framework.ux.panel.PanelListener
import dev.fruxz.sparkle.server.command.SparkleCommand
import dev.fruxz.sparkle.server.component.demo.DemoListener
Expand Down Expand Up @@ -68,6 +69,7 @@ class LocalSparklePlugin : SparklePlugin({
repeat(2_000) { 1.0 / 2 }
e.player.sendMessage("Clicked!")
})
flags += PanelFlag.NO_CLOSE

}

Expand Down

0 comments on commit 8c30c31

Please sign in to comment.