Skip to content

Commit

Permalink
Lazy-load recipe category extras to speed up display of all recipes
Browse files Browse the repository at this point in the history
  • Loading branch information
mezz committed Oct 2, 2024
1 parent 2d509e3 commit da366e6
Showing 1 changed file with 13 additions and 1 deletion.
Original file line number Diff line number Diff line change
Expand Up @@ -82,6 +82,7 @@ public class RecipeLayout<R> implements IRecipeLayoutDrawable<R>, IRecipeExtrasB
private final ImmutableRect2i recipeTransferButtonArea;
private final @Nullable ShapelessIcon shapelessIcon;
private final RecipeLayoutInputHandler<R> inputHandler;
private boolean extrasCreated = false;

private ImmutableRect2i area;

Expand Down Expand Up @@ -122,7 +123,6 @@ public static <T> Optional<IRecipeLayoutDrawable<T>> create(
recipeBackground,
recipeBorderPadding
);
recipeCategory.createRecipeExtras(recipeLayout, recipe, focuses);
return Optional.of(recipeLayout);
} catch (RuntimeException | LinkageError e) {
LOGGER.error("Error caught from Recipe Category: {}", recipeCategory.getRecipeType(), e);
Expand Down Expand Up @@ -176,13 +176,21 @@ public RecipeLayout(
recipeCategory.onDisplayedIngredientsUpdate(recipe, Collections.unmodifiableList(recipeCategorySlots), focuses);
}

public void ensureRecipeExtrasAreCreated() {
if (!extrasCreated) {
extrasCreated = true;
recipeCategory.createRecipeExtras(this, recipe, focuses);
}
}

@Override
public void setPosition(int posX, int posY) {
area = area.setPosition(posX, posY);
}

@Override
public void drawRecipe(GuiGraphics guiGraphics, int mouseX, int mouseY) {
ensureRecipeExtrasAreCreated();
@SuppressWarnings("removal")
IDrawable background = recipeCategory.getBackground();

Expand Down Expand Up @@ -259,6 +267,7 @@ public void drawRecipe(GuiGraphics guiGraphics, int mouseX, int mouseY) {

@Override
public void drawOverlays(GuiGraphics guiGraphics, int mouseX, int mouseY) {
ensureRecipeExtrasAreCreated();
RenderSystem.setShaderColor(1.0F, 1.0F, 1.0F, 1.0F);

final int recipeMouseX = mouseX - area.getX();
Expand Down Expand Up @@ -335,6 +344,7 @@ public Optional<IRecipeSlotDrawable> getRecipeSlotUnderMouse(double mouseX, doub

@Override
public Optional<RecipeSlotUnderMouse> getSlotUnderMouse(double mouseX, double mouseY) {
ensureRecipeExtrasAreCreated();
final double recipeMouseX = mouseX - area.getX();
final double recipeMouseY = mouseY - area.getY();

Expand Down Expand Up @@ -381,6 +391,7 @@ public IRecipeSlotsView getRecipeSlotsView() {

@Override
public IRecipeSlotDrawablesView getRecipeSlots() {
ensureRecipeExtrasAreCreated();
return () -> Collections.unmodifiableList(recipeCategorySlots);
}

Expand All @@ -396,6 +407,7 @@ public IJeiInputHandler getInputHandler() {

@Override
public void tick() {
ensureRecipeExtrasAreCreated();
for (IRecipeWidget widget : allWidgets) {
widget.tick();
}
Expand Down

0 comments on commit da366e6

Please sign in to comment.