Skip to content

Commit

Permalink
Make better use of ItemLike and Item#getDefaultInstance
Browse files Browse the repository at this point in the history
  • Loading branch information
mezz committed Sep 19, 2024
1 parent 35f90d5 commit 703632d
Show file tree
Hide file tree
Showing 24 changed files with 133 additions and 21 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,7 @@ public Item getBase(ItemStack ingredient) {

@Override
public ItemStack getDefaultIngredient(Item base) {
return new ItemStack(base);
return base.getDefaultInstance();
}
};

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@
import net.minecraft.nbt.CompoundTag;
import net.minecraft.world.item.ItemStack;
import net.minecraft.world.item.crafting.Ingredient;
import net.minecraft.world.level.ItemLike;
import net.minecraft.world.level.material.Fluid;
import org.jetbrains.annotations.ApiStatus;
import org.jetbrains.annotations.Nullable;
Expand Down Expand Up @@ -102,6 +103,26 @@ default THIS addItemStack(ItemStack itemStack) {
return addIngredient(VanillaTypes.ITEM_STACK, itemStack);
}

/**
* Convenience function to add one {@link ItemLike}.
*
* @since 15.19.4
*/
default IIngredientConsumer addItemLike(ItemLike itemLike) {
return addItemStack(itemLike.asItem().getDefaultInstance());
}

/**
* Convenience helper to add one Fluid ingredient with the default amount (one bucket).
*
* To add multiple Fluid ingredients, you can call this multiple times.
*
* @see #addFluidStack(Fluid, long) to add a Fluid with an amount.
* @see #addFluidStack(Fluid, long, CompoundTag) to add a Fluid with a {@link CompoundTag}.
* @since 15.19.4
*/
THIS addFluidStack(Fluid fluid);

/**
* Convenience helper to add one Fluid ingredient.
*
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@
import net.minecraft.nbt.CompoundTag;
import net.minecraft.world.item.ItemStack;
import net.minecraft.world.item.crafting.Ingredient;
import net.minecraft.world.level.ItemLike;
import net.minecraft.world.level.material.Fluid;
import org.jetbrains.annotations.ApiStatus;
import org.jetbrains.annotations.Nullable;
Expand Down Expand Up @@ -104,6 +105,26 @@ default IIngredientConsumer addItemStack(ItemStack itemStack) {
return addIngredient(VanillaTypes.ITEM_STACK, itemStack);
}

/**
* Convenience function to add one {@link ItemLike}.
*
* @since 15.19.4
*/
default IIngredientConsumer addItemLike(ItemLike itemLike) {
return addItemStack(itemLike.asItem().getDefaultInstance());
}

/**
* Convenience helper to add one Fluid ingredient with the default amount (one bucket).
*
* To add multiple Fluid ingredients, you can call this multiple times.
*
* @see #addFluidStack(Fluid, long) to add a Fluid with an amount.
* @see #addFluidStack(Fluid, long, CompoundTag) to add a Fluid with a {@link CompoundTag}.
* @since 15.19.4
*/
IIngredientConsumer addFluidStack(Fluid fluid);

/**
* Convenience helper to add one Fluid ingredient.
*
Expand Down
12 changes: 12 additions & 0 deletions CommonApi/src/main/java/mezz/jei/api/helpers/IGuiHelper.java
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@
import mezz.jei.api.recipe.RecipeIngredientRole;
import net.minecraft.resources.ResourceLocation;
import net.minecraft.world.item.ItemStack;
import net.minecraft.world.level.ItemLike;

/**
* Helps with the implementation of GUIs.
Expand Down Expand Up @@ -71,6 +72,17 @@ default IDrawable createDrawableItemStack(ItemStack ingredient) {
return createDrawableIngredient(VanillaTypes.ITEM_STACK, ingredient);
}

/**
* Returns a 16x16 drawable for the given ItemLike,
* matching the one JEI draws in the ingredient list.
*
* @see #createDrawableIngredient(IIngredientType, Object) for other ingredient types.
* @since 15.19.4
*/
default IDrawable createDrawableItemLike(ItemLike itemLike) {
return createDrawableIngredient(VanillaTypes.ITEM_STACK, itemLike.asItem().getDefaultInstance());
}

/**
* Returns a 16x16 drawable for the given ingredient,
* matching the one JEI draws in the ingredient list.
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@
import mezz.jei.api.recipe.RecipeType;
import mezz.jei.api.runtime.IIngredientManager;
import net.minecraft.world.item.ItemStack;
import net.minecraft.world.level.ItemLike;

public interface IRecipeCatalystRegistration {
/**
Expand All @@ -20,6 +21,23 @@ public interface IRecipeCatalystRegistration {
*/
IJeiHelpers getJeiHelpers();

/**
* Add an association between an {@link ItemStack} and what it can craft.
* (i.e. Furnace ItemStack can craft Smelting and Fuel Recipes)
* Allows players to see what ingredient they need to craft in order to make recipes from a recipe category.
*
* @param itemLike the {@link ItemLike} that can craft recipes (like a furnace or crafting table)
* @param recipeTypes the types of recipe that the ingredient is a catalyst for
*
* @see #addRecipeCatalyst(ItemStack, RecipeType...) to add {@link ItemStack} catalysts.
* @see #addRecipeCatalyst(IIngredientType, Object, RecipeType...) to add non-{@link ItemLike} catalysts.
*
* @since 15.19.4
*/
default void addRecipeCatalyst(ItemLike itemLike, RecipeType<?>... recipeTypes) {
addRecipeCatalyst(VanillaTypes.ITEM_STACK, itemLike.asItem().getDefaultInstance(), recipeTypes);
}

/**
* Add an association between an {@link ItemStack} and what it can craft.
* (i.e. Furnace ItemStack can craft Smelting and Fuel Recipes)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@
import mezz.jei.api.runtime.IIngredientVisibility;
import net.minecraft.network.chat.Component;
import net.minecraft.world.item.ItemStack;
import net.minecraft.world.level.ItemLike;

import java.util.List;

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

/**
* Add an info page for an ItemLike.
* Description pages show in the recipes for an ItemStack and tell the player a little about it.
*
* @param itemLike The ItemLike to describe
* @param descriptionComponents Text components for info text.
* New lines can be added with "\n" or by giving multiple descriptions.
* Long lines are wrapped automatically.
* Very long entries will span multiple pages automatically.
* @since 15.19.4
*/
default void addIngredientInfo(ItemLike itemLike, Component... descriptionComponents) {
addIngredientInfo(itemLike.asItem().getDefaultInstance(), VanillaTypes.ITEM_STACK, descriptionComponents);
}

/**
* Add an info page for an ItemStack.
* Description pages show in the recipes for an ItemStack and tell the player a little about it.
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -40,6 +40,11 @@ public IngredientAcceptorVoid addOptionalTypedIngredients(List<Optional<ITypedIn
return this;
}

@Override
public IngredientAcceptorVoid addFluidStack(Fluid fluid) {
return this;
}

@Override
public IngredientAcceptorVoid addFluidStack(Fluid fluid, long amount) {
return this;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -64,6 +64,12 @@ public <I> IRecipeSlotBuilder addIngredient(IIngredientType<I> ingredientType, I
return this;
}

@Override
public IRecipeSlotBuilder addFluidStack(Fluid fluid) {
this.ingredients.addFluidStack(fluid);
return this;
}

@Override
public IRecipeSlotBuilder addFluidStack(Fluid fluid, long amount) {
this.ingredients.addFluidStack(fluid, amount);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,6 @@
import mezz.jei.api.gui.builder.IRecipeSlotBuilder;
import mezz.jei.api.gui.drawable.IDrawable;
import mezz.jei.api.gui.ingredient.IRecipeSlotRichTooltipCallback;
import mezz.jei.api.gui.ingredient.IRecipeSlotTooltipCallback;
import mezz.jei.api.ingredients.IIngredientRenderer;
import mezz.jei.api.ingredients.IIngredientType;
import mezz.jei.api.ingredients.ITypedIngredient;
Expand Down Expand Up @@ -41,6 +40,12 @@ public <I> IRecipeSlotBuilder addIngredient(IIngredientType<I> ingredientType, I
return this;
}

@Override
public IRecipeSlotBuilder addFluidStack(Fluid fluid) {
this.ingredients.addFluidStack(fluid);
return this;
}

@Override
public IRecipeSlotBuilder addFluidStack(Fluid fluid, long amount) {
this.ingredients.addFluidStack(fluid, amount);
Expand Down Expand Up @@ -91,8 +96,9 @@ public <T> IRecipeSlotBuilder setCustomRenderer(IIngredientType<T> ingredientTyp
return this;
}

@SuppressWarnings("removal")
@Override
public IRecipeSlotBuilder addTooltipCallback(IRecipeSlotTooltipCallback tooltipCallback) {
public IRecipeSlotBuilder addTooltipCallback(mezz.jei.api.gui.ingredient.IRecipeSlotTooltipCallback tooltipCallback) {
return this;
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -86,6 +86,12 @@ public <I> DisplayIngredientAcceptor addTypedIngredient(ITypedIngredient<I> type
return this;
}

@Override
public DisplayIngredientAcceptor addFluidStack(Fluid fluid) {
IPlatformFluidHelperInternal<?> fluidHelper = Services.PLATFORM.getFluidHelper();
return addFluidInternal(fluidHelper, fluid, fluidHelper.bucketVolume(), null);
}

@Override
public DisplayIngredientAcceptor addFluidStack(Fluid fluid, long amount) {
IPlatformFluidHelperInternal<?> fluidHelper = Services.PLATFORM.getFluidHelper();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -83,6 +83,12 @@ public <I> SimpleIngredientAcceptor addTypedIngredient(ITypedIngredient<I> typed
return this;
}

@Override
public SimpleIngredientAcceptor addFluidStack(Fluid fluid) {
IPlatformFluidHelperInternal<?> fluidHelper = Services.PLATFORM.getFluidHelper();
return addFluidInternal(fluidHelper, fluid, fluidHelper.bucketVolume(), null);
}

@Override
public SimpleIngredientAcceptor addFluidStack(Fluid fluid, long amount) {
IPlatformFluidHelperInternal<?> fluidHelper = Services.PLATFORM.getFluidHelper();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -133,6 +133,7 @@ private <T> void drawIngredientName(Minecraft minecraft, GuiGraphics guiGraphics
guiGraphics.drawString(minecraft.font, jeiUid, 50, 52, 0, false);
}

@SuppressWarnings("removal")
@Override
public void setRecipe(IRecipeLayoutBuilder builder, DebugRecipe recipe, IFocusGroup focuses) {
// ITEM type
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,6 @@
import net.minecraft.core.Registry;
import net.minecraft.resources.ResourceKey;
import net.minecraft.resources.ResourceLocation;
import net.minecraft.world.item.ItemStack;
import net.minecraft.world.level.ItemLike;

import java.util.ArrayList;
Expand Down Expand Up @@ -163,7 +162,7 @@ private static <B extends ItemLike> boolean createAndRegisterItemLikeTagCategory
tagInfoRecipeMakers.add(new TagInfoRecipeMaker<>(
VanillaTypes.ITEM_STACK,
recipeType,
ItemStack::new,
i -> i.asItem().getDefaultInstance(),
registryKey
));
return true;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,6 @@
import net.minecraft.network.chat.Component;
import net.minecraft.resources.ResourceLocation;
import net.minecraft.tags.TagKey;
import net.minecraft.world.item.ItemStack;
import net.minecraft.world.item.Items;
import org.apache.commons.lang3.StringUtils;

Expand All @@ -45,7 +44,7 @@ public class TagInfoRecipeCategory<R extends ITagInfoRecipe, T extends RecipeTyp

public TagInfoRecipeCategory(IGuiHelper guiHelper, T recipeType, ResourceLocation registryLocation) {
this.background = guiHelper.createBlankDrawable(WIDTH, HEIGHT);
this.icon = guiHelper.createDrawableItemStack(new ItemStack(Items.NAME_TAG));
this.icon = guiHelper.createDrawableItemLike(Items.NAME_TAG);
this.slotDrawable = guiHelper.getSlotDrawable();
this.recipeType = recipeType;
String registryName = StringUtils.capitalize(registryLocation.getPath());
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,7 @@ public AnvilRecipeCategory(IGuiHelper guiHelper) {
background = guiHelper.drawableBuilder(Constants.RECIPE_GUI_VANILLA, 0, 168, 125, 18)
.addPadding(0, 20, 0, 0)
.build();
icon = guiHelper.createDrawableItemStack(new ItemStack(Blocks.ANVIL));
icon = guiHelper.createDrawableItemLike(Blocks.ANVIL);
}

@Override
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,6 @@
import net.minecraft.client.gui.GuiGraphics;
import net.minecraft.network.chat.Component;
import net.minecraft.resources.ResourceLocation;
import net.minecraft.world.item.ItemStack;
import net.minecraft.world.item.crafting.SmithingRecipe;
import net.minecraft.world.level.block.Blocks;
import org.jetbrains.annotations.Nullable;
Expand All @@ -38,7 +37,7 @@ public class SmithingRecipeCategory implements IRecipeCategory<SmithingRecipe>,
public SmithingRecipeCategory(IGuiHelper guiHelper) {
background = guiHelper.createBlankDrawable(108, 28);
slot = guiHelper.getSlotDrawable();
icon = guiHelper.createDrawableItemStack(new ItemStack(Blocks.SMITHING_TABLE));
icon = guiHelper.createDrawableItemLike(Blocks.SMITHING_TABLE);
Textures textures = Internal.getTextures();
recipeArrow = textures.getRecipeArrow();
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -38,7 +38,7 @@ public BrewingRecipeCategory(IGuiHelper guiHelper) {
background = guiHelper.drawableBuilder(location, 0, 0, 64, 60)
.addPadding(1, 0, 0, 50)
.build();
icon = guiHelper.createDrawableItemStack(new ItemStack(Blocks.BREWING_STAND));
icon = guiHelper.createDrawableItemLike(Blocks.BREWING_STAND);
localizedName = Component.translatable("gui.jei.category.brewing");

arrow = guiHelper.drawableBuilder(location, 64, 0, 9, 28)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,6 @@
import net.minecraft.client.gui.Font;
import net.minecraft.network.chat.Component;
import net.minecraft.resources.ResourceLocation;
import net.minecraft.world.item.ItemStack;
import net.minecraft.world.level.block.Blocks;

public class CompostableRecipeCategory implements IRecipeCategory<IJeiCompostingRecipe> {
Expand All @@ -31,7 +30,7 @@ public class CompostableRecipeCategory implements IRecipeCategory<IJeiComposting
public CompostableRecipeCategory(IGuiHelper guiHelper) {
background = guiHelper.createBlankDrawable(width, height);
slot = guiHelper.getSlotDrawable();
icon = guiHelper.createDrawableItemStack(new ItemStack(Blocks.COMPOSTER));
icon = guiHelper.createDrawableItemLike(Blocks.COMPOSTER);
localizedName = Component.translatable("gui.jei.category.compostable");
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,6 @@
import net.minecraft.client.gui.navigation.ScreenPosition;
import net.minecraft.network.chat.Component;
import net.minecraft.resources.ResourceLocation;
import net.minecraft.world.item.ItemStack;
import net.minecraft.world.item.crafting.AbstractCookingRecipe;
import net.minecraft.world.level.block.Block;

Expand All @@ -34,7 +33,7 @@ public AbstractCookingCategory(IGuiHelper guiHelper, Block icon, String translat
super(guiHelper);
this.background = guiHelper.createDrawable(Constants.RECIPE_GUI_VANILLA, 0, 114, 82, 54);
this.regularCookTime = regularCookTime;
this.icon = guiHelper.createDrawableItemStack(new ItemStack(icon));
this.icon = guiHelper.createDrawableItemLike(icon);
this.localizedName = Component.translatable(translationKey);
this.guiHelper = guiHelper;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,6 @@
import net.minecraft.client.gui.GuiGraphics;
import net.minecraft.network.chat.Component;
import net.minecraft.resources.ResourceLocation;
import net.minecraft.world.item.ItemStack;
import net.minecraft.world.item.crafting.CraftingRecipe;
import net.minecraft.world.item.crafting.Recipe;
import net.minecraft.world.level.block.Blocks;
Expand All @@ -43,7 +42,7 @@ public class CraftingRecipeCategory implements IExtendableRecipeCategory<Craftin
public CraftingRecipeCategory(IGuiHelper guiHelper) {
ResourceLocation location = Constants.RECIPE_GUI_VANILLA;
background = guiHelper.createDrawable(location, 0, 60, width, height);
icon = guiHelper.createDrawableItemStack(new ItemStack(Blocks.CRAFTING_TABLE));
icon = guiHelper.createDrawableItemLike(Blocks.CRAFTING_TABLE);
localizedName = Component.translatable("gui.jei.category.craftingTable");
craftingGridHelper = guiHelper.createCraftingGridHelper();
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -120,7 +120,7 @@ private ResourceLocation getRegistryName(T ingredient, Fluid fluid) {
public ItemStack getCheatItemStack(T ingredient) {
Fluid fluid = fluidType.getBase(ingredient);
Item filledBucket = fluid.getBucket();
return new ItemStack(filledBucket);
return filledBucket.getDefaultInstance();
}

@Override
Expand Down
Loading

0 comments on commit 703632d

Please sign in to comment.