diff --git a/src/main/kotlin/net/axay/kspigot/inventory/InventoryGUI.kt b/src/main/kotlin/net/axay/kspigot/inventory/InventoryGUI.kt index 49cd1595..84f7333d 100644 --- a/src/main/kotlin/net/axay/kspigot/inventory/InventoryGUI.kt +++ b/src/main/kotlin/net/axay/kspigot/inventory/InventoryGUI.kt @@ -3,13 +3,9 @@ package net.axay.kspigot.inventory import net.axay.kspigot.event.listen -import net.axay.kspigot.event.register import net.axay.kspigot.main.KSpigot import org.bukkit.entity.HumanEntity -import org.bukkit.event.EventHandler -import org.bukkit.event.Listener import org.bukkit.event.inventory.InventoryClickEvent -import org.bukkit.event.inventory.InventoryInteractEvent import org.bukkit.inventory.Inventory import org.bukkit.inventory.InventoryView @@ -71,7 +67,7 @@ class InventoryGUIClickEvent( class InventoryGUIData( val plugin: KSpigot, - val inventoryGUIType: InventoryGUIType<*>, + val inventoryType: InventoryType<*>, val title: String?, val pages: Map ) @@ -99,7 +95,7 @@ class InventoryGUIShared( inventoryGUIData: InventoryGUIData ) : InventoryGUI(inventoryGUIData) { - override val bukkitInventory by lazy { data.inventoryGUIType.createBukkitInv(null, data.title) } + override val bukkitInventory by lazy { data.inventoryType.createBukkitInv(null, data.title) } override fun isThisInv(inventory: Inventory) = inventory == bukkitInventory @@ -111,7 +107,7 @@ class InventoryGUIShared( data.pages[page]?.slots?.let { slots -> - val dimensions = data.inventoryGUIType.dimensions + val dimensions = data.inventoryType.dimensions if (ifOffset()) { dimensions.invSlots.forEach { diff --git a/src/main/kotlin/net/axay/kspigot/inventory/InventoryGUIBuilder.kt b/src/main/kotlin/net/axay/kspigot/inventory/InventoryGUIBuilder.kt index aea37bf4..d59e8f80 100644 --- a/src/main/kotlin/net/axay/kspigot/inventory/InventoryGUIBuilder.kt +++ b/src/main/kotlin/net/axay/kspigot/inventory/InventoryGUIBuilder.kt @@ -6,13 +6,13 @@ import net.axay.kspigot.main.KSpigot import org.bukkit.inventory.ItemStack inline fun KSpigot.inventoryGUI( - type: InventoryGUIType, + type: InventoryType, builder: InventoryGUIBuilder.() -> Unit, ) = InventoryGUIBuilder(this, type).apply(builder).build() class InventoryGUIBuilder( private val kSpigot: KSpigot, - val type: InventoryGUIType + val type: InventoryType ) { var title: String = "" @@ -31,7 +31,7 @@ class InventoryGUIBuilder( } class InventoryGUIPageBuilder( - val type: InventoryGUIType, + val type: InventoryType, val page: Int ) { @@ -99,7 +99,7 @@ class InventoryGUIPageBuilder( )) private fun slots(slots: InventorySlotCompound, element: InventoryGUISlot) - = slots.withGUIType(type).forEach { curSlot -> + = slots.withInvType(type).forEach { curSlot -> curSlot.realSlotIn(type.dimensions)?.let { guiSlots[it] = element } } diff --git a/src/main/kotlin/net/axay/kspigot/inventory/InventoryGUIPageChange.kt b/src/main/kotlin/net/axay/kspigot/inventory/InventoryGUIPageChange.kt index e5a9020f..256b9ec7 100644 --- a/src/main/kotlin/net/axay/kspigot/inventory/InventoryGUIPageChange.kt +++ b/src/main/kotlin/net/axay/kspigot/inventory/InventoryGUIPageChange.kt @@ -38,7 +38,7 @@ class InventoryGUIPageChanger(private val effect: InventoryGUIPageChangeEffect) InventoryGUIPageChangeEffect.SLIDE_HORIZONTALLY -> { - val width = gui.data.inventoryGUIType.dimensions.width + val width = gui.data.inventoryType.dimensions.width changePageEffect(gui.data.plugin, fromPage, toPage, width) { currentOffset, ifInverted -> if (ifInverted) { @@ -54,7 +54,7 @@ class InventoryGUIPageChanger(private val effect: InventoryGUIPageChangeEffect) InventoryGUIPageChangeEffect.SLIDE_VERTICALLY -> { - val height = gui.data.inventoryGUIType.dimensions.heigth + val height = gui.data.inventoryType.dimensions.heigth changePageEffect(gui.data.plugin, fromPage, toPage, height) { currentOffset, ifInverted -> if (ifInverted) { @@ -70,7 +70,7 @@ class InventoryGUIPageChanger(private val effect: InventoryGUIPageChangeEffect) InventoryGUIPageChangeEffect.SWIPE_HORIZONTALLY -> { - val width = gui.data.inventoryGUIType.dimensions.width + val width = gui.data.inventoryType.dimensions.width changePageEffect(gui.data.plugin, fromPage, toPage, width) { currentOffset, ifInverted -> if (ifInverted) { @@ -84,7 +84,7 @@ class InventoryGUIPageChanger(private val effect: InventoryGUIPageChangeEffect) InventoryGUIPageChangeEffect.SWIPE_VERTICALLY -> { - val height = gui.data.inventoryGUIType.dimensions.heigth + val height = gui.data.inventoryType.dimensions.heigth changePageEffect(gui.data.plugin, fromPage, toPage, height) { currentOffset, ifInverted -> if (ifInverted) { diff --git a/src/main/kotlin/net/axay/kspigot/inventory/InventoryGUISlots.kt b/src/main/kotlin/net/axay/kspigot/inventory/InventoryGUISlots.kt index 7cc8bf1a..f5e44a09 100644 --- a/src/main/kotlin/net/axay/kspigot/inventory/InventoryGUISlots.kt +++ b/src/main/kotlin/net/axay/kspigot/inventory/InventoryGUISlots.kt @@ -2,6 +2,8 @@ package net.axay.kspigot.inventory +import net.axay.kspigot.kotlinextensions.MinMaxPair + // INVENTORY data class InventoryDimensions(val width: Int, val heigth: Int) { @@ -69,24 +71,28 @@ data class InventorySlot(val row: Int, val slotInRow: Int) : Comparable { - fun withGUIType(invType: InventoryGUIType): Collection +internal interface InventorySlotCompound { + fun withInvType(invType: InventoryType): Collection } -open class SingleInventorySlot(val inventorySlot: InventorySlot) - : InventorySlotCompound { +open class SingleInventorySlot internal constructor( + val inventorySlot: InventorySlot +) : InventorySlotCompound { constructor(row: Int, slotInRow: Int) : this(InventorySlot(row, slotInRow)) private val slotAsList = listOf(inventorySlot) - override fun withGUIType(invType: InventoryGUIType) = slotAsList + override fun withInvType(invType: InventoryType) = slotAsList } -enum class InventorySlotRangeType { LINEAR, RECTANGLE } +internal enum class InventorySlotRangeType { + LINEAR, + RECTANGLE, +} -class InventorySlotRange ( +class InventorySlotRange internal constructor( startSlot: SingleInventorySlot, endSlot: SingleInventorySlot, @@ -99,16 +105,12 @@ class InventorySlotRange ( override val endInclusive: InventorySlot init { - if (startSlot.inventorySlot <= endSlot.inventorySlot) { - start = startSlot.inventorySlot - endInclusive = endSlot.inventorySlot - } else { - start = endSlot.inventorySlot - endInclusive = startSlot.inventorySlot - } + val minMaxPair = MinMaxPair(startSlot.inventorySlot, endSlot.inventorySlot) + start = minMaxPair.min + endInclusive = minMaxPair.max } - override fun withGUIType(invType: InventoryGUIType) + override fun withInvType(invType: InventoryType) = LinkedHashSet().apply { when (type) { @@ -145,24 +147,107 @@ class InventorySlotRange ( } -// SLOT RANGE OPERATOR FUNCTIONS - +/** + * This range contains all slots having an index between + * the indeces of the two given slots. + */ 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) = InventorySlotRange(this, slot, InventorySlotRangeType.RECTANGLE) +class InventoryRowSlots internal constructor( + val row: Int +) : InventorySlotCompound { + + override fun withInvType(invType: InventoryType) = HashSet().apply { + for (slotInRow in 1 .. invType.dimensions.width) + this += InventorySlot(row, slotInRow) + } + +} + +class InventoryColumnSlots internal constructor( + val column: Int +) : InventorySlotCompound { + + override fun withInvType(invType: InventoryType) = HashSet().apply { + for (row in 1 .. invType.dimensions.heigth) + this += InventorySlot(row, column) + } + +} + +class InventoryBorderSlots internal constructor( + val padding: Int +) : InventorySlotCompound { + + override fun withInvType(invType: InventoryType) = HashSet().apply { + + val dimensions = invType.dimensions + + for (currentPadding in 0 until padding) { + for (slotInRow in 1 + currentPadding .. dimensions.width - currentPadding) { + this += InventorySlot(1, slotInRow) + this += InventorySlot(dimensions.heigth, slotInRow) + } + for (row in 2 + currentPadding until dimensions.heigth - currentPadding) { + this += InventorySlot(row, 1) + this += InventorySlot(row, dimensions.width) + } + } + + } + +} + +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 { + + val dimensions = invType.dimensions + + if (ifBottomLeft) this += InventorySlot(1, 1) + if (ifBottomRight) this += InventorySlot(1, dimensions.width) + if (ifTopLeft) this += InventorySlot(dimensions.heigth, 1) + if (ifTopRight) this += InventorySlot(dimensions.heigth, dimensions.width) + + } + +} + // SLOT TYPE SAFETY +// COLUMNS + +interface ForColumnOne : ForInventoryWidthThree, ForInventoryWidthFive, ForInventoryWidthNine +interface ForColumnTwo : ForInventoryWidthThree, ForInventoryWidthFive, ForInventoryWidthNine +interface ForColumnThree : ForInventoryWidthThree, ForInventoryWidthFive, ForInventoryWidthNine +interface ForColumnFour : ForInventoryWidthFive, ForInventoryWidthNine +interface ForColumnFive : ForInventoryWidthFive, ForInventoryWidthNine +interface ForColumnSix : ForInventoryWidthNine +interface ForColumnSeven : ForInventoryWidthNine +interface ForColumnEight : ForInventoryWidthNine +interface ForColumnNine : ForInventoryWidthNine + // ROWS + interface ForRowOne : ForInventoryOneByNine, ForInventoryTwoByNine, ForInventoryThreeByNine, ForInventoryFourByNine, ForInventoryFiveByNine, ForInventorySixByNine interface ForRowTwo : ForInventoryTwoByNine, ForInventoryThreeByNine, ForInventoryFourByNine, ForInventoryFiveByNine, ForInventorySixByNine interface ForRowThree : ForInventoryThreeByNine, ForInventoryFourByNine, ForInventoryFiveByNine, ForInventorySixByNine interface ForRowFour : ForInventoryFourByNine, ForInventoryFiveByNine, ForInventorySixByNine interface ForRowFive : ForInventoryFiveByNine, ForInventorySixByNine interface ForRowSix : ForInventorySixByNine - // EDGE CASES: // ROW ONE interface ForRowOneSlotOneToThree : ForRowOne, ForInventoryOneByFive, ForInventoryThreeByThree @@ -171,8 +256,13 @@ interface ForRowOneSlotFourToFive : ForRowOne, ForInventoryOneByFive interface ForRowTwoSlotOneToThree : ForRowTwo, ForInventoryThreeByThree // ROW THREE interface ForRowThreeSlotOneToThree : ForRowThree, ForInventoryThreeByThree +// COMPLETE ROWS (including the edge cases) +interface ForCompleteRowOne : ForRowOne, ForRowOneSlotOneToThree, ForRowOneSlotFourToFive +interface ForCompleteRowTwo : ForRowTwo, ForRowTwoSlotOneToThree +interface ForCompleteRowThree : ForRowThree, ForRowThreeSlotOneToThree object Slots { + // ROW ONE val RowOneSlotOne = SingleInventorySlot(1, 1) val RowOneSlotTwo = SingleInventorySlot(1, 2) @@ -233,4 +323,41 @@ object Slots { val RowSixSlotSeven = SingleInventorySlot(6, 7) val RowSixSlotEight = SingleInventorySlot(6, 8) val RowSixSlotNine = SingleInventorySlot(6, 9) + + // ROW + val RowOne = InventoryRowSlots(1) + val RowTwo = InventoryRowSlots(2) + val RowThree = InventoryRowSlots(3) + val RowFour = InventoryRowSlots(4) + val RowFive = InventoryRowSlots(5) + val RowSix = InventoryRowSlots(6) + + // COLUMN + val ColumnOne = InventoryColumnSlots(1) + val ColumnTwo = InventoryColumnSlots(2) + val ColumnThree = InventoryColumnSlots(3) + val ColumnFour = InventoryColumnSlots(4) + val ColumnFive = InventoryColumnSlots(5) + val ColumnSix = InventoryColumnSlots(6) + val ColumnSeven = InventoryColumnSlots(7) + val ColumnEight = InventoryColumnSlots(8) + val ColumnNine = InventoryColumnSlots(9) + + // BORDER + val BorderPaddingOne = InventoryBorderSlots(1) + val BorderPaddingTwo = InventoryBorderSlots(2) + val BorderPaddingThree = InventoryBorderSlots(3) + val Border = BorderPaddingOne + + // CORNER + val Corners = InventoryCornerSlots() + val CornersLeft = InventoryCornerSlots(ifBottomLeft = true, ifTopLeft = true) + val CornersRight = InventoryCornerSlots(ifBottomRight = true, ifTopRight = true) + val CornersBottom = InventoryCornerSlots(ifBottomLeft = true, ifBottomRight = true) + val CornersTop = InventoryCornerSlots(ifTopLeft = true, ifTopRight = true) + val CornerBottomLeft = InventoryCornerSlots(ifBottomLeft = true) + val CornerBottomRight = InventoryCornerSlots(ifBottomRight = true) + val CornerTopLeft = InventoryCornerSlots(ifTopLeft = true) + val CornerTopRight = InventoryCornerSlots(ifTopRight = true) + } \ No newline at end of file diff --git a/src/main/kotlin/net/axay/kspigot/inventory/InventoryGUIType.kt b/src/main/kotlin/net/axay/kspigot/inventory/InventoryType.kt similarity index 53% rename from src/main/kotlin/net/axay/kspigot/inventory/InventoryGUIType.kt rename to src/main/kotlin/net/axay/kspigot/inventory/InventoryType.kt index 3c3bd468..7b3d269b 100644 --- a/src/main/kotlin/net/axay/kspigot/inventory/InventoryGUIType.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 InventoryGUIType( +class InventoryType( val dimensions: InventoryDimensions, val bukkitType: InventoryType? = null ) { @@ -16,14 +16,14 @@ class InventoryGUIType( companion object { - val ONE_BY_NINE = InventoryGUIType(InventoryDimensions(9, 1)) - val TWO_BY_NINE = InventoryGUIType(InventoryDimensions(9, 2)) - val THREE_BY_NINE = InventoryGUIType(InventoryDimensions(9, 3)) - val FOUR_BY_NINE = InventoryGUIType(InventoryDimensions(9, 4)) - val FIVE_BY_NINE = InventoryGUIType(InventoryDimensions(9, 5)) - val SIX_BY_NINE = InventoryGUIType(InventoryDimensions(9, 6)) - val ONE_BY_FIVE = InventoryGUIType(InventoryDimensions(5, 1), bukkitType = InventoryType.HOPPER) - val THREE_BY_THREE = InventoryGUIType(InventoryDimensions(3, 3), bukkitType = InventoryType.DROPPER) + val ONE_BY_NINE = InventoryType(InventoryDimensions(9, 1)) + val TWO_BY_NINE = InventoryType(InventoryDimensions(9, 2)) + val THREE_BY_NINE = InventoryType(InventoryDimensions(9, 3)) + val FOUR_BY_NINE = InventoryType(InventoryDimensions(9, 4)) + val FIVE_BY_NINE = InventoryType(InventoryDimensions(9, 5)) + val SIX_BY_NINE = InventoryType(InventoryDimensions(9, 6)) + val ONE_BY_FIVE = InventoryType(InventoryDimensions(5, 1), bukkitType = InventoryType.HOPPER) + val THREE_BY_THREE = InventoryType(InventoryDimensions(3, 3), bukkitType = InventoryType.DROPPER) } @@ -53,4 +53,8 @@ interface ForInventoryOneByFive : ForInventory interface ForEveryInventory : ForInventoryOneByNine, ForInventoryTwoByNine, ForInventoryThreeByNine, ForInventoryFourByNine, ForInventoryFiveByNine, ForInventorySixByNine, - ForInventoryThreeByThree, ForInventoryOneByFive \ No newline at end of file + ForInventoryThreeByThree, ForInventoryOneByFive + +interface ForInventoryWidthThree : ForInventoryThreeByThree +interface ForInventoryWidthFive : ForInventoryOneByFive +interface ForInventoryWidthNine : ForInventoryOneByNine, ForInventoryTwoByNine, ForInventoryThreeByNine, ForInventoryFourByNine, ForInventoryFiveByNine, ForInventorySixByNine \ No newline at end of file