Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
26 changes: 26 additions & 0 deletions src/main/java/com/gregtechceu/gtceu/api/codec/GTCodecUtils.java
Original file line number Diff line number Diff line change
@@ -1,8 +1,12 @@
package com.gregtechceu.gtceu.api.codec;

import com.gregtechceu.gtceu.GTCEu;

import com.google.gson.JsonParseException;
import com.mojang.datafixers.util.Either;
import com.mojang.serialization.Codec;
import com.mojang.serialization.DataResult;
import com.mojang.serialization.MapCodec;

import java.util.function.Function;

Expand Down Expand Up @@ -30,4 +34,26 @@ public static Codec<Long> longRange(long min, long max) {
public static <T> T unboxEither(Either<T, T> either) {
return either.map(Function.identity(), Function.identity());
}

public static <T> MapCodec<T> quietExceptionCodec(Codec<T> codec, String field, boolean isKubeLoaded) {
return codec.optionalFieldOf(field, null).flatXmap(
val -> {
if (val != null) return DataResult.success(val);

String msg = "Recipe " + field + " field is invalid!";
if (isKubeLoaded) {
throw quietException(msg);
} else {
GTCEu.LOGGER.error(msg);
}
return DataResult.error(() -> msg);
},
DataResult::success);
}

public static JsonParseException quietException(String msg) {
var ex = new JsonParseException(msg);
ex.setStackTrace(new StackTraceElement[0]);
return ex;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,8 @@
import java.util.*;
import java.util.function.Function;

import static com.gregtechceu.gtceu.api.codec.GTCodecUtils.quietExceptionCodec;

@SuppressWarnings("DataFlowIssue")
public class GTRecipeSerializer implements RecipeSerializer<GTRecipe> {

Expand Down Expand Up @@ -262,7 +264,7 @@ private static MapCodec<GTRecipe> makeCodec(boolean isKubeLoaded) {
CHANCE_LOGIC_MAP_CODEC.optionalFieldOf("tickOutputChanceLogics", Map.of()).forGetter(val -> val.tickOutputChanceLogics),
RecipeCondition.CODEC.listOf().optionalFieldOf("recipeConditions", List.of()).forGetter(val -> val.conditions),
CompoundTag.CODEC.optionalFieldOf("data", new CompoundTag()).forGetter(val -> val.data),
ExtraCodecs.NON_NEGATIVE_INT.fieldOf("duration").forGetter(val -> val.duration),
quietExceptionCodec(ExtraCodecs.NON_NEGATIVE_INT,"duration",isKubeLoaded).forGetter(val -> val.duration),
GTRegistries.RECIPE_CATEGORIES.byNameCodec().optionalFieldOf("category", GTRecipeCategory.DEFAULT).forGetter(val -> val.recipeCategory)
).apply(instance, GTRecipe::new));
} else {
Expand All @@ -279,7 +281,7 @@ private static MapCodec<GTRecipe> makeCodec(boolean isKubeLoaded) {
RecipeCondition.CODEC.listOf().optionalFieldOf("recipeConditions", List.of()).forGetter(val -> val.conditions),
IngredientActionHolder.CODEC.listOf().optionalFieldOf("kubejs:actions", List.of()).forGetter(val -> (List<IngredientActionHolder>) val.ingredientActions),
CompoundTag.CODEC.optionalFieldOf("data", new CompoundTag()).forGetter(val -> val.data),
ExtraCodecs.NON_NEGATIVE_INT.fieldOf("duration").forGetter(val -> val.duration),
quietExceptionCodec(ExtraCodecs.NON_NEGATIVE_INT,"duration",isKubeLoaded).forGetter(val -> val.duration),
GTRegistries.RECIPE_CATEGORIES.byNameCodec().optionalFieldOf("category", GTRecipeCategory.DEFAULT).forGetter(val -> val.recipeCategory)
).apply(instance, GTRecipe::new));
}
Expand Down
Loading