Skip to content

Commit

Permalink
Optimize adding ingredients to recipes for lookups
Browse files Browse the repository at this point in the history
  • Loading branch information
mezz committed Aug 12, 2024
1 parent 6c8f8f7 commit f4e0b3c
Show file tree
Hide file tree
Showing 12 changed files with 211 additions and 136 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@
import mezz.jei.common.platform.Services;
import mezz.jei.common.util.ImmutableRect2i;
import mezz.jei.common.util.SafeIngredientUtil;
import mezz.jei.library.ingredients.IngredientAcceptor;
import mezz.jei.library.ingredients.DisplayIngredientAcceptor;
import net.minecraft.ChatFormatting;
import net.minecraft.client.gui.GuiGraphics;
import net.minecraft.client.renderer.Rect2i;
Expand Down Expand Up @@ -64,7 +64,7 @@ public class RecipeSlot implements IRecipeSlotView, IRecipeSlotDrawable {
private List<Optional<ITypedIngredient<?>>> displayIngredients;

@Nullable
private IngredientAcceptor displayOverrides;
private DisplayIngredientAcceptor displayOverrides;

public RecipeSlot(
RecipeIngredientRole role,
Expand Down Expand Up @@ -342,7 +342,7 @@ public void clearDisplayOverrides() {
public IIngredientConsumer createDisplayOverrides() {
if (displayOverrides == null) {
IIngredientManager ingredientManager = Internal.getJeiRuntime().getIngredientManager();
displayOverrides = new IngredientAcceptor(ingredientManager);
displayOverrides = new DisplayIngredientAcceptor(ingredientManager);
}
return displayOverrides;
}
Expand Down
Original file line number Diff line number Diff line change
@@ -1,52 +1,27 @@
package mezz.jei.library.gui.recipes;

import mezz.jei.api.ingredients.IIngredientType;
import mezz.jei.api.ingredients.ITypedIngredient;
import mezz.jei.api.recipe.RecipeIngredientRole;
import mezz.jei.library.gui.recipes.layout.builder.RecipeSlotIngredients;
import mezz.jei.library.gui.recipes.supplier.builder.IngredientSlotBuilder;
import mezz.jei.library.ingredients.IIngredientSupplier;

import java.util.ArrayList;
import java.util.Collection;
import java.util.List;
import java.util.Optional;
import java.util.stream.Stream;
import java.util.Map;

public class RecipeLayoutIngredientSupplier implements IIngredientSupplier {
private final List<RecipeSlotIngredients> slots;
private final Map<RecipeIngredientRole, IngredientSlotBuilder> ingredientSlotBuilders;

public RecipeLayoutIngredientSupplier(List<RecipeSlotIngredients> slots) {
this.slots = slots;
public RecipeLayoutIngredientSupplier(Map<RecipeIngredientRole, IngredientSlotBuilder> ingredientSlotBuilders) {
this.ingredientSlotBuilders = ingredientSlotBuilders;
}

@Override
public Stream<? extends IIngredientType<?>> getIngredientTypes(RecipeIngredientRole role) {
return slots.stream()
.filter(slot -> slot.role() == role)
.map(RecipeSlotIngredients::types)
.flatMap(Collection::stream)
.distinct();
}

@Override
public <T> Stream<T> getIngredientStream(IIngredientType<T> ingredientType, RecipeIngredientRole role) {
return slots.stream()
.filter(slot -> slot.role() == role)
.flatMap(slot -> slot.ingredients().stream())
.map(ingredientType::castIngredient)
.flatMap(Optional::stream);
}

@Override
public Collection<Optional<ITypedIngredient<?>>> getIngredients(RecipeIngredientRole role) {
List<Optional<ITypedIngredient<?>>> ingredients = new ArrayList<>();

for (RecipeSlotIngredients slot : slots) {
if (slot.role() == role) {
ingredients.addAll(slot.ingredients());
}
public Collection<ITypedIngredient<?>> getIngredients(RecipeIngredientRole role) {
IngredientSlotBuilder ingredientSlotBuilder = ingredientSlotBuilders.get(role);
if (ingredientSlotBuilder == null) {
return List.of();
}

return ingredients;
return ingredientSlotBuilder.getAllIngredients();
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,7 @@
import mezz.jei.library.gui.recipes.OutputSlotTooltipCallback;
import mezz.jei.library.gui.recipes.RecipeLayout;
import mezz.jei.library.gui.recipes.ShapelessIcon;
import mezz.jei.library.ingredients.IngredientAcceptor;
import mezz.jei.library.ingredients.DisplayIngredientAcceptor;
import net.minecraft.resources.ResourceLocation;
import org.jetbrains.annotations.Nullable;

Expand Down Expand Up @@ -152,16 +152,16 @@ public void createFocusLink(IIngredientAcceptor<?>... slots) {
RecipeSlotBuilder builder = (RecipeSlotBuilder) slot;
builders.add(builder);

IngredientAcceptor ingredientAcceptor = builder.getIngredientAcceptor();
List<Optional<ITypedIngredient<?>>> allIngredients = ingredientAcceptor.getAllIngredients();
DisplayIngredientAcceptor displayIngredientAcceptor = builder.getIngredientAcceptor();
List<Optional<ITypedIngredient<?>>> allIngredients = displayIngredientAcceptor.getAllIngredients();
int ingredientCount = allIngredients.size();
if (count == -1) {
count = ingredientCount;
} else if (count != ingredientCount) {
IntSummaryStatistics stats = Arrays.stream(slots)
.map(RecipeSlotBuilder.class::cast)
.map(RecipeSlotBuilder::getIngredientAcceptor)
.map(IngredientAcceptor::getAllIngredients)
.map(DisplayIngredientAcceptor::getAllIngredients)
.mapToInt(Collection::size)
.summaryStatistics();
throw new IllegalArgumentException(
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@
import mezz.jei.library.gui.ingredients.ICycler;
import mezz.jei.library.gui.ingredients.RecipeSlot;
import mezz.jei.library.gui.ingredients.RendererOverrides;
import mezz.jei.library.ingredients.IngredientAcceptor;
import mezz.jei.library.ingredients.DisplayIngredientAcceptor;
import mezz.jei.common.platform.IPlatformFluidHelperInternal;
import mezz.jei.common.platform.Services;
import mezz.jei.common.util.ErrorUtil;
Expand All @@ -33,7 +33,7 @@
import java.util.Set;

public class RecipeSlotBuilder implements IRecipeSlotBuilder {
private final IngredientAcceptor ingredients;
private final DisplayIngredientAcceptor ingredients;
private final RecipeIngredientRole role;
private final List<IRecipeSlotTooltipCallback> tooltipCallbacks = new ArrayList<>();
private ImmutableRect2i rect;
Expand All @@ -44,7 +44,7 @@ public class RecipeSlotBuilder implements IRecipeSlotBuilder {
private @Nullable ISlottedWidgetFactory<?> assignedWidgetFactory;

public RecipeSlotBuilder(IIngredientManager ingredientManager, RecipeIngredientRole role, int x, int y) {
this.ingredients = new IngredientAcceptor(ingredientManager);
this.ingredients = new DisplayIngredientAcceptor(ingredientManager);
this.rect = new ImmutableRect2i(x, y, 16, 16);
this.role = role;
}
Expand Down Expand Up @@ -200,7 +200,7 @@ public IntSet getMatches(IFocusGroup focuses) {
return this.ingredients.getMatches(focuses, role);
}

public IngredientAcceptor getIngredientAcceptor() {
public DisplayIngredientAcceptor getIngredientAcceptor() {
return ingredients;
}

Expand Down

This file was deleted.

Original file line number Diff line number Diff line change
Expand Up @@ -6,15 +6,14 @@
import mezz.jei.api.ingredients.IIngredientRenderer;
import mezz.jei.api.ingredients.IIngredientType;
import mezz.jei.api.ingredients.ITypedIngredient;
import mezz.jei.api.recipe.RecipeIngredientRole;
import mezz.jei.api.runtime.IIngredientManager;
import mezz.jei.library.gui.recipes.layout.builder.RecipeSlotBuilder;
import mezz.jei.library.gui.recipes.layout.builder.RecipeSlotIngredients;
import mezz.jei.library.ingredients.IngredientAcceptor;
import mezz.jei.library.ingredients.SimpleIngredientAcceptor;
import net.minecraft.nbt.CompoundTag;
import net.minecraft.world.level.material.Fluid;
import org.jetbrains.annotations.Nullable;

import java.util.Collection;
import java.util.List;
import java.util.Optional;

Expand All @@ -23,12 +22,10 @@
* but doesn't bother building anything for drawing on screen.
*/
public class IngredientSlotBuilder implements IRecipeSlotBuilder {
private final IngredientAcceptor ingredients;
private final RecipeIngredientRole role;
private final SimpleIngredientAcceptor ingredients;

public IngredientSlotBuilder(IIngredientManager ingredientManager, RecipeIngredientRole role) {
this.ingredients = new IngredientAcceptor(ingredientManager);
this.role = role;
public IngredientSlotBuilder(IIngredientManager ingredientManager) {
this.ingredients = new SimpleIngredientAcceptor(ingredientManager);
}

@Override
Expand Down Expand Up @@ -103,11 +100,7 @@ public IRecipeSlotBuilder setSlotName(String slotName) {
return this;
}

public RecipeSlotIngredients getRecipeSlotIngredients() {
return new RecipeSlotIngredients(
this.role,
this.ingredients.getAllIngredients(),
this.ingredients.getIngredientTypes()
);
public Collection<ITypedIngredient<?>> getAllIngredients() {
return this.ingredients.getAllIngredients();
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -7,28 +7,31 @@
import mezz.jei.api.recipe.RecipeIngredientRole;
import mezz.jei.api.runtime.IIngredientManager;
import mezz.jei.library.gui.recipes.RecipeLayoutIngredientSupplier;
import mezz.jei.library.gui.recipes.layout.builder.RecipeSlotIngredients;
import mezz.jei.library.ingredients.IIngredientSupplier;

import java.util.ArrayList;
import java.util.List;
import java.util.EnumMap;
import java.util.Map;

/**
* Minimal version of {@link IRecipeLayoutBuilder} that can only return the ingredients,
* but doesn't bother building anything for drawing on screen.
* but doesn't bother building real slots or anything else for drawing on screen.
*/
public class IngredientSupplierBuilder implements IRecipeLayoutBuilder {
private final List<IngredientSlotBuilder> slots = new ArrayList<>();
private final IIngredientManager ingredientManager;
private final Map<RecipeIngredientRole, IngredientSlotBuilder> ingredientSlotBuilders;

public IngredientSupplierBuilder(IIngredientManager ingredientManager) {
this.ingredientManager = ingredientManager;
this.ingredientSlotBuilders = new EnumMap<>(RecipeIngredientRole.class);
}

@Override
public IRecipeSlotBuilder addSlot(RecipeIngredientRole role, int x, int y) {
IngredientSlotBuilder slot = new IngredientSlotBuilder(ingredientManager, role);
this.slots.add(slot);
IngredientSlotBuilder slot = ingredientSlotBuilders.get(role);
if (slot == null) {
slot = new IngredientSlotBuilder(ingredientManager);
ingredientSlotBuilders.put(role, slot);
}
return slot;
}

Expand Down Expand Up @@ -63,10 +66,6 @@ public void createFocusLink(IIngredientAcceptor<?>... slots) {
}

public IIngredientSupplier buildIngredientSupplier() {
List<RecipeSlotIngredients> ingredients = new ArrayList<>();
for (IngredientSlotBuilder slot : this.slots) {
ingredients.add(slot.getRecipeSlotIngredients());
}
return new RecipeLayoutIngredientSupplier(ingredients);
return new RecipeLayoutIngredientSupplier(this.ingredientSlotBuilders);
}
}
Loading

0 comments on commit f4e0b3c

Please sign in to comment.