Skip to content

Commit

Permalink
feat: add GUIHeadItem for better typing (#20)
Browse files Browse the repository at this point in the history
Breaking change
  • Loading branch information
DebitCardz authored Nov 26, 2023
1 parent 334963d commit c50302d
Show file tree
Hide file tree
Showing 5 changed files with 83 additions and 63 deletions.
4 changes: 2 additions & 2 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@ repositories {
}

dependencies {
implementation("com.github.DebitCardz:mc-chestui-plus:1.3.0")
implementation("com.github.DebitCardz:mc-chestui-plus:1.3.1")
}
```
### Maven
Expand All @@ -34,7 +34,7 @@ dependencies {
<dependency>
<groupId>com.github.DebitCardz</groupId>
<artifactId>mc-chestui-plus</artifactId>
<version>1.3.0</version>
<version>1.3.1</version>
</dependency>

```
Expand Down
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 = "1.3.0"
version = "1.3.1"

repositories {
mavenCentral()
Expand Down
1 change: 1 addition & 0 deletions src/main/kotlin/me/tech/mcchestui/GUI.kt
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@

package me.tech.mcchestui

import me.tech.mcchestui.item.GUIItem
import me.tech.mcchestui.listeners.GUIListener
import me.tech.mcchestui.utils.GUICloseEvent
import me.tech.mcchestui.utils.GUIDragItemEvent
Expand Down
61 changes: 61 additions & 0 deletions src/main/kotlin/me/tech/mcchestui/item/GUIHeadItem.kt
Original file line number Diff line number Diff line change
@@ -0,0 +1,61 @@
package me.tech.mcchestui.item

import com.destroystokyo.paper.profile.PlayerProfile
import me.tech.mcchestui.GUI
import org.bukkit.Material
import org.bukkit.OfflinePlayer
import org.bukkit.inventory.ItemStack
import org.bukkit.inventory.meta.SkullMeta

/**
* Construct a [ItemStack] to be placed in a [GUI.Slot].
* Automatically uses [Material.PLAYER_HEAD].
* @param builder [GUIHeadItem] builder.
*/
fun GUI.Slot.headItem(
builder: GUIHeadItem.() -> Unit = {}
): GUIHeadItem {
return guiHeadItem(builder)
}

/**
* Construct a [ItemStack] to be placed in a [GUI.Slot].
* Automatically uses [Material.PLAYER_HEAD].
* @param builder [GUIHeadItem] builder.
*/
fun guiHeadItem(
builder: GUIHeadItem.() -> Unit = {}
): GUIHeadItem {
return GUIHeadItem().apply(builder)
}

class GUIHeadItem : GUIItem(Material.PLAYER_HEAD) {
override val itemMeta: SkullMeta
get() = stack.itemMeta as SkullMeta

/**
* Current [OfflinePlayer] that owns the player head.
*
* @warning Will only apply on items that inherit [SkullMeta].
*/
var skullOwner: OfflinePlayer?
get() = itemMeta.owningPlayer
set(value) {
stack.editMeta(SkullMeta::class.java) {
it.owningPlayer = value
}
}

/**
* Current [PlayerProfile] that owns the player head.
*
* @warning Will only apply on items that inherit [SkullMeta].
*/
var playerProfile: PlayerProfile?
get() = itemMeta.playerProfile
set(value) {
stack.editMeta(SkullMeta::class.java) {
it.playerProfile = value
}
}
}
Original file line number Diff line number Diff line change
@@ -1,43 +1,39 @@
package me.tech.mcchestui
package me.tech.mcchestui.item

import com.destroystokyo.paper.profile.PlayerProfile
import me.tech.mcchestui.GUI
import net.kyori.adventure.text.Component
import net.kyori.adventure.text.format.TextDecoration
import org.bukkit.Material
import org.bukkit.OfflinePlayer
import org.bukkit.enchantments.Enchantment
import org.bukkit.inventory.ItemFlag
import org.bukkit.inventory.ItemStack
import org.bukkit.inventory.meta.ItemMeta
import org.bukkit.inventory.meta.SkullMeta

/**
* 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 {
return GUIItem(type).apply(builder)
return guiItem(type, builder)
}

/**
* Construct a [ItemStack] to be placed in a [GUI.Slot].
* @param type material type.
* @param builder [GUIItem] builder.
*/
// created for prop functions.
fun guiItem(
type: Material = Material.AIR,
builder: GUIItem.() -> Unit = {}
): GUIItem {
return GUIItem(type).apply(builder)
}

class GUIItem(
open class GUIItem(
type: Material
) {
/**
Expand All @@ -48,7 +44,7 @@ class GUIItem(
/**
* [ItemMeta] of the [GUIItem].
*/
val itemMeta
open val itemMeta: ItemMeta
get() = stack.itemMeta

/**
Expand All @@ -64,14 +60,6 @@ class GUIItem(
stack.apply(builder)
}

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

/**
* Current display name of the [ItemStack].
*/
Expand All @@ -84,8 +72,8 @@ class GUIItem(
value
}

meta {
displayName(name)
stack.editMeta {
it.displayName(name)
}
}

Expand All @@ -101,8 +89,8 @@ class GUIItem(
value
}

meta {
lore(sanitizeLore(lore))
stack.editMeta {
it.lore(sanitizeLore(lore))
}
}

Expand All @@ -115,48 +103,18 @@ class GUIItem(
stack.amount = value
}

/**
* Current [OfflinePlayer] that owns the player head.
*
* @warning Will only apply on items that inherit [SkullMeta].
*/
var skullOwner: OfflinePlayer?
get() = (itemMeta as? SkullMeta)?.owningPlayer
set(value) {
meta {
(this as? SkullMeta)?.owningPlayer = value
}
}

/**
* Current [PlayerProfile] that owns the player head.
*
* @warning Will only apply on items that inherit [SkullMeta].
*/
var playerProfile: PlayerProfile?
get() = (itemMeta as? SkullMeta)?.playerProfile
set(value) {
meta {
(this as? SkullMeta)?.playerProfile = value
}
}

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

Expand All @@ -169,8 +127,8 @@ class GUIItem(
var customModelData: Int
get() = itemMeta.customModelData
set(value) {
meta {
setCustomModelData(value)
stack.editMeta {
it.setCustomModelData(value)
}
}

Expand Down

0 comments on commit c50302d

Please sign in to comment.