Skip to content

Commit 4d89747

Browse files
committed
Optimize displaying recipes by using less-strict checking for rendered ingredients
1 parent b033bbe commit 4d89747

File tree

6 files changed

+108
-48
lines changed

6 files changed

+108
-48
lines changed

Gui/src/main/java/mezz/jei/gui/recipes/RecipesGui.java

Lines changed: 22 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -613,30 +613,43 @@ public Optional<IUserInputHandler> handleUserInput(Screen screen, UserInput inpu
613613
double mouseY = input.getMouseY();
614614
if (recipesGui.isMouseOver(mouseX, mouseY)) {
615615
if (recipesGui.recipeCategoryTitle.isMouseOver(mouseX, mouseY)) {
616-
if (input.is(keyBindings.getLeftClick()) && recipesGui.logic.showAllRecipes()) {
617-
return Optional.of(this);
618-
}
616+
if (input.is(keyBindings.getLeftClick()))
617+
if (input.isSimulate() || recipesGui.logic.showAllRecipes()) {
618+
return Optional.of(this);
619+
}
619620
}
620621
}
621622

622623
Minecraft minecraft = Minecraft.getInstance();
623624
if (input.is(keyBindings.getCloseRecipeGui()) || input.is(minecraft.options.keyInventory)) {
624-
recipesGui.onClose();
625+
if (!input.isSimulate()) {
626+
recipesGui.onClose();
627+
}
625628
return Optional.of(this);
626629
} else if (input.is(keyBindings.getRecipeBack())) {
627-
recipesGui.back();
630+
if (!input.isSimulate()) {
631+
recipesGui.back();
632+
}
628633
return Optional.of(this);
629634
} else if (input.is(keyBindings.getNextCategory())) {
630-
recipesGui.logic.nextRecipeCategory();
635+
if (!input.isSimulate()) {
636+
recipesGui.logic.nextRecipeCategory();
637+
}
631638
return Optional.of(this);
632639
} else if (input.is(keyBindings.getPreviousCategory())) {
633-
recipesGui.logic.previousRecipeCategory();
640+
if (!input.isSimulate()) {
641+
recipesGui.logic.previousRecipeCategory();
642+
}
634643
return Optional.of(this);
635644
} else if (input.is(keyBindings.getNextRecipePage())) {
636-
recipesGui.logic.nextPage();
645+
if (!input.isSimulate()) {
646+
recipesGui.logic.nextPage();
647+
}
637648
return Optional.of(this);
638649
} else if (input.is(keyBindings.getPreviousRecipePage())) {
639-
recipesGui.logic.previousPage();
650+
if (!input.isSimulate()) {
651+
recipesGui.logic.previousPage();
652+
}
640653
return Optional.of(this);
641654
}
642655

Library/src/main/java/mezz/jei/library/gui/recipes/layout/builder/RecipeLayoutBuilder.java

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -216,7 +216,6 @@ public RecipeLayout<T> buildRecipeLayout(
216216
);
217217

218218
for (Map.Entry<mezz.jei.api.gui.widgets.ISlottedWidgetFactory<?>, List<Pair<Integer, IRecipeSlotDrawable>>> e : widgetSlots.entrySet()) {
219-
// TODO: breaking change: add a type parameter to IRecipeLayoutBuilder to avoid this cast
220219
@SuppressWarnings("unchecked")
221220
mezz.jei.api.gui.widgets.ISlottedWidgetFactory<T> factory = (mezz.jei.api.gui.widgets.ISlottedWidgetFactory<T>) e.getKey();
222221
List<IRecipeSlotDrawable> slots = sortSlots(e.getValue());

Library/src/main/java/mezz/jei/library/ingredients/DisplayIngredientAcceptor.java

Lines changed: 24 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,8 @@
1818
import mezz.jei.common.util.ErrorUtil;
1919
import net.minecraft.core.Holder;
2020
import net.minecraft.core.component.DataComponentPatch;
21+
import net.minecraft.world.item.ItemStack;
22+
import net.minecraft.world.item.crafting.Ingredient;
2123
import net.minecraft.world.level.material.Fluid;
2224
import org.jetbrains.annotations.Nullable;
2325
import org.jetbrains.annotations.UnmodifiableView;
@@ -26,7 +28,6 @@
2628
import java.util.Collections;
2729
import java.util.List;
2830
import java.util.Optional;
29-
import java.util.function.Function;
3031

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

60-
List<Optional<ITypedIngredient<T>>> typedIngredients = TypedIngredient.createAndFilterInvalidList(this.ingredientManager, ingredientType, ingredients, false);
61+
List<Optional<ITypedIngredient<T>>> typedIngredients = TypedIngredient.createUnvalidatedList(ingredientType, ingredients);
62+
@SuppressWarnings("unchecked")
63+
List<Optional<ITypedIngredient<?>>> castTypedIngredients = (List<Optional<ITypedIngredient<?>>>) (Object) typedIngredients;
64+
this.ingredients.addAll(castTypedIngredients);
6165

62-
if (!typedIngredients.isEmpty()) {
63-
for (Optional<ITypedIngredient<T>> typedIngredientOptional : typedIngredients) {
64-
this.ingredients.add(typedIngredientOptional.map(Function.identity()));
65-
}
66-
}
66+
return this;
67+
}
68+
69+
@Override
70+
public DisplayIngredientAcceptor addIngredients(Ingredient ingredient) {
71+
Preconditions.checkNotNull(ingredient, "ingredient");
72+
73+
List<Optional<ITypedIngredient<ItemStack>>> typedIngredients = TypedIngredient.createUnvalidatedList(ingredient);
74+
@SuppressWarnings("unchecked")
75+
List<Optional<ITypedIngredient<?>>> castTypedIngredients = (List<Optional<ITypedIngredient<?>>>) (Object) typedIngredients;
76+
this.ingredients.addAll(castTypedIngredients);
6777

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

84-
Optional<ITypedIngredient<I>> copy = TypedIngredient.deepCopy(ingredientManager, typedIngredient);
85-
this.ingredients.add(copy.map(Function.identity()));
94+
this.ingredients.add(Optional.of(typedIngredient));
8695

8796
return this;
8897
}
@@ -141,8 +150,12 @@ public DisplayIngredientAcceptor addOptionalTypedIngredients(List<Optional<IType
141150
}
142151

143152
private <T> void addIngredientInternal(IIngredientType<T> ingredientType, @Nullable T ingredient) {
144-
Optional<ITypedIngredient<T>> typedIngredient = TypedIngredient.createAndFilterInvalid(this.ingredientManager, ingredientType, ingredient, false);
145-
this.ingredients.add(typedIngredient.map(Function.identity()));
153+
if (ingredient == null) {
154+
this.ingredients.add(Optional.empty());
155+
} else {
156+
ITypedIngredient<T> typedIngredient = TypedIngredient.createUnvalidated(ingredientType, ingredient);
157+
this.ingredients.add(Optional.of(typedIngredient));
158+
}
146159
}
147160

148161
@UnmodifiableView

Library/src/main/java/mezz/jei/library/ingredients/TypedIngredient.java

Lines changed: 28 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -9,17 +9,14 @@
99
import mezz.jei.api.runtime.IIngredientManager;
1010
import mezz.jei.library.ingredients.itemStacks.TypedItemStack;
1111
import net.minecraft.world.item.ItemStack;
12-
import org.apache.logging.log4j.LogManager;
13-
import org.apache.logging.log4j.Logger;
12+
import net.minecraft.world.item.crafting.Ingredient;
1413
import org.jetbrains.annotations.Nullable;
1514

1615
import java.util.ArrayList;
1716
import java.util.List;
1817
import java.util.Optional;
1918

2019
public final class TypedIngredient<T> implements ITypedIngredient<T> {
21-
private static final Logger LOGGER = LogManager.getLogger();
22-
2320
private static <T> void checkParameters(IIngredientType<T> ingredientType, T ingredient) {
2421
Preconditions.checkNotNull(ingredientType, "ingredientType");
2522
Preconditions.checkNotNull(ingredient, "ingredient");
@@ -104,6 +101,33 @@ public static <T> List<Optional<ITypedIngredient<T>>> createAndFilterInvalidList
104101
return results;
105102
}
106103

104+
public static <T> List<Optional<ITypedIngredient<T>>> createUnvalidatedList(
105+
IIngredientType<T> ingredientType,
106+
List<@Nullable T> ingredients
107+
) {
108+
List<Optional<ITypedIngredient<T>>> results = new ArrayList<>(ingredients.size());
109+
for (T ingredient : ingredients) {
110+
if (ingredient == null) {
111+
results.add(Optional.empty());
112+
} else {
113+
ITypedIngredient<T> result = createUnvalidated(ingredientType, ingredient);
114+
results.add(Optional.of(result));
115+
}
116+
}
117+
return results;
118+
}
119+
120+
public static List<Optional<ITypedIngredient<ItemStack>>> createUnvalidatedList(Ingredient ingredient) {
121+
ItemStack[] itemStacks = ingredient.getItems();
122+
123+
List<Optional<ITypedIngredient<ItemStack>>> results = new ArrayList<>(itemStacks.length);
124+
for (ItemStack itemStack : itemStacks) {
125+
ITypedIngredient<ItemStack> result = TypedItemStack.create(itemStack);
126+
results.add(Optional.of(result));
127+
}
128+
return results;
129+
}
130+
107131
public static <T> Optional<ITypedIngredient<T>> createAndFilterInvalid(
108132
IIngredientHelper<T> ingredientHelper,
109133
IIngredientType<T> ingredientType,
@@ -117,11 +141,6 @@ public static <T> Optional<ITypedIngredient<T>> createAndFilterInvalid(
117141
if (!ingredientHelper.isValidIngredient(ingredient)) {
118142
return Optional.empty();
119143
}
120-
if (!ingredientHelper.isIngredientOnServer(ingredient)) {
121-
String errorInfo = ingredientHelper.getErrorInfo(ingredient);
122-
LOGGER.warn("Ignoring ingredient that isn't on the server: {}", errorInfo);
123-
return Optional.empty();
124-
}
125144
} catch (RuntimeException e) {
126145
String ingredientInfo = ingredientHelper.getErrorInfo(ingredient);
127146
throw new IllegalArgumentException("Crashed when checking if ingredient is valid. Ingredient Info: " + ingredientInfo, e);

Library/src/main/java/mezz/jei/library/plugins/vanilla/VanillaPlugin.java

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -172,11 +172,11 @@ public void registerItemSubtypes(ISubtypeRegistration registration) {
172172
@Override
173173
public void registerIngredients(IModIngredientRegistration registration) {
174174
ISubtypeManager subtypeManager = registration.getSubtypeManager();
175-
StackHelper stackHelper = new StackHelper(subtypeManager);
176-
177-
List<ItemStack> itemStacks = ItemStackListFactory.create(stackHelper);
178175
IColorHelper colorHelper = registration.getColorHelper();
176+
177+
StackHelper stackHelper = new StackHelper(subtypeManager);
179178
ItemStackHelper itemStackHelper = new ItemStackHelper(stackHelper, colorHelper);
179+
List<ItemStack> itemStacks = ItemStackListFactory.create(stackHelper, itemStackHelper);
180180
ItemStackRenderer itemStackRenderer = new ItemStackRenderer();
181181
registration.register(
182182
VanillaTypes.ITEM_STACK,

Library/src/main/java/mezz/jei/library/plugins/vanilla/ingredients/ItemStackListFactory.java

Lines changed: 31 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -37,7 +37,7 @@
3737
public final class ItemStackListFactory {
3838
private static final Logger LOGGER = LogManager.getLogger();
3939

40-
public static List<ItemStack> create(StackHelper stackHelper) {
40+
public static List<ItemStack> create(StackHelper stackHelper, ItemStackHelper itemStackHelper) {
4141
IJeiClientConfigs jeiClientConfigs = Internal.getJeiClientConfigs();
4242
IClientConfig clientConfig = jeiClientConfigs.getClientConfig();
4343
final boolean showHidden = clientConfig.isShowHiddenItemsEnabled();
@@ -117,6 +117,7 @@ public static List<ItemStack> create(StackHelper stackHelper) {
117117
"displayItems",
118118
tab,
119119
stackHelper,
120+
itemStackHelper,
120121
itemList,
121122
itemUidSet
122123
);
@@ -126,6 +127,7 @@ public static List<ItemStack> create(StackHelper stackHelper) {
126127
"searchTabDisplayItems",
127128
tab,
128129
stackHelper,
130+
itemStackHelper,
129131
itemList,
130132
itemUidSet
131133
);
@@ -144,6 +146,7 @@ private static void addFromTab(
144146
String displayType,
145147
CreativeModeTab tab,
146148
StackHelper stackHelper,
149+
ItemStackHelper itemStackHelper,
147150
List<ItemStack> itemList,
148151
Set<Object> itemUidSet
149152
) {
@@ -153,20 +156,33 @@ private static void addFromTab(
153156
int duplicateInTabCount = 0;
154157
for (ItemStack itemStack : tabDisplayItems) {
155158
if (itemStack.isEmpty()) {
156-
LOGGER.error("Found an empty itemStack in '{}' creative tab's {}", tab, displayType);
157-
} else {
158-
Object itemKey = safeGetUid(stackHelper, itemStack);
159-
if (itemKey != null) {
160-
if (tabUidSet.contains(itemKey)) {
161-
duplicateInTab.add(itemKey);
162-
duplicateInTabCount++;
163-
}
164-
if (itemUidSet.add(itemKey)) {
165-
tabUidSet.add(itemKey);
166-
itemList.add(itemStack);
167-
added++;
168-
}
169-
}
159+
String errorInfo = itemStackHelper.getErrorInfo(itemStack);
160+
LOGGER.error("Found an empty itemStack in '{}' creative tab's {}: {}", tab, displayType, errorInfo);
161+
continue;
162+
}
163+
if (!itemStackHelper.isValidIngredient(itemStack)) {
164+
String errorInfo = itemStackHelper.getErrorInfo(itemStack);
165+
LOGGER.error("Ignoring ingredient in '{}' creative tab's {} that is considered invalid: {}", tab, displayType, errorInfo);
166+
continue;
167+
}
168+
if (!itemStackHelper.isIngredientOnServer(itemStack)) {
169+
String errorInfo = itemStackHelper.getErrorInfo(itemStack);
170+
LOGGER.warn("Ignoring ingredient in '{}' creative tab's {} that isn't on the server: {}", tab, displayType, errorInfo);
171+
continue;
172+
}
173+
Object itemKey = safeGetUid(stackHelper, itemStack);
174+
if (itemKey == null) {
175+
continue;
176+
}
177+
178+
if (tabUidSet.contains(itemKey)) {
179+
duplicateInTab.add(itemKey);
180+
duplicateInTabCount++;
181+
}
182+
if (itemUidSet.add(itemKey)) {
183+
tabUidSet.add(itemKey);
184+
itemList.add(itemStack);
185+
added++;
170186
}
171187
}
172188
if (LOGGER.isDebugEnabled()) {

0 commit comments

Comments
 (0)