From 2449e7dc751f823712fd6ea87f741c1fffc58f0b Mon Sep 17 00:00:00 2001 From: Tech <49742865+DebitCardz@users.noreply.github.com> Date: Tue, 1 Aug 2023 08:36:58 -0400 Subject: [PATCH] refactor: move GUIType to sealed interface (#7) This change simply leads to more type safety within the system and it's also just easier to manage. --- build.gradle.kts | 4 +- src/main/kotlin/me/tech/mcchestui/GUI.kt | 45 +++++------------ .../kotlin/me/tech/mcchestui/GUIHelper.kt | 21 +------- src/main/kotlin/me/tech/mcchestui/GUIType.kt | 50 ++++++++++++++++--- 4 files changed, 57 insertions(+), 63 deletions(-) diff --git a/build.gradle.kts b/build.gradle.kts index ecf732c..571e2a8 100644 --- a/build.gradle.kts +++ b/build.gradle.kts @@ -8,7 +8,7 @@ val githubActor = project.findProperty("gpr.user") as String? ?: System.getenv(" val githubToken = project.findProperty("gpr.key") as String? ?: System.getenv("GITHUB_TOKEN") group = "me.tech" -version = "0.0.8" +version = "1.0.0" repositories { mavenCentral() @@ -47,4 +47,4 @@ publishing { from(components["java"]) } } -} +} \ No newline at end of file diff --git a/src/main/kotlin/me/tech/mcchestui/GUI.kt b/src/main/kotlin/me/tech/mcchestui/GUI.kt index 2cd7f24..153792f 100644 --- a/src/main/kotlin/me/tech/mcchestui/GUI.kt +++ b/src/main/kotlin/me/tech/mcchestui/GUI.kt @@ -24,15 +24,8 @@ class GUI( plugin: JavaPlugin, val title: Component, val type: GUIType, - rows: Int, private val render: GUI.() -> Unit ): Listener { - @Deprecated( - message = "Use allowPlaceItem instead.", - replaceWith = ReplaceWith("allowItemPlacement") - ) - var allowShiftClicking: Boolean = false - /** * Allow for [ItemStack] to be placed in the [GUI]. */ @@ -63,27 +56,13 @@ class GUI( */ var onCloseInventory: GUICloseEvent? = null - val rows = - if(type == GUIType.CHEST) { - rows - // We can assert this won't be null since the - // only null value is already checked. - } else { - type.rows!! - } - - val inventory = - // Chest GUI. - if(type == GUIType.CHEST) { - plugin.server.createInventory(null, type.slotsPerRow * rows, title) - // Other GUi. - } else { - plugin.server.createInventory(null, type.inventoryType, title) - } - + val inventory = if(type is GUIType.Chest) { + plugin.server.createInventory(null, type.slotsPerRow * type.rows, title) + } else { + plugin.server.createInventory(null, type.inventoryType, title) + } - // prevent user from inputting row amount for anything other than chest. - private var slots = arrayOfNulls(type.slotsPerRow * this.rows) + private var slots = arrayOfNulls(type.slotsPerRow * type.rows) init { plugin.server.pluginManager.registerEvents(this, plugin) @@ -101,7 +80,7 @@ class GUI( */ fun refresh() { inventory.clear() - slots = arrayOfNulls(type.slotsPerRow * rows) + slots = arrayOfNulls(type.slotsPerRow * type.rows) this.render() } @@ -164,15 +143,15 @@ class GUI( val x1 = 2 // Just makes it work with 1 row chest guis. - val y1 = if(type == GUIType.CHEST && rows == 1 || type == GUIType.HOPPER) 1 else 2 + val y1 = if(type is GUIType.Chest && type.rows == 1 || type is GUIType.Hopper) 1 else 2 val x2 = type.slotsPerRow - 1 // Doesn't really matter if we hard code these values, // what're they gonna do? Change? well besides chest guis. val y2 = when(type) { - GUIType.CHEST -> if(rows > 2) rows - 1 else 2 - GUIType.HOPPER -> 1 - GUIType.DISPENSER -> 2 + is GUIType.Chest -> if(type.rows > 2) type.rows - 1 else 2 + is GUIType.Hopper -> 1 + is GUIType.Dispenser -> 2 } fill(x1, y1, x2, y2) { @@ -186,7 +165,7 @@ class GUI( * @param builder slot builder. */ fun all(builder: Slot.() -> Unit) { - fill(1, 1, type.slotsPerRow, rows, builder) + fill(1, 1, type.slotsPerRow, type.rows, builder) } /** * Set the item of the next available slot not occupied by any item. diff --git a/src/main/kotlin/me/tech/mcchestui/GUIHelper.kt b/src/main/kotlin/me/tech/mcchestui/GUIHelper.kt index 0bb3c3b..deb5e66 100644 --- a/src/main/kotlin/me/tech/mcchestui/GUIHelper.kt +++ b/src/main/kotlin/me/tech/mcchestui/GUIHelper.kt @@ -4,24 +4,6 @@ import net.kyori.adventure.text.Component import org.bukkit.entity.HumanEntity import org.bukkit.plugin.java.JavaPlugin -/** - * Create a GUI. - * Automatically sets the rows to 1. - * - * @param plugin - * @param title Title of the GUI - * @param type Type of GUI to generate - * @return [GUI] Object - */ -fun gui( - plugin: JavaPlugin, - title: Component, - type: GUIType, - render: GUI.() -> Unit -): GUI { - return gui(plugin, title, type, 1, render) -} - /** * Create a GUI. * @@ -35,10 +17,9 @@ fun gui( plugin: JavaPlugin, title: Component, type: GUIType, - rows: Int, render: GUI.() -> Unit ): GUI { - return GUI(plugin, title, type, rows, render).apply(render) + return GUI(plugin, title, type, render).apply(render) } /** diff --git a/src/main/kotlin/me/tech/mcchestui/GUIType.kt b/src/main/kotlin/me/tech/mcchestui/GUIType.kt index 4dd420e..83c9d40 100644 --- a/src/main/kotlin/me/tech/mcchestui/GUIType.kt +++ b/src/main/kotlin/me/tech/mcchestui/GUIType.kt @@ -8,12 +8,46 @@ package me.tech.mcchestui import org.bukkit.event.inventory.InventoryType -enum class GUIType( - val inventoryType: InventoryType, - val slotsPerRow: Int, - val rows: Int? -) { - CHEST(InventoryType.CHEST, 9, null), - DISPENSER(InventoryType.DISPENSER, 3, 3), - HOPPER(InventoryType.HOPPER, 5, 1); +sealed interface GUIType { + val slotsPerRow: Int + val rows: Int + val inventoryType: InventoryType + + data class Chest(override val rows: Int) : GUIType { + override val slotsPerRow: Int + get() = 9 + + override val inventoryType: InventoryType + get() = InventoryType.CHEST + + init { + if(rows < 1 || rows > 6) { + throw IllegalArgumentException( + "chest rows cannot be ${if(rows < 1) "below" else "above"} $rows." + ) + } + } + } + + object Dispenser : GUIType { + override val slotsPerRow: Int + get() = 3 + + override val rows: Int + get() = 3 + + override val inventoryType: InventoryType + get() = InventoryType.DISPENSER + } + + object Hopper : GUIType { + override val slotsPerRow: Int + get() = 5 + + override val rows: Int + get() = 1 + + override val inventoryType: InventoryType + get() = InventoryType.HOPPER + } } \ No newline at end of file