Skip to content

Commit

Permalink
Add convenience function for setting the standard slot background
Browse files Browse the repository at this point in the history
  • Loading branch information
mezz committed Sep 19, 2024
1 parent b5ba8ce commit be35470
Show file tree
Hide file tree
Showing 9 changed files with 37 additions and 18 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
import mezz.jei.api.gui.drawable.IDrawable;
import mezz.jei.api.gui.ingredient.IRecipeSlotRichTooltipCallback;
import mezz.jei.api.gui.ingredient.IRecipeSlotsView;
import mezz.jei.api.helpers.IGuiHelper;
import mezz.jei.api.ingredients.IIngredientRenderer;
import mezz.jei.api.ingredients.IIngredientType;
import mezz.jei.api.recipe.category.IRecipeCategory;
Expand Down Expand Up @@ -51,6 +52,16 @@ public interface IRecipeSlotBuilder extends IIngredientAcceptor<IRecipeSlotBuild
*/
IRecipeSlotBuilder setSlotName(String slotName);

/**
* Set a normal slot background to draw behind the slot's ingredients.
* This background is 18x18 pixels and offset by -1, -1 to match vanilla slots.
*
* @see IGuiHelper#getSlotDrawable() for the slot background drawable.
*
* @since 19.18.7
*/
IRecipeSlotBuilder setStandardSlotBackground();

/**
* Set a custom background to draw behind the slot's ingredients.
*
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,16 +4,19 @@
import it.unimi.dsi.fastutil.ints.IntSet;
import mezz.jei.api.gui.builder.IRecipeSlotBuilder;
import mezz.jei.api.gui.drawable.IDrawable;
import mezz.jei.api.gui.drawable.IDrawableStatic;
import mezz.jei.api.gui.ingredient.IRecipeSlotDrawable;
import mezz.jei.api.gui.ingredient.IRecipeSlotRichTooltipCallback;
import mezz.jei.api.gui.widgets.ISlottedWidgetFactory;
import mezz.jei.api.helpers.IGuiHelper;
import mezz.jei.api.ingredients.IIngredientRenderer;
import mezz.jei.api.ingredients.IIngredientType;
import mezz.jei.api.ingredients.IIngredientTypeWithSubtypes;
import mezz.jei.api.ingredients.ITypedIngredient;
import mezz.jei.api.recipe.IFocusGroup;
import mezz.jei.api.recipe.RecipeIngredientRole;
import mezz.jei.api.runtime.IIngredientManager;
import mezz.jei.common.Internal;
import mezz.jei.common.gui.elements.OffsetDrawable;
import mezz.jei.common.platform.IPlatformFluidHelperInternal;
import mezz.jei.common.platform.Services;
Expand Down Expand Up @@ -101,6 +104,14 @@ public IRecipeSlotBuilder addOptionalTypedIngredients(List<Optional<ITypedIngred
return this;
}

@Override
public IRecipeSlotBuilder setStandardSlotBackground() {
IGuiHelper guiHelper = Internal.getJeiRuntime().getJeiHelpers().getGuiHelper();
IDrawableStatic background = guiHelper.getSlotDrawable();
this.background = OffsetDrawable.create(background, -1, -1);
return this;
}

@Override
public IRecipeSlotBuilder setBackground(IDrawable background, int xOffset, int yOffset) {
ErrorUtil.checkNotNull(background, "background");
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -75,6 +75,11 @@ public IRecipeSlotBuilder addOptionalTypedIngredients(List<Optional<ITypedIngred
return this;
}

@Override
public IRecipeSlotBuilder setStandardSlotBackground() {
return this;
}

@Override
public IRecipeSlotBuilder setBackground(IDrawable background, int xOffset, int yOffset) {
return this;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -73,13 +73,13 @@ public void setRecipe(IRecipeLayoutBuilder builder, ObnoxiouslyLargeRecipe recip
int yPos = yOffset + (y * slotHeight);
ItemStack stack = iterator.next();
builder.addSlot(RecipeIngredientRole.INPUT, xPos + 1, yPos + 1)
.setBackground(slotBackground, -1, -1)
.setStandardSlotBackground()
.addItemStack(stack);
}
}

builder.addSlot(RecipeIngredientRole.OUTPUT, WIDTH + slotWidth, HEIGHT / 2)
.setBackground(slotBackground, -1, -1)
.setStandardSlotBackground()
.addItemStack(iterator.next());
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -87,7 +87,7 @@ public void setRecipe(IRecipeLayoutBuilder builder, IJeiIngredientInfoRecipe rec
int xPos = (recipeWidth - 16) / 2;

IRecipeSlotBuilder inputSlotBuilder = builder.addSlot(RecipeIngredientRole.INPUT, xPos, 1)
.setBackground(slotBackground, -1, -1);
.setStandardSlotBackground();

IIngredientAcceptor<?> outputSlotBuilder = builder.addInvisibleIngredients(RecipeIngredientRole.OUTPUT);

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,6 @@
import mezz.jei.api.gui.builder.IRecipeLayoutBuilder;
import mezz.jei.api.gui.builder.ITooltipBuilder;
import mezz.jei.api.gui.drawable.IDrawable;
import mezz.jei.api.gui.drawable.IDrawableStatic;
import mezz.jei.api.gui.ingredient.IRecipeSlotsView;
import mezz.jei.api.gui.widgets.IScrollGridWidgetFactory;
import mezz.jei.api.helpers.IGuiHelper;
Expand Down Expand Up @@ -41,12 +40,10 @@ public class TagInfoRecipeCategory<R extends ITagInfoRecipe, T extends RecipeTyp
private final Component localizedName;
private final ImmutableRect2i nameArea;
private final IScrollGridWidgetFactory<?> scrollGridFactory;
private final IDrawableStatic slotDrawable;

public TagInfoRecipeCategory(IGuiHelper guiHelper, T recipeType, ResourceLocation registryLocation) {
this.background = guiHelper.createBlankDrawable(WIDTH, HEIGHT);
this.icon = guiHelper.createDrawableItemLike(Items.NAME_TAG);
this.slotDrawable = guiHelper.getSlotDrawable();
this.recipeType = recipeType;
Component registryName = Component.translatableWithFallback(
"gui.jei.category.registry." + ResourceLocationUtil.sanitizePath(registryLocation.getPath()),
Expand Down Expand Up @@ -89,7 +86,7 @@ public void setRecipe(IRecipeLayoutBuilder builder, R recipe, IFocusGroup focuse
ScreenRectangle gridArea = scrollGridFactory.getArea();
builder.addSlot(RecipeIngredientRole.INPUT, gridArea.position().x() + 1, 1)
.addTypedIngredients(recipe.getTypedIngredients())
.setBackground(slotDrawable, -1, -1);
.setStandardSlotBackground();

for (ITypedIngredient<?> stack : recipe.getTypedIngredients()) {
builder.addSlotToWidget(RecipeIngredientRole.OUTPUT, scrollGridFactory)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -34,13 +34,11 @@
public class SmithingRecipeCategory implements IRecipeCategory<RecipeHolder<SmithingRecipe>>, IExtendableSmithingRecipeCategory {
private final IDrawable background;
private final IDrawable icon;
private final IDrawable slot;
private final IDrawable recipeArrow;
private final Map<Class<? extends SmithingRecipe>, ISmithingCategoryExtension<?>> extensions = new HashMap<>();

public SmithingRecipeCategory(IGuiHelper guiHelper) {
background = guiHelper.createBlankDrawable(108, 28);
slot = guiHelper.getSlotDrawable();
icon = guiHelper.createDrawableItemLike(Blocks.SMITHING_TABLE);
Textures textures = Internal.getTextures();
recipeArrow = textures.getRecipeArrow();
Expand Down Expand Up @@ -76,16 +74,16 @@ public void setRecipe(IRecipeLayoutBuilder builder, RecipeHolder<SmithingRecipe>
}

IRecipeSlotBuilder templateSlot = builder.addSlot(RecipeIngredientRole.INPUT, 1, 6)
.setBackground(slot, -1, -1);
.setStandardSlotBackground();

IRecipeSlotBuilder baseSlot = builder.addSlot(RecipeIngredientRole.INPUT, 19, 6)
.setBackground(slot, -1, -1);
.setStandardSlotBackground();

IRecipeSlotBuilder additionSlot = builder.addSlot(RecipeIngredientRole.INPUT, 37, 6)
.setBackground(slot, -1, -1);
.setStandardSlotBackground();

IRecipeSlotBuilder outputSlot = builder.addSlot(RecipeIngredientRole.OUTPUT, 91, 6)
.setBackground(slot, -1, -1);
.setStandardSlotBackground();

extension.setTemplate(recipe, templateSlot);
extension.setBase(recipe, baseSlot);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,6 @@
public class BrewingRecipeCategory implements IRecipeCategory<IJeiBrewingRecipe> {
private final IDrawable background;
private final IDrawable icon;
private final IDrawable slotDrawable;
private final Component localizedName;
private final IDrawableAnimated arrow;
private final IDrawableAnimated bubbles;
Expand All @@ -49,8 +48,6 @@ public BrewingRecipeCategory(IGuiHelper guiHelper) {
.buildAnimated(bubblesTickTimer, IDrawableAnimated.StartDirection.BOTTOM);

blazeHeat = guiHelper.createDrawable(location, 64, 29, 18, 4);

slotDrawable = guiHelper.getSlotDrawable();
}

@Override
Expand Down Expand Up @@ -104,7 +101,7 @@ public void setRecipe(IRecipeLayoutBuilder builder, IJeiBrewingRecipe recipe, IF

builder.addSlot(RecipeIngredientRole.OUTPUT, 81, 3)
.addItemStack(recipe.getPotionOutput())
.setBackground(slotDrawable, -1, -1);
.setStandardSlotBackground();
}

@Override
Expand Down
2 changes: 1 addition & 1 deletion gradle.properties
Original file line number Diff line number Diff line change
Expand Up @@ -74,4 +74,4 @@ modrinthId=u6dRKJwZ
jUnitVersion=5.8.2

# Version
specificationVersion=19.18.6
specificationVersion=19.18.7

0 comments on commit be35470

Please sign in to comment.