Merge pull request #25 from Skyslycer/feature/on-close-gui
Feature: Method that runs when a player closes the inventory
This commit is contained in:
@@ -24,8 +24,8 @@ repositories {
|
|||||||
}
|
}
|
||||||
|
|
||||||
dependencies {
|
dependencies {
|
||||||
compileOnly("org.spigotmc", "spigot", "1.17-R0.1-SNAPSHOT")
|
compileOnly("org.spigotmc", "spigot", "1.17.1-R0.1-SNAPSHOT")
|
||||||
testCompileOnly("org.spigotmc", "spigot", "1.17-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-serialization-json:1.2.1")
|
||||||
api("org.jetbrains.kotlinx:kotlinx-coroutines-core:1.5.0")
|
api("org.jetbrains.kotlinx:kotlinx-coroutines-core:1.5.0")
|
||||||
|
@@ -1,7 +1,7 @@
|
|||||||
# KSpigot
|
# KSpigot
|
||||||
|
|
||||||
[  ](https://repo1.maven.org/maven2/net/axay/kspigot/)
|
[  ](https://repo1.maven.org/maven2/net/axay/kspigot/)
|
||||||
[  ](https://bluefireoly.github.io/KSpigot/)
|
[  ](https://jakobkmar.github.io/KSpigot/)
|
||||||
[  ](https://discord.gg/CJDUVuJ) <br>
|
[  ](https://discord.gg/CJDUVuJ) <br>
|
||||||
|
|
||||||
KSpigot is a Kotlin extension for the popular [spigot server software](https://spigotmc.org/) for minecraft. It adds
|
KSpigot is a Kotlin extension for the popular [spigot server software](https://spigotmc.org/) for minecraft. It adds
|
||||||
|
@@ -19,6 +19,7 @@ class GUIData<T : ForInventory>(
|
|||||||
val transitionTo: InventoryChangeEffect?,
|
val transitionTo: InventoryChangeEffect?,
|
||||||
val transitionFrom: InventoryChangeEffect?,
|
val transitionFrom: InventoryChangeEffect?,
|
||||||
internal val generalOnClick: ((GUIClickEvent<T>) -> Unit)?,
|
internal val generalOnClick: ((GUIClickEvent<T>) -> Unit)?,
|
||||||
|
internal val onClose: ((GUICloseEvent<T>) -> Unit)?
|
||||||
)
|
)
|
||||||
|
|
||||||
abstract class GUI<T : ForInventory>(
|
abstract class GUI<T : ForInventory>(
|
||||||
@@ -95,9 +96,15 @@ class GUIIndividual<T : ForInventory>(
|
|||||||
}
|
}
|
||||||
|
|
||||||
init {
|
init {
|
||||||
if (resetOnClose) {
|
if (resetOnClose || data.onClose != null) {
|
||||||
listen<InventoryCloseEvent> {
|
listen<InventoryCloseEvent> {
|
||||||
deleteInstance(it.player as? Player ?: return@listen)
|
if (data.onClose != null && playerInstances[it.player]?.bukkitInventory == it.inventory) {
|
||||||
|
data.onClose.invoke(GUICloseEvent(it, playerInstances[it.player]!!, it.player as Player))
|
||||||
|
}
|
||||||
|
|
||||||
|
if (resetOnClose) {
|
||||||
|
deleteInstance(it.player as? Player ?: return@listen)
|
||||||
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@@ -3,6 +3,7 @@
|
|||||||
package net.axay.kspigot.gui
|
package net.axay.kspigot.gui
|
||||||
|
|
||||||
import net.axay.kspigot.gui.elements.*
|
import net.axay.kspigot.gui.elements.*
|
||||||
|
import org.bukkit.event.inventory.InventoryCloseEvent
|
||||||
import org.bukkit.inventory.ItemStack
|
import org.bukkit.inventory.ItemStack
|
||||||
import kotlin.math.absoluteValue
|
import kotlin.math.absoluteValue
|
||||||
|
|
||||||
@@ -46,6 +47,8 @@ class GUIBuilder<T : ForInventory>(
|
|||||||
|
|
||||||
private var onClickElement: ((GUIClickEvent<T>) -> Unit)? = null
|
private var onClickElement: ((GUIClickEvent<T>) -> Unit)? = null
|
||||||
|
|
||||||
|
private var onClose: ((GUICloseEvent<T>) -> Unit)? = null
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Opens the builder for a new page and adds
|
* Opens the builder for a new page and adds
|
||||||
* the new page to the GUI.
|
* the new page to the GUI.
|
||||||
@@ -63,8 +66,16 @@ class GUIBuilder<T : ForInventory>(
|
|||||||
onClickElement = onClick
|
onClickElement = onClick
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* A callback executed when the user closes
|
||||||
|
* the inventory.
|
||||||
|
*/
|
||||||
|
fun onClose(onClose: (GUICloseEvent<T>) -> Unit) {
|
||||||
|
this.onClose = onClose
|
||||||
|
}
|
||||||
|
|
||||||
internal fun build() = guiCreator.createInstance(
|
internal fun build() = guiCreator.createInstance(
|
||||||
GUIData(type, title, guiPages, defaultPage, transitionTo, transitionFrom, onClickElement)
|
GUIData(type, title, guiPages, defaultPage, transitionTo, transitionFrom, onClickElement, onClose)
|
||||||
)
|
)
|
||||||
}
|
}
|
||||||
|
|
||||||
|
10
src/main/kotlin/net/axay/kspigot/gui/GUICloseEvent.kt
Normal file
10
src/main/kotlin/net/axay/kspigot/gui/GUICloseEvent.kt
Normal file
@@ -0,0 +1,10 @@
|
|||||||
|
package net.axay.kspigot.gui
|
||||||
|
|
||||||
|
import org.bukkit.entity.Player
|
||||||
|
import org.bukkit.event.inventory.InventoryCloseEvent
|
||||||
|
|
||||||
|
class GUICloseEvent<T : ForInventory>(
|
||||||
|
val bukkitEvent: InventoryCloseEvent,
|
||||||
|
val guiInstance: GUIInstance<T>,
|
||||||
|
val player: Player,
|
||||||
|
)
|
Reference in New Issue
Block a user