Added support for CustomModelData

This commit is contained in:
Jakob K
2020-07-23 20:09:08 +02:00
parent 324a326a87
commit 53f7292d17

View File

@@ -0,0 +1,42 @@
package net.axay.kspigot.items
import org.bukkit.Material
import org.bukkit.inventory.ItemStack
import org.bukkit.inventory.meta.ItemMeta
/**
* This class defines a material in combination
* with a specific custom model data value.
*
* It is useful if you work with custom items
* defined in resourcepacks, where you do not
* only need a material to define a specific item
* type, but the value of the custom model data
* aswell.
*/
data class CustomItemIdentifier(val customModelData: Int, val placeHolderMaterial: Material) {
constructor(itemStack: ItemStack) :
this(
kotlin.run {
val itemMeta = itemStack.itemMeta
if (itemMeta != null && itemMeta.hasCustomModelData()) {
return@run itemMeta.customModelData
}
return@run 0
},
itemStack.type
)
val itemStack: ItemStack?
get() {
val itemStack = ItemStack(placeHolderMaterial, 1)
val itemMeta: ItemMeta? = itemStack.itemMeta
return if (itemMeta != null) {
itemMeta.setCustomModelData(customModelData)
itemStack.itemMeta = itemMeta
itemStack
} else null
}
}