Fix duplicate link

This commit is contained in:
LLytho 2022-07-14 23:02:22 +02:00
parent b9ca0dbfc7
commit 9ecf9ff1c9
3 changed files with 77 additions and 81 deletions

View file

@ -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<RawRecipe> 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<RawRecipe> getRecipes() {
return Collections.unmodifiableSet(recipes);
}
@Override
public String toString() {
return "Link{currentMaster=" + currentMaster + ", recipes=" + recipes.size() + "}";
}
}

View file

@ -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<RawRecipe> 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<RawRecipe> getRecipes() {
return Collections.unmodifiableSet(recipes);
}
@Override
public String toString() {
return "Link{currentMaster=" + currentMaster + ", recipes=" + recipes.size() + "}";
}
}
}

View file

@ -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<ResourceLocation, List<RawRecipe.DuplicateLink>> 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<DuplicateLink> duplicateLinks = rawRecipes
List<RawRecipe.DuplicateLink> duplicateLinks = rawRecipes
.stream()
.map(RawRecipe::getDuplicateLink)
.filter(Objects::nonNull)
.distinct()
.toList();
String s = "";
if(duplicateLinks.size() > 0) {
links.put(type, duplicateLinks);
}
});
// Map<ResourceLocation, List<RawRecipe>> duplicates = new HashMap<>();
// rawRecipesByType.forEach((type, rawRecipes) -> {
// List<RawRecipe> 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 {