From fccd23b1c9e57bc4c84e3470daf8704093b245a1 Mon Sep 17 00:00:00 2001 From: malte0811 Date: Sat, 21 Sep 2024 10:00:48 +0200 Subject: [PATCH] Fix generated list recipe serializer For the stream codec we want to send over the resolved list, not just the ID --- .../api/crafting/cache/IListRecipe.java | 4 ++-- .../common/crafting/GeneratedListRecipe.java | 22 ++++++++----------- .../serializers/GeneratedListSerializer.java | 21 +++++++++++++----- 3 files changed, 27 insertions(+), 20 deletions(-) diff --git a/src/api/java/blusunrize/immersiveengineering/api/crafting/cache/IListRecipe.java b/src/api/java/blusunrize/immersiveengineering/api/crafting/cache/IListRecipe.java index 425f42e731..581c1a77e1 100644 --- a/src/api/java/blusunrize/immersiveengineering/api/crafting/cache/IListRecipe.java +++ b/src/api/java/blusunrize/immersiveengineering/api/crafting/cache/IListRecipe.java @@ -8,11 +8,11 @@ package blusunrize.immersiveengineering.api.crafting.cache; -import blusunrize.immersiveengineering.api.crafting.IESerializableRecipe; +import net.minecraft.world.item.crafting.Recipe; import java.util.List; public interface IListRecipe { - List getSubRecipes(); + List> getSubRecipes(); } diff --git a/src/main/java/blusunrize/immersiveengineering/common/crafting/GeneratedListRecipe.java b/src/main/java/blusunrize/immersiveengineering/common/crafting/GeneratedListRecipe.java index 74b7bd9270..5044e4c075 100644 --- a/src/main/java/blusunrize/immersiveengineering/common/crafting/GeneratedListRecipe.java +++ b/src/main/java/blusunrize/immersiveengineering/common/crafting/GeneratedListRecipe.java @@ -17,6 +17,7 @@ import net.minecraft.core.HolderLookup.Provider; import net.minecraft.resources.ResourceLocation; import net.minecraft.world.item.ItemStack; +import net.minecraft.world.item.crafting.Recipe; import net.minecraft.world.item.crafting.RecipeSerializer; import javax.annotation.Nonnull; @@ -30,7 +31,7 @@ import static blusunrize.immersiveengineering.ImmersiveEngineering.rl; -public class GeneratedListRecipe extends IESerializableRecipe implements IListRecipe +public class GeneratedListRecipe, E> extends IESerializableRecipe implements IListRecipe { public static Map> LIST_GENERATORS = new HashMap<>(); public static Supplier>> SERIALIZER; @@ -54,7 +55,7 @@ public class GeneratedListRecipe extends IESe } @Nullable - private List cachedRecipes; + private List> cachedRecipes; private final RecipeListGenerator generator; private E earlyResult; private final ResourceLocation generatorID; @@ -73,7 +74,7 @@ public class GeneratedListRecipe extends IESe return new GeneratedListRecipe<>(id, gen); } - public static GeneratedListRecipe resolved(ResourceLocation id, List recipes) + public static GeneratedListRecipe resolved(ResourceLocation id, List> recipes) { GeneratedListRecipe result = fromInternal(id); result.cachedRecipes = recipes; @@ -111,10 +112,10 @@ public boolean isSpecial() return true; } - public List getSubRecipes() + public List> getSubRecipes() { if(cachedRecipes==null) - cachedRecipes = generator.generator().apply(earlyResult); + cachedRecipes = List.copyOf(generator.generator().apply(earlyResult)); return cachedRecipes; } @@ -123,19 +124,14 @@ public ResourceLocation getGeneratorID() return generatorID; } - public ResourceLocation getSubSerializer() - { - return generator.serialized; - } - - public record RecipeListGenerator( + public record RecipeListGenerator, EarlyResult>( Supplier makeEarlyResult, Function> generator, ResourceLocation serialized, IERecipeTypes.TypeWithClass recipeType ) { - public static RecipeListGenerator fromSerializer( + public static , ER> RecipeListGenerator fromSerializer( Supplier makeEarlyResult, Function> generator, Holder> serialized, @@ -146,7 +142,7 @@ public static RecipeListGenerator fr return new RecipeListGenerator<>(makeEarlyResult, generator, serializedKey, recipeType); } - public static + public static > RecipeListGenerator simple( Supplier> generator, Holder> serialized, diff --git a/src/main/java/blusunrize/immersiveengineering/common/crafting/serializers/GeneratedListSerializer.java b/src/main/java/blusunrize/immersiveengineering/common/crafting/serializers/GeneratedListSerializer.java index 5b65ca7760..e8ba598eb4 100644 --- a/src/main/java/blusunrize/immersiveengineering/common/crafting/serializers/GeneratedListSerializer.java +++ b/src/main/java/blusunrize/immersiveengineering/common/crafting/serializers/GeneratedListSerializer.java @@ -10,20 +10,31 @@ package blusunrize.immersiveengineering.common.crafting.serializers; import blusunrize.immersiveengineering.api.crafting.IERecipeSerializer; -import malte0811.dualcodecs.DualCodecs; -import malte0811.dualcodecs.DualMapCodec; import blusunrize.immersiveengineering.api.wires.WireType; import blusunrize.immersiveengineering.common.crafting.GeneratedListRecipe; import blusunrize.immersiveengineering.common.register.IEItems.Misc; +import com.mojang.serialization.MapCodec; +import malte0811.dualcodecs.DualMapCodec; import net.minecraft.network.RegistryFriendlyByteBuf; +import net.minecraft.network.codec.ByteBufCodecs; +import net.minecraft.network.codec.StreamCodec; +import net.minecraft.resources.ResourceLocation; import net.minecraft.world.item.ItemStack; +import net.minecraft.world.item.crafting.Recipe; public class GeneratedListSerializer extends IERecipeSerializer> { - public static final DualMapCodec> CODECS = DualCodecs.RESOURCE_LOCATION - .castStream() + private static final MapCodec> CODEC = ResourceLocation.CODEC .fieldOf("generatorID") - .map(GeneratedListRecipe::from, GeneratedListRecipe::getGeneratorID); + .xmap(GeneratedListRecipe::from, GeneratedListRecipe::getGeneratorID); + private static final StreamCodec> STREAM_CODEC = StreamCodec.composite( + ResourceLocation.STREAM_CODEC, GeneratedListRecipe::getGeneratorID, + Recipe.STREAM_CODEC.apply(ByteBufCodecs.list()), GeneratedListRecipe::getSubRecipes, + GeneratedListRecipe::resolved + ); + public static final DualMapCodec> CODECS = new DualMapCodec<>( + CODEC, STREAM_CODEC + ); @Override protected DualMapCodec> codecs()