Skip to content

Commit

Permalink
refactor: support tags in repairable
Browse files Browse the repository at this point in the history
  • Loading branch information
Boy0000 committed Nov 2, 2024
1 parent 4d67570 commit 08e338a
Showing 1 changed file with 11 additions and 2 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@ import org.bukkit.Registry
import org.bukkit.entity.EntityType
import org.bukkit.inventory.EquipmentSlot
import org.bukkit.inventory.ItemStack
import org.bukkit.inventory.ItemType
import org.bukkit.inventory.meta.trim.ArmorTrim
import org.bukkit.map.MapCursor
import org.bukkit.potion.PotionEffect
Expand Down Expand Up @@ -225,10 +226,18 @@ object SerializableDataTypes {
@Serializable
@JvmInline
value class Repairable(val repairable: List<@Serializable(KeySerializer::class) Key>) : DataType {
constructor(repairable: io.papermc.paper.datacomponent.item.Repairable) : this(repairable.types().resolve(Registry.ITEM).map { it.key() })
constructor(repairable: io.papermc.paper.datacomponent.item.Repairable) :
this(repairable.types().resolve(Registry.ITEM).map { it.key() })

override fun setDataType(itemStack: ItemStack) {
itemStack.setData(DataComponentTypes.REPAIRABLE, io.papermc.paper.datacomponent.item.Repairable.repairable(RegistrySet.keySetFromValues(RegistryKey.ITEM, repairable.mapNotNull(Registry.ITEM::get))
val items = repairable.mapNotNull { Registry.ITEM.get(it)?.key()?.let { TypedKey.create(RegistryKey.ITEM, it) } }
val tags = repairable.mapNotNull {
runCatching { Registry.ITEM.getTag(TagKey.create(RegistryKey.ITEM, it)) }.getOrNull()?.values()
}.flatten()

itemStack.setData(DataComponentTypes.REPAIRABLE, io.papermc.paper.datacomponent.item.Repairable.repairable(
RegistrySet.keySet(RegistryKey.ITEM, items.plus(tags).filterNotNull().toMutableList())
))
}
}

Expand Down

0 comments on commit 08e338a

Please sign in to comment.