Skip to content

Commit

Permalink
Focus the previous recipe if it's still visible when loading usage re… (
Browse files Browse the repository at this point in the history
#263)

* Focus the previous recipe if it's still visible when loading usage recipes.

* Reduced code duplication by extracting getRecipeHandlers

* Moved RecipeHandlerQuery to its own package-private file

* Refactored RecipeHandlerQuery methods

* Saving the current recipe when left clicking a machine's "Recipes" button

* Restored prevScreen so we can restore it with overlay

* Genericized GuiRecipe so that currentHandlers is no longer hidden in subclasses

* Cleaned up overlay handling

* Fixed trivial IDE warnings & bad JavaDoc on files I touched
  • Loading branch information
YannickMG authored Jun 29, 2022
1 parent f7a900e commit 5bbf275
Show file tree
Hide file tree
Showing 15 changed files with 177 additions and 201 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -150,7 +150,7 @@ public String getRecipeName() {
}

@Override
public List<String> handleTooltip(GuiRecipe gui, List<String> currenttip, int recipe) {
public List<String> handleTooltip(GuiRecipe<?> gui, List<String> currenttip, int recipe) {
currenttip = super.handleTooltip(gui, currenttip, recipe);
Point mousepos = GuiDraw.getMousePosition();
Point relMouse = new Point(mousepos.x - gui.guiLeft, mousepos.y - gui.guiTop);
Expand Down
96 changes: 23 additions & 73 deletions src/main/java/codechicken/nei/recipe/GuiCraftingRecipe.java
Original file line number Diff line number Diff line change
@@ -1,88 +1,46 @@
package codechicken.nei.recipe;

import codechicken.core.TaskProfiler;
import codechicken.nei.ItemList;
import codechicken.nei.ItemPanels;
import codechicken.nei.NEIClientConfig;
import codechicken.nei.NEIClientUtils;
import codechicken.nei.PositionedStack;
import java.util.ArrayList;
import java.util.List;
import java.util.concurrent.ExecutionException;
import java.util.stream.Collectors;
import net.minecraft.client.Minecraft;
import net.minecraft.client.gui.GuiScreen;
import net.minecraft.entity.player.EntityPlayer;
import net.minecraft.item.ItemStack;
import net.minecraft.util.ChatComponentTranslation;
import net.minecraft.util.EnumChatFormatting;
import net.minecraft.util.IChatComponent;

public class GuiCraftingRecipe extends GuiRecipe {
public class GuiCraftingRecipe extends GuiRecipe<ICraftingHandler> {
public static ArrayList<ICraftingHandler> craftinghandlers = new ArrayList<>();
public static ArrayList<ICraftingHandler> serialCraftingHandlers = new ArrayList<>();

public static boolean openRecipeGui(String outputId, Object... results) {
return openRecipeGui(outputId, false, results);
}

public static boolean openRecipeGui(String outputId, Boolean overlay, Object... results) {
Minecraft mc = NEIClientUtils.mc();
GuiScreen prevscreen = mc.currentScreen; // instanceof GuiContainer ? (GuiContainer) mc.currentScreen : null;

ArrayList<ICraftingHandler> handlers;
TaskProfiler profiler = ProfilerRecipeHandler.getProfiler();
profiler.start("recipe.concurrent.crafting");

// Pre-find the fuels so we're not fighting over it
FuelRecipeHandler.findFuelsOnceParallel();

try {
handlers = serialCraftingHandlers.stream()
.map(h -> h.getRecipeHandler(outputId, results))
.filter(h -> h.numRecipes() > 0)
.collect(Collectors.toCollection(ArrayList::new));

handlers.addAll(ItemList.forkJoinPool
.submit(() -> craftinghandlers.parallelStream()
.map(h -> h.getRecipeHandler(outputId, results))
.filter(h -> h.numRecipes() > 0)
.collect(Collectors.toCollection(ArrayList::new)))
.get());
} catch (InterruptedException | ExecutionException e) {
e.printStackTrace();
EntityPlayer player = Minecraft.getMinecraft().thePlayer;
if (player != null) {
IChatComponent chat = new ChatComponentTranslation("nei.chat.recipe.error");
chat.getChatStyle().setColor(EnumChatFormatting.RED);
player.addChatComponentMessage(chat);
}
return false;
} finally {
profiler.end();
}

RecipeHandlerQuery<ICraftingHandler> recipeQuery = new RecipeHandlerQuery<>(
h -> h.getRecipeHandler(outputId, results), craftinghandlers, serialCraftingHandlers);
ArrayList<ICraftingHandler> handlers = recipeQuery.runWithProfiling("recipe.concurrent.crafting");
if (handlers.isEmpty()) return false;

handlers.sort(NEIClientConfig.HANDLER_COMPARATOR);
Minecraft mc = NEIClientUtils.mc();

BookmarkRecipeId recipeId = null;
BookmarkRecipeId recipeId = (NEIClientConfig.saveCurrentRecipeInBookmarksEnabled() && "item".equals(outputId))
? getRecipeId(mc.currentScreen, (ItemStack) results[0])
: getCurrentRecipe();

if ("item".equals(outputId)) {
recipeId = getRecipeId(prevscreen, (ItemStack) results[0]);
}
if (overlay && recipeId == null) return false;

GuiCraftingRecipe gui = new GuiCraftingRecipe(prevscreen, handlers, recipeId);
GuiCraftingRecipe gui = new GuiCraftingRecipe(handlers, recipeId);

mc.displayGuiScreen(gui);

if (NEIClientConfig.saveCurrentRecipeInBookmarksEnabled())
if (!NEIClientUtils.shiftKey() || overlay) {
gui.openTargetRecipe(gui.recipeId);
}
if (recipeId != null && (!NEIClientUtils.shiftKey() || overlay)) {
gui.openTargetRecipe(gui.recipeId);
}

if (overlay) {
if (!NEIClientConfig.saveCurrentRecipeInBookmarksEnabled() || gui.recipeId == null) {
mc.displayGuiScreen(prevscreen);
return false;
}
gui.overlayRecipe(gui.recipeId.position);
}

Expand All @@ -92,8 +50,8 @@ public static boolean openRecipeGui(String outputId, Boolean overlay, Object...
protected static BookmarkRecipeId getRecipeId(GuiScreen gui, ItemStack stackover) {

if (gui instanceof GuiRecipe) {
final List<PositionedStack> ingredients = ((GuiRecipe) gui).getFocusedRecipeIngredients();
final String handlerName = ((GuiRecipe) gui).getHandlerName();
final List<PositionedStack> ingredients = ((GuiRecipe<?>) gui).getFocusedRecipeIngredients();
final String handlerName = ((GuiRecipe<?>) gui).getHandlerName();

if (ingredients != null && !ingredients.isEmpty()) {
return new BookmarkRecipeId(handlerName, ingredients);
Expand All @@ -103,16 +61,12 @@ protected static BookmarkRecipeId getRecipeId(GuiScreen gui, ItemStack stackover
return ItemPanels.bookmarkPanel.getBookmarkRecipeId(stackover);
}

private GuiCraftingRecipe(GuiScreen prevgui, ArrayList<ICraftingHandler> handlers, BookmarkRecipeId recipeId) {
this(prevgui, handlers);
private GuiCraftingRecipe(ArrayList<ICraftingHandler> handlers, BookmarkRecipeId recipeId) {
super(NEIClientUtils.mc().currentScreen);
this.currenthandlers = handlers;
this.recipeId = recipeId;
}

private GuiCraftingRecipe(GuiScreen prevgui, ArrayList<ICraftingHandler> handlers) {
super(prevgui);
currenthandlers = handlers;
}

public static void registerRecipeHandler(ICraftingHandler handler) {
final String handlerId = handler.getHandlerId();
if (craftinghandlers.stream().anyMatch(h -> h.getHandlerId().equals(handlerId))
Expand All @@ -123,12 +77,8 @@ public static void registerRecipeHandler(ICraftingHandler handler) {
else craftinghandlers.add(handler);
}

public ArrayList<? extends IRecipeHandler> getCurrentRecipeHandlers() {
@Override
public ArrayList<ICraftingHandler> getCurrentRecipeHandlers() {
return currenthandlers;
}

public ArrayList<ICraftingHandler> currenthandlers;

public static ArrayList<ICraftingHandler> craftinghandlers = new ArrayList<>();
public static ArrayList<ICraftingHandler> serialCraftingHandlers = new ArrayList<>();
}
26 changes: 19 additions & 7 deletions src/main/java/codechicken/nei/recipe/GuiRecipe.java
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,7 @@
import java.util.regex.Matcher;
import java.util.stream.Collectors;
import java.util.stream.IntStream;
import net.minecraft.client.Minecraft;
import net.minecraft.client.gui.GuiButton;
import net.minecraft.client.gui.GuiScreen;
import net.minecraft.client.gui.inventory.GuiContainer;
Expand All @@ -38,7 +39,7 @@
import org.lwjgl.input.Keyboard;
import org.lwjgl.opengl.GL11;

public abstract class GuiRecipe extends GuiContainer
public abstract class GuiRecipe<H extends IRecipeHandler> extends GuiContainer
implements IGuiContainerOverlay,
IGuiClientSide,
IGuiHandleMouseWheel,
Expand Down Expand Up @@ -66,7 +67,7 @@ public abstract class GuiRecipe extends GuiContainer
final DrawableResource bgBottom =
new DrawableBuilder("nei:textures/gui/recipebg.png", 0, BG_BOTTOM_Y, 176, BG_BOTTOM_HEIGHT).build();

public ArrayList<? extends IRecipeHandler> currenthandlers = new ArrayList<>();
public ArrayList<H> currenthandlers = new ArrayList<>();

public int page;
public int recipetype;
Expand All @@ -80,13 +81,13 @@ public abstract class GuiRecipe extends GuiContainer
private GuiButton nexttype;
private GuiButton prevtype;

private int OVERLAY_BUTTON_ID_START = 4;
private final int OVERLAY_BUTTON_ID_START = 4;
private GuiButton[] overlayButtons;

private final Rectangle area = new Rectangle();
private final GuiRecipeTabs recipeTabs;
private final GuiRecipeCatalyst guiRecipeCatalyst;
private IRecipeHandler handler;
private H handler;
private HandlerInfo handlerInfo;

private int yShift = 0;
Expand Down Expand Up @@ -236,7 +237,7 @@ public void openTargetRecipe(BookmarkRecipeId recipeId) {
if (recipeId.handlerName != null) {

for (int j = 0; j < currenthandlers.size(); j++) {
IRecipeHandler localHandler = currenthandlers.get(j);
H localHandler = currenthandlers.get(j);
HandlerInfo localHandlerInfo = GuiRecipeTab.getHandlerInfo(localHandler);

if (localHandlerInfo.getHandlerName().equals(recipeId.handlerName)) {
Expand Down Expand Up @@ -293,7 +294,7 @@ public String getHandlerName() {
return handlerInfo.getHandlerName();
}

public IRecipeHandler getHandler() {
public H getHandler() {
return handler;
}

Expand Down Expand Up @@ -686,7 +687,7 @@ public Point getRecipePosition(int recipe) {
return new Point(5, 32 + yShift + ((recipe % getRecipesPerPage()) * handlerInfo.getHeight()));
}

public abstract ArrayList<? extends IRecipeHandler> getCurrentRecipeHandlers();
public abstract ArrayList<H> getCurrentRecipeHandlers();

@Override
public VisiblityData modifyVisiblity(GuiContainer gui, VisiblityData currentVisibility) {
Expand All @@ -713,4 +714,15 @@ public boolean hideItemPanelSlot(GuiContainer gui, int x, int y, int w, int h) {
// Because some of the handlers *cough avaritia* are oversized
return area.intersects(x, y, w, h);
}

static BookmarkRecipeId getCurrentRecipe() {
Minecraft mc = NEIClientUtils.mc();
if (mc.currentScreen instanceof GuiRecipe) {
GuiRecipe<?> gui = (GuiRecipe<?>) mc.currentScreen;
return new BookmarkRecipeId(
gui.handlerInfo.getHandlerName(),
gui.getHandler().getIngredientStacks(gui.page * gui.getRecipesPerPage()));
}
return null;
}
}
6 changes: 3 additions & 3 deletions src/main/java/codechicken/nei/recipe/GuiRecipeCatalyst.java
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@
import org.lwjgl.util.Rectangle;

public class GuiRecipeCatalyst extends INEIGuiAdapter {
private GuiRecipe guiRecipe;
private GuiRecipe<?> guiRecipe;
public static final int ingredientSize = 16;
public static final int ingredientBorder = 1;
public static final int tabBorder = 5;
Expand All @@ -18,7 +18,7 @@ public class GuiRecipeCatalyst extends INEIGuiAdapter {
private static final Rectangle catalystRect = new Rectangle();
private static final Rectangle targetRect = new Rectangle();

public GuiRecipeCatalyst(GuiRecipe guiRecipe) {
public GuiRecipeCatalyst(GuiRecipe<?> guiRecipe) {
this.guiRecipe = guiRecipe;
}

Expand Down Expand Up @@ -49,7 +49,7 @@ public void draw() {
@Override
public boolean hideItemPanelSlot(GuiContainer gui, int x, int y, int w, int h) {
if (!(gui instanceof GuiRecipe)) return false;
guiRecipe = (GuiRecipe) gui;
guiRecipe = (GuiRecipe<?>) gui;
int catalystsSize =
RecipeCatalysts.getRecipeCatalysts(guiRecipe.getHandler()).size();
if (catalystsSize == 0) return false;
Expand Down
4 changes: 2 additions & 2 deletions src/main/java/codechicken/nei/recipe/GuiRecipeTab.java
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,7 @@ public abstract class GuiRecipeTab extends Widget {
public static HashMap<String, HandlerInfo> handlerAdderFromIMC = new HashMap<>();
public static Set<String> handlerRemoverFromIMC = new HashSet<>();

private final GuiRecipe guiRecipe;
private final GuiRecipe<?> guiRecipe;
private final IRecipeHandler handler;
private final String handlerName;
private final String handlerID;
Expand All @@ -54,7 +54,7 @@ public abstract class GuiRecipeTab extends Widget {

protected abstract int getForegroundIconY();

public GuiRecipeTab(GuiRecipe guiRecipe, IRecipeHandler handler, int x, int y) {
public GuiRecipeTab(GuiRecipe<?> guiRecipe, IRecipeHandler handler, int x, int y) {
super();
this.x = x;
this.y = y;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ public class GuiRecipeTabCreative extends GuiRecipeTab {
private static final DrawableResource unselectedTabImage =
new DrawableBuilder("minecraft:textures/gui/container/creative_inventory/tabs.png", 28, 0, 28, 30).build();

public GuiRecipeTabCreative(GuiRecipe guiRecipe, IRecipeHandler handler, int x, int y) {
public GuiRecipeTabCreative(GuiRecipe<?> guiRecipe, IRecipeHandler handler, int x, int y) {
super(guiRecipe, handler, x, y);
}

Expand Down
2 changes: 1 addition & 1 deletion src/main/java/codechicken/nei/recipe/GuiRecipeTabJEI.java
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ public class GuiRecipeTabJEI extends GuiRecipeTab {
private static final DrawableResource unselectedTabImage =
new DrawableBuilder("nei:textures/nei_tabbed_sprites.png", 24, 16, 24, 24).build();

public GuiRecipeTabJEI(GuiRecipe guiRecipe, IRecipeHandler handler, int x, int y) {
public GuiRecipeTabJEI(GuiRecipe<?> guiRecipe, IRecipeHandler handler, int x, int y) {
super(guiRecipe, handler, x, y);
}

Expand Down
8 changes: 3 additions & 5 deletions src/main/java/codechicken/nei/recipe/GuiRecipeTabs.java
Original file line number Diff line number Diff line change
Expand Up @@ -8,12 +8,11 @@
import java.util.List;

public class GuiRecipeTabs {
private final GuiRecipe guiRecipe;
private final GuiRecipe<?> guiRecipe;
private final List<GuiRecipeTab> tabs = new ArrayList<>();
private final List<Button> buttons = new ArrayList<>();

private final Rectangle area = new Rectangle();
private boolean creative_tab_style;

private int pageCount = 1;
private int pageNumber = 0;
Expand All @@ -22,13 +21,12 @@ public class GuiRecipeTabs {
private int tabWidth;
private int tabHeight;

public GuiRecipeTabs(GuiRecipe guiRecipe) {
public GuiRecipeTabs(GuiRecipe<?> guiRecipe) {
this.guiRecipe = guiRecipe;
}

public void initLayout() {
creative_tab_style = NEIClientConfig.useCreativeTabStyle();
if (creative_tab_style) {
if (NEIClientConfig.useCreativeTabStyle()) {
tabWidth = GuiRecipeTabCreative.TAB_WIDTH;
tabHeight = GuiRecipeTabCreative.TAB_HEIGHT;
} else {
Expand Down
Loading

0 comments on commit 5bbf275

Please sign in to comment.