Skip to content

Commit

Permalink
Optimize displaying recipes by using less-strict checking for rendere…
Browse files Browse the repository at this point in the history
…d ingredients
  • Loading branch information
mezz committed Oct 2, 2024
1 parent b033bbe commit 4d89747
Show file tree
Hide file tree
Showing 6 changed files with 108 additions and 48 deletions.
31 changes: 22 additions & 9 deletions Gui/src/main/java/mezz/jei/gui/recipes/RecipesGui.java
Original file line number Diff line number Diff line change
Expand Up @@ -613,30 +613,43 @@ public Optional<IUserInputHandler> handleUserInput(Screen screen, UserInput inpu
double mouseY = input.getMouseY();
if (recipesGui.isMouseOver(mouseX, mouseY)) {
if (recipesGui.recipeCategoryTitle.isMouseOver(mouseX, mouseY)) {
if (input.is(keyBindings.getLeftClick()) && recipesGui.logic.showAllRecipes()) {
return Optional.of(this);
}
if (input.is(keyBindings.getLeftClick()))
if (input.isSimulate() || recipesGui.logic.showAllRecipes()) {
return Optional.of(this);
}
}
}

Minecraft minecraft = Minecraft.getInstance();
if (input.is(keyBindings.getCloseRecipeGui()) || input.is(minecraft.options.keyInventory)) {
recipesGui.onClose();
if (!input.isSimulate()) {
recipesGui.onClose();
}
return Optional.of(this);
} else if (input.is(keyBindings.getRecipeBack())) {
recipesGui.back();
if (!input.isSimulate()) {
recipesGui.back();
}
return Optional.of(this);
} else if (input.is(keyBindings.getNextCategory())) {
recipesGui.logic.nextRecipeCategory();
if (!input.isSimulate()) {
recipesGui.logic.nextRecipeCategory();
}
return Optional.of(this);
} else if (input.is(keyBindings.getPreviousCategory())) {
recipesGui.logic.previousRecipeCategory();
if (!input.isSimulate()) {
recipesGui.logic.previousRecipeCategory();
}
return Optional.of(this);
} else if (input.is(keyBindings.getNextRecipePage())) {
recipesGui.logic.nextPage();
if (!input.isSimulate()) {
recipesGui.logic.nextPage();
}
return Optional.of(this);
} else if (input.is(keyBindings.getPreviousRecipePage())) {
recipesGui.logic.previousPage();
if (!input.isSimulate()) {
recipesGui.logic.previousPage();
}
return Optional.of(this);
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -216,7 +216,6 @@ public RecipeLayout<T> buildRecipeLayout(
);

for (Map.Entry<mezz.jei.api.gui.widgets.ISlottedWidgetFactory<?>, List<Pair<Integer, IRecipeSlotDrawable>>> e : widgetSlots.entrySet()) {
// TODO: breaking change: add a type parameter to IRecipeLayoutBuilder to avoid this cast
@SuppressWarnings("unchecked")
mezz.jei.api.gui.widgets.ISlottedWidgetFactory<T> factory = (mezz.jei.api.gui.widgets.ISlottedWidgetFactory<T>) e.getKey();
List<IRecipeSlotDrawable> slots = sortSlots(e.getValue());
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,8 @@
import mezz.jei.common.util.ErrorUtil;
import net.minecraft.core.Holder;
import net.minecraft.core.component.DataComponentPatch;
import net.minecraft.world.item.ItemStack;
import net.minecraft.world.item.crafting.Ingredient;
import net.minecraft.world.level.material.Fluid;
import org.jetbrains.annotations.Nullable;
import org.jetbrains.annotations.UnmodifiableView;
Expand All @@ -26,7 +28,6 @@
import java.util.Collections;
import java.util.List;
import java.util.Optional;
import java.util.function.Function;

public class DisplayIngredientAcceptor implements IIngredientAcceptor<DisplayIngredientAcceptor> {
private final IIngredientManager ingredientManager;
Expand Down Expand Up @@ -57,13 +58,22 @@ public <T> DisplayIngredientAcceptor addIngredients(IIngredientType<T> ingredien
ErrorUtil.checkNotNull(ingredientType, "ingredientType");
Preconditions.checkNotNull(ingredients, "ingredients");

List<Optional<ITypedIngredient<T>>> typedIngredients = TypedIngredient.createAndFilterInvalidList(this.ingredientManager, ingredientType, ingredients, false);
List<Optional<ITypedIngredient<T>>> typedIngredients = TypedIngredient.createUnvalidatedList(ingredientType, ingredients);
@SuppressWarnings("unchecked")
List<Optional<ITypedIngredient<?>>> castTypedIngredients = (List<Optional<ITypedIngredient<?>>>) (Object) typedIngredients;
this.ingredients.addAll(castTypedIngredients);

if (!typedIngredients.isEmpty()) {
for (Optional<ITypedIngredient<T>> typedIngredientOptional : typedIngredients) {
this.ingredients.add(typedIngredientOptional.map(Function.identity()));
}
}
return this;
}

@Override
public DisplayIngredientAcceptor addIngredients(Ingredient ingredient) {
Preconditions.checkNotNull(ingredient, "ingredient");

List<Optional<ITypedIngredient<ItemStack>>> typedIngredients = TypedIngredient.createUnvalidatedList(ingredient);
@SuppressWarnings("unchecked")
List<Optional<ITypedIngredient<?>>> castTypedIngredients = (List<Optional<ITypedIngredient<?>>>) (Object) typedIngredients;
this.ingredients.addAll(castTypedIngredients);

return this;
}
Expand All @@ -81,8 +91,7 @@ public <T> DisplayIngredientAcceptor addIngredient(IIngredientType<T> ingredient
public <I> DisplayIngredientAcceptor addTypedIngredient(ITypedIngredient<I> typedIngredient) {
ErrorUtil.checkNotNull(typedIngredient, "typedIngredient");

Optional<ITypedIngredient<I>> copy = TypedIngredient.deepCopy(ingredientManager, typedIngredient);
this.ingredients.add(copy.map(Function.identity()));
this.ingredients.add(Optional.of(typedIngredient));

return this;
}
Expand Down Expand Up @@ -141,8 +150,12 @@ public DisplayIngredientAcceptor addOptionalTypedIngredients(List<Optional<IType
}

private <T> void addIngredientInternal(IIngredientType<T> ingredientType, @Nullable T ingredient) {
Optional<ITypedIngredient<T>> typedIngredient = TypedIngredient.createAndFilterInvalid(this.ingredientManager, ingredientType, ingredient, false);
this.ingredients.add(typedIngredient.map(Function.identity()));
if (ingredient == null) {
this.ingredients.add(Optional.empty());
} else {
ITypedIngredient<T> typedIngredient = TypedIngredient.createUnvalidated(ingredientType, ingredient);
this.ingredients.add(Optional.of(typedIngredient));
}
}

@UnmodifiableView
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -9,17 +9,14 @@
import mezz.jei.api.runtime.IIngredientManager;
import mezz.jei.library.ingredients.itemStacks.TypedItemStack;
import net.minecraft.world.item.ItemStack;
import org.apache.logging.log4j.LogManager;
import org.apache.logging.log4j.Logger;
import net.minecraft.world.item.crafting.Ingredient;
import org.jetbrains.annotations.Nullable;

import java.util.ArrayList;
import java.util.List;
import java.util.Optional;

public final class TypedIngredient<T> implements ITypedIngredient<T> {
private static final Logger LOGGER = LogManager.getLogger();

private static <T> void checkParameters(IIngredientType<T> ingredientType, T ingredient) {
Preconditions.checkNotNull(ingredientType, "ingredientType");
Preconditions.checkNotNull(ingredient, "ingredient");
Expand Down Expand Up @@ -104,6 +101,33 @@ public static <T> List<Optional<ITypedIngredient<T>>> createAndFilterInvalidList
return results;
}

public static <T> List<Optional<ITypedIngredient<T>>> createUnvalidatedList(
IIngredientType<T> ingredientType,
List<@Nullable T> ingredients
) {
List<Optional<ITypedIngredient<T>>> results = new ArrayList<>(ingredients.size());
for (T ingredient : ingredients) {
if (ingredient == null) {
results.add(Optional.empty());
} else {
ITypedIngredient<T> result = createUnvalidated(ingredientType, ingredient);
results.add(Optional.of(result));
}
}
return results;
}

public static List<Optional<ITypedIngredient<ItemStack>>> createUnvalidatedList(Ingredient ingredient) {
ItemStack[] itemStacks = ingredient.getItems();

List<Optional<ITypedIngredient<ItemStack>>> results = new ArrayList<>(itemStacks.length);
for (ItemStack itemStack : itemStacks) {
ITypedIngredient<ItemStack> result = TypedItemStack.create(itemStack);
results.add(Optional.of(result));
}
return results;
}

public static <T> Optional<ITypedIngredient<T>> createAndFilterInvalid(
IIngredientHelper<T> ingredientHelper,
IIngredientType<T> ingredientType,
Expand All @@ -117,11 +141,6 @@ public static <T> Optional<ITypedIngredient<T>> createAndFilterInvalid(
if (!ingredientHelper.isValidIngredient(ingredient)) {
return Optional.empty();
}
if (!ingredientHelper.isIngredientOnServer(ingredient)) {
String errorInfo = ingredientHelper.getErrorInfo(ingredient);
LOGGER.warn("Ignoring ingredient that isn't on the server: {}", errorInfo);
return Optional.empty();
}
} catch (RuntimeException e) {
String ingredientInfo = ingredientHelper.getErrorInfo(ingredient);
throw new IllegalArgumentException("Crashed when checking if ingredient is valid. Ingredient Info: " + ingredientInfo, e);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -172,11 +172,11 @@ public void registerItemSubtypes(ISubtypeRegistration registration) {
@Override
public void registerIngredients(IModIngredientRegistration registration) {
ISubtypeManager subtypeManager = registration.getSubtypeManager();
StackHelper stackHelper = new StackHelper(subtypeManager);

List<ItemStack> itemStacks = ItemStackListFactory.create(stackHelper);
IColorHelper colorHelper = registration.getColorHelper();

StackHelper stackHelper = new StackHelper(subtypeManager);
ItemStackHelper itemStackHelper = new ItemStackHelper(stackHelper, colorHelper);
List<ItemStack> itemStacks = ItemStackListFactory.create(stackHelper, itemStackHelper);
ItemStackRenderer itemStackRenderer = new ItemStackRenderer();
registration.register(
VanillaTypes.ITEM_STACK,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,7 @@
public final class ItemStackListFactory {
private static final Logger LOGGER = LogManager.getLogger();

public static List<ItemStack> create(StackHelper stackHelper) {
public static List<ItemStack> create(StackHelper stackHelper, ItemStackHelper itemStackHelper) {
IJeiClientConfigs jeiClientConfigs = Internal.getJeiClientConfigs();
IClientConfig clientConfig = jeiClientConfigs.getClientConfig();
final boolean showHidden = clientConfig.isShowHiddenItemsEnabled();
Expand Down Expand Up @@ -117,6 +117,7 @@ public static List<ItemStack> create(StackHelper stackHelper) {
"displayItems",
tab,
stackHelper,
itemStackHelper,
itemList,
itemUidSet
);
Expand All @@ -126,6 +127,7 @@ public static List<ItemStack> create(StackHelper stackHelper) {
"searchTabDisplayItems",
tab,
stackHelper,
itemStackHelper,
itemList,
itemUidSet
);
Expand All @@ -144,6 +146,7 @@ private static void addFromTab(
String displayType,
CreativeModeTab tab,
StackHelper stackHelper,
ItemStackHelper itemStackHelper,
List<ItemStack> itemList,
Set<Object> itemUidSet
) {
Expand All @@ -153,20 +156,33 @@ private static void addFromTab(
int duplicateInTabCount = 0;
for (ItemStack itemStack : tabDisplayItems) {
if (itemStack.isEmpty()) {
LOGGER.error("Found an empty itemStack in '{}' creative tab's {}", tab, displayType);
} else {
Object itemKey = safeGetUid(stackHelper, itemStack);
if (itemKey != null) {
if (tabUidSet.contains(itemKey)) {
duplicateInTab.add(itemKey);
duplicateInTabCount++;
}
if (itemUidSet.add(itemKey)) {
tabUidSet.add(itemKey);
itemList.add(itemStack);
added++;
}
}
String errorInfo = itemStackHelper.getErrorInfo(itemStack);
LOGGER.error("Found an empty itemStack in '{}' creative tab's {}: {}", tab, displayType, errorInfo);
continue;
}
if (!itemStackHelper.isValidIngredient(itemStack)) {
String errorInfo = itemStackHelper.getErrorInfo(itemStack);
LOGGER.error("Ignoring ingredient in '{}' creative tab's {} that is considered invalid: {}", tab, displayType, errorInfo);
continue;
}
if (!itemStackHelper.isIngredientOnServer(itemStack)) {
String errorInfo = itemStackHelper.getErrorInfo(itemStack);
LOGGER.warn("Ignoring ingredient in '{}' creative tab's {} that isn't on the server: {}", tab, displayType, errorInfo);
continue;
}
Object itemKey = safeGetUid(stackHelper, itemStack);
if (itemKey == null) {
continue;
}

if (tabUidSet.contains(itemKey)) {
duplicateInTab.add(itemKey);
duplicateInTabCount++;
}
if (itemUidSet.add(itemKey)) {
tabUidSet.add(itemKey);
itemList.add(itemStack);
added++;
}
}
if (LOGGER.isDebugEnabled()) {
Expand Down

0 comments on commit 4d89747

Please sign in to comment.