From 59a8070bffc4634cc4c150241e7bd5e6a4857c47 Mon Sep 17 00:00:00 2001 From: Tech Date: Wed, 28 Jun 2023 17:59:12 -0400 Subject: [PATCH] misc: code cleanup --- build.gradle.kts | 2 +- .../kotlin/me/tech/mcchestui/GUIHelper.kt | 16 ++++-- src/main/kotlin/me/tech/mcchestui/GUIItem.kt | 52 +++++++++++-------- 3 files changed, 42 insertions(+), 28 deletions(-) diff --git a/build.gradle.kts b/build.gradle.kts index 9dd72d8..98a6f3e 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.5" +version = "0.0.7" repositories { mavenCentral() diff --git a/src/main/kotlin/me/tech/mcchestui/GUIHelper.kt b/src/main/kotlin/me/tech/mcchestui/GUIHelper.kt index 2645de9..0bb3c3b 100644 --- a/src/main/kotlin/me/tech/mcchestui/GUIHelper.kt +++ b/src/main/kotlin/me/tech/mcchestui/GUIHelper.kt @@ -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, @@ -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, @@ -40,4 +41,9 @@ fun gui( return GUI(plugin, title, type, rows, render).apply(render) } -fun Player.openGUI(gui: GUI) = openInventory(gui.inventory) \ No newline at end of file +/** + * Open a [GUI] for a [HumanEntity]. + * + * @param gui [GUI] to open. + */ +fun HumanEntity.openGUI(gui: GUI) = openInventory(gui.inventory) \ No newline at end of file diff --git a/src/main/kotlin/me/tech/mcchestui/GUIItem.kt b/src/main/kotlin/me/tech/mcchestui/GUIItem.kt index 94730cc..40df641 100644 --- a/src/main/kotlin/me/tech/mcchestui/GUIItem.kt +++ b/src/main/kotlin/me/tech/mcchestui/GUIItem.kt @@ -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( @@ -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]. @@ -65,9 +73,9 @@ class GUIItem( value } - itemMeta.apply { + meta { displayName(name) - }.also { stack.itemMeta = it } + } } /** @@ -82,9 +90,9 @@ class GUIItem( value } - itemMeta.apply { + meta { lore(sanitizeLore(lore)) - }.also { stack.itemMeta = it } + } } /** @@ -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 + } } /** @@ -117,9 +125,9 @@ 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 + } } /** @@ -127,18 +135,18 @@ class GUIItem( */ 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 @@ -150,9 +158,9 @@ class GUIItem( var customModelData: Int get() = itemMeta.customModelData set(value) { - itemMeta.apply { + meta { setCustomModelData(value) - }.also { stack.itemMeta = it } + } } /**