Skip to content

Commit

Permalink
feat: ModelTexturesSerializer & other RP stuff
Browse files Browse the repository at this point in the history
  • Loading branch information
Boy0000 committed Aug 4, 2024
1 parent e0df226 commit 16fd188
Show file tree
Hide file tree
Showing 4 changed files with 50 additions and 0 deletions.
1 change: 1 addition & 0 deletions idofront-serializers/build.gradle.kts
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@ dependencies {
compileOnly(libs.minecraft.plugin.mythic.crucible)
compileOnly(libs.minecraft.plugin.oraxen)
compileOnly(libs.minecraft.plugin.itemsadder)
compileOnly(libs.creative.api)
implementation(project(":idofront-util"))
implementation(project(":idofront-logging"))
implementation(project(":idofront-text-components"))
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,39 @@
package com.mineinabyss.idofront.serialization

import com.charleskorn.kaml.*
import kotlinx.serialization.*
import kotlinx.serialization.descriptors.SerialDescriptor
import kotlinx.serialization.encoding.Decoder
import kotlinx.serialization.encoding.Encoder
import net.kyori.adventure.key.Key
import team.unnamed.creative.model.ModelTexture
import team.unnamed.creative.model.ModelTextures

@Serializable
@SerialName("ModelTextures")
class ModelTexturesSurrogate(
val layers: List<@Serializable(KeySerializer::class) Key> = emptyList(),
val particle: @Serializable(KeySerializer::class) Key? = null,
val variables: Map<String, @Serializable(KeySerializer::class) Key> = emptyMap(),
) {
@Transient val modelTextures = ModelTextures.of(layers.map(ModelTexture::ofKey), particle?.let(ModelTexture::ofKey), variables.mapValues { ModelTexture.ofKey(it.value) }.toMap())
}

object ModelTexturesSerializer : KSerializer<ModelTexturesSurrogate> {
override val descriptor: SerialDescriptor = ContextualSerializer(ModelTexturesSurrogate::class).descriptor

override fun deserialize(decoder: Decoder): ModelTexturesSurrogate {
val node = (decoder as? YamlInput)?.node?.yamlMap?.get<YamlNode>("textures") ?: return ModelTexturesSurrogate.serializer().deserialize(decoder)

return when (node) {
is YamlScalar -> ModelTexturesSurrogate(mutableListOf(Key.key(node.content)))
is YamlList -> ModelTexturesSurrogate(node.items.mapNotNull { (it as? YamlScalar)?.content?.let(Key::key) }.toMutableList())
is YamlMap -> ModelTexturesSurrogate(variables = node.entries.mapNotNull { it.key.content to ((it.value as? YamlScalar)?.content?.let(Key::key) ?: return@mapNotNull null) }.toMap())
else -> ModelTexturesSurrogate()
}
}

override fun serialize(encoder: Encoder, value: ModelTexturesSurrogate) {
ModelTexturesSurrogate.serializer().serialize(encoder, value)
}
}
1 change: 1 addition & 0 deletions idofront-util/build.gradle.kts
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ plugins {

dependencies {
compileOnly(libs.kotlinx.serialization.json)
compileOnly(libs.creative.serializer.minecraft)
implementation(project(":idofront-logging"))
implementation(project(":idofront-di"))
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
package com.mineinabyss.idofront.resourcepacks

import team.unnamed.creative.serialize.minecraft.MinecraftResourcePackReader
import team.unnamed.creative.serialize.minecraft.MinecraftResourcePackWriter

object ResourcePacks {
val resourcePackWriter = MinecraftResourcePackWriter.builder().prettyPrinting(true).build()
val resourcePackReader = MinecraftResourcePackReader.builder().lenient(true).build()
}

0 comments on commit 16fd188

Please sign in to comment.