From f7e19dbb5b0af6b7481f12a8172b747bfd6361f0 Mon Sep 17 00:00:00 2001 From: bluefireoly Date: Tue, 29 Sep 2020 22:23:42 +0200 Subject: [PATCH] Inventory GUI update - e.g.: "gui[Slots.RowThreeSlotOne] = ItemStack(Material.BAMBOO)" now possible --- .../axay/kspigot/inventory/InventoryGUI.kt | 33 +++++++++++-------- .../kspigot/inventory/InventoryGUIBuilder.kt | 16 ++++----- .../kspigot/inventory/InventoryGUISlots.kt | 32 +++++++++--------- .../axay/kspigot/inventory/InventoryType.kt | 2 +- 4 files changed, 45 insertions(+), 38 deletions(-) diff --git a/src/main/kotlin/net/axay/kspigot/inventory/InventoryGUI.kt b/src/main/kotlin/net/axay/kspigot/inventory/InventoryGUI.kt index 84f7333d..3f13cf44 100644 --- a/src/main/kotlin/net/axay/kspigot/inventory/InventoryGUI.kt +++ b/src/main/kotlin/net/axay/kspigot/inventory/InventoryGUI.kt @@ -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() + private val registered = HashSet>() - 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( val bukkitEvent: InventoryClickEvent, - val gui: InventoryGUI, + val gui: InventoryGUI ) /* * INVENTORY GUI */ -class InventoryGUIData( +class InventoryGUIData( val plugin: KSpigot, - val inventoryType: InventoryType<*>, + val inventoryType: InventoryType, val title: String?, val pages: Map ) -abstract class InventoryGUI( - val data: InventoryGUIData +abstract class InventoryGUI( + val data: InventoryGUIData ) { 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, 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( + inventoryGUIData: InventoryGUIData +) : InventoryGUI(inventoryGUIData) { override val bukkitInventory by lazy { data.inventoryType.createBukkitInv(null, data.title) } @@ -143,6 +146,10 @@ class InventoryGUIShared( } + override fun set(slot: InventorySlotCompound, value: ItemStack) { + slot.withInvType(data.inventoryType).forEach { } + } + } class InventoryGUIPage( diff --git a/src/main/kotlin/net/axay/kspigot/inventory/InventoryGUIBuilder.kt b/src/main/kotlin/net/axay/kspigot/inventory/InventoryGUIBuilder.kt index d59e8f80..9447511d 100644 --- a/src/main/kotlin/net/axay/kspigot/inventory/InventoryGUIBuilder.kt +++ b/src/main/kotlin/net/axay/kspigot/inventory/InventoryGUIBuilder.kt @@ -10,9 +10,9 @@ inline fun KSpigot.inventoryGUI( builder: InventoryGUIBuilder.() -> Unit, ) = InventoryGUIBuilder(this, type).apply(builder).build() -class InventoryGUIBuilder( +class InventoryGUIBuilder( private val kSpigot: KSpigot, - val type: InventoryType + val type: InventoryType ) { var title: String = "" @@ -30,8 +30,8 @@ class InventoryGUIBuilder( } -class InventoryGUIPageBuilder( - val type: InventoryType, +class InventoryGUIPageBuilder( + val type: InventoryType, val page: Int ) { @@ -45,7 +45,7 @@ class InventoryGUIPageBuilder( * actions. If clicked, the specified [onClick] * function is invoked. */ - fun button(slots: InventorySlotCompound, itemStack: ItemStack, onClick: (InventoryGUIClickEvent) -> Unit) + fun button(slots: InventorySlotCompound, itemStack: ItemStack, onClick: (InventoryGUIClickEvent) -> Unit) = slots(slots, InventoryGUIButton(InventoryGUIElementData(itemStack), onClick)) /** @@ -67,7 +67,7 @@ class InventoryGUIPageBuilder( * This is a button which loads the specified * [toPage] if clicked. */ - fun pageChanger(slots: InventorySlotCompound, itemStack: ItemStack, toPage: Int, onChange: ((InventoryGUIClickEvent) -> Unit)? = null) + fun pageChanger(slots: InventorySlotCompound, itemStack: ItemStack, toPage: Int, onChange: ((InventoryGUIClickEvent) -> Unit)? = null) = slots(slots, InventoryGUIButtonPageChange( InventoryGUIElementData(itemStack), InventoryGUIPageChangeCalculator.InventoryGUIConsistentPageCalculator(toPage), @@ -79,7 +79,7 @@ class InventoryGUIPageBuilder( * page if clicked, and if a previous page * exists it is loaded. */ - fun previousPage(slots: InventorySlotCompound, itemStack: ItemStack, onChange: ((InventoryGUIClickEvent) -> Unit)? = null) + fun previousPage(slots: InventorySlotCompound, itemStack: ItemStack, onChange: ((InventoryGUIClickEvent) -> Unit)? = null) = slots(slots, InventoryGUIButtonPageChange( InventoryGUIElementData(itemStack), InventoryGUIPageChangeCalculator.InventoryGUIPreviousPageCalculator, @@ -91,7 +91,7 @@ class InventoryGUIPageBuilder( * page if clicked, and if a next page * exists it is loaded. */ - fun nextPage(slots: InventorySlotCompound, itemStack: ItemStack, onChange: ((InventoryGUIClickEvent) -> Unit)? = null) + fun nextPage(slots: InventorySlotCompound, itemStack: ItemStack, onChange: ((InventoryGUIClickEvent) -> Unit)? = null) = slots(slots, InventoryGUIButtonPageChange( InventoryGUIElementData(itemStack), InventoryGUIPageChangeCalculator.InventoryGUINextPageCalculator, diff --git a/src/main/kotlin/net/axay/kspigot/inventory/InventoryGUISlots.kt b/src/main/kotlin/net/axay/kspigot/inventory/InventoryGUISlots.kt index 85b974d1..13abd9df 100644 --- a/src/main/kotlin/net/axay/kspigot/inventory/InventoryGUISlots.kt +++ b/src/main/kotlin/net/axay/kspigot/inventory/InventoryGUISlots.kt @@ -72,10 +72,10 @@ data class InventorySlot(val row: Int, val slotInRow: Int) : Comparable { - fun withInvType(invType: InventoryType): Collection + fun withInvType(invType: InventoryType): Collection } -open class SingleInventorySlot internal constructor( +open class SingleInventorySlot internal constructor( val inventorySlot: InventorySlot ) : InventorySlotCompound { @@ -83,7 +83,7 @@ open class SingleInventorySlot internal constructor( private val slotAsList = listOf(inventorySlot) - override fun withInvType(invType: InventoryType) = slotAsList + override fun withInvType(invType: InventoryType) = slotAsList } @@ -94,8 +94,8 @@ internal enum class InventorySlotRangeType { class InventorySlotRange internal constructor( - startSlot: SingleInventorySlot, - endSlot: SingleInventorySlot, + startSlot: SingleInventorySlot, + endSlot: SingleInventorySlot, private val type: InventorySlotRangeType @@ -110,7 +110,7 @@ class InventorySlotRange internal constructor( endInclusive = minMaxPair.max } - override fun withInvType(invType: InventoryType) + override fun withInvType(invType: InventoryType) = LinkedHashSet().apply { when (type) { @@ -151,43 +151,43 @@ class InventorySlotRange internal constructor( * This range contains all slots having an index between * the indeces of the two given slots. */ -infix fun SingleInventorySlot.linTo(slot: SingleInventorySlot) +infix fun SingleInventorySlot.linTo(slot: SingleInventorySlot) = 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 SingleInventorySlot.rectTo(slot: SingleInventorySlot) +infix fun SingleInventorySlot.rectTo(slot: SingleInventorySlot) = InventorySlotRange(this, slot, InventorySlotRangeType.RECTANGLE) -class InventoryRowSlots internal constructor( +class InventoryRowSlots internal constructor( val row: Int ) : InventorySlotCompound { - override fun withInvType(invType: InventoryType) = HashSet().apply { + override fun withInvType(invType: InventoryType) = HashSet().apply { for (slotInRow in 1 .. invType.dimensions.width) this += InventorySlot(row, slotInRow) } } -class InventoryColumnSlots internal constructor( +class InventoryColumnSlots internal constructor( val column: Int ) : InventorySlotCompound { - override fun withInvType(invType: InventoryType) = HashSet().apply { + override fun withInvType(invType: InventoryType) = HashSet().apply { for (row in 1 .. invType.dimensions.heigth) this += InventorySlot(row, column) } } -class InventoryBorderSlots internal constructor( +class InventoryBorderSlots internal constructor( val padding: Int ) : InventorySlotCompound { - override fun withInvType(invType: InventoryType) = HashSet().apply { + override fun withInvType(invType: InventoryType) = HashSet().apply { val dimensions = invType.dimensions @@ -206,14 +206,14 @@ class InventoryBorderSlots internal constructor( } -class InventoryCornerSlots internal constructor( +class InventoryCornerSlots internal constructor( val ifBottomLeft: Boolean = false, val ifBottomRight: Boolean = false, val ifTopLeft: Boolean = false, val ifTopRight: Boolean = false ) : InventorySlotCompound { - override fun withInvType(invType: InventoryType) = HashSet().apply { + override fun withInvType(invType: InventoryType) = HashSet().apply { val dimensions = invType.dimensions diff --git a/src/main/kotlin/net/axay/kspigot/inventory/InventoryType.kt b/src/main/kotlin/net/axay/kspigot/inventory/InventoryType.kt index 7b3d269b..7c6dd689 100644 --- a/src/main/kotlin/net/axay/kspigot/inventory/InventoryType.kt +++ b/src/main/kotlin/net/axay/kspigot/inventory/InventoryType.kt @@ -7,7 +7,7 @@ import org.bukkit.event.inventory.InventoryType import org.bukkit.inventory.Inventory import org.bukkit.inventory.InventoryHolder -class InventoryType( +class InventoryType( val dimensions: InventoryDimensions, val bukkitType: InventoryType? = null ) {