Painter Recipes updates

This commit is contained in:
DBotThePony 2023-11-16 21:02:19 +07:00
parent 6d21760621
commit b26f109f56
Signed by: DBot
GPG Key ID: DCC23B5715498507

View File

@ -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<out DyeColor?, Int>
) : Recipe<Container> {
) : IMatteryRecipe<Container> {
val dyes: Object2IntMap<DyeColor?> = 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<DyeColorWrapper> = StringRepresentable.fromEnum(DyeColorWrapper::values)
@JvmStatic
protected val dyesFieldCodec: MapCodec<Map<DyeColor?, Int>> = PredicatedCodecList<Map<DyeColorWrapper, Int>>(
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<PainterRecipe> { context ->
RecordCodecBuilder.create {
it.group(
context.ingredients.fieldOf("input").forGetter(PainterRecipe::input),
ItemStack.CODEC.fieldOf("output").forGetter(PainterRecipe::output),
PredicatedCodecList<Map<DyeColorWrapper, Int>>(
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<PainterArmorDyeRecipe> { _ ->
RecordCodecBuilder.create {
it.group(
PredicatedCodecList<Map<DyeColorWrapper, Int>>(
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)
}
}