diff --git a/src/main/kotlin/cc/modlabs/kpaper/extensions/InventoryExtensions.kt b/src/main/kotlin/cc/modlabs/kpaper/extensions/InventoryExtensions.kt index dce10a3..35d1166 100644 --- a/src/main/kotlin/cc/modlabs/kpaper/extensions/InventoryExtensions.kt +++ b/src/main/kotlin/cc/modlabs/kpaper/extensions/InventoryExtensions.kt @@ -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 @@ -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? = arrayOf()) { + fillEmpty(inv) + if (identifier != null) inv.identify(identifier, *identifiers) + player.openInventory(inv) +} + +fun Inventory.fillEmpty(filler: ItemStack, identifier: String? = null, vararg identifiers: Map? = 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? = 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) } \ No newline at end of file diff --git a/src/main/kotlin/cc/modlabs/kpaper/main/KPlugin.kt b/src/main/kotlin/cc/modlabs/kpaper/main/KPlugin.kt index 3c6184d..802d6f3 100644 --- a/src/main/kotlin/cc/modlabs/kpaper/main/KPlugin.kt +++ b/src/main/kotlin/cc/modlabs/kpaper/main/KPlugin.kt @@ -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() {