Skip to content

Commit

Permalink
feat: support ingredient notation in item recipe results
Browse files Browse the repository at this point in the history
  • Loading branch information
klikli-dev committed Aug 21, 2024
1 parent 2b7d7ce commit 3e7e994
Show file tree
Hide file tree
Showing 2 changed files with 28 additions and 1 deletion.
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,11 @@
package com.klikli_dev.theurgy.content.recipe.result;

import com.klikli_dev.theurgy.registry.RecipeResultRegistry;
import com.klikli_dev.theurgy.util.TheurgyExtraCodecs;
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;
Expand All @@ -16,7 +20,18 @@
*/
public class ItemRecipeResult extends RecipeResult {

public static final MapCodec<ItemRecipeResult> CODEC = MapCodec.assumeMapUnsafe(ItemStack.STRICT_CODEC.xmap(ItemRecipeResult::new, ItemRecipeResult::getStack));
public static final MapCodec<ItemRecipeResult> 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<ItemRecipeResult> ITEM_STACK_COMPAT_CODEC = MapCodec.assumeMapUnsafe(ItemStack.STRICT_CODEC.xmap(ItemRecipeResult::new, ItemRecipeResult::getStack));

public static final MapCodec<ItemRecipeResult> CODEC = TheurgyExtraCodecs.mapWithAlternative(
ITEM_STACK_COMPAT_CODEC,
INGREDIENT_COMPAT_CODEC
);

public static final StreamCodec<RegistryFriendlyByteBuf, ItemRecipeResult> STREAM_CODEC = StreamCodec.composite(
ItemStack.OPTIONAL_STREAM_CODEC,
Expand Down
12 changes: 12 additions & 0 deletions src/main/java/com/klikli_dev/theurgy/util/TheurgyExtraCodecs.java
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,9 @@

import com.google.common.graph.EndpointPair;
import com.google.common.graph.MutableGraph;
import com.mojang.datafixers.util.Either;
import com.mojang.serialization.Codec;
import com.mojang.serialization.MapCodec;
import com.mojang.serialization.codecs.RecordCodecBuilder;
import io.netty.buffer.ByteBuf;
import it.unimi.dsi.fastutil.objects.Object2ObjectArrayMap;
Expand Down Expand Up @@ -38,6 +40,16 @@ public class TheurgyExtraCodecs {

public static final Codec<FluidStack> SINGLE_FLUID_CODEC = BuiltInRegistries.FLUID.byNameCodec().xmap(fluid -> new FluidStack(fluid, 1), FluidStack::getFluid);

public static <T> MapCodec<T> mapWithAlternative(final MapCodec<T> primary, final MapCodec<? extends T> alternative) {
return Codec.mapEither(
primary,
alternative
).xmap(
Either::unwrap,
Either::left
);
}

@SuppressWarnings("UnstableApiUsage")
public static <V> Codec<MutableGraph<V>> graph(Codec<V> elementCodec, Supplier<MutableGraph<V>> graphSupplier) {
return internalGraph(elementCodec).xmap(
Expand Down

0 comments on commit 3e7e994

Please sign in to comment.