Inventory GUI update

- e.g.: "gui[Slots.RowThreeSlotOne] = ItemStack(Material.BAMBOO)" now possible
This commit is contained in:
bluefireoly
2020-09-29 22:23:42 +02:00
parent b57f74d3f6
commit f7e19dbb5b
4 changed files with 45 additions and 38 deletions

View File

@@ -8,10 +8,11 @@ import org.bukkit.entity.HumanEntity
import org.bukkit.event.inventory.InventoryClickEvent
import org.bukkit.inventory.Inventory
import org.bukkit.inventory.InventoryView
import org.bukkit.inventory.ItemStack
// EXTENSIONS
fun HumanEntity.openGUI(gui: InventoryGUI, page: Int = 1): InventoryView? {
fun HumanEntity.openGUI(gui: InventoryGUI<*>, page: Int = 1): InventoryView? {
closeInventory()
gui.loadPage(page)
return openInventory(gui.bukkitInventory)
@@ -21,13 +22,13 @@ fun HumanEntity.openGUI(gui: InventoryGUI, page: Int = 1): InventoryView? {
class InventoryGUIHolder(kSpigot: KSpigot) : AutoCloseable {
private val registered = HashSet<InventoryGUI>()
private val registered = HashSet<InventoryGUI<*>>()
fun register(inventoryGUI: InventoryGUI) {
fun register(inventoryGUI: InventoryGUI<*>) {
registered += inventoryGUI
}
fun unregister(inventoryGUI: InventoryGUI) {
fun unregister(inventoryGUI: InventoryGUI<*>) {
registered -= inventoryGUI
}
@@ -56,24 +57,24 @@ class InventoryGUIHolder(kSpigot: KSpigot) : AutoCloseable {
// EVENT
class InventoryGUIClickEvent(
class InventoryGUIClickEvent<T : ForInventory>(
val bukkitEvent: InventoryClickEvent,
val gui: InventoryGUI,
val gui: InventoryGUI<T>
)
/*
* INVENTORY GUI
*/
class InventoryGUIData(
class InventoryGUIData<T : ForInventory>(
val plugin: KSpigot,
val inventoryType: InventoryType<*>,
val inventoryType: InventoryType<T>,
val title: String?,
val pages: Map<Int, InventoryGUIPage>
)
abstract class InventoryGUI(
val data: InventoryGUIData
abstract class InventoryGUI<T : ForInventory>(
val data: InventoryGUIData<T>
) {
var currentPage: Int? = null; protected set
@@ -84,6 +85,8 @@ abstract class InventoryGUI(
abstract fun isThisInv(inventory: Inventory): Boolean
abstract operator fun set(slot: InventorySlotCompound<T>, value: ItemStack)
fun register() = data.plugin.inventoryGUIHolder.register(this)
fun unregister() = data.plugin.inventoryGUIHolder.unregister(this)
@@ -91,9 +94,9 @@ abstract class InventoryGUI(
// Inventory GUI implementations
class InventoryGUIShared(
inventoryGUIData: InventoryGUIData
) : InventoryGUI(inventoryGUIData) {
class InventoryGUIShared<T : ForInventory>(
inventoryGUIData: InventoryGUIData<T>
) : InventoryGUI<T>(inventoryGUIData) {
override val bukkitInventory by lazy { data.inventoryType.createBukkitInv(null, data.title) }
@@ -143,6 +146,10 @@ class InventoryGUIShared(
}
override fun set(slot: InventorySlotCompound<T>, value: ItemStack) {
slot.withInvType(data.inventoryType).forEach { }
}
}
class InventoryGUIPage(

View File

@@ -10,9 +10,9 @@ inline fun <T : ForInventory> KSpigot.inventoryGUI(
builder: InventoryGUIBuilder<T>.() -> Unit,
) = InventoryGUIBuilder(this, type).apply(builder).build()
class InventoryGUIBuilder<in T : ForInventory>(
class InventoryGUIBuilder<T : ForInventory>(
private val kSpigot: KSpigot,
val type: InventoryType<in T>
val type: InventoryType<T>
) {
var title: String = ""
@@ -30,8 +30,8 @@ class InventoryGUIBuilder<in T : ForInventory>(
}
class InventoryGUIPageBuilder<in T : ForInventory>(
val type: InventoryType<in T>,
class InventoryGUIPageBuilder<T : ForInventory>(
val type: InventoryType<T>,
val page: Int
) {
@@ -45,7 +45,7 @@ class InventoryGUIPageBuilder<in T : ForInventory>(
* actions. If clicked, the specified [onClick]
* function is invoked.
*/
fun button(slots: InventorySlotCompound<T>, itemStack: ItemStack, onClick: (InventoryGUIClickEvent) -> Unit)
fun button(slots: InventorySlotCompound<T>, itemStack: ItemStack, onClick: (InventoryGUIClickEvent<T>) -> Unit)
= slots(slots, InventoryGUIButton(InventoryGUIElementData(itemStack), onClick))
/**
@@ -67,7 +67,7 @@ class InventoryGUIPageBuilder<in T : ForInventory>(
* This is a button which loads the specified
* [toPage] if clicked.
*/
fun pageChanger(slots: InventorySlotCompound<T>, itemStack: ItemStack, toPage: Int, onChange: ((InventoryGUIClickEvent) -> Unit)? = null)
fun pageChanger(slots: InventorySlotCompound<T>, itemStack: ItemStack, toPage: Int, onChange: ((InventoryGUIClickEvent<T>) -> Unit)? = null)
= slots(slots, InventoryGUIButtonPageChange(
InventoryGUIElementData(itemStack),
InventoryGUIPageChangeCalculator.InventoryGUIConsistentPageCalculator(toPage),
@@ -79,7 +79,7 @@ class InventoryGUIPageBuilder<in T : ForInventory>(
* page if clicked, and if a previous page
* exists it is loaded.
*/
fun previousPage(slots: InventorySlotCompound<T>, itemStack: ItemStack, onChange: ((InventoryGUIClickEvent) -> Unit)? = null)
fun previousPage(slots: InventorySlotCompound<T>, itemStack: ItemStack, onChange: ((InventoryGUIClickEvent<T>) -> Unit)? = null)
= slots(slots, InventoryGUIButtonPageChange(
InventoryGUIElementData(itemStack),
InventoryGUIPageChangeCalculator.InventoryGUIPreviousPageCalculator,
@@ -91,7 +91,7 @@ class InventoryGUIPageBuilder<in T : ForInventory>(
* page if clicked, and if a next page
* exists it is loaded.
*/
fun nextPage(slots: InventorySlotCompound<T>, itemStack: ItemStack, onChange: ((InventoryGUIClickEvent) -> Unit)? = null)
fun nextPage(slots: InventorySlotCompound<T>, itemStack: ItemStack, onChange: ((InventoryGUIClickEvent<T>) -> Unit)? = null)
= slots(slots, InventoryGUIButtonPageChange(
InventoryGUIElementData(itemStack),
InventoryGUIPageChangeCalculator.InventoryGUINextPageCalculator,

View File

@@ -72,10 +72,10 @@ data class InventorySlot(val row: Int, val slotInRow: Int) : Comparable<Inventor
}
interface InventorySlotCompound<out T : ForInventory> {
fun withInvType(invType: InventoryType<in T>): Collection<InventorySlot>
fun withInvType(invType: InventoryType<T>): Collection<InventorySlot>
}
open class SingleInventorySlot<out T : ForInventory> internal constructor(
open class SingleInventorySlot<T : ForInventory> internal constructor(
val inventorySlot: InventorySlot
) : InventorySlotCompound<T> {
@@ -83,7 +83,7 @@ open class SingleInventorySlot<out T : ForInventory> internal constructor(
private val slotAsList = listOf(inventorySlot)
override fun withInvType(invType: InventoryType<in T>) = slotAsList
override fun withInvType(invType: InventoryType<T>) = slotAsList
}
@@ -94,8 +94,8 @@ internal enum class InventorySlotRangeType {
class InventorySlotRange<out T : ForInventory> internal constructor(
startSlot: SingleInventorySlot<T>,
endSlot: SingleInventorySlot<T>,
startSlot: SingleInventorySlot<out T>,
endSlot: SingleInventorySlot<out T>,
private val type: InventorySlotRangeType
@@ -110,7 +110,7 @@ class InventorySlotRange<out T : ForInventory> internal constructor(
endInclusive = minMaxPair.max
}
override fun withInvType(invType: InventoryType<in T>)
override fun withInvType(invType: InventoryType<T>)
= LinkedHashSet<InventorySlot>().apply {
when (type) {
@@ -151,43 +151,43 @@ class InventorySlotRange<out T : ForInventory> internal constructor(
* This range contains all slots having an index between
* the indeces of the two given slots.
*/
infix fun <T : ForInventory> SingleInventorySlot<T>.linTo(slot: SingleInventorySlot<T>)
infix fun <T : ForInventory> SingleInventorySlot<out T>.linTo(slot: SingleInventorySlot<out T>)
= InventorySlotRange(this, slot, InventorySlotRangeType.LINEAR)
/**
* This range contains all slots inside of a thought rectangle
* with the two given slots as two opposite corners of the rectangle.
*/
infix fun <T : ForInventory> SingleInventorySlot<T>.rectTo(slot: SingleInventorySlot<T>)
infix fun <T : ForInventory> SingleInventorySlot<out T>.rectTo(slot: SingleInventorySlot<out T>)
= InventorySlotRange(this, slot, InventorySlotRangeType.RECTANGLE)
class InventoryRowSlots<out T : ForInventory> internal constructor(
class InventoryRowSlots<T : ForInventory> internal constructor(
val row: Int
) : InventorySlotCompound<T> {
override fun withInvType(invType: InventoryType<in T>) = HashSet<InventorySlot>().apply {
override fun withInvType(invType: InventoryType<T>) = HashSet<InventorySlot>().apply {
for (slotInRow in 1 .. invType.dimensions.width)
this += InventorySlot(row, slotInRow)
}
}
class InventoryColumnSlots<out T : ForInventory> internal constructor(
class InventoryColumnSlots<T : ForInventory> internal constructor(
val column: Int
) : InventorySlotCompound<T> {
override fun withInvType(invType: InventoryType<in T>) = HashSet<InventorySlot>().apply {
override fun withInvType(invType: InventoryType<T>) = HashSet<InventorySlot>().apply {
for (row in 1 .. invType.dimensions.heigth)
this += InventorySlot(row, column)
}
}
class InventoryBorderSlots<out T : ForInventory> internal constructor(
class InventoryBorderSlots<T : ForInventory> internal constructor(
val padding: Int
) : InventorySlotCompound<T> {
override fun withInvType(invType: InventoryType<in T>) = HashSet<InventorySlot>().apply {
override fun withInvType(invType: InventoryType<T>) = HashSet<InventorySlot>().apply {
val dimensions = invType.dimensions
@@ -206,14 +206,14 @@ class InventoryBorderSlots<out T : ForInventory> internal constructor(
}
class InventoryCornerSlots<out T : ForInventory> internal constructor(
class InventoryCornerSlots<T : ForInventory> internal constructor(
val ifBottomLeft: Boolean = false,
val ifBottomRight: Boolean = false,
val ifTopLeft: Boolean = false,
val ifTopRight: Boolean = false
) : InventorySlotCompound<T> {
override fun withInvType(invType: InventoryType<in T>) = HashSet<InventorySlot>().apply {
override fun withInvType(invType: InventoryType<T>) = HashSet<InventorySlot>().apply {
val dimensions = invType.dimensions

View File

@@ -7,7 +7,7 @@ import org.bukkit.event.inventory.InventoryType
import org.bukkit.inventory.Inventory
import org.bukkit.inventory.InventoryHolder
class InventoryType<T : ForInventory>(
class InventoryType<in T : ForInventory>(
val dimensions: InventoryDimensions,
val bukkitType: InventoryType? = null
) {