Skip to content

Commit

Permalink
🔥 Working on RefreshBlock component
Browse files Browse the repository at this point in the history
  • Loading branch information
Matt-MX committed Nov 6, 2024
1 parent 4f9f0d9 commit 527b659
Show file tree
Hide file tree
Showing 11 changed files with 94 additions and 17 deletions.
2 changes: 1 addition & 1 deletion .idea/misc.xml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

14 changes: 14 additions & 0 deletions api/src/main/kotlin/com/mattmx/ktgui/Replyable.kt
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
package com.mattmx.ktgui

import net.kyori.adventure.text.Component
import org.bukkit.entity.Player

interface Replyable {

infix fun reply(component: Component) {
getTarget().sendMessage(component)
}

fun getTarget() : Player

}
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package com.mattmx.ktgui.components.button

import com.mattmx.ktgui.GuiManager
import com.mattmx.ktgui.Replyable
import com.mattmx.ktgui.extensions.getOpenGui
import com.mattmx.ktgui.item.DslIBuilder
import com.mattmx.ktgui.item.builder
Expand All @@ -27,7 +28,7 @@ data class ButtonClickedEvent<T : IGuiButton<*>>(
val player: Player,
val event: InventoryClickEvent,
val itemClicked: ItemStack? = event.currentItem
) : Event(), Cancellable {
) : Event(), Cancellable, Replyable {
val slot = event.rawSlot
val currentGui = player.getOpenGui()
private var callbackShouldContinue = true
Expand All @@ -53,7 +54,9 @@ data class ButtonClickedEvent<T : IGuiButton<*>>(

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

fun reply(component: Component) = player.sendMessage(component)
override fun getTarget(): Player {
return player
}

fun forceClose() {
GuiManager.clearGui(player)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -264,6 +264,7 @@ open class GuiScreen(
return totalSlots() - 1
}

@Deprecated("Use slots.middle", replaceWith = ReplaceWith("slots.middle"))
fun middle(): Int {
return if (type == null) {
(floor(rows * 0.5) + 4).toInt()
Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
package com.mattmx.ktgui.components.screen.slot

import com.mattmx.ktgui.components.button.GuiButton
import com.mattmx.ktgui.components.screen.GuiScreen
import kotlin.math.floor

class Slots(
private val parent: GuiScreen
Expand All @@ -13,8 +15,10 @@ class Slots(

val first: Int
get() = parent.first()
val middle: Int
get() = parent.middle()
val center: Int
get() = if (parent.type == null) {
row(floor(parent.rows * 0.5).toInt()).middle
} else floor(parent.totalSlots() * 0.5).toInt()
val last: Int
get() = parent.last()

Expand All @@ -26,6 +30,14 @@ class Slots(
return Row(y).column(x)
}

fun after(sibling: GuiButton<*>): Int {
return (sibling.slots().maxOrNull() ?: (first - 1)) + 1
}

fun before(sibling: GuiButton<*>): Int {
return (sibling.slots().minOrNull() ?: (last + 1)) - 1
}

class Row(
val row: Int
) {
Expand Down
6 changes: 5 additions & 1 deletion api/src/main/kotlin/com/mattmx/ktgui/dsl/gui.kt
Original file line number Diff line number Diff line change
Expand Up @@ -46,4 +46,8 @@ fun GuiScreen.effect(block: GuiScreen.() -> Unit) =
EffectBlock(this, block).apply { this@effect.addEffect(this) }

fun GuiScreen.refresh(repeat: Long, block: GuiScreen.() -> Unit) =
RefreshBlock(repeat, this, block).apply { this@refresh.addRefreshBlock(this) }
RefreshBlock(repeat, this, block)
.apply {
block.invoke(this@refresh)
this@refresh.addRefreshBlock(this)
}
54 changes: 47 additions & 7 deletions api/src/main/kotlin/com/mattmx/ktgui/f.kt
Original file line number Diff line number Diff line change
Expand Up @@ -2,21 +2,61 @@ package com.mattmx.ktgui

import com.mattmx.ktgui.components.button.named
import com.mattmx.ktgui.components.screen.GuiScreen
import com.mattmx.ktgui.components.screen.slot.Slots
import com.mattmx.ktgui.components.title
import com.mattmx.ktgui.dsl.button
import com.mattmx.ktgui.dsl.gui
import com.mattmx.ktgui.dsl.refresh
import com.mattmx.ktgui.utils.not
import com.mattmx.ktgui.utils.pretty
import org.bukkit.Material
import org.bukkit.entity.Player
import java.time.Duration
import java.time.Instant

fun createMenu(player: Player): GuiScreen {
fun createMenu(): GuiScreen {
return gui(3) {
title { !"Balls" }

button(Material.RED_STAINED_GLASS) {
named { !"Test" }
} slot slots.of(3, 3)
val guiOpened = Instant.now()
var x = 1

} andOpen player
val testButton = button(Material.RED_STAINED_GLASS) {
named { !"Test ${x.pretty()}" }

click.left {
x++

update()
}

click.drop {
reply(!"&dmeow :3")
}

click.right {
x--

update()
}

} slot slots.center

button(Material.COMPARATOR) {
named(!"&9Something sexy")

lore {
+!"&fLore Test"
}

click.any {
isCancelled = false
}
} slot slots.before(testButton) - 1

refresh(20L) {
button(Material.CLOCK) {
named { !"&aOpen for ${Duration.between(guiOpened, Instant.now()).pretty()}" }
} slot slots.after(testButton) + 1
}

}
}
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:\Users\Mangr\.gradle\jdks\adoptium-21-x64-hotspot-windows\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.4-alpha-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 1827.78 ms.
Finished after 1888.19 ms.
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 @@ -68,7 +68,8 @@ class KotlinGui : JavaPlugin() {
"new-multi-screen-cram" to { NewCramMultiPageExample() },
"new-multi-screen-cram-strategy" to { CramStrategyExample() },
"new-multi-screen" to { NewMultiPageExample() },
"hotbar" to { HotbarExample() }
"hotbar" to { HotbarExample() },
"f" to { Example { createMenu() andOpen it } }
))
GuiHookExample.registerListener(this)

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,6 @@ package com.mattmx.ktgui.examples

import org.bukkit.entity.Player

interface Example {
fun interface Example {
fun run(player: Player)
}
4 changes: 3 additions & 1 deletion plugin/src/main/resources/plugin.yml
Original file line number Diff line number Diff line change
Expand Up @@ -7,9 +7,11 @@ authors: [ MattMX ]
website: https://github.com/Matt-MX/KtPaperGui
description: Declarative GUI Library for Paper.

depend:
- MCKotlin-Paper

softdepend:
- PlaceholderAPI
- MCKotlin-Paper

commands:
ktgui-cmd-examples:
Expand Down

0 comments on commit 527b659

Please sign in to comment.