From 09e9713db7549818c39fb47e69a153f706f0491b Mon Sep 17 00:00:00 2001 From: Kli Kli Date: Wed, 21 Aug 2024 10:13:13 +0200 Subject: [PATCH] feat: support ingredient notation in item recipe results --- .../recipe/result/ItemRecipeResult.java | 17 ++++++++++++++++- .../occultism/util/OccultismExtraCodecs.java | 12 ++++++++++++ 2 files changed, 28 insertions(+), 1 deletion(-) diff --git a/src/main/java/com/klikli_dev/occultism/crafting/recipe/result/ItemRecipeResult.java b/src/main/java/com/klikli_dev/occultism/crafting/recipe/result/ItemRecipeResult.java index d04f6e9a6..013c4823d 100644 --- a/src/main/java/com/klikli_dev/occultism/crafting/recipe/result/ItemRecipeResult.java +++ b/src/main/java/com/klikli_dev/occultism/crafting/recipe/result/ItemRecipeResult.java @@ -5,7 +5,11 @@ package com.klikli_dev.occultism.crafting.recipe.result; import com.klikli_dev.occultism.registry.OccultismRecipeResults; +import com.klikli_dev.occultism.util.OccultismExtraCodecs; +import com.mojang.serialization.Codec; import com.mojang.serialization.MapCodec; +import com.mojang.serialization.codecs.RecordCodecBuilder; +import net.minecraft.core.component.DataComponentPatch; import net.minecraft.network.RegistryFriendlyByteBuf; import net.minecraft.network.codec.StreamCodec; import net.minecraft.world.item.ItemStack; @@ -16,7 +20,18 @@ */ public class ItemRecipeResult extends RecipeResult { - public static final MapCodec CODEC = MapCodec.assumeMapUnsafe(ItemStack.STRICT_CODEC.xmap(ItemRecipeResult::new, ItemRecipeResult::getStack)); + public static final MapCodec INGREDIENT_COMPAT_CODEC = RecordCodecBuilder.mapCodec((builder) -> builder.group( + ItemStack.ITEM_NON_AIR_CODEC.fieldOf("item").forGetter(t -> t.stack.getItemHolder()), + Codec.INT.fieldOf("count").forGetter(t -> t.stack.getCount()), + DataComponentPatch.CODEC.optionalFieldOf("components", DataComponentPatch.EMPTY).forGetter(t -> t.stack.getComponentsPatch()) + ).apply(builder, (item, count, components) -> new ItemRecipeResult(new ItemStack(item, count, components)))); + + public static final MapCodec ITEM_STACK_COMPAT_CODEC = MapCodec.assumeMapUnsafe(ItemStack.STRICT_CODEC.xmap(ItemRecipeResult::new, ItemRecipeResult::getStack)); + + public static final MapCodec CODEC = OccultismExtraCodecs.mapWithAlternative( + ITEM_STACK_COMPAT_CODEC, + INGREDIENT_COMPAT_CODEC + ); public static final StreamCodec STREAM_CODEC = StreamCodec.composite( ItemStack.OPTIONAL_STREAM_CODEC, diff --git a/src/main/java/com/klikli_dev/occultism/util/OccultismExtraCodecs.java b/src/main/java/com/klikli_dev/occultism/util/OccultismExtraCodecs.java index fac5aa32c..a55a771fe 100644 --- a/src/main/java/com/klikli_dev/occultism/util/OccultismExtraCodecs.java +++ b/src/main/java/com/klikli_dev/occultism/util/OccultismExtraCodecs.java @@ -1,9 +1,21 @@ package com.klikli_dev.occultism.util; +import com.mojang.datafixers.util.Either; import com.mojang.serialization.Codec; +import com.mojang.serialization.MapCodec; import java.util.UUID; public class OccultismExtraCodecs { public static final Codec UUID = Codec.STRING.xmap(java.util.UUID::fromString, java.util.UUID::toString); + + public static MapCodec mapWithAlternative(final MapCodec primary, final MapCodec alternative) { + return Codec.mapEither( + primary, + alternative + ).xmap( + Either::unwrap, + Either::left + ); + } }