From 9ecf9ff1c9595dbee646e80a7aaa60fddcebaf3b Mon Sep 17 00:00:00 2001 From: LLytho <29131229+LLytho@users.noreply.github.com> Date: Thu, 14 Jul 2022 23:02:22 +0200 Subject: [PATCH] Fix duplicate link --- .../unified/recipe/DuplicateLink.java | 38 --------- .../unified/recipe/RawRecipe.java | 79 ++++++++++++++++--- .../unified/recipe/RecipeTransformer.java | 41 ++-------- 3 files changed, 77 insertions(+), 81 deletions(-) delete mode 100644 Common/src/main/java/com/almostreliable/unified/recipe/DuplicateLink.java diff --git a/Common/src/main/java/com/almostreliable/unified/recipe/DuplicateLink.java b/Common/src/main/java/com/almostreliable/unified/recipe/DuplicateLink.java deleted file mode 100644 index 3dbd1cc..0000000 --- a/Common/src/main/java/com/almostreliable/unified/recipe/DuplicateLink.java +++ /dev/null @@ -1,38 +0,0 @@ -package com.almostreliable.unified.recipe; - -import java.util.Collections; -import java.util.HashSet; -import java.util.Objects; -import java.util.Set; - -public class DuplicateLink { - private RawRecipe currentMaster; - private final Set recipes = new HashSet<>(); - - public DuplicateLink(RawRecipe master) { - updateMaster(master); - } - - void updateMaster(RawRecipe master) { - Objects.requireNonNull(master); - addDuplicate(master); - this.currentMaster = master; - } - - void addDuplicate(RawRecipe recipe) { - recipes.add(recipe); - } - - public RawRecipe getMaster() { - return currentMaster; - } - - public Set getRecipes() { - return Collections.unmodifiableSet(recipes); - } - - @Override - public String toString() { - return "Link{currentMaster=" + currentMaster + ", recipes=" + recipes.size() + "}"; - } -} diff --git a/Common/src/main/java/com/almostreliable/unified/recipe/RawRecipe.java b/Common/src/main/java/com/almostreliable/unified/recipe/RawRecipe.java index 9c18c4c..682ad9e 100644 --- a/Common/src/main/java/com/almostreliable/unified/recipe/RawRecipe.java +++ b/Common/src/main/java/com/almostreliable/unified/recipe/RawRecipe.java @@ -6,9 +6,7 @@ import com.google.gson.JsonObject; import net.minecraft.resources.ResourceLocation; import javax.annotation.Nullable; -import java.util.LinkedHashMap; -import java.util.List; -import java.util.Objects; +import java.util.*; public class RawRecipe { private final ResourceLocation id; @@ -40,14 +38,19 @@ public class RawRecipe { return originalRecipe; } - void linkDuplicate(RawRecipe recipe) { - Objects.requireNonNull(recipe); - if(recipe.getDuplicateLink() != null) { - AlmostUnified.LOG.error("Recipe {} already linked", recipe.getId()); + private void setDuplicateLink(@Nullable DuplicateLink duplicateLink) { + Objects.requireNonNull(duplicateLink); + if (hasDuplicateLink()) { + throw new IllegalStateException("Recipe already linked"); } - this.duplicateLink = new DuplicateLink(this); - this.duplicateLink.addDuplicate(recipe); + this.duplicateLink = duplicateLink; + this.duplicateLink.addDuplicate(this); + } + + + public boolean hasDuplicateLink() { + return duplicateLink != null; } @Nullable @@ -115,4 +118,62 @@ public class RawRecipe { String transformed = transformedRecipe != null ? " (transformed)" : ""; return String.format("['%s'] %s%s%s", type, id, duplicate, transformed); } + + public boolean handleDuplicate(RawRecipe recipe) { + if (hasDuplicateLink()) { + throw new IllegalStateException("Recipe already linked"); + } + + DuplicateLink link = recipe.getDuplicateLink(); + if(link != null) { + RawRecipe compare = compare(link.getMaster()); + if(compare != null) { + link.updateMaster(this); + setDuplicateLink(link); + return true; + } + } else { + RawRecipe compare = compare(recipe); + if(compare != null) { + DuplicateLink newLink = new DuplicateLink(compare); + setDuplicateLink(newLink); + recipe.setDuplicateLink(newLink); + return true; + } + } + + return false; + } + + public static class DuplicateLink { + private RawRecipe currentMaster; + private final Set recipes = new HashSet<>(); + + private DuplicateLink(RawRecipe master) { + updateMaster(master); + } + + private void updateMaster(RawRecipe master) { + Objects.requireNonNull(master); + addDuplicate(master); + this.currentMaster = master; + } + + private void addDuplicate(RawRecipe recipe) { + recipes.add(recipe); + } + + public RawRecipe getMaster() { + return currentMaster; + } + + public Set getRecipes() { + return Collections.unmodifiableSet(recipes); + } + + @Override + public String toString() { + return "Link{currentMaster=" + currentMaster + ", recipes=" + recipes.size() + "}"; + } + } } diff --git a/Common/src/main/java/com/almostreliable/unified/recipe/RecipeTransformer.java b/Common/src/main/java/com/almostreliable/unified/recipe/RecipeTransformer.java index 816c8a3..e97125d 100644 --- a/Common/src/main/java/com/almostreliable/unified/recipe/RecipeTransformer.java +++ b/Common/src/main/java/com/almostreliable/unified/recipe/RecipeTransformer.java @@ -9,6 +9,7 @@ import com.google.gson.JsonPrimitive; import net.minecraft.resources.ResourceLocation; import javax.annotation.Nullable; +import java.util.HashMap; import java.util.List; import java.util.Map; import java.util.Objects; @@ -42,6 +43,7 @@ public class RecipeTransformer { RecipeTransformationResult recipeTransformationResult = new RecipeTransformationResult(); + Map> links = new HashMap<>(); rawRecipesByType.forEach((type, rawRecipes) -> { for (int curIndex = 0; curIndex < rawRecipes.size(); curIndex++) { RawRecipe curRecipe = rawRecipes.get(curIndex); @@ -53,27 +55,18 @@ public class RecipeTransformer { handleDuplicate(curRecipe, rawRecipes); } } - // TODO remove later - List duplicateLinks = rawRecipes + List duplicateLinks = rawRecipes .stream() .map(RawRecipe::getDuplicateLink) .filter(Objects::nonNull) .distinct() .toList(); - - String s = ""; + if(duplicateLinks.size() > 0) { + links.put(type, duplicateLinks); + } }); -// Map> duplicates = new HashMap<>(); - -// rawRecipesByType.forEach((type, rawRecipes) -> { -// List duplicatesForType = rawRecipes.stream().filter(RawRecipe::isDuplicate).collect(Collectors.toList()); -// if(!duplicatesForType.isEmpty()) { -// duplicates.put(type, duplicatesForType); -// } -// }); - recipeTransformationResult.end(); // for (var entry : recipes.entrySet()) { @@ -103,32 +96,12 @@ public class RecipeTransformer { return; } - if (handleDuplicate(curRecipe, rawRecipe)) { + if (curRecipe.handleDuplicate(rawRecipe)) { return; } } } - private boolean handleDuplicate(RawRecipe curRecipe, RawRecipe rawRecipe) { - DuplicateLink link = rawRecipe.getDuplicateLink(); - if(link != null) { - RawRecipe master = link.getMaster(); - RawRecipe compare = curRecipe.compare(master); - if(compare != null) { - link.updateMaster(compare); - return true; - } - } else { - RawRecipe compare = curRecipe.compare(rawRecipe); - if(compare != null) { - rawRecipe.linkDuplicate(compare); - return true; - } - } - return false; - } - - @Nullable public JsonObject transformRecipe(ResourceLocation recipeId, JsonObject json) { try {