Skip to content

Commit

Permalink
refactor: move to one-indexed slots (#4)
Browse files Browse the repository at this point in the history
  • Loading branch information
DebitCardz authored Aug 1, 2023
1 parent e8cfeea commit ca67118
Show file tree
Hide file tree
Showing 2 changed files with 25 additions and 23 deletions.
2 changes: 1 addition & 1 deletion build.gradle.kts
Original file line number Diff line number Diff line change
Expand Up @@ -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()
Expand Down
46 changes: 24 additions & 22 deletions src/main/kotlin/me/tech/mcchestui/GUI.kt
Original file line number Diff line number Diff line change
Expand Up @@ -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(
Expand Down Expand Up @@ -64,25 +63,27 @@ class GUI(
*/
var onCloseInventory: GUICloseEvent? = null

var slots = arrayOfNulls<Slot>(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<Slot>(type.slotsPerRow * this.rows)

init {
plugin.server.pluginManager.registerEvents(this, plugin)
Expand Down Expand Up @@ -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) {
Expand All @@ -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
Expand All @@ -199,7 +201,7 @@ class GUI(
return
}

slot(firstEmptySlot, builder)
slot(firstEmptySlot + 1, builder)
}

@EventHandler(priority = EventPriority.HIGHEST)
Expand Down

0 comments on commit ca67118

Please sign in to comment.