Add Top-Level State and more Utils

Signed-off-by: Chaoscaot <max@maxsp.de>
This commit is contained in:
2026-06-10 16:07:37 +02:00
parent a23530074c
commit a6681ffaf6
4 changed files with 16 additions and 15 deletions
@@ -26,7 +26,7 @@ import org.bukkit.inventory.ItemStack
class TestInv(player: Player): UIInventory(player) { class TestInv(player: Player): UIInventory(player) {
override fun render() { override fun view() {
inventory(3, title = "TEST_TITLE") { inventory(3, title = "TEST_TITLE") {
group { group {
item { item {
@@ -19,28 +19,28 @@
package de.steamwar.kotlin.ui package de.steamwar.kotlin.ui
import de.steamwar.kotlin.KotlinCore
import de.steamwar.kotlin.ui.context.WindowContext import de.steamwar.kotlin.ui.context.WindowContext
import net.kyori.adventure.text.event.ClickEvent
import org.bukkit.Bukkit
import org.bukkit.entity.Player import org.bukkit.entity.Player
import org.bukkit.event.EventHandler
import org.bukkit.event.Listener
import org.bukkit.event.inventory.InventoryClickEvent
import org.bukkit.event.inventory.InventoryCloseEvent
import org.bukkit.event.inventory.InventoryType import org.bukkit.event.inventory.InventoryType
abstract class UIInventory(val player: Player) { abstract class UIInventory(val player: Player): RenderBoundary() {
lateinit var window: UIWindow var window: UIWindow? = null
abstract fun render() abstract fun view()
fun open() { fun open() {
if (!::window.isInitialized) { if (window == null) {
render() view()
assert(window != null) { "View method must create a inventory" }
} }
window.open() window!!.open()
}
override fun render() {
window?.onClose()
window = null
open()
} }
protected fun inventory(size: Int, title: String, init: WindowContext.() -> Unit) { protected fun inventory(size: Int, title: String, init: WindowContext.() -> Unit) {
@@ -32,6 +32,7 @@ import org.bukkit.event.inventory.InventoryCloseEvent
import org.bukkit.event.inventory.InventoryType import org.bukkit.event.inventory.InventoryType
import org.bukkit.inventory.Inventory import org.bukkit.inventory.Inventory
import org.bukkit.inventory.InventoryHolder import org.bukkit.inventory.InventoryHolder
import org.bukkit.inventory.InventoryView
class UIWindow(val player: Player, val render: WindowContext.() -> Unit): InventoryHolder { class UIWindow(val player: Player, val render: WindowContext.() -> Unit): InventoryHolder {
val onClicks = mutableMapOf<Int, (event: InventoryClickEvent) -> Unit>() val onClicks = mutableMapOf<Int, (event: InventoryClickEvent) -> Unit>()
@@ -32,7 +32,7 @@ fun ItemStack.count(count: Int) = apply { amount = count }
fun ItemStack.name(name: Component) = apply { itemMeta = itemMeta.also { it.displayName(name) } } fun ItemStack.name(name: Component) = apply { itemMeta = itemMeta.also { it.displayName(name) } }
fun ItemStack.lore(lore: List<Component>) = apply { itemMeta = itemMeta.also { it.lore(lore) } } fun ItemStack.lored(lore: List<Component>) = apply { itemMeta = itemMeta.also { it.lore(lore) } }
fun String.asMaterial(): Material = Material.matchMaterial(this) ?: Material.BARRIER fun String.asMaterial(): Material = Material.matchMaterial(this) ?: Material.BARRIER