Skip to content

Commit

Permalink
feat: add even more components
Browse files Browse the repository at this point in the history
  • Loading branch information
Boy0000 committed Jun 20, 2024
1 parent 12857c5 commit de32b32
Show file tree
Hide file tree
Showing 2 changed files with 143 additions and 12 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ import io.papermc.paper.block.BlockPredicate
import io.papermc.paper.component.DataComponentType
import io.papermc.paper.component.DataComponentTypes
import io.papermc.paper.component.item.*
import io.papermc.paper.component.item.MapDecorations.DecorationEntry
import io.papermc.paper.component.item.Tool.Rule
import io.papermc.paper.registry.RegistryAccess
import io.papermc.paper.registry.RegistryKey
Expand All @@ -12,13 +13,13 @@ import io.papermc.paper.registry.set.RegistrySet
import io.papermc.paper.registry.tag.TagKey
import kotlinx.serialization.Serializable
import net.kyori.adventure.key.Key
import net.kyori.adventure.text.Component
import net.kyori.adventure.util.TriState
import org.bukkit.Color
import org.bukkit.Registry
import org.bukkit.inventory.ItemStack
import org.bukkit.inventory.meta.trim.ArmorTrim
import org.bukkit.inventory.meta.trim.TrimMaterial
import org.bukkit.inventory.meta.trim.TrimPattern
import org.bukkit.map.MapCursor
import org.bukkit.potion.PotionEffect

object SerializableDataTypes {
Expand Down Expand Up @@ -103,6 +104,83 @@ object SerializableDataTypes {
}
}

@Serializable
@JvmInline
value class ChargedProjectiles(private val projectiles: List<SerializableItemStack>) : DataType {
constructor(vararg projectiles: ItemStack) : this(projectiles.map { it.toSerializable() })
constructor(projectiles: io.papermc.paper.component.item.ChargedProjectiles) : this(
projectiles.projectiles().map { it.toSerializable() })

override fun setDataType(itemStack: ItemStack) {
itemStack.setData(
DataComponentTypes.CHARGED_PROJECTILES,
io.papermc.paper.component.item.ChargedProjectiles.chargedProjectiles(projectiles.mapNotNull { it.toItemStackOrNull() })
)
}
}

@Serializable
@JvmInline
value class BundleContent(private val contents: List<SerializableItemStack>) : DataType {
constructor(vararg contents: ItemStack) : this(contents.map { it.toSerializable() })
constructor(contents: BundleContents) : this(contents.contents().map { it.toSerializable() })

override fun setDataType(itemStack: ItemStack) {
itemStack.setData(
DataComponentTypes.BUNDLE_CONTENTS,
BundleContents.bundleContents(contents.mapNotNull { it.toItemStackOrNull() })
)
}
}

@Serializable
data class WrittenBook(
val title: String,
val author: String,
val generation: Int,
val resolved: Boolean,
val pages: List<@Serializable(MiniMessageSerializer::class) Component> = emptyList()
) : DataType {
constructor(written: WrittenBookContent) : this(
written.title().raw(),
written.author(),
written.generation(),
written.resolved(),
written.pages().map { it.raw() }
)

override fun setDataType(itemStack: ItemStack) {
itemStack.setData(
DataComponentTypes.WRITTEN_BOOK_CONTENT,
WrittenBookContent.writtenBookContent(title, author).resolved(resolved).generation(generation)
.addPages(pages).build()
)
}
}

@Serializable
data class WritableBook(val pages: List<String>) : DataType {
constructor(writable: WritableBookContent) : this(writable.pages().map { it.raw() })

override fun setDataType(itemStack: ItemStack) {
itemStack.setData(
DataComponentTypes.WRITABLE_BOOK_CONTENT,
WritableBookContent.writeableBookContent().addPages(pages).build()
)
}
}

@Serializable
data class MapDecoration(val type: MapCursor.Type, val x: Double, val z: Double, val rotation: Float) {
constructor(entry: DecorationEntry) : this(entry.type(), entry.x(), entry.z(), entry.rotation())
val paperDecoration: DecorationEntry = DecorationEntry.of(type, x, z, rotation)

companion object {
fun toPaperDecorations(decorations: List<MapDecoration>) =
decorations.mapIndexed { i, mapDecoration -> i.toString() to mapDecoration.paperDecoration }.toMap()
}
}

@Serializable
data class Tool(
val rules: List<Rule> = emptyList(),
Expand Down Expand Up @@ -303,8 +381,9 @@ object SerializableDataTypes {
}

@Serializable
data class MapColor(
val color: @Serializable(ColorSerializer::class) Color,
@JvmInline
value class MapColor(
private val color: @Serializable(ColorSerializer::class) Color,
) : DataType {
constructor(mapItemColor: MapItemColor) : this(mapItemColor.mapColor())

Expand All @@ -320,16 +399,44 @@ object SerializableDataTypes {
val pattern: @Serializable(KeySerializer::class) Key,
val showInToolTip: Boolean = true
) : DataType {
constructor(trim: ItemArmorTrim) : this(trim.armorTrim().material.key(), trim.armorTrim().pattern.key(), trim.showInTooltip())
constructor(trim: ItemArmorTrim) : this(
trim.armorTrim().material.key(),
trim.armorTrim().pattern.key(),
trim.showInTooltip()
)

override fun setDataType(itemStack: ItemStack) {
val trimMaterial = RegistryAccess.registryAccess().getRegistry(RegistryKey.TRIM_MATERIAL).get(material) ?: error("Invalid TrimMaterial: " + material.asString())
val trimPattern = RegistryAccess.registryAccess().getRegistry(RegistryKey.TRIM_PATTERN).get(pattern) ?: error("Invalid TrimPattern: " + pattern.asString())
itemStack.setData(DataComponentTypes.TRIM, ItemArmorTrim.itemArmorTrim(ArmorTrim(trimMaterial, trimPattern), showInToolTip))
val trimMaterial = RegistryAccess.registryAccess().getRegistry(RegistryKey.TRIM_MATERIAL).get(material)
?: error("Invalid TrimMaterial: " + material.asString())
val trimPattern = RegistryAccess.registryAccess().getRegistry(RegistryKey.TRIM_PATTERN).get(pattern)
?: error("Invalid TrimPattern: " + pattern.asString())
itemStack.setData(
DataComponentTypes.TRIM,
ItemArmorTrim.itemArmorTrim(ArmorTrim(trimMaterial, trimPattern), showInToolTip)
)
}

}

@Serializable
data class JukeboxPlayable(
val jukeboxSong: @Serializable(KeySerializer::class) Key,
val showInToolTip: Boolean = true
) : DataType {
constructor(jukeboxPlayable: io.papermc.paper.component.item.JukeboxPlayable) :
this(jukeboxPlayable.jukeboxSong().key(), jukeboxPlayable.showInTooltip())

override fun setDataType(itemStack: ItemStack) {
val jukeboxRegistry = RegistryAccess.registryAccess().getRegistry(RegistryKey.JUKEBOX_SONG)
val jukeboxSong = jukeboxRegistry.get(jukeboxSong) ?: return
itemStack.setData(
DataComponentTypes.JUKEBOX_PLAYABLE,
io.papermc.paper.component.item.JukeboxPlayable.jukeboxPlayable().showInTooltip(showInToolTip)
.jukeboxSong(jukeboxSong)
)
}
}

@Serializable
data class AttributeModifiers(
val attributes: List<SerializableAttribute>,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -8,9 +8,10 @@ import com.mineinabyss.idofront.plugin.Plugins
import dev.lone.itemsadder.api.CustomStack
import io.lumine.mythiccrucible.MythicCrucible
import io.papermc.paper.component.DataComponentTypes
import io.papermc.paper.component.item.BundleContents
import io.papermc.paper.component.item.ItemLore
import io.papermc.paper.component.item.MapDecorations
import io.papermc.paper.component.item.MapID
import io.papermc.paper.component.item.MapPostProcessing
import io.th0rgal.oraxen.OraxenPlugin
import io.th0rgal.oraxen.api.OraxenItems
import kotlinx.serialization.Contextual
Expand Down Expand Up @@ -59,15 +60,21 @@ data class BaseSerializableItemStack(
@EncodeDefault(NEVER) val canBreak: SerializableDataTypes.CanBreak? = null,
@EncodeDefault(NEVER) val dyedColor: SerializableDataTypes.DyedColor? = null,
@EncodeDefault(NEVER) val mapColor: SerializableDataTypes.MapColor? = null,
@EncodeDefault(NEVER) val mapDecorations: List<SerializableDataTypes.MapDecoration>? = null,
@EncodeDefault(NEVER) val trim: SerializableDataTypes.Trim? = null,
@EncodeDefault(NEVER) val jukeboxPlayable: SerializableDataTypes.JukeboxPlayable? = null,
@EncodeDefault(NEVER) val chargedProjectiles: SerializableDataTypes.ChargedProjectiles? = null,
@EncodeDefault(NEVER) val bundleContents: SerializableDataTypes.BundleContent? = null,
@EncodeDefault(NEVER) val writableBook: SerializableDataTypes.WritableBook? = null,
@EncodeDefault(NEVER) val writtenBook: SerializableDataTypes.WrittenBook? = null,

@EncodeDefault(NEVER) val bundleContents: List<SerializableItemStack>? = null,
@EncodeDefault(NEVER) val recipes: List<@Serializable(KeySerializer::class) Key>? = null,
@EncodeDefault(NEVER) val enchantmentGlintOverride: Boolean? = null,
@EncodeDefault(NEVER) val maxStackSize: Int? = null,
@EncodeDefault(NEVER) val rarity: ItemRarity? = null,
@EncodeDefault(NEVER) val repairCost: Int? = null,
@EncodeDefault(NEVER) val mapId: Int? = null,
@EncodeDefault(NEVER) val mapPostProcessing: MapPostProcessing? = null,


@EncodeDefault(NEVER) val prefab: String? = null,
Expand Down Expand Up @@ -160,10 +167,21 @@ data class BaseSerializableItemStack(
canPlaceOn?.setDataType(applyTo)
canBreak?.setDataType(applyTo)
trim?.setDataType(applyTo)
jukeboxPlayable?.setDataType(applyTo)
chargedProjectiles?.setDataType(applyTo)
bundleContents?.setDataType(applyTo)
writableBook?.setDataType(applyTo)
writtenBook?.setDataType(applyTo)

bundleContents?.let { applyTo.setData(DataComponentTypes.BUNDLE_CONTENTS, BundleContents.bundleContents(bundleContents.map { it.toItemStack() })) }
mapId?.let { applyTo.setData(DataComponentTypes.MAP_ID, MapID.mapId().mapId(mapId).build()) }
mapPostProcessing?.let { applyTo.setData(DataComponentTypes.MAP_POST_PROCESSING, it) }
mapColor?.setDataType(applyTo)
mapDecorations?.let {
applyTo.setData(
DataComponentTypes.MAP_DECORATIONS,
MapDecorations.mapDecorations(SerializableDataTypes.MapDecoration.toPaperDecorations(mapDecorations))
)
}

SerializableDataTypes.setData(applyTo, DataComponentTypes.FIRE_RESISTANT, fireResistant)
SerializableDataTypes.setData(applyTo, DataComponentTypes.HIDE_TOOLTIP, hideTooltip)
Expand Down Expand Up @@ -222,11 +240,17 @@ fun ItemStack.toSerializable(): SerializableItemStack = with(itemMeta) {
tool = getData(DataComponentTypes.TOOL)?.let(SerializableDataTypes::Tool),
canPlaceOn = getData(DataComponentTypes.CAN_PLACE_ON)?.let(SerializableDataTypes::CanPlaceOn),
canBreak = getData(DataComponentTypes.CAN_BREAK)?.let(SerializableDataTypes::CanBreak),
bundleContents = getData(DataComponentTypes.BUNDLE_CONTENTS)?.contents()?.map { it.toSerializable() },
trim = getData(DataComponentTypes.TRIM)?.let(SerializableDataTypes::Trim),
jukeboxPlayable = getData(DataComponentTypes.JUKEBOX_PLAYABLE)?.let(SerializableDataTypes::JukeboxPlayable),
chargedProjectiles = getData(DataComponentTypes.CHARGED_PROJECTILES)?.let(SerializableDataTypes::ChargedProjectiles),
bundleContents = getData(DataComponentTypes.BUNDLE_CONTENTS)?.let(SerializableDataTypes::BundleContent),
writableBook = getData(DataComponentTypes.WRITABLE_BOOK_CONTENT)?.let(SerializableDataTypes::WritableBook),
writtenBook = getData(DataComponentTypes.WRITTEN_BOOK_CONTENT)?.let(SerializableDataTypes::WrittenBook),

mapId = getData(DataComponentTypes.MAP_ID)?.id(),
mapColor = getData(DataComponentTypes.MAP_COLOR)?.let(SerializableDataTypes::MapColor),
mapPostProcessing = getData(DataComponentTypes.MAP_POST_PROCESSING),
mapDecorations = getData(DataComponentTypes.MAP_DECORATIONS)?.let { it.decorations.map { e -> SerializableDataTypes.MapDecoration(e.value) } },

fireResistant = SerializableDataTypes.FireResistant.takeIf { hasData(DataComponentTypes.FIRE_RESISTANT) },
hideTooltip = SerializableDataTypes.HideToolTip.takeIf { hasData(DataComponentTypes.HIDE_TOOLTIP) },
Expand Down

0 comments on commit de32b32

Please sign in to comment.