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
Original file line number Diff line number Diff line change
Expand Up @@ -40,6 +40,7 @@
import org.jetbrains.annotations.NotNull;
import org.jetbrains.annotations.Nullable;
import org.jetbrains.annotations.UnknownNullability;
import org.jetbrains.annotations.Unmodifiable;

import java.util.*;
import java.util.stream.Collectors;
Expand Down Expand Up @@ -72,7 +73,7 @@ public FluidIngredient copyWithModifier(FluidIngredient content, ContentModifier
}

@Override
public List<Object> compressIngredients(Collection<Object> ingredients) {
public List<Object> compressIngredients(@Unmodifiable Collection<Object> ingredients) {
List<Object> list = new ObjectArrayList<>(ingredients.size());
for (Object item : ingredients) {
if (item instanceof FluidIngredient fluid) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -46,6 +46,7 @@
import org.jetbrains.annotations.NotNull;
import org.jetbrains.annotations.Nullable;
import org.jetbrains.annotations.UnknownNullability;
import org.jetbrains.annotations.Unmodifiable;

import java.util.*;
import java.util.stream.Collectors;
Expand Down Expand Up @@ -78,7 +79,7 @@ public Ingredient copyWithModifier(Ingredient content, ContentModifier modifier)
}

@Override
public List<Object> compressIngredients(Collection<Object> ingredients) {
public List<Object> compressIngredients(@Unmodifiable Collection<Object> ingredients) {
List<Object> list = new ObjectArrayList<>(ingredients.size());
for (Object item : ingredients) {
if (item instanceof Ingredient ingredient) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,7 @@
import org.apache.commons.lang3.mutable.MutableInt;
import org.jetbrains.annotations.NotNull;
import org.jetbrains.annotations.Nullable;
import org.jetbrains.annotations.Unmodifiable;

import java.util.*;

Expand Down Expand Up @@ -115,7 +116,7 @@ public boolean isRecipeSearchFilter() {
return false;
}

public List<Object> compressIngredients(Collection<Object> ingredients) {
public List<Object> compressIngredients(@Unmodifiable Collection<Object> ingredients) {
return new ArrayList<>(ingredients);
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,8 +4,6 @@
import com.gregtechceu.gtceu.api.gui.widget.EnumSelectorWidget;
import com.gregtechceu.gtceu.api.recipe.GTRecipeType;
import com.gregtechceu.gtceu.api.recipe.content.Content;
import com.gregtechceu.gtceu.api.recipe.lookup.GTRecipeLookup;
import com.gregtechceu.gtceu.api.recipe.lookup.ingredient.MapIngredientTypeManager;
import com.gregtechceu.gtceu.common.data.GTRecipeTypes;
import com.gregtechceu.gtceu.utils.ItemStackHashStrategy;

Expand All @@ -19,7 +17,7 @@

import it.unimi.dsi.fastutil.objects.Object2IntOpenCustomHashMap;

import java.util.List;
import java.util.Collections;
import java.util.function.Consumer;

public class SmartItemFilter implements ItemFilter {
Expand Down Expand Up @@ -90,10 +88,11 @@ public int testItemCount(ItemStack itemStack) {

private int lookup(ItemStack itemStack) {
ItemStack copy = itemStack.copyWithCount(Integer.MAX_VALUE);
var ingredients = MapIngredientTypeManager.getFrom(copy, ItemRecipeCapability.CAP);
var recipe = filterMode.lookup.recurseIngredientTreeFindRecipe(List.of(ingredients),
filterMode.lookup.getLookup(), r -> true);
if (recipe == null) return 0;
var recipe = filterMode.recipeType.db()
.find(Collections.singletonMap(ItemRecipeCapability.CAP, Collections.singletonList(copy)), r -> true);
if (recipe == null) {
return 0;
}
for (Content content : recipe.getInputContents(ItemRecipeCapability.CAP)) {
var stacks = ItemRecipeCapability.CAP.of(content.getContent()).getItems();
for (var stack : stacks) {
Expand Down Expand Up @@ -121,13 +120,13 @@ private enum SmartFilteringMode implements EnumSelectorWidget.SelectableEnum {

private static final SmartFilteringMode[] VALUES = values();
private final String localeName;
private final GTRecipeLookup lookup;
private final GTRecipeType recipeType;
private final Object2IntOpenCustomHashMap<ItemStack> cache = new Object2IntOpenCustomHashMap<>(
ItemStackHashStrategy.comparingAllButCount());

SmartFilteringMode(String localeName, GTRecipeType type) {
this.localeName = localeName;
this.lookup = type.getLookup();
this.recipeType = type;
}

@Override
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,8 @@
import com.gregtechceu.gtceu.api.gui.SteamTexture;
import com.gregtechceu.gtceu.api.recipe.category.GTRecipeCategory;
import com.gregtechceu.gtceu.api.recipe.chance.boost.ChanceBoostFunction;
import com.gregtechceu.gtceu.api.recipe.lookup.GTRecipeLookup;
import com.gregtechceu.gtceu.api.recipe.lookup.RecipeAdditionHandler;
import com.gregtechceu.gtceu.api.recipe.lookup.RecipeDB;
import com.gregtechceu.gtceu.api.recipe.ui.GTRecipeTypeUI;
import com.gregtechceu.gtceu.api.sound.SoundEntry;
import com.gregtechceu.gtceu.data.recipe.builder.GTRecipeBuilder;
Expand All @@ -30,6 +31,7 @@
import lombok.Getter;
import lombok.Setter;
import lombok.experimental.Accessors;
import org.jetbrains.annotations.ApiStatus;
import org.jetbrains.annotations.NotNull;
import org.jetbrains.annotations.Nullable;

Expand Down Expand Up @@ -79,8 +81,10 @@ public class GTRecipeType implements RecipeType<GTRecipe> {
private final GTRecipeCategory category;
@Getter
private final Map<GTRecipeCategory, Set<GTRecipe>> categoryMap = new Object2ObjectOpenHashMap<>();
private final RecipeDB db = new RecipeDB();
@ApiStatus.Internal
@Getter
private final GTRecipeLookup lookup = new GTRecipeLookup(this);
private final RecipeAdditionHandler additionHandler = new RecipeAdditionHandler(db);
@Setter
@Getter
private boolean offsetVoltageText = false;
Expand Down Expand Up @@ -195,7 +199,10 @@ public String toString() {

public @NotNull Iterator<GTRecipe> searchRecipe(IRecipeCapabilityHolder holder, Predicate<GTRecipe> canHandle) {
if (!holder.hasCapabilityProxies()) return Collections.emptyIterator();
var iterator = getLookup().getRecipeIterator(holder, canHandle);
var iterator = db.iterator(holder, canHandle);
if (iterator == null) {
return Collections.emptyIterator();
}
boolean any = false;
while (iterator.hasNext()) {
GTRecipe recipe = iterator.next();
Expand Down Expand Up @@ -321,6 +328,10 @@ public Set<GTRecipe> getRecipesInCategory(GTRecipeCategory category) {
return Collections.unmodifiableSet(categoryMap.getOrDefault(category, Set.of()));
}

public @NotNull RecipeDB db() {
return db;
}

public interface ICustomRecipeLogic {

/**
Expand Down
37 changes: 11 additions & 26 deletions src/main/java/com/gregtechceu/gtceu/api/recipe/lookup/Branch.java
Original file line number Diff line number Diff line change
Expand Up @@ -5,42 +5,19 @@

import com.mojang.datafixers.util.Either;
import it.unimi.dsi.fastutil.objects.Object2ObjectOpenHashMap;
import org.jetbrains.annotations.ApiStatus;
import org.jetbrains.annotations.NotNull;

import java.util.Map;
import java.util.stream.Stream;

public class Branch {
@ApiStatus.Internal
final class Branch {

// Keys on this have *(should)* have unique hashcodes.
private Map<AbstractMapIngredient, Either<GTRecipe, Branch>> nodes;
// Keys on this have collisions, and must be differentiated by equality.
private Map<AbstractMapIngredient, Either<GTRecipe, Branch>> specialNodes;

public Stream<GTRecipe> getRecipes(boolean filterHidden) {
Stream<GTRecipe> stream = null;
if (nodes != null) {
stream = nodes.values().stream()
.flatMap(either -> either.map(Stream::of, right -> right.getRecipes(filterHidden)));
}
if (specialNodes != null) {
if (stream == null) {
stream = specialNodes.values().stream()
.flatMap(either -> either.map(Stream::of, right -> right.getRecipes(filterHidden)));
} else {
stream = Stream.concat(stream, specialNodes.values().stream()
.flatMap(either -> either.map(Stream::of, right -> right.getRecipes(filterHidden))));
}
}
if (stream == null) {
return Stream.empty();
}
if (filterHidden) {
// stream = stream.filter(t -> !t.isHidden());
}
return stream;
}

public boolean isEmptyBranch() {
return (nodes == null || nodes.isEmpty()) && (specialNodes == null || specialNodes.isEmpty());
}
Expand All @@ -60,4 +37,12 @@ public Map<AbstractMapIngredient, Either<GTRecipe, Branch>> getSpecialNodes() {
}
return specialNodes;
}

/**
* Removes all nodes in the branch
*/
public void clear() {
this.specialNodes = null;
this.nodes = null;
}
}
Loading