Skip to content

Commit

Permalink
Fix generated list recipe serializer
Browse files Browse the repository at this point in the history
For the stream codec we want to send over the resolved list, not just the ID
  • Loading branch information
malte0811 committed Sep 21, 2024
1 parent e4254cf commit fccd23b
Show file tree
Hide file tree
Showing 3 changed files with 27 additions and 20 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -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<? extends IESerializableRecipe> getSubRecipes();
List<Recipe<?>> getSubRecipes();
}
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -30,7 +31,7 @@

import static blusunrize.immersiveengineering.ImmersiveEngineering.rl;

public class GeneratedListRecipe<R extends IESerializableRecipe, E> extends IESerializableRecipe implements IListRecipe
public class GeneratedListRecipe<R extends Recipe<?>, E> extends IESerializableRecipe implements IListRecipe
{
public static Map<ResourceLocation, RecipeListGenerator<?, ?>> LIST_GENERATORS = new HashMap<>();
public static Supplier<IERecipeSerializer<GeneratedListRecipe<?, ?>>> SERIALIZER;
Expand All @@ -54,7 +55,7 @@ public class GeneratedListRecipe<R extends IESerializableRecipe, E> extends IESe
}

@Nullable
private List<? extends IESerializableRecipe> cachedRecipes;
private List<Recipe<?>> cachedRecipes;
private final RecipeListGenerator<R, E> generator;
private E earlyResult;
private final ResourceLocation generatorID;
Expand All @@ -73,7 +74,7 @@ public class GeneratedListRecipe<R extends IESerializableRecipe, E> extends IESe
return new GeneratedListRecipe<>(id, gen);
}

public static GeneratedListRecipe<?, ?> resolved(ResourceLocation id, List<IESerializableRecipe> recipes)
public static GeneratedListRecipe<?, ?> resolved(ResourceLocation id, List<Recipe<?>> recipes)
{
GeneratedListRecipe<?, ?> result = fromInternal(id);
result.cachedRecipes = recipes;
Expand Down Expand Up @@ -111,10 +112,10 @@ public boolean isSpecial()
return true;
}

public List<? extends IESerializableRecipe> getSubRecipes()
public List<Recipe<?>> getSubRecipes()
{
if(cachedRecipes==null)
cachedRecipes = generator.generator().apply(earlyResult);
cachedRecipes = List.copyOf(generator.generator().apply(earlyResult));
return cachedRecipes;
}

Expand All @@ -123,19 +124,14 @@ public ResourceLocation getGeneratorID()
return generatorID;
}

public ResourceLocation getSubSerializer()
{
return generator.serialized;
}

public record RecipeListGenerator<T extends IESerializableRecipe, EarlyResult>(
public record RecipeListGenerator<T extends Recipe<?>, EarlyResult>(
Supplier<EarlyResult> makeEarlyResult,
Function<EarlyResult, List<? extends T>> generator,
ResourceLocation serialized,
IERecipeTypes.TypeWithClass<T> recipeType
)
{
public static <T extends IESerializableRecipe, ER> RecipeListGenerator<T, ER> fromSerializer(
public static <T extends Recipe<?>, ER> RecipeListGenerator<T, ER> fromSerializer(
Supplier<ER> makeEarlyResult,
Function<ER, List<? extends T>> generator,
Holder<? extends RecipeSerializer<?>> serialized,
Expand All @@ -146,7 +142,7 @@ public static <T extends IESerializableRecipe, ER> RecipeListGenerator<T, ER> fr
return new RecipeListGenerator<>(makeEarlyResult, generator, serializedKey, recipeType);
}

public static <R extends IESerializableRecipe>
public static <R extends Recipe<?>>
RecipeListGenerator<R, ?> simple(
Supplier<List<? extends R>> generator,
Holder<? extends RecipeSerializer<?>> serialized,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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<GeneratedListRecipe<?, ?>>
{
public static final DualMapCodec<RegistryFriendlyByteBuf, GeneratedListRecipe<?, ?>> CODECS = DualCodecs.RESOURCE_LOCATION
.<RegistryFriendlyByteBuf>castStream()
private static final MapCodec<GeneratedListRecipe<?, ?>> CODEC = ResourceLocation.CODEC
.fieldOf("generatorID")
.map(GeneratedListRecipe::from, GeneratedListRecipe::getGeneratorID);
.xmap(GeneratedListRecipe::from, GeneratedListRecipe::getGeneratorID);
private static final StreamCodec<RegistryFriendlyByteBuf, GeneratedListRecipe<?, ?>> STREAM_CODEC = StreamCodec.composite(
ResourceLocation.STREAM_CODEC, GeneratedListRecipe::getGeneratorID,
Recipe.STREAM_CODEC.apply(ByteBufCodecs.list()), GeneratedListRecipe::getSubRecipes,
GeneratedListRecipe::resolved
);
public static final DualMapCodec<RegistryFriendlyByteBuf, GeneratedListRecipe<?, ?>> CODECS = new DualMapCodec<>(
CODEC, STREAM_CODEC
);

@Override
protected DualMapCodec<RegistryFriendlyByteBuf, GeneratedListRecipe<?, ?>> codecs()
Expand Down

0 comments on commit fccd23b

Please sign in to comment.