Skip to content

Commit

Permalink
Finished impl of new pagination screens #31
Browse files Browse the repository at this point in the history
  • Loading branch information
Matt-MX committed Jul 5, 2024
1 parent 1072890 commit 8260a8a
Show file tree
Hide file tree
Showing 13 changed files with 153 additions and 42 deletions.
2 changes: 1 addition & 1 deletion api/.gradle/caches/paperweight/taskCache/reobfJar.log
Original file line number Diff line number Diff line change
@@ -1,2 +1,2 @@
Command: C:\Program Files\Java\jdk-17\bin\java.exe -Xmx1G -classpath C:\Users\Mangr\.gradle\caches\modules-2\files-2.1\net.fabricmc\tiny-remapper\0.10.1\c293b2384ae12af74f407fa3aaa553bba4ac6763\tiny-remapper-0.10.1-fat.jar net.fabricmc.tinyremapper.Main D:\PC\Projects\KtBukkitGui\api\build\libs\ktgui-2.4.1-dev-all.jar D:\PC\Projects\KtBukkitGui\api\build\libs\api-2.4.1.jar C:\Users\Mangr\.gradle\caches\paperweight-userdev\ff775525efc29c3503a07d1006e63e5695a742b7505cf63e157d49d32419c69f\module\io.papermc.paper\dev-bundle\1.20.4-R0.1-SNAPSHOT\paperweight\setupCache\extractDevBundle.dir\data\mojang+yarn-spigot-reobf.tiny mojang+yarn spigot C:\Users\Mangr\.gradle\caches\paperweight-userdev\ff775525efc29c3503a07d1006e63e5695a742b7505cf63e157d49d32419c69f\module\io.papermc.paper\dev-bundle\1.20.4-R0.1-SNAPSHOT\paperweight\setupCache\applyMojangMappedPaperclipPatch.jar --threads=1
Finished after 2771.45 ms.
Finished after 2784.17 ms.
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ import com.mattmx.ktgui.GuiManager
import com.mattmx.ktgui.extensions.getOpenGui
import com.mattmx.ktgui.item.DslIBuilder
import com.mattmx.ktgui.item.builder
import net.kyori.adventure.text.Component
import org.bukkit.Material
import org.bukkit.entity.Player
import org.bukkit.event.Cancellable
Expand Down Expand Up @@ -52,6 +53,8 @@ data class ButtonClickedEvent<T : IGuiButton<*>>(

override fun setCancelled(cancel: Boolean) = shouldContinueCallback(!cancel)

fun reply(component: Component) = player.sendMessage(component)

fun forceClose() {
GuiManager.clearGui(player)
player.closeInventory()
Expand Down
38 changes: 17 additions & 21 deletions api/src/main/kotlin/com/mattmx/ktgui/components/button/GuiButton.kt
Original file line number Diff line number Diff line change
Expand Up @@ -50,23 +50,23 @@ open class GuiButton<T : GuiButton<T>>(
if (item == null) item = ItemStack(material)
}

open fun lore(block: MutableList<Component>.() -> Unit): T {
item?.editMeta {
val newLore = mutableListOf<Component>().apply(block)
it.lore(newLore.map { line -> Component.empty().decoration(TextDecoration.ITALIC, false).append(line) })
}
open fun lore(block: LoreList.() -> Unit): T {
val loreList = LoreList(item?.itemMeta?.lore()).apply(block)
lore(*loreList.toTypedArray())
return this as T
}

@JavaCompatibility
fun lore(vararg lines: Component): T {
open fun lore(vararg lines: Component): T {
item?.editMeta {
it.lore(lines.map { line -> Component.empty().decoration(TextDecoration.ITALIC, false).append(line) })
it.lore(lines.map { line ->
Component.empty().decoration(TextDecoration.ITALIC, false).append(line)
})
}
return this as T
}

infix fun named(name: Component?): T {
open infix fun named(name: Component?): T {
item?.editMeta {
if (name != null)
it?.displayName(Component.empty().decoration(TextDecoration.ITALIC, false).append(name))
Expand All @@ -80,12 +80,12 @@ open class GuiButton<T : GuiButton<T>>(
return this as T
}

fun slots(vararg slots: Int): T {
open fun slots(vararg slots: Int): T {
slots.forEach { slot(it) }
return this as T
}

fun removeSlots(vararg slot: Int): T = apply {
open fun removeSlots(vararg slot: Int): T = apply {
if (hasParent()) {
slots?.removeAll(slot.toSet())
parent.clearSlot(*slot)
Expand All @@ -108,7 +108,7 @@ open class GuiButton<T : GuiButton<T>>(
return this as T
}

fun hasParent() = this::parent.isInitialized
open fun hasParent() = this::parent.isInitialized

override infix fun childOf(parent: IGuiScreen): T {
this.parent = parent
Expand All @@ -120,14 +120,14 @@ open class GuiButton<T : GuiButton<T>>(

}

fun materialOf(materialName: String?, fallback: Material): T {
open fun materialOf(materialName: String?, fallback: Material): T {
val materialNameFormatted = materialName?.uppercase()?.replace(" ", "_")
val mat = Material.values().firstOrNull { it.name == materialNameFormatted }
mat?.also { material(it) } ?: material(fallback)
return this as T
}

infix fun material(material: Material): T {
open infix fun material(material: Material): T {
item?.let {
it.type = material
return this as T
Expand All @@ -136,19 +136,19 @@ open class GuiButton<T : GuiButton<T>>(
return this as T
}

infix fun customModelData(model: Int): T {
open infix fun customModelData(model: Int): T {
val meta = item?.itemMeta ?: return this as T
meta.setCustomModelData(model)
item?.itemMeta = meta
return this as T
}

infix fun amount(amount: Int): T {
open infix fun amount(amount: Int): T {
item?.let { it.amount = amount }
return this as T
}

infix fun fromItemBuilder(builder: DslIBuilder): T {
open infix fun fromItemBuilder(builder: DslIBuilder): T {
item = builder.build()
return this as T
}
Expand All @@ -158,15 +158,11 @@ open class GuiButton<T : GuiButton<T>>(
}

@JavaCompatibility
fun click(type: ClickType, block: Consumer<ButtonClickedEvent<T>>): T {
open fun click(type: ClickType, block: Consumer<ButtonClickedEvent<T>>): T {
click.handleClicks({ block.accept(this) }, type)
return this as T
}

fun test(b: Consumer<StringBuilder>) {

}

inline infix fun click(block: ClickCallback<T>.() -> Unit): T {
block.invoke(click)
return this as T
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,8 +4,6 @@ import net.kyori.adventure.text.Component
import net.kyori.adventure.text.format.TextDecoration
import org.bukkit.entity.Player
import org.bukkit.inventory.ItemStack
import kotlin.math.max
import kotlin.math.min

class LoreCycleButton : GuiButton<LoreCycleButton>() {
var selected = 0
Expand Down Expand Up @@ -38,7 +36,7 @@ class LoreCycleButton : GuiButton<LoreCycleButton>() {
}

@Deprecated("This should not be used in this class.", ReplaceWith("this"))
override fun lore(lore: MutableList<Component>.() -> Unit): LoreCycleButton {
override fun lore(block: LoreList.() -> Unit): LoreCycleButton {
return this
}

Expand Down
11 changes: 11 additions & 0 deletions api/src/main/kotlin/com/mattmx/ktgui/components/button/LoreList.kt
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
package com.mattmx.ktgui.components.button

import net.kyori.adventure.text.Component

class LoreList(
existing: List<Component>?
) : ArrayList<Component>(existing ?: emptyList()) {

operator fun Component.unaryPlus() = add(this)

}
Original file line number Diff line number Diff line change
Expand Up @@ -184,12 +184,12 @@ open class GuiScreen(
Bukkit.getScheduler().runTask(GuiManager.owningPlugin) { ->
player.openInventory(inventory)
player.setOpenGui(this)
open(player)
open.invoke(player)
}
} else {
player.openInventory(inventory)
player.setOpenGui(this)
open(player)
open.invoke(player)
}
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -36,9 +36,9 @@ class GuiCramMultiPageScreen(
}

fun nextSlotToFill(sub: GuiScreen): Int? {
var nextSlot = sub.slotsUsed().max() + 1
var nextSlot = (sub.slotsUsed().maxOrNull() ?: -1) + 1

while (nextSlot in reservedSlots() && nextSlot <= sub.totalSlots()) {
while (nextSlot in reservedSlots() && nextSlot < sub.totalSlots()) {
nextSlot++
}

Expand All @@ -49,7 +49,7 @@ class GuiCramMultiPageScreen(

fun isFull(sub: GuiScreen) = sub.slotsUsed().size >= totalSlots() - reservedSlots().size

fun reservedSlots() = this.slotsUsed() + extraReservedSlots
fun reservedSlots() = (this.slotsUsed() + extraReservedSlots).toSet()

}

Expand Down
Original file line number Diff line number Diff line change
@@ -1,12 +1,19 @@
package com.mattmx.ktgui.components.screen.pagination

import com.mattmx.ktgui.GuiManager
import com.mattmx.ktgui.components.button.ButtonClickedEvent
import com.mattmx.ktgui.components.button.IGuiButton
import com.mattmx.ktgui.components.screen.GuiScreen
import com.mattmx.ktgui.dsl.button
import com.mattmx.ktgui.event.EventCallback
import com.mattmx.ktgui.utils.not
import net.kyori.adventure.text.Component
import org.bukkit.Bukkit
import org.bukkit.Material
import org.bukkit.entity.Player
import org.bukkit.event.inventory.InventoryClickEvent
import org.bukkit.inventory.Inventory
import org.bukkit.inventory.ItemStack
import java.util.*
import kotlin.math.max
import kotlin.math.min
Expand All @@ -24,8 +31,64 @@ open class GuiMultiPageScreen(
val pageChange = EventCallback<Int>()
val pages = Collections.synchronizedList(arrayListOf<GuiScreen>())

override fun refresh() {
val inv = arrayOfNulls<ItemStack?>(totalSlots())

// Now apply the items
val current = pages.getOrNull(currentPage)

current?.items?.forEach { (slot, item) ->
inv[slot] = item.formatIntoItemStack()
}

items.forEach { (slot, item) ->
if (slot < inv.size)
inv[slot] = item.formatIntoItemStack()
}

GuiManager.getPlayers(this)
.forEach { player ->
for ((index, item) in inv.withIndex()) {
player.openInventory.setItem(index, item)
}
}
}

override fun open(player: Player) {
TODO()
// format the items
val inv = Bukkit.createInventory(player, totalSlots(), title)

if (firePreBuildEvent(player)) return

// Now apply the items
val current = pages.getOrNull(currentPage)

current?.items?.forEach { (slot, item) ->
inv.setItem(slot, item.formatIntoItemStack(player))
}

items.forEach { (slot, item) ->
if (slot < inv.size)
inv.setItem(slot, item.formatIntoItemStack(player))
}

openIfNotCancelled(player, inv)
}

override fun click(e: InventoryClickEvent) {
val currentPage = pages.getOrNull(currentPage)

val button = items[e.rawSlot] ?: currentPage?.items?.get(e.rawSlot)

val event = ButtonClickedEvent<IGuiButton<*>>(e.whoClicked as Player, e)
if (button != null)
event.button = button

click.run(event)

if (event.shouldContinueCallback()) {
button?.onButtonClick(event)
}
}

infix fun page(block: GuiScreen.() -> Unit) = page(null, block)
Expand All @@ -38,12 +101,20 @@ open class GuiMultiPageScreen(
}
}

open fun navigatePreviousPage(e: ButtonClickedEvent<*>) = navigatePreviousPage()
open fun navigateNextPage(e: ButtonClickedEvent<*>) = navigateNextPage()

open fun navigatePreviousPage() {
currentPage = max(0, currentPage - 1)
}

open fun navigateNextPage() {
currentPage = min(pages.size, currentPage + 1)
currentPage = min(pages.size - 1, currentPage + 1)
}

override fun destroy() {
pages.forEach { it.destroy() }
super.destroy()
}
}

Expand Down
3 changes: 3 additions & 0 deletions api/src/main/kotlin/com/mattmx/ktgui/utils/color.kt
Original file line number Diff line number Diff line change
Expand Up @@ -53,3 +53,6 @@ operator fun Component.plus(component: Component) = this.append(component)
infix fun Component.clickEvent(event: ClickEvent) = clickEvent(event)
infix fun <T> Component.hoverEvent(event: HoverEvent<T>) = hoverEvent(event)
operator fun String.not() = component()

val String.translatable
get() = Component.translatable(this)
2 changes: 1 addition & 1 deletion plugin/.gradle/caches/paperweight/taskCache/reobfJar.log
Original file line number Diff line number Diff line change
@@ -1,2 +1,2 @@
Command: C:\Program Files\Java\jdk-17\bin\java.exe -Xmx1G -classpath C:\Users\Mangr\.gradle\caches\modules-2\files-2.1\net.fabricmc\tiny-remapper\0.10.1\c293b2384ae12af74f407fa3aaa553bba4ac6763\tiny-remapper-0.10.1-fat.jar net.fabricmc.tinyremapper.Main D:\PC\Projects\KtBukkitGui\plugin\build\libs\ktgui-plugin-2.4.1-dev-all.jar D:\PC\Projects\KtBukkitGui\plugin\build\libs\plugin-unspecified.jar C:\Users\Mangr\.gradle\caches\paperweight-userdev\ff775525efc29c3503a07d1006e63e5695a742b7505cf63e157d49d32419c69f\module\io.papermc.paper\dev-bundle\1.20.4-R0.1-SNAPSHOT\paperweight\setupCache\extractDevBundle.dir\data\mojang+yarn-spigot-reobf.tiny mojang+yarn spigot C:\Users\Mangr\.gradle\caches\paperweight-userdev\ff775525efc29c3503a07d1006e63e5695a742b7505cf63e157d49d32419c69f\module\io.papermc.paper\dev-bundle\1.20.4-R0.1-SNAPSHOT\paperweight\setupCache\applyMojangMappedPaperclipPatch.jar --threads=1
Finished after 1940.16 ms.
Finished after 1634.00 ms.
2 changes: 1 addition & 1 deletion plugin/src/main/kotlin/com/mattmx/ktgui/KotlinGui.kt
Original file line number Diff line number Diff line change
Expand Up @@ -67,7 +67,7 @@ class KotlinGui : JavaPlugin() {
"refresh-scoreboard" to { signalScoreboardExample },
"new-multi-screen-cram" to { NewCramMultiPageExample() },
"new-multi-screen" to { NewMultiPageExample() },
"player-inventory" to { HotbarExample() }
"hotbar" to { HotbarExample() }
)
GuiHookExample.registerListener(this)

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,24 +3,42 @@ package com.mattmx.ktgui.examples
import com.mattmx.ktgui.components.screen.pagination.cramMultiPageScreen
import com.mattmx.ktgui.dsl.button
import com.mattmx.ktgui.utils.not
import com.mattmx.ktgui.utils.plus
import com.mattmx.ktgui.utils.translatable
import net.kyori.adventure.text.format.TextColor
import org.bukkit.Material
import org.bukkit.entity.Player

class NewCramMultiPageExample : Example {
val gui = cramMultiPageScreen(!"Materials") {
val gui = cramMultiPageScreen(!"Materials &71") {
// These are slots that we should not cram fill with items
// In this example we're reserving the bottom row for pagination controls.
reserve(last() - 8..last())

button(Material.SPECTRAL_ARROW) {
named(!"&aNext")
click.left { navigateNextPage() }
click.left(::navigateNextPage)
} slot last()

button(Material.SPECTRAL_ARROW) {
named(!"&cLast")
click.left { navigatePreviousPage() }
click.left(::navigatePreviousPage)
} slot last() - 8

+Material.values().map { button(it) {} }
pageChange {
title = !"Materials &7${currentPage + 1}"
}

+Material.values().mapIndexed { index, material ->
button(material) {
lore {
+!"&aThis is item $index"
}
click.left {
reply(!"&aClicked " + material.translationKey().translatable + !"&a ($index)")
}
}
}
}

override fun run(player: Player) = gui.open(player)
Expand Down
Loading

0 comments on commit 8260a8a

Please sign in to comment.