Skip to content

Commit

Permalink
Update custom item code
Browse files Browse the repository at this point in the history
  • Loading branch information
esotericenderman committed Oct 28, 2024
1 parent 88db73c commit b0c3d22
Show file tree
Hide file tree
Showing 2 changed files with 8 additions and 17 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -15,9 +15,9 @@ abstract class CustomItem(private val plugin: CustomItemPlugin, private val item
plugin.customItemManager.addCustomItem(itemId, this)
}

protected abstract fun generateCustomItem(baseCustomItem: ItemStack?, player: Player?): ItemStack
protected abstract fun generateCustomItem(baseCustomItem: ItemStack, player: Player): ItemStack

fun getCustomItem(player: Player?): ItemStack {
fun getCustomItem(player: Player): ItemStack {
val item = ItemStack(material)
item.editMeta { meta: ItemMeta ->
meta.persistentDataContainer.set(
Expand All @@ -26,6 +26,7 @@ abstract class CustomItem(private val plugin: CustomItemPlugin, private val item
itemId
)
}

return generateCustomItem(item, player)
}

Expand All @@ -38,15 +39,9 @@ abstract class CustomItem(private val plugin: CustomItemPlugin, private val item
return false
}

val dataContainerItemIdValue =
itemStack.itemMeta.persistentDataContainer.get(plugin.customItemManager.customItemIdKey, PersistentDataType.STRING)
?: return false
val dataContainerItemIdValue = itemStack.itemMeta.persistentDataContainer.get(plugin.customItemManager.customItemIdKey, PersistentDataType.STRING) ?: return false

return try {
itemId === dataContainerItemIdValue
} catch (exception: IllegalArgumentException) {
false
}
return itemId == dataContainerItemIdValue
}

fun give(player: Player) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,16 +4,12 @@ import org.bukkit.NamespacedKey
import org.bukkit.entity.Player
import org.bukkit.plugin.java.JavaPlugin

class CustomItemManager(private val plugin: JavaPlugin) {
class CustomItemManager(plugin: JavaPlugin) {

private val customItemMap: MutableMap<String, CustomItem> = HashMap()

val customItemIdKey: NamespacedKey = NamespacedKey(plugin, "custom_item_id")

fun getPlugin(): JavaPlugin {
return plugin
}

fun addCustomItem(itemId: String, customItem: CustomItem) {
customItemMap[itemId] = customItem
}
Expand All @@ -22,7 +18,7 @@ class CustomItemManager(private val plugin: JavaPlugin) {
return customItemMap[itemId]
}

fun giveCustomItem(itemId: String, player: Player?) {
customItemMap[itemId]!!.give(player!!)
fun giveCustomItem(itemId: String, player: Player) {
customItemMap[itemId]!!.give(player)
}
}

0 comments on commit b0c3d22

Please sign in to comment.