From b26f109f56aa1fbcc26f7607ee0e1050b6df7510 Mon Sep 17 00:00:00 2001 From: DBotThePony Date: Thu, 16 Nov 2023 21:02:19 +0700 Subject: [PATCH] Painter Recipes updates --- .../mc/otm/recipe/PainterRecipe.kt | 34 +++++++++---------- 1 file changed, 17 insertions(+), 17 deletions(-) diff --git a/src/main/kotlin/ru/dbotthepony/mc/otm/recipe/PainterRecipe.kt b/src/main/kotlin/ru/dbotthepony/mc/otm/recipe/PainterRecipe.kt index a93aa55e3..f8deb91d0 100644 --- a/src/main/kotlin/ru/dbotthepony/mc/otm/recipe/PainterRecipe.kt +++ b/src/main/kotlin/ru/dbotthepony/mc/otm/recipe/PainterRecipe.kt @@ -1,6 +1,7 @@ package ru.dbotthepony.mc.otm.recipe import com.mojang.serialization.Codec +import com.mojang.serialization.MapCodec import com.mojang.serialization.codecs.RecordCodecBuilder import it.unimi.dsi.fastutil.objects.Object2IntArrayMap import it.unimi.dsi.fastutil.objects.Object2IntMap @@ -31,7 +32,7 @@ import java.util.function.Predicate abstract class AbstractPainterRecipe( dyes: Map -) : Recipe { +) : IMatteryRecipe { val dyes: Object2IntMap = Object2IntMaps.unmodifiable(Object2IntArrayMap(dyes)) override fun getToastSymbol(): ItemStack = ItemStack(MItems.PAINTER) @@ -85,6 +86,17 @@ abstract class AbstractPainterRecipe( return refName } } + + companion object { + private val wrapperCodec: Codec = StringRepresentable.fromEnum(DyeColorWrapper::values) + + @JvmStatic + protected val dyesFieldCodec: MapCodec> = PredicatedCodecList>( + wrapperCodec.xmap({ mapOf(it to 1) }, { it.keys.first() }) to Predicate { it.keys.size == 1 && it.values.first() == 1 }, + Codec.list(wrapperCodec).xmap({ it.associateWith { 1 } }, { ArrayList(it.keys) }) to Predicate { it.values.all { it == 1 } }, + Codec.unboundedMap(wrapperCodec, Codec.INT.minRange(1)) to Predicate { true } + ).fieldOf("dyes").xmap({ it.mapKeys { it.key.key } }, { it.mapKeys { k -> DyeColorWrapper.entries.first { k.key == it.key } } }) + } } class PainterRecipe( @@ -142,22 +154,16 @@ class PainterRecipe( } override fun getOutput(container: Container): ItemStack { - return this.output + return output.copy() } companion object { - private val wrapperCodec = StringRepresentable.fromEnum(DyeColorWrapper::values) - val SERIALIZER = Codec2RecipeSerializer { context -> RecordCodecBuilder.create { it.group( context.ingredients.fieldOf("input").forGetter(PainterRecipe::input), ItemStack.CODEC.fieldOf("output").forGetter(PainterRecipe::output), - PredicatedCodecList>( - wrapperCodec.xmap({ mapOf(it to 1) }, { it.keys.first() }) to Predicate { it.keys.size == 1 && it.values.first() == 1 }, - Codec.list(wrapperCodec).xmap({ it.associateWith { 1 } }, { ArrayList(it.keys) }) to Predicate { it.values.all { it == 1 } }, - Codec.unboundedMap(wrapperCodec, Codec.INT.minRange(1)) to Predicate { true } - ).fieldOf("dyes").xmap({ it.mapKeys { it.key.key } }, { it.mapKeys { k -> DyeColorWrapper.entries.first { k.key == it.key } } }).forGetter(PainterRecipe::dyes), + dyesFieldCodec.forGetter(AbstractPainterRecipe::dyes), ).apply(it, ::PainterRecipe) } } @@ -212,7 +218,7 @@ class PainterArmorDyeRecipe( if (entry.key == null) { (output.item as DyeableLeatherItem).clearColor(output) } else { - output = DyeableLeatherItem.dyeArmor(output.copy(), listOf(DyeItem.byColor(entry.key!!))) + output = DyeableLeatherItem.dyeArmor(output, listOf(DyeItem.byColor(entry.key!!))) } } @@ -220,16 +226,10 @@ class PainterArmorDyeRecipe( } companion object { - private val wrapperCodec = StringRepresentable.fromEnum(DyeColorWrapper::values) - val SERIALIZER = Codec2RecipeSerializer { _ -> RecordCodecBuilder.create { it.group( - PredicatedCodecList>( - wrapperCodec.xmap({ mapOf(it to 1) }, { it.keys.first() }) to Predicate { it.keys.size == 1 && it.values.first() == 1 }, - Codec.list(wrapperCodec).xmap({ it.associateWith { 1 } }, { ArrayList(it.keys) }) to Predicate { it.values.all { it == 1 } }, - Codec.unboundedMap(wrapperCodec, Codec.INT.minRange(1)) to Predicate { true } - ).fieldOf("dyes").xmap({ it.mapKeys { it.key.key } }, { it.mapKeys { k -> DyeColorWrapper.entries.first { k.key == it.key } } }).forGetter(PainterArmorDyeRecipe::dyes), + dyesFieldCodec.forGetter(AbstractPainterRecipe::dyes), ).apply(it, ::PainterArmorDyeRecipe) } }