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

View File

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

View File

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