-
Notifications
You must be signed in to change notification settings - Fork 5
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Starting to impl hotbar screen for #37
- Loading branch information
Showing
4 changed files
with
92 additions
and
1 deletion.
There are no files selected for viewing
43 changes: 43 additions & 0 deletions
43
api/src/main/kotlin/com/mattmx/ktgui/components/screen/InventoryHotbarScreen.kt
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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
10
api/src/main/kotlin/com/mattmx/ktgui/event/ScrollHotbarEvent.kt
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 | ||
) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
37 changes: 37 additions & 0 deletions
37
plugin/src/main/kotlin/com/mattmx/ktgui/examples/HotbarExample.kt
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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) | ||
} |