Skip to content

Commit

Permalink
Add built-in IDrawable for drawing basic ingredients
Browse files Browse the repository at this point in the history
  • Loading branch information
mezz committed Apr 22, 2018
1 parent bc646c4 commit 50d773f
Show file tree
Hide file tree
Showing 9 changed files with 90 additions and 4 deletions.
2 changes: 1 addition & 1 deletion gradle.properties
Expand Up @@ -5,4 +5,4 @@ curse_project_id=238222

version_major=4
version_minor=9
version_patch=0
version_patch=1
7 changes: 7 additions & 0 deletions src/api/java/mezz/jei/api/IGuiHelper.java
@@ -1,6 +1,7 @@
package mezz.jei.api;

import mezz.jei.api.gui.ICraftingGridHelper;
import mezz.jei.api.gui.IDrawable;
import mezz.jei.api.gui.IDrawableAnimated;
import mezz.jei.api.gui.IDrawableStatic;
import mezz.jei.api.gui.ITickTimer;
Expand Down Expand Up @@ -45,6 +46,12 @@ public interface IGuiHelper {
*/
IDrawableStatic createBlankDrawable(int width, int height);

/**
* Returns a 16x16 drawable for the given ingredient, matching the one JEI draws in the ingredient list.
* @since JEI 4.9.1
*/
<V> IDrawable createDrawableIngredient(V ingredient);

/**
* Create a crafting grid helper.
* Helps set crafting-grid-style GuiItemStackGroup.
Expand Down
15 changes: 13 additions & 2 deletions src/main/java/mezz/jei/gui/GuiHelper.java
Expand Up @@ -2,18 +2,22 @@

import mezz.jei.api.IGuiHelper;
import mezz.jei.api.gui.ICraftingGridHelper;
import mezz.jei.api.gui.IDrawable;
import mezz.jei.api.gui.IDrawableAnimated;
import mezz.jei.api.gui.IDrawableStatic;
import mezz.jei.api.gui.ITickTimer;
import mezz.jei.api.ingredients.IIngredientRegistry;
import mezz.jei.api.ingredients.IIngredientRenderer;
import mezz.jei.config.Constants;
import mezz.jei.gui.elements.DrawableAnimated;
import mezz.jei.gui.elements.DrawableBlank;
import mezz.jei.gui.elements.DrawableIngredient;
import mezz.jei.gui.elements.DrawableResource;
import mezz.jei.util.ErrorUtil;
import net.minecraft.util.ResourceLocation;

public class GuiHelper implements IGuiHelper {

private final IIngredientRegistry ingredientRegistry;
private final IDrawableStatic slotDrawable;
private final IDrawableStatic tabSelected;
private final IDrawableStatic tabUnselected;
Expand All @@ -23,7 +27,8 @@ public class GuiHelper implements IGuiHelper {
private final IDrawableStatic arrowNext;
private final IDrawableStatic plusSign;

public GuiHelper() {
public GuiHelper(IIngredientRegistry ingredientRegistry) {
this.ingredientRegistry = ingredientRegistry;
slotDrawable = createDrawable(Constants.RECIPE_BACKGROUND, 196, 93, 18, 18);

tabSelected = createDrawable(Constants.RECIPE_BACKGROUND, 196, 15, 24, 24);
Expand Down Expand Up @@ -96,6 +101,12 @@ public IDrawableStatic createBlankDrawable(int width, int height) {
return new DrawableBlank(width, height);
}

@Override
public <V> IDrawable createDrawableIngredient(V ingredient) {
IIngredientRenderer<V> ingredientRenderer = ingredientRegistry.getIngredientRenderer(ingredient);
return new DrawableIngredient<>(ingredient, ingredientRenderer);
}

@Override
public ICraftingGridHelper createCraftingGridHelper(int craftInputSlot1, int craftOutputSlot) {
return new CraftingGridHelper(craftInputSlot1, craftOutputSlot);
Expand Down
34 changes: 34 additions & 0 deletions src/main/java/mezz/jei/gui/elements/DrawableIngredient.java
@@ -0,0 +1,34 @@
package mezz.jei.gui.elements;

import mezz.jei.api.gui.IDrawable;
import mezz.jei.api.ingredients.IIngredientRenderer;
import net.minecraft.client.Minecraft;
import net.minecraft.client.renderer.GlStateManager;

public class DrawableIngredient<V> implements IDrawable {
private final V ingredient;
private final IIngredientRenderer<V> ingredientRenderer;

public DrawableIngredient(V ingredient, IIngredientRenderer<V> ingredientRenderer) {
this.ingredient = ingredient;
this.ingredientRenderer = ingredientRenderer;
}

@Override
public int getWidth() {
return 16;
}

@Override
public int getHeight() {
return 16;
}

@Override
public void draw(Minecraft minecraft, int xOffset, int yOffset) {
GlStateManager.enableDepth();
this.ingredientRenderer.render(minecraft, xOffset, yOffset, ingredient);
GlStateManager.enableAlpha();
GlStateManager.disableDepth();
}
}
Expand Up @@ -9,13 +9,16 @@
import mezz.jei.api.recipe.VanillaRecipeCategoryUid;
import mezz.jei.config.Constants;
import net.minecraft.init.Blocks;
import net.minecraft.item.ItemStack;

public class AnvilRecipeCategory implements IRecipeCategory<AnvilRecipeWrapper> {

private final IDrawable background;
private final IDrawable icon;

public AnvilRecipeCategory(IGuiHelper guiHelper) {
background = guiHelper.createDrawable(Constants.RECIPE_GUI_VANILLA, 0, 168, 125, 18, 0, 20, 0, 0);
icon = guiHelper.createDrawableIngredient(new ItemStack(Blocks.ANVIL));
}

@Override
Expand All @@ -38,6 +41,11 @@ public IDrawable getBackground() {
return background;
}

@Override
public IDrawable getIcon() {
return icon;
}

@Override
public void setRecipe(IRecipeLayout recipeLayout, AnvilRecipeWrapper recipeWrapper, IIngredients ingredients) {
IGuiItemStackGroup guiItemStacks = recipeLayout.getItemStacks();
Expand Down
Expand Up @@ -14,6 +14,8 @@
import mezz.jei.gui.elements.DrawableAnimated;
import mezz.jei.util.Translator;
import net.minecraft.client.Minecraft;
import net.minecraft.init.Items;
import net.minecraft.item.ItemStack;
import net.minecraft.util.ResourceLocation;

public class BrewingRecipeCategory implements IRecipeCategory<BrewingRecipeWrapper> {
Expand All @@ -25,6 +27,7 @@ public class BrewingRecipeCategory implements IRecipeCategory<BrewingRecipeWrapp
private static final int outputSlot = 4; // for display only

private final IDrawable background;
private final IDrawable icon;
private final IDrawable slotDrawable;
private final String localizedName;
private final IDrawableAnimated arrow;
Expand All @@ -34,6 +37,7 @@ public class BrewingRecipeCategory implements IRecipeCategory<BrewingRecipeWrapp
public BrewingRecipeCategory(IGuiHelper guiHelper) {
ResourceLocation location = Constants.RECIPE_GUI_VANILLA;
background = guiHelper.createDrawable(location, 0, 0, 64, 60, 1, 0, 0, 40);
icon = guiHelper.createDrawableIngredient(new ItemStack(Items.BREWING_STAND));
localizedName = Translator.translateToLocal("gui.jei.category.brewing");

IDrawableStatic brewArrowDrawable = guiHelper.createDrawable(location, 64, 0, 9, 28);
Expand Down Expand Up @@ -68,6 +72,11 @@ public IDrawable getBackground() {
return background;
}

@Override
public IDrawable getIcon() {
return icon;
}

@Override
public void drawExtras(Minecraft minecraft) {
blazeHeat.draw(minecraft, 5, 30);
Expand Down
Expand Up @@ -19,6 +19,7 @@
import mezz.jei.util.Translator;
import net.minecraft.client.Minecraft;
import net.minecraft.client.gui.GuiScreen;
import net.minecraft.init.Blocks;
import net.minecraft.item.ItemStack;
import net.minecraft.util.ResourceLocation;
import net.minecraft.util.text.TextFormatting;
Expand All @@ -32,12 +33,14 @@ public class CraftingRecipeCategory implements IRecipeCategory<IRecipeWrapper> {
public static final int height = 54;

private final IDrawable background;
private final IDrawable icon;
private final String localizedName;
private final ICraftingGridHelper craftingGridHelper;

public CraftingRecipeCategory(IGuiHelper guiHelper) {
ResourceLocation location = Constants.RECIPE_GUI_VANILLA;
background = guiHelper.createDrawable(location, 0, 60, width, height);
icon = guiHelper.createDrawableIngredient(new ItemStack(Blocks.CRAFTING_TABLE));
localizedName = Translator.translateToLocal("gui.jei.category.craftingTable");
craftingGridHelper = guiHelper.createCraftingGridHelper(craftInputSlot1, craftOutputSlot);
}
Expand All @@ -62,6 +65,11 @@ public IDrawable getBackground() {
return background;
}

@Override
public IDrawable getIcon() {
return icon;
}

@Override
public void setRecipe(IRecipeLayout recipeLayout, IRecipeWrapper recipeWrapper, IIngredients ingredients) {
IGuiItemStackGroup guiItemStacks = recipeLayout.getItemStacks();
Expand Down
Expand Up @@ -9,14 +9,18 @@
import mezz.jei.config.Constants;
import mezz.jei.util.Translator;
import net.minecraft.client.Minecraft;
import net.minecraft.init.Blocks;
import net.minecraft.item.ItemStack;

public class FurnaceSmeltingCategory extends FurnaceRecipeCategory<SmeltingRecipe> {
private final IDrawable background;
private final IDrawable icon;
private final String localizedName;

public FurnaceSmeltingCategory(IGuiHelper guiHelper) {
super(guiHelper);
background = guiHelper.createDrawable(Constants.RECIPE_GUI_VANILLA, 0, 114, 82, 54);
icon = guiHelper.createDrawableIngredient(new ItemStack(Blocks.FURNACE));
localizedName = Translator.translateToLocal("gui.jei.category.smelting");
}

Expand All @@ -25,6 +29,11 @@ public IDrawable getBackground() {
return background;
}

@Override
public IDrawable getIcon() {
return icon;
}

@Override
public void drawExtras(Minecraft minecraft) {
animatedFlame.draw(minecraft, 1, 20);
Expand Down
2 changes: 1 addition & 1 deletion src/main/java/mezz/jei/runtime/JeiHelpers.java
Expand Up @@ -19,7 +19,7 @@ public class JeiHelpers implements IJeiHelpers {
private final IVanillaRecipeFactory vanillaRecipeFactory = new VanillaRecipeFactory();

public JeiHelpers(IIngredientRegistry ingredientRegistry, IngredientBlacklistInternal ingredientBlacklistInternal, StackHelper stackHelper) {
this.guiHelper = new GuiHelper();
this.guiHelper = new GuiHelper(ingredientRegistry);
this.stackHelper = stackHelper;
this.ingredientBlacklist = new IngredientBlacklist(ingredientRegistry, ingredientBlacklistInternal);
this.recipeTransferHandlerHelper = new RecipeTransferHandlerHelper();
Expand Down

0 comments on commit 50d773f

Please sign in to comment.