Files
resourcepack
src
.gitignore
build.gradle.kts
gradle.properties
README.md
settings.gradle.kts
Code
23 1,139
src/main/kotlin/com/karmasmp/ghostblade/item/GhostBladeItem.kt
package com.karmasmp.ghostblade.item
import com.karmasmp.ghostblade.GhostBladePlugin
import com.karmasmp.ghostblade.identity.Identity
import net.kyori.adventure.text.Component
import net.kyori.adventure.text.format.TextDecoration
import org.bukkit.ChatColor
import org.bukkit.Material
import org.bukkit.NamespacedKey
import org.bukkit.inventory.ItemStack
import org.bukkit.persistence.PersistentDataType
class GhostBladeItem(private val plugin: GhostBladePlugin) {
companion object {
private const val GHOST_BLADE_KEY = "ghost_blade"
private const val IDENTITIES_KEY = "stored_identities"
private const val PRIMED_KEY = "primed"
}
private val ghostBladeKey = NamespacedKey(plugin, GHOST_BLADE_KEY)
private val identitiesKey = NamespacedKey(plugin, IDENTITIES_KEY)
private val primedKey = NamespacedKey(plugin, PRIMED_KEY)
fun createGhostBlade(): ItemStack {
val item = ItemStack(Material.NETHERITE_SWORD)
val meta = item.itemMeta ?: return item
val name = ChatColor.translateAlternateColorCodes('&', plugin.config.getString("item.name") ?: "&f&lGhost Blade")
meta.displayName(Component.text(name).decoration(TextDecoration.ITALIC, false))
val loreList = plugin.config.getStringList("item.lore").map {
val colored = ChatColor.translateAlternateColorCodes('&', it.replace("{count}", "0"))
Component.text(colored).decoration(TextDecoration.ITALIC, false)
}
meta.lore(loreList)
val customModelData = plugin.config.getInt("custom-model-data", 1001)
meta.setCustomModelData(customModelData)
meta.persistentDataContainer.set(ghostBladeKey, PersistentDataType.BYTE, 1.toByte())
meta.isUnbreakable = true
item.itemMeta = meta
return item
}
fun isGhostBlade(item: ItemStack?): Boolean {
if (item == null || item.type != Material.NETHERITE_SWORD) return false
val meta = item.itemMeta ?: return false
return meta.persistentDataContainer.has(ghostBladeKey, PersistentDataType.BYTE)
}
fun getStoredIdentities(item: ItemStack): List<Identity> {
if (!isGhostBlade(item)) return emptyList()
val meta = item.itemMeta ?: return emptyList()
val data = meta.persistentDataContainer.get(identitiesKey, PersistentDataType.STRING) ?: return emptyList()
if (data.isEmpty()) return emptyList()
return data.split(";;").mapNotNull { Identity.fromString(it) }
}
fun addIdentity(item: ItemStack, identity: Identity): ItemStack {
if (!isGhostBlade(item)) return item
val meta = item.itemMeta ?: return item
val identities = getStoredIdentities(item).toMutableList()
val maxIdentities = plugin.config.getInt("storage.max-identities", 10)
if (identities.size >= maxIdentities) {
identities.removeAt(0)
}
identities.add(identity)
val dataString = identities.joinToString(";;") { it.toString() }
meta.persistentDataContainer.set(identitiesKey, PersistentDataType.STRING, dataString)
updateLore(meta, identities.size)
item.itemMeta = meta
return item
}
fun isPrimed(item: ItemStack): Boolean {
if (!isGhostBlade(item)) return false
val meta = item.itemMeta ?: return false
return meta.persistentDataContainer.has(primedKey, PersistentDataType.BYTE)
}
fun setPrimed(item: ItemStack, primed: Boolean): ItemStack {
if (!isGhostBlade(item)) return item
val meta = item.itemMeta ?: return item
if (primed) {
meta.persistentDataContainer.set(primedKey, PersistentDataType.BYTE, 1.toByte())
} else {
meta.persistentDataContainer.remove(primedKey)
}
item.itemMeta = meta
return item
}
private fun updateLore(meta: org.bukkit.inventory.meta.ItemMeta, count: Int) {
val loreList = plugin.config.getStringList("item.lore").map {
val colored = ChatColor.translateAlternateColorCodes('&', it.replace("{count}", count.toString()))
Component.text(colored).decoration(TextDecoration.ITALIC, false)
}
meta.lore(loreList)
}
}