Skip to content

Commit

Permalink
Close #536 Expose "itemstack under mouse" internal API
Browse files Browse the repository at this point in the history
  • Loading branch information
mezz committed Nov 4, 2016
1 parent 0c64e94 commit 25aa7ea
Show file tree
Hide file tree
Showing 6 changed files with 94 additions and 2 deletions.
23 changes: 22 additions & 1 deletion src/main/java/mezz/jei/JeiRuntime.java
@@ -1,21 +1,29 @@
package mezz.jei;

import java.util.ArrayList;
import java.util.List;

import mezz.jei.api.IJeiRuntime;
import mezz.jei.api.gui.IAdvancedGuiHandler;
import mezz.jei.gui.ItemListOverlay;
import mezz.jei.gui.recipes.RecipesGui;
import net.minecraft.client.gui.GuiScreen;
import net.minecraft.client.gui.inventory.GuiContainer;

public class JeiRuntime implements IJeiRuntime {

private final RecipeRegistry recipeRegistry;
private final ItemListOverlay itemListOverlay;
private final RecipesGui recipesGui;
private final IngredientRegistry ingredientRegistry;
private final List<IAdvancedGuiHandler<?>> advancedGuiHandlers;

public JeiRuntime(RecipeRegistry recipeRegistry, ItemListOverlay itemListOverlay, RecipesGui recipesGui, IngredientRegistry ingredientRegistry) {
public JeiRuntime(RecipeRegistry recipeRegistry, ItemListOverlay itemListOverlay, RecipesGui recipesGui, IngredientRegistry ingredientRegistry, List<IAdvancedGuiHandler<?>> advancedGuiHandlers) {
this.recipeRegistry = recipeRegistry;
this.itemListOverlay = itemListOverlay;
this.recipesGui = recipesGui;
this.ingredientRegistry = ingredientRegistry;
this.advancedGuiHandlers = advancedGuiHandlers;
}

public void close() {
Expand Down Expand Up @@ -45,4 +53,17 @@ public RecipesGui getRecipesGui() {
public IngredientRegistry getIngredientRegistry() {
return ingredientRegistry;
}

public List<IAdvancedGuiHandler<?>> getActiveAdvancedGuiHandlers(GuiScreen guiScreen) {
List<IAdvancedGuiHandler<?>> activeAdvancedGuiHandler = new ArrayList<IAdvancedGuiHandler<?>>();
if (guiScreen instanceof GuiContainer) {
for (IAdvancedGuiHandler<?> advancedGuiHandler : advancedGuiHandlers) {
Class<?> guiContainerClass = advancedGuiHandler.getGuiContainerClass();
if (guiContainerClass.isInstance(guiScreen)) {
activeAdvancedGuiHandler.add(advancedGuiHandler);
}
}
}
return activeAdvancedGuiHandler;
}
}
2 changes: 1 addition & 1 deletion src/main/java/mezz/jei/JeiStarter.java
Expand Up @@ -59,7 +59,7 @@ public void start(List<IModPlugin> plugins, boolean showProgressBar) {
List<IAdvancedGuiHandler<?>> advancedGuiHandlers = modRegistry.getAdvancedGuiHandlers();
ItemListOverlay itemListOverlay = new ItemListOverlay(itemFilter, advancedGuiHandlers, ingredientRegistry);
RecipesGui recipesGui = new RecipesGui(recipeRegistry);
JeiRuntime jeiRuntime = new JeiRuntime(recipeRegistry, itemListOverlay, recipesGui, ingredientRegistry);
JeiRuntime jeiRuntime = new JeiRuntime(recipeRegistry, itemListOverlay, recipesGui, ingredientRegistry, advancedGuiHandlers);
Internal.setRuntime(jeiRuntime);
Log.info("Built runtime in {} ms", System.currentTimeMillis() - start_time);

Expand Down
21 changes: 21 additions & 0 deletions src/main/java/mezz/jei/api/gui/BlankAdvancedGuiHandler.java
@@ -0,0 +1,21 @@
package mezz.jei.api.gui;

import javax.annotation.Nullable;
import java.awt.Rectangle;
import java.util.List;

import net.minecraft.client.gui.inventory.GuiContainer;

public abstract class BlankAdvancedGuiHandler implements IAdvancedGuiHandler {
@Nullable
@Override
public List<Rectangle> getGuiExtraAreas(GuiContainer guiContainer) {
return null;
}

@Nullable
@Override
public Object getIngredientUnderMouse(int mouseX, int mouseY) {
return null;
}
}
17 changes: 17 additions & 0 deletions src/main/java/mezz/jei/api/gui/IAdvancedGuiHandler.java
Expand Up @@ -5,11 +5,13 @@
import java.util.List;

import mezz.jei.api.IModRegistry;
import mezz.jei.api.ingredients.IModIngredientRegistration;
import net.minecraft.client.gui.inventory.GuiContainer;

/**
* Allows plugins to change how JEI is displayed next to their mod's guis.
* Register your implementation with {@link IModRegistry#addAdvancedGuiHandlers(IAdvancedGuiHandler[])}.
* @see BlankAdvancedGuiHandler
*/
public interface IAdvancedGuiHandler<T extends GuiContainer> {
/**
Expand All @@ -25,4 +27,19 @@ public interface IAdvancedGuiHandler<T extends GuiContainer> {
*/
@Nullable
List<Rectangle> getGuiExtraAreas(T guiContainer);

/**
* Return anything under the mouse that JEI could not normally detect, used for JEI recipe lookups.
* <p>
* This is useful for guis that don't have normal slots (which is how JEI normally detects items under the mouse).
* <p>
* This can also be used to let JEI look up liquids in tanks directly, by returning a FluidStack.
* Works with any ingredient type that has been registered with {@link IModIngredientRegistration}.
*
* @param mouseX the current X position of the mouse in screen coordinates.
* @param mouseY the current Y position of the mouse in screen coordinates.
* @since JEI 3.13.2
*/
@Nullable
Object getIngredientUnderMouse(int mouseX, int mouseY);
}
21 changes: 21 additions & 0 deletions src/main/java/mezz/jei/input/GuiContainerWrapper.java
@@ -1,7 +1,11 @@
package mezz.jei.input;

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

import mezz.jei.Internal;
import mezz.jei.JeiRuntime;
import mezz.jei.api.gui.IAdvancedGuiHandler;
import net.minecraft.client.Minecraft;
import net.minecraft.client.gui.GuiScreen;
import net.minecraft.client.gui.inventory.GuiContainer;
Expand All @@ -24,6 +28,23 @@ public IClickedIngredient<?> getIngredientUnderMouse(int mouseX, int mouseY) {
return new ClickedIngredient<ItemStack>(stack);
}
}

JeiRuntime runtime = Internal.getRuntime();
if (runtime != null) {
List<IAdvancedGuiHandler<?>> activeAdvancedGuiHandlers = runtime.getActiveAdvancedGuiHandlers(guiScreen);
for (IAdvancedGuiHandler advancedGuiHandler : activeAdvancedGuiHandlers) {
Object clicked;
try {
clicked = advancedGuiHandler.getIngredientUnderMouse(mouseX, mouseY);
} catch (AbstractMethodError ignored) { // legacy
continue;
}
if (clicked != null) {
return new ClickedIngredient<Object>(clicked);
}
}
}

return null;
}

Expand Down
12 changes: 12 additions & 0 deletions src/main/java/mezz/jei/plugins/jei/JEIInternalPlugin.java
Expand Up @@ -29,6 +29,9 @@
import net.minecraft.client.gui.inventory.GuiBrewingStand;
import net.minecraft.init.Items;
import net.minecraft.item.ItemStack;
import net.minecraftforge.fluids.Fluid;
import net.minecraftforge.fluids.FluidRegistry;
import net.minecraftforge.fluids.FluidStack;

@JEIPlugin
public class JEIInternalPlugin extends BlankModPlugin {
Expand Down Expand Up @@ -95,6 +98,15 @@ public List<Rectangle> getGuiExtraAreas(GuiBrewingStand guiContainer) {
new Rectangle(guiContainer.guiLeft + guiContainer.xSize, guiContainer.guiTop + 40, size, size)
);
}

@Nullable
@Override
public Object getIngredientUnderMouse(int mouseX, int mouseY) {
if (mouseX < 10 && mouseY < 10) {
return new FluidStack(FluidRegistry.WATER, Fluid.BUCKET_VOLUME);
}
return null;
}
});
}
}
Expand Down

0 comments on commit 25aa7ea

Please sign in to comment.