All Downloads are FREE. Search and download functionalities are using the official Maven repository.

net.silkmc.silk.persistence.CompoundKey.kt Maven / Gradle / Ivy

package net.silkmc.silk.persistence

import net.minecraft.nbt.Tag
import net.minecraft.resources.ResourceLocation
import net.silkmc.silk.core.logging.logWarning
import net.silkmc.silk.nbt.serialization.Nbt
import net.silkmc.silk.nbt.serialization.decodeFromNbtElement
import net.silkmc.silk.nbt.serialization.encodeToNbtElement
import kotlin.reflect.full.isSubclassOf

/**
 * Creates a [CompoundKey] which can be used to read and write data
 * to a [PersistentCompound] in a typesafe way.
 *
 * @param id the unique identifier for this key, this should contain your mod id
 */
inline fun  compoundKey(id: ResourceLocation) =
    object : CompoundKey(id) {
        init {
            if (T::class.isSubclassOf(Tag::class))
                logWarning("Usage of compoundKey function with NbtElement / Tag as type detected! You probably want to use nbtElementCompoundKey instead.")
        }

        override fun convertValueToNbtElement(value: T) =
            Nbt.encodeToNbtElement(value)

        override fun convertNbtElementToValue(nbtElement: Tag) =
            Nbt.decodeFromNbtElement(nbtElement)
    }

/**
 * Creates a [CompoundKey] which can be used to read and write data
 * to a [PersistentCompound] in a typesafe way.
 *
 * This compound key is meant specifically for values of the type [Tag].
 * These can be treated differently, as they don't have to be converted
 * or serialized.
 *
 * @param id the unique identifier for this key, this should contain your mod id
 */
inline fun  nbtElementCompoundKey(id: ResourceLocation) =
    object : CompoundKey(id) {
        override fun convertValueToNbtElement(value: T) = value

        override fun convertNbtElementToValue(nbtElement: Tag) = nbtElement as T
    }

/**
 * Creates a [CompoundKey] which can be used to read and write data
 * to a [PersistentCompound] in a typesafe way.
 *
 * This compound key allows you to specify custom serialization or conversion logic
 * to convert elements of the type [T] to NbtElements of the type [NbtType].
 *
 * @param id the unique identifier for this key, this should contain your mod id
 */
inline fun  customCompoundKey(
    id: ResourceLocation,
    crossinline valueToNbt: (value: T) -> NbtType,
    crossinline nbtToValue: (nbtElement: NbtType) -> T
) = object : CompoundKey(id) {
    override fun convertValueToNbtElement(value: T) = valueToNbt(value)
    override fun convertNbtElementToValue(nbtElement: Tag) = nbtToValue(nbtElement as NbtType)
}

/**
 * Creates a [CompoundKey] which can be used to read and write data
 * to a [PersistentCompound] in a typesafe way.
 *
 * This compound key allows you to specify custom serialization or conversion logic
 * to convert elements of the type [T] to NbtElements of the type [Tag] (this is
 * the version of the [customCompoundKey] function with no specific NbtElement type).
 *
 * @param id the unique identifier for this key, this should contain your mod id
 */
@JvmName("customCompoundKeyNbtElement")
inline fun  customCompoundKey(
    id: ResourceLocation,
    crossinline convertValueToNbtElement: (value: T) -> Tag,
    crossinline convertNbtElementToValue: (nbtElement: Tag) -> T
) = customCompoundKey(id, convertValueToNbtElement, convertNbtElementToValue)

abstract class CompoundKey(val name: String) {
    constructor(id: ResourceLocation) : this(id.toString())

    internal abstract fun convertValueToNbtElement(value: T): Tag

    @PublishedApi
    internal abstract fun convertNbtElementToValue(nbtElement: Tag): T
}




© 2015 - 2025 Weber Informatics LLC | Privacy Policy