Skip to content

Commit

Permalink
Starting to impl hotbar screen for #37
Browse files Browse the repository at this point in the history
  • Loading branch information
Matt-MX committed Jul 4, 2024
1 parent 223cc07 commit 1072890
Show file tree
Hide file tree
Showing 4 changed files with 92 additions and 1 deletion.
Original file line number Diff line number Diff line change
@@ -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<Int, (Player) -> Unit>()
val dropItem = EventCallback<PlayerDropItemEvent>()
val scroll = EventCallback<ScrollHotbarEvent>()
var startingHeldSlot = Optional.empty<Int>()
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)
10 changes: 10 additions & 0 deletions api/src/main/kotlin/com/mattmx/ktgui/event/ScrollHotbarEvent.kt
Original file line number Diff line number Diff line change
@@ -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
)
3 changes: 2 additions & 1 deletion plugin/src/main/kotlin/com/mattmx/ktgui/KotlinGui.kt
Original file line number Diff line number Diff line change
Expand Up @@ -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)

Expand Down
37 changes: 37 additions & 0 deletions plugin/src/main/kotlin/com/mattmx/ktgui/examples/HotbarExample.kt
Original file line number Diff line number Diff line change
@@ -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<Clicked>") }
} 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)
}

0 comments on commit 1072890

Please sign in to comment.