Skip to content

Commit

Permalink
Added time utils for ticks, added postBuild block to run after butt…
Browse files Browse the repository at this point in the history
…ons create item stacks
  • Loading branch information
Matt-MX committed Jan 22, 2024
1 parent eb51352 commit 6fd0ec7
Show file tree
Hide file tree
Showing 6 changed files with 51 additions and 4 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,8 @@ open class GuiButton<T : GuiButton<T>>(
protected set
var ifTexturePackActive: ((T) -> Unit)? = null
protected set
var postBuild: ((ItemStack) -> Unit)? = null
protected set

// todo should remove this once we have no need for it (parent has been declared)
private var slots: ArrayList<Int>? = null
Expand Down Expand Up @@ -122,6 +124,13 @@ open class GuiButton<T : GuiButton<T>>(
return this as T
}

/**
* Code runs after the [GuiButton] has built an [ItemStack]
*/
infix fun postBuild(block: ItemStack.() -> Unit) = apply {
postBuild = block
} as T

infix fun fromItemBuilder(builder: DslIBuilder) : T {
item = builder.build()
return this as T
Expand Down Expand Up @@ -162,9 +171,9 @@ open class GuiButton<T : GuiButton<T>>(
if (player?.hasResourcePack() == true && ifTexturePackActive != null) {
val copy = this.copy(this.parent)
ifTexturePackActive!!(copy)
return copy.item
return copy.item.apply { this?.let { postBuild?.invoke(it) } }
}
return getItemStack()?.clone()
return getItemStack()?.clone().apply { this?.let { postBuild?.invoke(it) } }
}

override fun slots(): List<Int> {
Expand Down
16 changes: 16 additions & 0 deletions api/src/main/kotlin/com/mattmx/ktgui/utils/time.kt
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
package com.mattmx.ktgui.utils

val Int.ticks
get() = this.toLong()

val Int.seconds
get() = ticks * 20L

val Double.seconds
get() = (this * 20).toLong()

val Int.minutes
get() = seconds * 60L

val Double.minutes
get() = (this * 20 * 60).toLong()
2 changes: 1 addition & 1 deletion build.gradle.kts
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@ subprojects {
}
dependencies {
compileOnly("org.jetbrains.kotlin:kotlin-stdlib:1.7.10")
compileOnly("io.papermc.paper:paper-api:1.19.4-R0.1-SNAPSHOT")
compileOnly("io.papermc.paper:paper-api:1.20.2-R0.1-SNAPSHOT")
compileOnly("me.clip:placeholderapi:2.11.1")
}

Expand Down
7 changes: 7 additions & 0 deletions plugin/src/main/kotlin/com/mattmx/ktgui/KotlinGui.kt
Original file line number Diff line number Diff line change
@@ -1,9 +1,16 @@
package com.mattmx.ktgui

import com.mattmx.ktgui.commands.simpleCommand
import com.mattmx.ktgui.dsl.event
import com.mattmx.ktgui.examples.*
import com.mattmx.ktgui.utils.not
import org.bukkit.Bukkit
import org.bukkit.Material
import org.bukkit.NamespacedKey
import org.bukkit.block.Block
import org.bukkit.entity.FallingBlock
import org.bukkit.event.entity.EntityChangeBlockEvent
import org.bukkit.persistence.PersistentDataType
import org.bukkit.plugin.java.JavaPlugin
import java.util.logging.Logger

Expand Down
15 changes: 15 additions & 0 deletions plugin/src/main/kotlin/com/mattmx/ktgui/examples/GuiHookExample.kt
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,8 @@ import com.mattmx.ktgui.utils.not
import org.bukkit.Material
import org.bukkit.entity.Player
import org.bukkit.event.inventory.ClickType
import org.bukkit.inventory.meta.PotionMeta
import org.bukkit.potion.PotionType

class GuiHookExample : Example {
val gui = gui(!"Title", 3) {
Expand All @@ -23,6 +25,19 @@ class GuiHookExample : Example {
add(!"&fThis button was added regularly.")
}
} slots listOf(11, 13)

button(Material.LINGERING_POTION) {
named(!"&fA potion")
lore {
add(!"&fThis is a custom potion")
}
// Will run before the button is made into an itemstack
postBuild {
editMeta(PotionMeta::class.java) { meta ->
meta.basePotionType = PotionType.STRONG_STRENGTH
}
}
} slot last()
}

override fun run(player: Player) = gui.open(player)
Expand Down
2 changes: 1 addition & 1 deletion plugin/src/main/resources/plugin.yml
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
name: KtGUI
version: '${version}'
main: com.mattmx.ktgui.KotlinGui
api-version: 1.20
api-version: 1.17
prefix: KtGUI
authors: [ MattMX ]
description: Declarative GUI Library for Paper.
Expand Down

0 comments on commit 6fd0ec7

Please sign in to comment.