Skip to content

Commit

Permalink
feat: More inventory extensions
Browse files Browse the repository at this point in the history
  • Loading branch information
CoasterFreakDE committed Jan 3, 2025
1 parent cd48321 commit 7f66313
Show file tree
Hide file tree
Showing 2 changed files with 83 additions and 5 deletions.
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
package cc.modlabs.kpaper.extensions

import cc.modlabs.kpaper.consts.NAMESPACE_GUI_IDENTIFIER
import cc.modlabs.kpaper.consts.NAMESPACE_ITEM_IDENTIFIER
import cc.modlabs.kpaper.inventory.ItemBuilder
import cc.modlabs.kpaper.inventory.toItemBuilder
import dev.fruxz.stacked.text
import org.bukkit.Material
Expand Down Expand Up @@ -103,4 +105,84 @@ fun Inventory.isIdentifiedAs(identifier: String): Boolean {
NAMESPACE_GUI_IDENTIFIER,
PersistentDataType.STRING
) == identifier
}

val Inventory.identifier: String?
get() = this.getItem(0)?.itemMeta?.persistentDataContainer?.get(
NAMESPACE_GUI_IDENTIFIER,
PersistentDataType.STRING
)

fun Inventory.identifier(namespacedKey: NamespacedKey): String? {
return this.getItem(0)?.itemMeta?.persistentDataContainer?.get(
namespacedKey,
PersistentDataType.STRING
)
}

fun ItemStack.isIdentifiedAs(identifier: String): Boolean {
return this.itemMeta?.persistentDataContainer?.get(
NAMESPACE_ITEM_IDENTIFIER,
PersistentDataType.STRING
) == identifier
}

val ItemStack.identifier: String?
get() = this.itemMeta?.persistentDataContainer?.get(
NAMESPACE_ITEM_IDENTIFIER,
PersistentDataType.STRING
)

fun ItemStack.identifier(namespacedKey: NamespacedKey): String? {
return this.itemMeta?.persistentDataContainer?.get(
namespacedKey,
PersistentDataType.STRING
)
}

fun ItemStack.hasKey(namespacedKey: NamespacedKey): Boolean {
return this.itemMeta?.persistentDataContainer?.has(
namespacedKey,
PersistentDataType.STRING
) == true
}

fun ItemStack.getKey(namespacedKey: NamespacedKey): String? {
return this.itemMeta?.persistentDataContainer?.get(
namespacedKey,
PersistentDataType.STRING
)
}

val PLACEHOLDER_GRAY = ItemBuilder(Material.GRAY_STAINED_GLASS_PANE) {
display(" ")
}.build()

fun Inventory.setItem(range: IntProgression, item: ItemStack) {
range.forEach { this.setItem(it, item) }
}

fun fillEmptyAndOpenInventory(player: Player, inv: Inventory, identifier: String? = null, vararg identifiers: Map<NamespacedKey, String>? = arrayOf()) {
fillEmpty(inv)
if (identifier != null) inv.identify(identifier, *identifiers)
player.openInventory(inv)
}

fun Inventory.fillEmpty(filler: ItemStack, identifier: String? = null, vararg identifiers: Map<NamespacedKey, String>? = arrayOf()) {
for (i in 0 until this.size) {
if (this.getItem(i) == null || this.getItem(i)!!.type == Material.AIR) {
this.setItem(i, filler)
}
}
if (identifier != null) this.identify(identifier, *identifiers)
}

fun fillEmpty(inventory: Inventory, identifier: String? = null, vararg identifiers: Map<NamespacedKey, String>? = arrayOf()) {
val item = PLACEHOLDER_GRAY
for (i in 0 until inventory.size) {
if (inventory.getItem(i) == null || inventory.getItem(i)!!.type == Material.AIR) {
inventory.setItem(i, item)
}
}
if (identifier != null) inventory.identify(identifier, *identifiers)
}
6 changes: 1 addition & 5 deletions src/main/kotlin/cc/modlabs/kpaper/main/KPlugin.kt
Original file line number Diff line number Diff line change
Expand Up @@ -2,14 +2,10 @@

import org.bukkit.plugin.java.JavaPlugin

val PluginMainInstance: KPlugin get() = PluginInstance

/**
* The main plugin instance. Less complicated name for internal usage.
*/
@PublishedApi
internal lateinit var PluginInstance: KPlugin
private set
lateinit var PluginInstance: KPlugin


abstract class KPlugin : JavaPlugin() {
Expand Down

0 comments on commit 7f66313

Please sign in to comment.