Skip to content

Commit

Permalink
refactor: move GUIType to sealed interface (#7)
Browse files Browse the repository at this point in the history
This change simply leads to more type safety within the system and it's also just easier to manage.
  • Loading branch information
DebitCardz authored Aug 1, 2023
1 parent 6749bbf commit 2449e7d
Show file tree
Hide file tree
Showing 4 changed files with 57 additions and 63 deletions.
4 changes: 2 additions & 2 deletions 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.8"
version = "1.0.0"

repositories {
mavenCentral()
Expand Down Expand Up @@ -47,4 +47,4 @@ publishing {
from(components["java"])
}
}
}
}
45 changes: 12 additions & 33 deletions src/main/kotlin/me/tech/mcchestui/GUI.kt
Original file line number Diff line number Diff line change
Expand Up @@ -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].
*/
Expand Down Expand Up @@ -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<Slot>(type.slotsPerRow * this.rows)
private var slots = arrayOfNulls<Slot>(type.slotsPerRow * type.rows)

init {
plugin.server.pluginManager.registerEvents(this, plugin)
Expand All @@ -101,7 +80,7 @@ class GUI(
*/
fun refresh() {
inventory.clear()
slots = arrayOfNulls(type.slotsPerRow * rows)
slots = arrayOfNulls(type.slotsPerRow * type.rows)

this.render()
}
Expand Down Expand Up @@ -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) {
Expand All @@ -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.
Expand Down
21 changes: 1 addition & 20 deletions src/main/kotlin/me/tech/mcchestui/GUIHelper.kt
Original file line number Diff line number Diff line change
Expand Up @@ -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.
*
Expand All @@ -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)
}

/**
Expand Down
50 changes: 42 additions & 8 deletions src/main/kotlin/me/tech/mcchestui/GUIType.kt
Original file line number Diff line number Diff line change
Expand Up @@ -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
}
}

0 comments on commit 2449e7d

Please sign in to comment.