Skip to content

Commit 703632d

Browse files
committed
Make better use of ItemLike and Item#getDefaultInstance
1 parent 35f90d5 commit 703632d

24 files changed

+133
-21
lines changed

CommonApi/src/main/java/mezz/jei/api/constants/VanillaTypes.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -35,7 +35,7 @@ public Item getBase(ItemStack ingredient) {
3535

3636
@Override
3737
public ItemStack getDefaultIngredient(Item base) {
38-
return new ItemStack(base);
38+
return base.getDefaultInstance();
3939
}
4040
};
4141

CommonApi/src/main/java/mezz/jei/api/gui/builder/IIngredientAcceptor.java

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@
66
import net.minecraft.nbt.CompoundTag;
77
import net.minecraft.world.item.ItemStack;
88
import net.minecraft.world.item.crafting.Ingredient;
9+
import net.minecraft.world.level.ItemLike;
910
import net.minecraft.world.level.material.Fluid;
1011
import org.jetbrains.annotations.ApiStatus;
1112
import org.jetbrains.annotations.Nullable;
@@ -102,6 +103,26 @@ default THIS addItemStack(ItemStack itemStack) {
102103
return addIngredient(VanillaTypes.ITEM_STACK, itemStack);
103104
}
104105

106+
/**
107+
* Convenience function to add one {@link ItemLike}.
108+
*
109+
* @since 15.19.4
110+
*/
111+
default IIngredientConsumer addItemLike(ItemLike itemLike) {
112+
return addItemStack(itemLike.asItem().getDefaultInstance());
113+
}
114+
115+
/**
116+
* Convenience helper to add one Fluid ingredient with the default amount (one bucket).
117+
*
118+
* To add multiple Fluid ingredients, you can call this multiple times.
119+
*
120+
* @see #addFluidStack(Fluid, long) to add a Fluid with an amount.
121+
* @see #addFluidStack(Fluid, long, CompoundTag) to add a Fluid with a {@link CompoundTag}.
122+
* @since 15.19.4
123+
*/
124+
THIS addFluidStack(Fluid fluid);
125+
105126
/**
106127
* Convenience helper to add one Fluid ingredient.
107128
*

CommonApi/src/main/java/mezz/jei/api/gui/builder/IIngredientConsumer.java

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@
66
import net.minecraft.nbt.CompoundTag;
77
import net.minecraft.world.item.ItemStack;
88
import net.minecraft.world.item.crafting.Ingredient;
9+
import net.minecraft.world.level.ItemLike;
910
import net.minecraft.world.level.material.Fluid;
1011
import org.jetbrains.annotations.ApiStatus;
1112
import org.jetbrains.annotations.Nullable;
@@ -104,6 +105,26 @@ default IIngredientConsumer addItemStack(ItemStack itemStack) {
104105
return addIngredient(VanillaTypes.ITEM_STACK, itemStack);
105106
}
106107

108+
/**
109+
* Convenience function to add one {@link ItemLike}.
110+
*
111+
* @since 15.19.4
112+
*/
113+
default IIngredientConsumer addItemLike(ItemLike itemLike) {
114+
return addItemStack(itemLike.asItem().getDefaultInstance());
115+
}
116+
117+
/**
118+
* Convenience helper to add one Fluid ingredient with the default amount (one bucket).
119+
*
120+
* To add multiple Fluid ingredients, you can call this multiple times.
121+
*
122+
* @see #addFluidStack(Fluid, long) to add a Fluid with an amount.
123+
* @see #addFluidStack(Fluid, long, CompoundTag) to add a Fluid with a {@link CompoundTag}.
124+
* @since 15.19.4
125+
*/
126+
IIngredientConsumer addFluidStack(Fluid fluid);
127+
107128
/**
108129
* Convenience helper to add one Fluid ingredient.
109130
*

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

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,7 @@
1616
import mezz.jei.api.recipe.RecipeIngredientRole;
1717
import net.minecraft.resources.ResourceLocation;
1818
import net.minecraft.world.item.ItemStack;
19+
import net.minecraft.world.level.ItemLike;
1920

2021
/**
2122
* Helps with the implementation of GUIs.
@@ -71,6 +72,17 @@ default IDrawable createDrawableItemStack(ItemStack ingredient) {
7172
return createDrawableIngredient(VanillaTypes.ITEM_STACK, ingredient);
7273
}
7374

75+
/**
76+
* Returns a 16x16 drawable for the given ItemLike,
77+
* matching the one JEI draws in the ingredient list.
78+
*
79+
* @see #createDrawableIngredient(IIngredientType, Object) for other ingredient types.
80+
* @since 15.19.4
81+
*/
82+
default IDrawable createDrawableItemLike(ItemLike itemLike) {
83+
return createDrawableIngredient(VanillaTypes.ITEM_STACK, itemLike.asItem().getDefaultInstance());
84+
}
85+
7486
/**
7587
* Returns a 16x16 drawable for the given ingredient,
7688
* matching the one JEI draws in the ingredient list.

CommonApi/src/main/java/mezz/jei/api/registration/IRecipeCatalystRegistration.java

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@
66
import mezz.jei.api.recipe.RecipeType;
77
import mezz.jei.api.runtime.IIngredientManager;
88
import net.minecraft.world.item.ItemStack;
9+
import net.minecraft.world.level.ItemLike;
910

1011
public interface IRecipeCatalystRegistration {
1112
/**
@@ -20,6 +21,23 @@ public interface IRecipeCatalystRegistration {
2021
*/
2122
IJeiHelpers getJeiHelpers();
2223

24+
/**
25+
* Add an association between an {@link ItemStack} and what it can craft.
26+
* (i.e. Furnace ItemStack can craft Smelting and Fuel Recipes)
27+
* Allows players to see what ingredient they need to craft in order to make recipes from a recipe category.
28+
*
29+
* @param itemLike the {@link ItemLike} that can craft recipes (like a furnace or crafting table)
30+
* @param recipeTypes the types of recipe that the ingredient is a catalyst for
31+
*
32+
* @see #addRecipeCatalyst(ItemStack, RecipeType...) to add {@link ItemStack} catalysts.
33+
* @see #addRecipeCatalyst(IIngredientType, Object, RecipeType...) to add non-{@link ItemLike} catalysts.
34+
*
35+
* @since 15.19.4
36+
*/
37+
default void addRecipeCatalyst(ItemLike itemLike, RecipeType<?>... recipeTypes) {
38+
addRecipeCatalyst(VanillaTypes.ITEM_STACK, itemLike.asItem().getDefaultInstance(), recipeTypes);
39+
}
40+
2341
/**
2442
* Add an association between an {@link ItemStack} and what it can craft.
2543
* (i.e. Furnace ItemStack can craft Smelting and Fuel Recipes)

CommonApi/src/main/java/mezz/jei/api/registration/IRecipeRegistration.java

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@
99
import mezz.jei.api.runtime.IIngredientVisibility;
1010
import net.minecraft.network.chat.Component;
1111
import net.minecraft.world.item.ItemStack;
12+
import net.minecraft.world.level.ItemLike;
1213

1314
import java.util.List;
1415

@@ -75,6 +76,21 @@ default IIngredientVisibility getIngredientVisibility() {
7576
*/
7677
<T> void addIngredientInfo(List<T> ingredients, IIngredientType<T> ingredientType, Component... descriptionComponents);
7778

79+
/**
80+
* Add an info page for an ItemLike.
81+
* Description pages show in the recipes for an ItemStack and tell the player a little about it.
82+
*
83+
* @param itemLike The ItemLike to describe
84+
* @param descriptionComponents Text components for info text.
85+
* New lines can be added with "\n" or by giving multiple descriptions.
86+
* Long lines are wrapped automatically.
87+
* Very long entries will span multiple pages automatically.
88+
* @since 15.19.4
89+
*/
90+
default void addIngredientInfo(ItemLike itemLike, Component... descriptionComponents) {
91+
addIngredientInfo(itemLike.asItem().getDefaultInstance(), VanillaTypes.ITEM_STACK, descriptionComponents);
92+
}
93+
7894
/**
7995
* Add an info page for an ItemStack.
8096
* Description pages show in the recipes for an ItemStack and tell the player a little about it.

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

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -40,6 +40,11 @@ public IngredientAcceptorVoid addOptionalTypedIngredients(List<Optional<ITypedIn
4040
return this;
4141
}
4242

43+
@Override
44+
public IngredientAcceptorVoid addFluidStack(Fluid fluid) {
45+
return this;
46+
}
47+
4348
@Override
4449
public IngredientAcceptorVoid addFluidStack(Fluid fluid, long amount) {
4550
return this;

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

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -64,6 +64,12 @@ public <I> IRecipeSlotBuilder addIngredient(IIngredientType<I> ingredientType, I
6464
return this;
6565
}
6666

67+
@Override
68+
public IRecipeSlotBuilder addFluidStack(Fluid fluid) {
69+
this.ingredients.addFluidStack(fluid);
70+
return this;
71+
}
72+
6773
@Override
6874
public IRecipeSlotBuilder addFluidStack(Fluid fluid, long amount) {
6975
this.ingredients.addFluidStack(fluid, amount);

Library/src/main/java/mezz/jei/library/gui/recipes/supplier/builder/IngredientSlotBuilder.java

Lines changed: 8 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,6 @@
33
import mezz.jei.api.gui.builder.IRecipeSlotBuilder;
44
import mezz.jei.api.gui.drawable.IDrawable;
55
import mezz.jei.api.gui.ingredient.IRecipeSlotRichTooltipCallback;
6-
import mezz.jei.api.gui.ingredient.IRecipeSlotTooltipCallback;
76
import mezz.jei.api.ingredients.IIngredientRenderer;
87
import mezz.jei.api.ingredients.IIngredientType;
98
import mezz.jei.api.ingredients.ITypedIngredient;
@@ -41,6 +40,12 @@ public <I> IRecipeSlotBuilder addIngredient(IIngredientType<I> ingredientType, I
4140
return this;
4241
}
4342

43+
@Override
44+
public IRecipeSlotBuilder addFluidStack(Fluid fluid) {
45+
this.ingredients.addFluidStack(fluid);
46+
return this;
47+
}
48+
4449
@Override
4550
public IRecipeSlotBuilder addFluidStack(Fluid fluid, long amount) {
4651
this.ingredients.addFluidStack(fluid, amount);
@@ -91,8 +96,9 @@ public <T> IRecipeSlotBuilder setCustomRenderer(IIngredientType<T> ingredientTyp
9196
return this;
9297
}
9398

99+
@SuppressWarnings("removal")
94100
@Override
95-
public IRecipeSlotBuilder addTooltipCallback(IRecipeSlotTooltipCallback tooltipCallback) {
101+
public IRecipeSlotBuilder addTooltipCallback(mezz.jei.api.gui.ingredient.IRecipeSlotTooltipCallback tooltipCallback) {
96102
return this;
97103
}
98104

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

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -86,6 +86,12 @@ public <I> DisplayIngredientAcceptor addTypedIngredient(ITypedIngredient<I> type
8686
return this;
8787
}
8888

89+
@Override
90+
public DisplayIngredientAcceptor addFluidStack(Fluid fluid) {
91+
IPlatformFluidHelperInternal<?> fluidHelper = Services.PLATFORM.getFluidHelper();
92+
return addFluidInternal(fluidHelper, fluid, fluidHelper.bucketVolume(), null);
93+
}
94+
8995
@Override
9096
public DisplayIngredientAcceptor addFluidStack(Fluid fluid, long amount) {
9197
IPlatformFluidHelperInternal<?> fluidHelper = Services.PLATFORM.getFluidHelper();

0 commit comments

Comments
 (0)