updated spigot and added on close function for closing a gui

This commit is contained in:
Skyslycer
2021-08-16 00:30:43 +02:00
parent d58193f161
commit e4681af822
3 changed files with 19 additions and 5 deletions

View File

@@ -24,8 +24,8 @@ repositories {
}
dependencies {
compileOnly("org.spigotmc", "spigot", "1.17-R0.1-SNAPSHOT")
testCompileOnly("org.spigotmc", "spigot", "1.17-R0.1-SNAPSHOT")
compileOnly("org.spigotmc", "spigot", "1.17.1-R0.1-SNAPSHOT")
testCompileOnly("org.spigotmc", "spigot", "1.17.1-R0.1-SNAPSHOT")
api("org.jetbrains.kotlinx:kotlinx-serialization-json:1.2.1")
api("org.jetbrains.kotlinx:kotlinx-coroutines-core:1.5.0")

View File

@@ -19,6 +19,7 @@ class GUIData<T : ForInventory>(
val transitionTo: InventoryChangeEffect?,
val transitionFrom: InventoryChangeEffect?,
internal val generalOnClick: ((GUIClickEvent<T>) -> Unit)?,
internal val onClose: ((InventoryCloseEvent) -> Unit)?
)
abstract class GUI<T : ForInventory>(
@@ -95,9 +96,15 @@ class GUIIndividual<T : ForInventory>(
}
init {
if (resetOnClose) {
if (resetOnClose || data.onClose != null) {
listen<InventoryCloseEvent> {
deleteInstance(it.player as? Player ?: return@listen)
if (data.onClose != null && playerInstances[it.player]?.bukkitInventory == it.inventory) {
data.onClose.invoke(it)
}
if (resetOnClose) {
deleteInstance(it.player as? Player ?: return@listen)
}
}
}

View File

@@ -3,6 +3,7 @@
package net.axay.kspigot.gui
import net.axay.kspigot.gui.elements.*
import org.bukkit.event.inventory.InventoryCloseEvent
import org.bukkit.inventory.ItemStack
import kotlin.math.absoluteValue
@@ -46,6 +47,8 @@ class GUIBuilder<T : ForInventory>(
private var onClickElement: ((GUIClickEvent<T>) -> Unit)? = null
private var onClose: ((InventoryCloseEvent) -> Unit)? = null
/**
* Opens the builder for a new page and adds
* the new page to the GUI.
@@ -63,8 +66,12 @@ class GUIBuilder<T : ForInventory>(
onClickElement = onClick
}
fun onClose(onClose: (InventoryCloseEvent) -> Unit) {
this.onClose = onClose
}
internal fun build() = guiCreator.createInstance(
GUIData(type, title, guiPages, defaultPage, transitionTo, transitionFrom, onClickElement)
GUIData(type, title, guiPages, defaultPage, transitionTo, transitionFrom, onClickElement, onClose)
)
}