diff options
author | Linnea Gräf <nea@nea.moe> | 2024-12-07 13:26:03 +0100 |
---|---|---|
committer | Linnea Gräf <nea@nea.moe> | 2024-12-07 13:26:03 +0100 |
commit | bf7795df22ca7892fae1238403feebb57c005562 (patch) | |
tree | 3a49db5bade38498f976321b6e9008203f2d4d3b /src/texturePacks/java/moe/nea/firmament/features/texturepack/PredicateModel.kt | |
parent | cdb5e60f52eea9030af9ca2d4a49913664023965 (diff) | |
download | Firmament-bf7795df22ca7892fae1238403feebb57c005562.tar.gz Firmament-bf7795df22ca7892fae1238403feebb57c005562.tar.bz2 Firmament-bf7795df22ca7892fae1238403feebb57c005562.zip |
WIP: Port to compilation on 1.21.4
Diffstat (limited to 'src/texturePacks/java/moe/nea/firmament/features/texturepack/PredicateModel.kt')
-rw-r--r-- | src/texturePacks/java/moe/nea/firmament/features/texturepack/PredicateModel.kt | 85 |
1 files changed, 85 insertions, 0 deletions
diff --git a/src/texturePacks/java/moe/nea/firmament/features/texturepack/PredicateModel.kt b/src/texturePacks/java/moe/nea/firmament/features/texturepack/PredicateModel.kt new file mode 100644 index 0000000..e21e69d --- /dev/null +++ b/src/texturePacks/java/moe/nea/firmament/features/texturepack/PredicateModel.kt @@ -0,0 +1,85 @@ +package moe.nea.firmament.features.texturepack + +import com.mojang.serialization.Codec +import com.mojang.serialization.MapCodec +import com.mojang.serialization.codecs.RecordCodecBuilder +import net.minecraft.client.item.ItemModelManager +import net.minecraft.client.render.item.ItemRenderState +import net.minecraft.client.render.item.model.ItemModel +import net.minecraft.client.render.item.model.ItemModelTypes +import net.minecraft.client.render.model.ResolvableModel +import net.minecraft.client.world.ClientWorld +import net.minecraft.entity.LivingEntity +import net.minecraft.item.ItemStack +import net.minecraft.item.ModelTransformationMode + +class PredicateModel { + data class Baked( + val fallback: ItemModel, + val overrides: List<Override> + ) : ItemModel { + data class Override( + val model: ItemModel, + val predicate: FirmamentModelPredicate, + ) + + override fun update( + state: ItemRenderState, + stack: ItemStack, + resolver: ItemModelManager, + transformationMode: ModelTransformationMode, + world: ClientWorld?, + user: LivingEntity?, + seed: Int + ) { + val model = + overrides + .find { it.predicate.test(stack) } + ?.model + ?: fallback + model.update(state, stack, resolver, transformationMode, world, user, seed) + } + } + + data class Unbaked( + val fallback: ItemModel.Unbaked, + val overrides: List<Override>, + ) : ItemModel.Unbaked { + companion object { + val OVERRIDE_CODEC: Codec<Override> = RecordCodecBuilder.create { + it.group( + ItemModelTypes.CODEC.fieldOf("model").forGetter(Override::model), + CustomModelOverrideParser.LEGACY_CODEC.fieldOf("predicate").forGetter(Override::predicate), + ).apply(it, Unbaked::Override) + } + val CODEC: MapCodec<Unbaked> = + RecordCodecBuilder.mapCodec { + it.group( + ItemModelTypes.CODEC.fieldOf("fallback").forGetter(Unbaked::fallback), + OVERRIDE_CODEC.listOf().fieldOf("overrides").forGetter(Unbaked::overrides), + ).apply(it, ::Unbaked) + } + } + + data class Override( + val model: ItemModel.Unbaked, + val predicate: FirmamentModelPredicate, + ) + + override fun resolve(resolver: ResolvableModel.Resolver) { + fallback.resolve(resolver) + overrides.forEach { it.model.resolve(resolver) } + } + + override fun getCodec(): MapCodec<out Unbaked> { + return CODEC + } + + override fun bake(context: ItemModel.BakeContext): ItemModel { + return Baked( + fallback.bake(context), + overrides.map { Baked.Override(it.model.bake(context), it.predicate) } + ) + } + } +} |