Skip to content

Commit

Permalink
Close #255 API to open the Recipes Gui for items, fluids, and categories
Browse files Browse the repository at this point in the history
  • Loading branch information
mezz committed Apr 21, 2016
1 parent 02b0e30 commit 1779fc2
Show file tree
Hide file tree
Showing 9 changed files with 185 additions and 33 deletions.
8 changes: 8 additions & 0 deletions changelog.html
@@ -1,3 +1,11 @@
<h2>3.2.12</h2>
<h4>API:</h4>
<ul>
<li>Add methods to open the Recipes Gui for an ItemStack, Fluid, or Recipe Category.</li>
</ul>
<br/>
<hr/>

<h2>3.2.11</h2>
<h4>Features:</h4>
<ul>
Expand Down
2 changes: 1 addition & 1 deletion gradle.properties
Expand Up @@ -5,4 +5,4 @@ curse_project_id=238222

version_major=3
version_minor=2
version_patch=11
version_patch=12
38 changes: 23 additions & 15 deletions src/main/java/mezz/jei/GuiEventHandler.java
Expand Up @@ -21,25 +21,17 @@ public class GuiEventHandler {
@Nonnull
private static final String showRecipesText = Translator.translateToLocal("jei.tooltip.show.recipes");
@Nullable
private ItemListOverlay itemListOverlay;
@Nullable
private InputHandler inputHandler;
@Nullable
private GuiContainer previousGui = null;

public void setItemListOverlay(@Nullable ItemListOverlay itemListOverlay) {
if (this.itemListOverlay != null && this.itemListOverlay.isOpen()) {
this.itemListOverlay.close();
}

this.itemListOverlay = itemListOverlay;
}

@SubscribeEvent
public void onGuiInit(@Nonnull GuiScreenEvent.InitGuiEvent.Post event) {
if (itemListOverlay == null) {
JeiRuntime runtime = Internal.getRuntime();
if (runtime == null) {
return;
}
ItemListOverlay itemListOverlay = runtime.getItemListOverlay();

GuiScreen gui = event.getGui();
if (gui instanceof GuiContainer) {
Expand All @@ -53,9 +45,11 @@ public void onGuiInit(@Nonnull GuiScreenEvent.InitGuiEvent.Post event) {

@SubscribeEvent
public void onGuiOpen(@Nonnull GuiOpenEvent event) {
if (itemListOverlay == null) {
JeiRuntime runtime = Internal.getRuntime();
if (runtime == null) {
return;
}
ItemListOverlay itemListOverlay = runtime.getItemListOverlay();

GuiScreen gui = event.getGui();
if (gui instanceof GuiContainer) {
Expand All @@ -75,7 +69,13 @@ public void onGuiOpen(@Nonnull GuiOpenEvent event) {

@SubscribeEvent
public void onDrawBackgroundEventPost(@Nonnull GuiScreenEvent.BackgroundDrawnEvent event) {
if (itemListOverlay != null && itemListOverlay.isOpen()) {
JeiRuntime runtime = Internal.getRuntime();
if (runtime == null) {
return;
}

ItemListOverlay itemListOverlay = runtime.getItemListOverlay();
if (itemListOverlay.isOpen()) {
GuiScreen gui = event.getGui();
itemListOverlay.updateGui(gui);
itemListOverlay.drawScreen(gui.mc, event.getMouseX(), event.getMouseY());
Expand All @@ -84,7 +84,8 @@ public void onDrawBackgroundEventPost(@Nonnull GuiScreenEvent.BackgroundDrawnEve

@SubscribeEvent
public void onDrawScreenEventPost(@Nonnull GuiScreenEvent.DrawScreenEvent.Post event) {
if (itemListOverlay == null) {
JeiRuntime runtime = Internal.getRuntime();
if (runtime == null) {
return;
}

Expand All @@ -97,17 +98,24 @@ public void onDrawScreenEventPost(@Nonnull GuiScreenEvent.DrawScreenEvent.Post e
}
}

ItemListOverlay itemListOverlay = runtime.getItemListOverlay();
if (itemListOverlay.isOpen()) {
itemListOverlay.drawTooltips(gui.mc, event.getMouseX(), event.getMouseY());
}
}

@SubscribeEvent
public void onClientTick(@Nonnull TickEvent.ClientTickEvent event) {
if (itemListOverlay == null || event.phase == TickEvent.Phase.END) {
JeiRuntime runtime = Internal.getRuntime();
if (runtime == null) {
return;
}

if (event.phase == TickEvent.Phase.END) {
return;
}

ItemListOverlay itemListOverlay = runtime.getItemListOverlay();
if (itemListOverlay.isOpen()) {
itemListOverlay.handleTick();
}
Expand Down
3 changes: 3 additions & 0 deletions src/main/java/mezz/jei/Internal.java
Expand Up @@ -27,6 +27,9 @@ public static JeiRuntime getRuntime() {
}

public static void setRuntime(JeiRuntime runtime) {
if (Internal.runtime != null) {
Internal.runtime.close();
}
Internal.runtime = runtime;
}

Expand Down
25 changes: 22 additions & 3 deletions src/main/java/mezz/jei/JeiRuntime.java
@@ -1,18 +1,31 @@
package mezz.jei;

import javax.annotation.Nonnull;

import mezz.jei.api.IJeiRuntime;
import mezz.jei.api.IRecipesGui;
import mezz.jei.gui.ItemListOverlay;
import mezz.jei.gui.RecipesGui;

import javax.annotation.Nonnull;

public class JeiRuntime implements IJeiRuntime {

private final RecipeRegistry recipeRegistry;
private final ItemListOverlay itemListOverlay;
private final RecipesGui recipesGui;

public JeiRuntime(RecipeRegistry recipeRegistry, ItemListOverlay itemListOverlay) {
public JeiRuntime(RecipeRegistry recipeRegistry, ItemListOverlay itemListOverlay, RecipesGui recipesGui) {
this.recipeRegistry = recipeRegistry;
this.itemListOverlay = itemListOverlay;
this.recipesGui = recipesGui;
}

public void close() {
if (itemListOverlay.isOpen()) {
itemListOverlay.close();
}
if (recipesGui.isOpen()) {
recipesGui.close();
}
}

@Nonnull
Expand All @@ -26,4 +39,10 @@ public RecipeRegistry getRecipeRegistry() {
public ItemListOverlay getItemListOverlay() {
return itemListOverlay;
}

@Nonnull
@Override
public IRecipesGui getRecipesGui() {
return recipesGui;
}
}
8 changes: 4 additions & 4 deletions src/main/java/mezz/jei/ProxyCommonClient.java
Expand Up @@ -7,6 +7,7 @@
import mezz.jei.config.KeyBindings;
import mezz.jei.config.SessionData;
import mezz.jei.gui.ItemListOverlay;
import mezz.jei.gui.RecipesGui;
import mezz.jei.network.packets.PacketJEI;
import mezz.jei.plugins.jei.JEIInternalPlugin;
import mezz.jei.plugins.vanilla.VanillaPlugin;
Expand Down Expand Up @@ -44,7 +45,6 @@
public class ProxyCommonClient extends ProxyCommon {
@Nullable
private ItemFilter itemFilter;
private GuiEventHandler guiEventHandler;
private List<IModPlugin> plugins;

private static void initVersionChecker() {
Expand Down Expand Up @@ -110,7 +110,7 @@ public void init(@Nonnull FMLInitializationEvent event) {
KeyBindings.init();
MinecraftForge.EVENT_BUS.register(this);

guiEventHandler = new GuiEventHandler();
GuiEventHandler guiEventHandler = new GuiEventHandler();
MinecraftForge.EVENT_BUS.register(guiEventHandler);

fixVanillaItemHasSubtypes();
Expand Down Expand Up @@ -172,9 +172,9 @@ private void startJEI() {

itemFilter = new ItemFilter(itemRegistry);
ItemListOverlay itemListOverlay = new ItemListOverlay(itemFilter, advancedGuiHandlers);
guiEventHandler.setItemListOverlay(itemListOverlay);
RecipesGui recipesGui = new RecipesGui();

JeiRuntime jeiRuntime = new JeiRuntime(recipeRegistry, itemListOverlay);
JeiRuntime jeiRuntime = new JeiRuntime(recipeRegistry, itemListOverlay, recipesGui);
Internal.setRuntime(jeiRuntime);

iterator = plugins.iterator();
Expand Down
6 changes: 6 additions & 0 deletions src/main/java/mezz/jei/api/IJeiRuntime.java
Expand Up @@ -9,4 +9,10 @@ public interface IJeiRuntime {

@Nonnull
IItemListOverlay getItemListOverlay();

/**
* @since JEI 3.2.12
*/
@Nonnull
IRecipesGui getRecipesGui();
}
53 changes: 53 additions & 0 deletions src/main/java/mezz/jei/api/IRecipesGui.java
@@ -0,0 +1,53 @@
package mezz.jei.api;

import net.minecraft.item.ItemStack;
import net.minecraftforge.fluids.Fluid;

import javax.annotation.Nonnull;
import java.util.List;

/**
* JEI's gui for displaying recipes. Use this interface to open recipes.
*
* @since JEI 3.2.12
*/
public interface IRecipesGui {
/**
* Show recipes for an {@link ItemStack}.
* Opens the {@link IRecipesGui} if it is closed.
*
* @param focus the {@link ItemStack} result.
*/
void showRecipes(@Nonnull ItemStack focus);

/**
* Show recipes for a {@link Fluid}.
* Opens the {@link IRecipesGui} if it is closed.
*
* @param focus the {@link Fluid} result.
*/
void showRecipes(@Nonnull Fluid focus);

/**
* Show recipes that use an {@link ItemStack} as an ingredient.
* Opens the {@link IRecipesGui} if it is closed.
*
* @param focus the {@link ItemStack} ingredient.
*/
void showUses(@Nonnull ItemStack focus);

/**
* Show recipes that use a {@link Fluid} as an ingredient.
* Opens the {@link IRecipesGui} if it is closed.
*
* @param focus the {@link Fluid} ingredient.
*/
void showUses(@Nonnull Fluid focus);

/**
* Show entire categories of recipes.
*
* @param recipeCategoryUids a list of categories to display, in order. Must not be empty.
*/
void showCategories(@Nonnull List<String> recipeCategoryUids);
}
75 changes: 65 additions & 10 deletions src/main/java/mezz/jei/gui/RecipesGui.java
@@ -1,12 +1,14 @@
package mezz.jei.gui;

import mezz.jei.api.IRecipesGui;
import mezz.jei.api.gui.IDrawable;
import mezz.jei.api.recipe.IRecipeCategory;
import mezz.jei.config.Constants;
import mezz.jei.config.KeyBindings;
import mezz.jei.input.IShowsRecipeFocuses;
import mezz.jei.input.InputHandler;
import mezz.jei.transfer.RecipeTransferUtil;
import mezz.jei.util.Log;
import mezz.jei.util.StringUtil;
import mezz.jei.util.Translator;
import net.minecraft.client.Minecraft;
Expand All @@ -16,7 +18,9 @@
import net.minecraft.client.renderer.GlStateManager;
import net.minecraft.entity.player.EntityPlayer;
import net.minecraft.inventory.Container;
import net.minecraft.item.ItemStack;
import net.minecraft.util.ResourceLocation;
import net.minecraftforge.fluids.Fluid;
import net.minecraftforge.fml.client.config.GuiButtonExt;
import net.minecraftforge.fml.client.config.HoverChecker;
import org.lwjgl.input.Mouse;
Expand All @@ -28,7 +32,7 @@
import java.util.ArrayList;
import java.util.List;

public class RecipesGui extends GuiScreen implements IShowsRecipeFocuses {
public class RecipesGui extends GuiScreen implements IRecipesGui, IShowsRecipeFocuses {
private static final int borderPadding = 6;
private static final int textPadding = 5;
private static final int buttonWidth = 13;
Expand Down Expand Up @@ -254,21 +258,45 @@ protected void keyTyped(char typedChar, int keyCode) throws IOException {
}
}

public boolean isOpen() {
return mc.currentScreen == this;
}

private void open() {
if (mc.currentScreen != this) {
if (!isOpen()) {
parentScreen = mc.currentScreen;
}
mc.displayGuiScreen(this);
}

private void close() {
if (parentScreen != null) {
mc.displayGuiScreen(parentScreen);
parentScreen = null;
} else {
mc.thePlayer.closeScreen();
public void close() {
if (isOpen()) {
if (parentScreen != null) {
mc.displayGuiScreen(parentScreen);
parentScreen = null;
} else {
mc.thePlayer.closeScreen();
}
logic.clearHistory();
}
}

@Override
public void showRecipes(@Nullable ItemStack focus) {
if (focus == null) {
Log.error("Null focus", new NullPointerException());
return;
}
showRecipes(new Focus(focus));
}

@Override
public void showRecipes(@Nullable Fluid focus) {
if (focus == null) {
Log.error("Null focus", new NullPointerException());
return;
}
logic.clearHistory();
showRecipes(new Focus(focus));
}

public void showRecipes(@Nonnull Focus focus) {
Expand All @@ -278,14 +306,41 @@ public void showRecipes(@Nonnull Focus focus) {
}
}

@Override
public void showUses(@Nullable ItemStack focus) {
if (focus == null) {
Log.error("Null focus", new NullPointerException());
return;
}
showUses(new Focus(focus));
}

@Override
public void showUses(@Nullable Fluid focus) {
if (focus == null) {
Log.error("Null focus", new NullPointerException());
return;
}
showUses(new Focus(focus));
}

public void showUses(@Nonnull Focus focus) {
focus.setMode(Focus.Mode.INPUT);
if (logic.setFocus(focus)) {
open();
}
}

public void showCategories(@Nonnull List<String> recipeCategoryUids) {
@Override
public void showCategories(@Nullable List<String> recipeCategoryUids) {
if (recipeCategoryUids == null) {
Log.error("Null recipeCategoryUids", new NullPointerException());
return;
}
if (recipeCategoryUids.isEmpty()) {
Log.error("Empty recipeCategoryUids", new IllegalArgumentException());
return;
}
if (logic.setCategoryFocus(recipeCategoryUids)) {
open();
}
Expand Down

0 comments on commit 1779fc2

Please sign in to comment.