From ca6711816d8acf9de3981d5bdb08f813243b00ea Mon Sep 17 00:00:00 2001 From: Tech <49742865+DebitCardz@users.noreply.github.com> Date: Mon, 31 Jul 2023 20:45:55 -0400 Subject: [PATCH] refactor: move to one-indexed slots (#4) --- build.gradle.kts | 2 +- src/main/kotlin/me/tech/mcchestui/GUI.kt | 46 ++++++++++++------------ 2 files changed, 25 insertions(+), 23 deletions(-) diff --git a/build.gradle.kts b/build.gradle.kts index 98a6f3e..ecf732c 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.7" +version = "0.0.8" repositories { mavenCentral() diff --git a/src/main/kotlin/me/tech/mcchestui/GUI.kt b/src/main/kotlin/me/tech/mcchestui/GUI.kt index b01115f..2cd7f24 100644 --- a/src/main/kotlin/me/tech/mcchestui/GUI.kt +++ b/src/main/kotlin/me/tech/mcchestui/GUI.kt @@ -16,16 +16,15 @@ import org.bukkit.event.inventory.InventoryCloseEvent import org.bukkit.event.inventory.InventoryDragEvent import org.bukkit.inventory.ItemStack import org.bukkit.plugin.java.JavaPlugin -import java.time.Duration -fun toSlot(x: Int, y: Int, type: GUIType) = x + (y * type.slotsPerRow) +fun toSlot(x: Int, y: Int, type: GUIType) = (x - 1) + ((y - 1) * type.slotsPerRow) fun fromSlot(s: Int, type: GUIType) = Pair(s % type.slotsPerRow, s / type.slotsPerRow) class GUI( plugin: JavaPlugin, val title: Component, val type: GUIType, - val rows: Int, + rows: Int, private val render: GUI.() -> Unit ): Listener { @Deprecated( @@ -64,25 +63,27 @@ class GUI( */ var onCloseInventory: GUICloseEvent? = null - var slots = arrayOfNulls(type.slotsPerRow * rows) + 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. + // Other GUi. } else { plugin.server.createInventory(null, type.inventoryType, title) } - private val inventoryRows = - if(type == GUIType.CHEST) { - rows - // We can assert this won't be null since the - // only null value is already checked. - } else { - type.rows!! - } + + // prevent user from inputting row amount for anything other than chest. + private var slots = arrayOfNulls(type.slotsPerRow * this.rows) init { plugin.server.pluginManager.registerEvents(this, plugin) @@ -161,17 +162,17 @@ class GUI( fun fillBorder(builder: Slot.() -> Unit) { all(builder) - val x1 = 1 + val x1 = 2 // Just makes it work with 1 row chest guis. - val y1 = if(type == GUIType.CHEST && rows == 1) 0 else 1 + val y1 = if(type == GUIType.CHEST && rows == 1 || type == GUIType.HOPPER) 1 else 2 - val x2 = type.slotsPerRow - 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) inventoryRows - 2 else 1 - GUIType.HOPPER -> 0 - GUIType.DISPENSER -> 1 + GUIType.CHEST -> if(rows > 2) rows - 1 else 2 + GUIType.HOPPER -> 1 + GUIType.DISPENSER -> 2 } fill(x1, y1, x2, y2) { @@ -184,8 +185,9 @@ class GUI( * * @param builder slot builder. */ - fun all(builder: Slot.() -> Unit) = fill(0, 0, type.slotsPerRow - 1, inventoryRows - 1, builder) - + fun all(builder: Slot.() -> Unit) { + fill(1, 1, type.slotsPerRow, rows, builder) + } /** * Set the item of the next available slot not occupied by any item. * Any null item slot will be overridden as this method only checks for @@ -199,7 +201,7 @@ class GUI( return } - slot(firstEmptySlot, builder) + slot(firstEmptySlot + 1, builder) } @EventHandler(priority = EventPriority.HIGHEST)