Skip to content

Commit

Permalink
misc: code cleanup
Browse files Browse the repository at this point in the history
  • Loading branch information
DebitCardz committed Jun 28, 2023
1 parent 0af0aa6 commit 59a8070
Show file tree
Hide file tree
Showing 3 changed files with 42 additions and 28 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.5"
version = "0.0.7"

repositories {
mavenCentral()
Expand Down
16 changes: 11 additions & 5 deletions src/main/kotlin/me/tech/mcchestui/GUIHelper.kt
Original file line number Diff line number Diff line change
@@ -1,17 +1,17 @@
package me.tech.mcchestui

import net.kyori.adventure.text.Component
import net.kyori.adventure.text.TextComponent
import org.bukkit.entity.Player
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
* @return [GUI] Object
*/
fun gui(
plugin: JavaPlugin,
Expand All @@ -24,11 +24,12 @@ fun gui(

/**
* Create a GUI.
*
* @param plugin
* @param title Title of the GUI
* @param type Type of GUI to generate
* @param rows Amount of rows in the Chest GUI
* @return GUI Object
* @return [GUI] Object
*/
fun gui(
plugin: JavaPlugin,
Expand All @@ -40,4 +41,9 @@ fun gui(
return GUI(plugin, title, type, rows, render).apply(render)
}

fun Player.openGUI(gui: GUI) = openInventory(gui.inventory)
/**
* Open a [GUI] for a [HumanEntity].
*
* @param gui [GUI] to open.
*/
fun HumanEntity.openGUI(gui: GUI) = openInventory(gui.inventory)
52 changes: 30 additions & 22 deletions src/main/kotlin/me/tech/mcchestui/GUIItem.kt
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,13 @@ import org.bukkit.inventory.ItemStack
import org.bukkit.inventory.meta.ItemMeta
import org.bukkit.inventory.meta.SkullMeta

fun item(type: Material = Material.AIR, builder: GUIItem.() -> Unit = {}) =
/**
* Construct a [ItemStack] to be placed in a [GUI.Slot].
* @param type material type.
* @param builder [GUIItem] builder.
*/
// ensure backwards compatibility.
fun GUI.Slot.item(type: Material = Material.AIR, builder: GUIItem.() -> Unit = {}) =
GUIItem(type).apply(builder)

class GUIItem(
Expand All @@ -43,15 +49,17 @@ class GUIItem(
* Modify the [ItemStack] of the [GUIItem].
* @param builder [ItemStack] builder.
*/
fun stack(builder: ItemStack.() -> Unit) =
fun stack(builder: ItemStack.() -> Unit) {
stack.apply(builder)
}

/**
* Modify the [ItemMeta] of the [GUIItem].
* @param builder [ItemMeta] builder.
*/
fun meta(builder: ItemMeta.() -> Unit) =
itemMeta?.apply(builder)?.also { stack.itemMeta = it }
fun meta(builder: ItemMeta.() -> Unit) {
stack.editMeta(builder)
}

/**
* Current display name of the [ItemStack].
Expand All @@ -65,9 +73,9 @@ class GUIItem(
value
}

itemMeta.apply {
meta {
displayName(name)
}.also { stack.itemMeta = it }
}
}

/**
Expand All @@ -82,9 +90,9 @@ class GUIItem(
value
}

itemMeta.apply {
meta {
lore(sanitizeLore(lore))
}.also { stack.itemMeta = it }
}
}

/**
Expand All @@ -104,9 +112,9 @@ class GUIItem(
var skullOwner: OfflinePlayer?
get() = (itemMeta as? SkullMeta)?.owningPlayer
set(value) {
(itemMeta as? SkullMeta)
?.apply { owningPlayer = value }
?.also { stack.itemMeta = it }
meta {
(this as? SkullMeta)?.owningPlayer = value
}
}

/**
Expand All @@ -117,28 +125,28 @@ class GUIItem(
var playerProfile: PlayerProfile?
get() = (itemMeta as? SkullMeta)?.playerProfile
set(value) {
(itemMeta as? SkullMeta)
?.apply { playerProfile = value }
?.also { stack.itemMeta = it }
meta {
(this as? SkullMeta)?.playerProfile = value
}
}

/**
* Whether the [ItemStack] is glowing.
*/
var glowing: Boolean = false
set(value) {
// Add glow.
if(value) {
itemMeta.apply {
// Add glow.
meta {
addEnchant(Enchantment.ARROW_INFINITE, 0, true)
addItemFlags(ItemFlag.HIDE_ENCHANTS)
}.also { stack.itemMeta = it }
// Remove glow.
}
} else {
itemMeta.apply {
// Remove glow.
meta {
removeEnchant(Enchantment.ARROW_INFINITE)
removeItemFlags(ItemFlag.HIDE_ENCHANTS)
}.also { stack.itemMeta = it }
}
}

field = value
Expand All @@ -150,9 +158,9 @@ class GUIItem(
var customModelData: Int
get() = itemMeta.customModelData
set(value) {
itemMeta.apply {
meta {
setCustomModelData(value)
}.also { stack.itemMeta = it }
}
}

/**
Expand Down

0 comments on commit 59a8070

Please sign in to comment.