From 1072890b1143511406f136a85aaecdf6040b1281 Mon Sep 17 00:00:00 2001 From: MattMX Date: Thu, 4 Jul 2024 23:53:24 +0100 Subject: [PATCH] Starting to impl hotbar screen for #37 --- .../screen/InventoryHotbarScreen.kt | 43 +++++++++++++++++++ .../mattmx/ktgui/event/ScrollHotbarEvent.kt | 10 +++++ .../main/kotlin/com/mattmx/ktgui/KotlinGui.kt | 3 +- .../mattmx/ktgui/examples/HotbarExample.kt | 37 ++++++++++++++++ 4 files changed, 92 insertions(+), 1 deletion(-) create mode 100644 api/src/main/kotlin/com/mattmx/ktgui/components/screen/InventoryHotbarScreen.kt create mode 100644 api/src/main/kotlin/com/mattmx/ktgui/event/ScrollHotbarEvent.kt create mode 100644 plugin/src/main/kotlin/com/mattmx/ktgui/examples/HotbarExample.kt diff --git a/api/src/main/kotlin/com/mattmx/ktgui/components/screen/InventoryHotbarScreen.kt b/api/src/main/kotlin/com/mattmx/ktgui/components/screen/InventoryHotbarScreen.kt new file mode 100644 index 0000000..93599b2 --- /dev/null +++ b/api/src/main/kotlin/com/mattmx/ktgui/components/screen/InventoryHotbarScreen.kt @@ -0,0 +1,43 @@ +package com.mattmx.ktgui.components.screen + +import com.mattmx.ktgui.components.button.GuiButton +import com.mattmx.ktgui.event.EventCallback +import com.mattmx.ktgui.event.ScrollHotbarEvent +import net.kyori.adventure.text.Component +import net.kyori.adventure.title.Title +import org.bukkit.entity.Player +import org.bukkit.event.inventory.InventoryType +import org.bukkit.event.player.PlayerDropItemEvent +import java.util.* + +open class InventoryHotbarScreen : GuiScreen(Component.empty(), type = InventoryType.PLAYER) { + val holding = hashMapOf Unit>() + val dropItem = EventCallback() + val scroll = EventCallback() + var startingHeldSlot = Optional.empty() + private set + + override fun open(player: Player) { + TODO() + } + + infix fun startingHeldSlot(slot: Int) = apply { + this.startingHeldSlot = Optional.of(slot) + } + + infix fun GuiButton<*>.holding(block: Player.() -> Unit) = apply { + slots().forEach { slot -> holding[slot] = block } + } + + infix fun GuiButton<*>.holdingSendActionBar(component: Component) = holding { + sendActionBar(component) + } + + infix fun GuiButton<*>.holdingSendTitle(title: Title) = holding { showTitle(title) } + + fun hotbarMiddle() = 4 + fun hotbarLast() = 8 +} + +fun hotbar(block: InventoryHotbarScreen.() -> Unit) = + InventoryHotbarScreen().apply(block) \ No newline at end of file diff --git a/api/src/main/kotlin/com/mattmx/ktgui/event/ScrollHotbarEvent.kt b/api/src/main/kotlin/com/mattmx/ktgui/event/ScrollHotbarEvent.kt new file mode 100644 index 0000000..df1589b --- /dev/null +++ b/api/src/main/kotlin/com/mattmx/ktgui/event/ScrollHotbarEvent.kt @@ -0,0 +1,10 @@ +package com.mattmx.ktgui.event + +import org.bukkit.entity.Player +import org.bukkit.event.player.PlayerItemHeldEvent + +class ScrollHotbarEvent( + val player: Player, + val difference: Int, + val event: PlayerItemHeldEvent +) \ No newline at end of file diff --git a/plugin/src/main/kotlin/com/mattmx/ktgui/KotlinGui.kt b/plugin/src/main/kotlin/com/mattmx/ktgui/KotlinGui.kt index 146cb09..21af13e 100644 --- a/plugin/src/main/kotlin/com/mattmx/ktgui/KotlinGui.kt +++ b/plugin/src/main/kotlin/com/mattmx/ktgui/KotlinGui.kt @@ -66,7 +66,8 @@ class KotlinGui : JavaPlugin() { "config-gui" to { GuiConfigExample() }, "refresh-scoreboard" to { signalScoreboardExample }, "new-multi-screen-cram" to { NewCramMultiPageExample() }, - "new-multi-screen" to { NewMultiPageExample() } + "new-multi-screen" to { NewMultiPageExample() }, + "player-inventory" to { HotbarExample() } ) GuiHookExample.registerListener(this) diff --git a/plugin/src/main/kotlin/com/mattmx/ktgui/examples/HotbarExample.kt b/plugin/src/main/kotlin/com/mattmx/ktgui/examples/HotbarExample.kt new file mode 100644 index 0000000..0f603df --- /dev/null +++ b/plugin/src/main/kotlin/com/mattmx/ktgui/examples/HotbarExample.kt @@ -0,0 +1,37 @@ +package com.mattmx.ktgui.examples + +import com.mattmx.ktgui.components.screen.hotbar +import com.mattmx.ktgui.dsl.button +import com.mattmx.ktgui.utils.not +import org.bukkit.Material +import org.bukkit.entity.Player +import kotlin.math.abs + +class HotbarExample : Example { + val gui = hotbar { + var counter = 0 + scroll { + event.isCancelled = false + counter += difference + player.sendActionBar(!"&fScrolled: $counter ${if (difference > 0) "&a+" else "&c-"}${abs(difference)}") + + if (counter > 100) { + open(player) + } + } + + button(Material.NETHER_STAR) { + named(!"&aServer Selector") + click.left { player.sendMessage(!"&7") } + } slot hotbarMiddle() holdingSendActionBar !"&7Click to change servers." + + if (counter > 100) { + button(Material.PLAYER_HEAD) { + named(!"&aYou found a secret!") + } slot hotbarLast() + } + + } + + override fun run(player: Player) = gui.open(player) +} \ No newline at end of file