Skip to content

Commit 6712a3c

Browse files
committed
Optimize getting subtypes for typed ItemStacks
1 parent 93ddf24 commit 6712a3c

File tree

14 files changed

+129
-12
lines changed

14 files changed

+129
-12
lines changed

Common/src/main/java/mezz/jei/common/util/StackHelper.java

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@
22

33
import mezz.jei.api.constants.VanillaTypes;
44
import mezz.jei.api.helpers.IStackHelper;
5+
import mezz.jei.api.ingredients.ITypedIngredient;
56
import mezz.jei.api.ingredients.subtypes.ISubtypeManager;
67
import mezz.jei.api.ingredients.subtypes.UidContext;
78
import net.minecraft.core.registries.Registries;
@@ -50,6 +51,16 @@ public Object getUidForStack(ItemStack stack, UidContext context) {
5051
return item;
5152
}
5253

54+
@Override
55+
public Object getUidForStack(ITypedIngredient<ItemStack> typedIngredient, UidContext context) {
56+
Item item = typedIngredient.getBaseIngredient(VanillaTypes.ITEM_STACK);
57+
Object subtypeData = subtypeManager.getSubtypeData(VanillaTypes.ITEM_STACK, typedIngredient, context);
58+
if (subtypeData != null) {
59+
return List.of(item, subtypeData);
60+
}
61+
return item;
62+
}
63+
5364
@SuppressWarnings("removal")
5465
@Override
5566
public String getUniqueIdentifierForStack(ItemStack stack, UidContext context) {

CommonApi/src/main/java/mezz/jei/api/helpers/IStackHelper.java

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
package mezz.jei.api.helpers;
22

3+
import mezz.jei.api.ingredients.ITypedIngredient;
34
import org.jetbrains.annotations.Nullable;
45

56
import mezz.jei.api.ingredients.subtypes.UidContext;
@@ -20,6 +21,14 @@ public interface IStackHelper {
2021
*/
2122
Object getUidForStack(ItemStack stack, UidContext context);
2223

24+
/**
25+
* Gets the unique identifier for a stack, ignoring NBT on items without subtypes, and uses the {@link ISubtypeManager}.
26+
* If two unique identifiers are equal, then the items can be considered equivalent.
27+
*
28+
* @since 19.19.4
29+
*/
30+
Object getUidForStack(ITypedIngredient<ItemStack> stack, UidContext context);
31+
2332
/**
2433
* Similar to ItemStack.areItemStacksEqual but ignores NBT on items without subtypes, and uses the {@link ISubtypeManager}
2534
* @since 7.3.0

CommonApi/src/main/java/mezz/jei/api/ingredients/IIngredientHelper.java

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -55,6 +55,21 @@ default Object getUid(V ingredient, UidContext context) {
5555
return getUniqueId(ingredient, context);
5656
}
5757

58+
59+
/**
60+
* Unique ID for use in comparing and looking up ingredients.
61+
*
62+
* Returns an {@link Object} so that UID creation can be optimized.
63+
* Make sure the returned value implements {@link Object#equals} and {@link Object#hashCode}.
64+
*
65+
* Replaces {@link #getUniqueId(Object, UidContext)}.
66+
*
67+
* @since 19.19.4
68+
*/
69+
default Object getUid(ITypedIngredient<V> typedIngredient, UidContext context) {
70+
return getUniqueId(typedIngredient.getIngredient(), context);
71+
}
72+
5873
/**
5974
* Unique ID for use in grouping ingredients together.
6075
* This is used for hiding groups of ingredients together at once.

CommonApi/src/main/java/mezz/jei/api/ingredients/ITypedIngredient.java

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -42,6 +42,17 @@ default <V> Optional<V> getIngredient(IIngredientType<V> ingredientType) {
4242
return ingredientType.castIngredient(getIngredient());
4343
}
4444

45+
/**
46+
* @return the ingredient's base ingredient. (For example, an ItemStack's base ingredient is the Item)
47+
*
48+
* @see IIngredientTypeWithSubtypes#getBase
49+
*
50+
* @since 19.19.4
51+
*/
52+
default <B> B getBaseIngredient(IIngredientTypeWithSubtypes<B, T> ingredientType) {
53+
return ingredientType.getBase(getIngredient());
54+
}
55+
4556
/**
4657
* @return the ItemStack wrapped by this instance, only this holds an ItemStack ingredient.
4758
* This is useful when handling a wildcard generic instance of `ITypedIngredient<?>`.

CommonApi/src/main/java/mezz/jei/api/ingredients/subtypes/ISubtypeManager.java

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@
22

33
import mezz.jei.api.constants.VanillaTypes;
44
import mezz.jei.api.ingredients.IIngredientTypeWithSubtypes;
5+
import mezz.jei.api.ingredients.ITypedIngredient;
56
import mezz.jei.api.registration.ISubtypeRegistration;
67
import net.minecraft.world.item.ItemStack;
78
import org.jetbrains.annotations.Nullable;
@@ -32,6 +33,15 @@ default Object getSubtypeData(ItemStack ingredient, UidContext context) {
3233
@Nullable
3334
<T> Object getSubtypeData(IIngredientTypeWithSubtypes<?, T> ingredientType, T ingredient, UidContext context);
3435

36+
/**
37+
* Get the data from a typed ingredient that is relevant to comparing and telling subtypes apart.
38+
* Returns null if the typed ingredient has no information used for subtypes.
39+
*
40+
* @since 19.19.4
41+
*/
42+
@Nullable
43+
<B, T> Object getSubtypeData(IIngredientTypeWithSubtypes<B, T> ingredientType, ITypedIngredient<T> typedIngredient, UidContext context);
44+
3545
/**
3646
* Get the data from an ItemStack that is relevant to comparing and telling subtypes apart.
3747
* Returns {@link IIngredientSubtypeInterpreter#NONE} if the ItemStack has no information used for subtypes.

Library/src/main/java/mezz/jei/library/ingredients/itemStacks/FullTypedItemStack.java

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -30,6 +30,11 @@ protected TypedItemStack getNormalized() {
3030
return NormalizedTypedItemStack.create(itemHolder, dataComponentPatch);
3131
}
3232

33+
@Override
34+
protected Item getItem() {
35+
return itemHolder.value();
36+
}
37+
3338
@Override
3439
public String toString() {
3540
return "TypedItemStack{" +

Library/src/main/java/mezz/jei/library/ingredients/itemStacks/NormalizedTypedItem.java

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,11 @@ public TypedItemStack getNormalized() {
2121
return this;
2222
}
2323

24+
@Override
25+
protected Item getItem() {
26+
return itemHolder.value();
27+
}
28+
2429
@Override
2530
public String toString() {
2631
return "SimpleItemStack{" +

Library/src/main/java/mezz/jei/library/ingredients/itemStacks/NormalizedTypedItemStack.java

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -34,6 +34,11 @@ public TypedItemStack getNormalized() {
3434
return this;
3535
}
3636

37+
@Override
38+
protected Item getItem() {
39+
return itemHolder.value();
40+
}
41+
3742
@Override
3843
public String toString() {
3944
return "NormalizedTypedItemStack{" +

Library/src/main/java/mezz/jei/library/ingredients/itemStacks/TypedItemStack.java

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,9 @@
55
import com.google.common.cache.LoadingCache;
66
import mezz.jei.api.constants.VanillaTypes;
77
import mezz.jei.api.ingredients.IIngredientType;
8+
import mezz.jei.api.ingredients.IIngredientTypeWithSubtypes;
89
import mezz.jei.api.ingredients.ITypedIngredient;
10+
import net.minecraft.world.item.Item;
911
import net.minecraft.world.item.ItemStack;
1012

1113
import java.time.Duration;
@@ -53,11 +55,20 @@ public final Optional<ItemStack> getItemStack() {
5355
return Optional.of(getIngredient());
5456
}
5557

58+
@Override
59+
public final <B> B getBaseIngredient(IIngredientTypeWithSubtypes<B, ItemStack> ingredientType) {
60+
Item item = getItem();
61+
Class<? extends B> ingredientBaseClass = ingredientType.getIngredientBaseClass();
62+
return ingredientBaseClass.cast(item);
63+
}
64+
5665
@Override
5766
public final IIngredientType<ItemStack> getType() {
5867
return VanillaTypes.ITEM_STACK;
5968
}
6069

70+
protected abstract Item getItem();
71+
6172
protected abstract TypedItemStack getNormalized();
6273

6374
protected abstract ItemStack createItemStackUncached();

Library/src/main/java/mezz/jei/library/ingredients/subtypes/SubtypeInterpreters.java

Lines changed: 10 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -2,10 +2,10 @@
22

33
import mezz.jei.api.ingredients.IIngredientTypeWithSubtypes;
44
import mezz.jei.api.ingredients.subtypes.ISubtypeInterpreter;
5+
import org.jetbrains.annotations.Nullable;
56

67
import java.util.IdentityHashMap;
78
import java.util.Map;
8-
import java.util.Optional;
99

1010
public class SubtypeInterpreters {
1111
private final Map<Object, ISubtypeInterpreter<?>> map;
@@ -41,12 +41,18 @@ public <B, I> boolean addInterpreter(IIngredientTypeWithSubtypes<B, I> type, B b
4141
return true;
4242
}
4343

44-
public <B, I> Optional<ISubtypeInterpreter<I>> get(IIngredientTypeWithSubtypes<B, I> type, I ingredient) {
44+
@Nullable
45+
public <B, I> ISubtypeInterpreter<I> get(IIngredientTypeWithSubtypes<B, I> type, I ingredient) {
4546
B base = type.getBase(ingredient);
46-
ISubtypeInterpreter<?> interpreter = map.get(base);
47+
return getFromBase(type, base);
48+
}
49+
50+
@Nullable
51+
public <B, I> ISubtypeInterpreter<I> getFromBase(@SuppressWarnings("unused") IIngredientTypeWithSubtypes<B, I> type, B ingredientBase) {
52+
ISubtypeInterpreter<?> interpreter = map.get(ingredientBase);
4753
@SuppressWarnings("unchecked")
4854
ISubtypeInterpreter<I> cast = (ISubtypeInterpreter<I>) interpreter;
49-
return Optional.ofNullable(cast);
55+
return cast;
5056
}
5157

5258
public <B, T> boolean contains(IIngredientTypeWithSubtypes<B, T> type, T ingredient) {

0 commit comments

Comments
 (0)