Skip to content
Open
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
Original file line number Diff line number Diff line change
Expand Up @@ -79,6 +79,11 @@ public List<EmiStack> getEmiStacks() {
return stacks;
}

@ApiStatus.Internal
public void setEmiStacks(List<EmiStack> stacks) {
this.stacks = stacks;
}

@Override
public long getAmount() {
return amount;
Expand Down
19 changes: 19 additions & 0 deletions xplat/src/main/java/dev/emi/emi/registry/EmiRecipes.java
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,10 @@
import java.util.function.Predicate;
import java.util.stream.Collectors;

import dev.emi.emi.api.stack.TagEmiIngredient;
import dev.emi.emi.util.EmiStackListEqualityStrategy;
import it.unimi.dsi.fastutil.ints.Int2ObjectOpenHashMap;
import it.unimi.dsi.fastutil.objects.ObjectOpenCustomHashSet;
import org.jetbrains.annotations.Nullable;

import com.google.common.collect.Iterables;
Expand Down Expand Up @@ -80,6 +84,20 @@ public static void clear() {
}
}

private static void deduplicateTagInputs(List<EmiRecipe> recipes) {
ObjectOpenCustomHashSet<List<EmiStack>> stackLists = new ObjectOpenCustomHashSet<>(EmiStackListEqualityStrategy.INSTANCE);
for (EmiRecipe recipe : recipes) {
List<EmiIngredient> recipeInputs = recipe.getInputs();
for (EmiIngredient recipeInput : recipeInputs) {
if (recipeInput instanceof TagEmiIngredient tagIngredient) {
List<EmiStack> stackList = tagIngredient.getEmiStacks();
List<EmiStack> canonicalStackList = stackLists.addOrGet(stackList);
tagIngredient.setEmiStacks(canonicalStackList);
}
}
}
}

public static void bake() {
long start = System.currentTimeMillis();
recipes.addAll(EmiData.recipes.stream().map(r -> r.get()).toList());
Expand All @@ -103,6 +121,7 @@ public static void bake() {
}
return true;
}).toList();
deduplicateTagInputs(filtered);
Map<EmiRecipeCategory, List<EmiIngredient>> filteredWorkstations = Maps.newHashMap();
for (Map.Entry<EmiRecipeCategory, List<EmiIngredient>> entry : workstations.entrySet()) {
List<EmiIngredient> w = entry.getValue().stream().filter(s -> !EmiHidden.isDisabled(s)).toList();
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,58 @@
package dev.emi.emi.util;

import dev.emi.emi.api.stack.Comparison;
import dev.emi.emi.api.stack.EmiStack;
import it.unimi.dsi.fastutil.Hash;

import java.util.List;

public final class EmiStackListEqualityStrategy implements Hash.Strategy<List<EmiStack>> {
public static final EmiStackListEqualityStrategy INSTANCE = new EmiStackListEqualityStrategy();

private EmiStackListEqualityStrategy() {}

@Override
public int hashCode(List<EmiStack> o) {
return o.hashCode();
}

private boolean stacksIdentical(EmiStack a, EmiStack b) {
if (a == b) {
return true;
}
if (a == null || b == null) {
return false;
}
// Check regular properties like item/components first
if(!a.isEqual(b, Comparison.compareComponents())) {
return false;
}
// We only need to be more strict when comparing two stacks that are not empty
if(a.isEmpty()) {
return true;
}
return stacksIdentical(a.getRemainder(), b.getRemainder()) &&
a.getChance() == b.getChance() &&
a.getAmount() == b.getAmount();
}

@Override
public boolean equals(List<EmiStack> a, List<EmiStack> b) {
if(a == b) {
return true;
}
if(a == null || b == null) {
return false;
}
if (a.size() != b.size()) {
return false;
}
int size = a.size();
for (int i = 0; i < size; i++) {
if (!stacksIdentical(a.get(i), b.get(i))) {
return false;
}
}
return true;
}
}