From d8b4afd57f79e690e4f0e3d6b6efd6091b964de1 Mon Sep 17 00:00:00 2001 From: rlnt Date: Tue, 22 Oct 2024 14:51:56 +0200 Subject: [PATCH] add skipping for recipes with invalid recipe type fixes #100 --- CHANGELOG.md | 1 + .../unification/recipe/RecipeLink.java | 23 ++++++++++++++++--- .../unification/recipe/RecipeTransformer.java | 4 +++- Common/src/test/java/testmod/TestUtils.java | 4 ++-- 4 files changed, 26 insertions(+), 6 deletions(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index de5b032..06db0aa 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -6,6 +6,7 @@ All notable changes to this project will be documented in this file. - added logging for cases where items are assigned to multiple unification tags - added logging for cases where the recipe type can't be found +- added skipping logic for recipes with invalid recipe types - added Turkish translation ([#102](https://github.com/AlmostReliable/almostunified/pull/102)) - fixed crash when runtime isn't loaded ([#101](https://github.com/AlmostReliable/almostunified/issues/101)) - fixed newly created custom tags not being considered for unification diff --git a/Common/src/main/java/com/almostreliable/unified/unification/recipe/RecipeLink.java b/Common/src/main/java/com/almostreliable/unified/unification/recipe/RecipeLink.java index 857b8ec..823e418 100644 --- a/Common/src/main/java/com/almostreliable/unified/unification/recipe/RecipeLink.java +++ b/Common/src/main/java/com/almostreliable/unified/unification/recipe/RecipeLink.java @@ -2,6 +2,7 @@ package com.almostreliable.unified.unification.recipe; import net.minecraft.resources.ResourceLocation; +import com.almostreliable.unified.AlmostUnifiedCommon; import com.almostreliable.unified.api.unification.recipe.RecipeData; import com.almostreliable.unified.utils.JsonCompare; @@ -15,19 +16,35 @@ import java.util.HashSet; import java.util.Set; import java.util.stream.Collectors; -public class RecipeLink implements RecipeData { +public final class RecipeLink implements RecipeData { + private final ResourceLocation id; private final ResourceLocation type; private final JsonObject originalRecipe; @Nullable private DuplicateLink duplicateLink; @Nullable private JsonObject unifiedRecipe; - public RecipeLink(ResourceLocation id, JsonObject originalRecipe) { + private RecipeLink(ResourceLocation id, JsonObject originalRecipe, ResourceLocation type) { this.id = id; this.originalRecipe = originalRecipe; + this.type = type; + } + @Nullable + public static RecipeLink of(ResourceLocation id, JsonObject originalRecipe) { + ResourceLocation type = ResourceLocation.tryParse(originalRecipe.get("type").getAsString()); + if (type == null) { + AlmostUnifiedCommon.LOGGER.warn("Could not detect recipe type for recipe '{}', skipping.", id); + return null; + } + + return new RecipeLink(id, originalRecipe, type); + } + + public static RecipeLink ofOrThrow(ResourceLocation id, JsonObject originalRecipe) { try { - this.type = ResourceLocation.parse(originalRecipe.get("type").getAsString()); + ResourceLocation type = ResourceLocation.parse(originalRecipe.get("type").getAsString()); + return new RecipeLink(id, originalRecipe, type); } catch (Exception e) { throw new IllegalArgumentException("could not detect recipe type for recipe " + id); } diff --git a/Common/src/main/java/com/almostreliable/unified/unification/recipe/RecipeTransformer.java b/Common/src/main/java/com/almostreliable/unified/unification/recipe/RecipeTransformer.java index 3b9b4af..a1f58e2 100644 --- a/Common/src/main/java/com/almostreliable/unified/unification/recipe/RecipeTransformer.java +++ b/Common/src/main/java/com/almostreliable/unified/unification/recipe/RecipeTransformer.java @@ -31,6 +31,7 @@ import java.util.HashSet; import java.util.LinkedHashSet; import java.util.List; import java.util.Map; +import java.util.Objects; import java.util.Set; import java.util.function.Consumer; import java.util.stream.Collectors; @@ -111,7 +112,8 @@ public class RecipeTransformer { return recipes .entrySet() .stream() - .map(entry -> new RecipeLink(entry.getKey(), entry.getValue().getAsJsonObject())) + .map(entry -> RecipeLink.of(entry.getKey(), entry.getValue().getAsJsonObject())) + .filter(Objects::nonNull) .sorted(Comparator.comparing(entry -> entry.getId().toString())) .collect(Collectors.groupingByConcurrent(RecipeLink::getType)); } diff --git a/Common/src/test/java/testmod/TestUtils.java b/Common/src/test/java/testmod/TestUtils.java index e5b5728..c68f7fc 100644 --- a/Common/src/test/java/testmod/TestUtils.java +++ b/Common/src/test/java/testmod/TestUtils.java @@ -83,11 +83,11 @@ public class TestUtils { public static RecipeLink recipe(String jsonStr) { var json = json(jsonStr); - return new RecipeLink(ResourceLocation.parse("test"), json); + return RecipeLink.ofOrThrow(ResourceLocation.parse("test"), json); } public static RecipeLink recipe(JsonObject json) { - return new RecipeLink(ResourceLocation.parse("test"), json); + return RecipeLink.ofOrThrow(ResourceLocation.parse("test"), json); } public static JsonObject json(String json) {