From b60727c5f6af75c73bd4bcad15b60a2bb19c264c Mon Sep 17 00:00:00 2001 From: slprime <31038811+slprime@users.noreply.github.com> Date: Fri, 26 Jan 2024 22:26:40 +0200 Subject: [PATCH] Change read recipeIndex from GuiRecipe (#38) Co-authored-by: slprime --- .../nee/client/NEEContainerDrawHandler.java | 16 +++++++++----- .../vfyjxf/nee/nei/NEECraftingHelper.java | 22 +++++++++++++------ 2 files changed, 25 insertions(+), 13 deletions(-) diff --git a/src/main/java/com/github/vfyjxf/nee/client/NEEContainerDrawHandler.java b/src/main/java/com/github/vfyjxf/nee/client/NEEContainerDrawHandler.java index 05b89ad..feac40e 100644 --- a/src/main/java/com/github/vfyjxf/nee/client/NEEContainerDrawHandler.java +++ b/src/main/java/com/github/vfyjxf/nee/client/NEEContainerDrawHandler.java @@ -52,7 +52,7 @@ public class NEEContainerDrawHandler { private int oldType; public Field overlayButtonsField; - public Method recipesPerPageMethod; + public Method getRecipeIndicesMethod; private boolean drawRequestTooltip; private boolean drawMissingTooltip; private boolean drawCraftableTooltip; @@ -62,9 +62,9 @@ public NEEContainerDrawHandler() { isGtnhNei = true; try { this.overlayButtonsField = GuiRecipe.class.getDeclaredField("overlayButtons"); - this.recipesPerPageMethod = GuiRecipe.class.getDeclaredMethod("getRecipesPerPage"); + this.getRecipeIndicesMethod = GuiRecipe.class.getDeclaredMethod("getRecipeIndices"); this.overlayButtonsField.setAccessible(true); - this.recipesPerPageMethod.setAccessible(true); + this.getRecipeIndicesMethod.setAccessible(true); } catch (NoSuchMethodException | NoSuchFieldException e) { isGtnhNei = false; } @@ -201,9 +201,13 @@ private void initIngredientTracker(GuiRecipe gui) { int recipeIndex = -1; if (isGtnhNei) { try { - int OVERLAY_BUTTON_ID_START = 4; - recipeIndex = gui.page * (int) recipesPerPageMethod.invoke(gui) + overlayButton.id - - OVERLAY_BUTTON_ID_START; + final int OVERLAY_BUTTON_ID_START = 4; + final List indices = (List) getRecipeIndicesMethod.invoke(gui); + final int refIndex = overlayButton.id - OVERLAY_BUTTON_ID_START; + + if (refIndex >= 0 && refIndex < indices.size()) { + recipeIndex = indices.get(refIndex); + } } catch (IllegalAccessException | InvocationTargetException e) { e.printStackTrace(); } diff --git a/src/main/java/com/github/vfyjxf/nee/nei/NEECraftingHelper.java b/src/main/java/com/github/vfyjxf/nee/nei/NEECraftingHelper.java index 8f79254..067de5c 100644 --- a/src/main/java/com/github/vfyjxf/nee/nei/NEECraftingHelper.java +++ b/src/main/java/com/github/vfyjxf/nee/nei/NEECraftingHelper.java @@ -29,6 +29,7 @@ import org.lwjgl.input.Keyboard; import com.github.vfyjxf.nee.NotEnoughEnergistics; +import com.github.vfyjxf.nee.client.NEEContainerDrawHandler; import com.github.vfyjxf.nee.config.NEEConfig; import com.github.vfyjxf.nee.network.NEENetworkHandler; import com.github.vfyjxf.nee.network.packet.PacketOpenCraftAmount; @@ -274,26 +275,33 @@ public void onActionPerformedEventPre(GuiScreenEvent.ActionPerformedEvent.Pre ev boolean isPatternTerm = isPatternTerm(guiRecipe.firstGui); boolean isCraftingTerm = isGuiCraftingTerm(guiRecipe.firstGui); if (isCraftingTerm || isPatternTerm) { - int recipesPerPage = 2; IRecipeHandler handler = (IRecipeHandler) guiRecipe.currenthandlers.get(guiRecipe.recipetype); + int recipeIndex = -1; if (isGtnhNei) { try { - recipesPerPage = (int) ReflectionHelper - .findMethod(GuiRecipe.class, guiRecipe, new String[] { "getRecipesPerPage" }) + final List indices = (List) NEEContainerDrawHandler.instance.getRecipeIndicesMethod .invoke(guiRecipe); + final int refIndex = event.button.id - OVERLAY_BUTTON_ID_START; + + if (refIndex >= 0 && refIndex < indices.size()) { + recipeIndex = indices.get(refIndex); + } } catch (IllegalAccessException | InvocationTargetException e) { e.printStackTrace(); } + } else { + int recipesPerPage = 2; + recipeIndex = guiRecipe.page * recipesPerPage + event.button.id - OVERLAY_BUTTON_ID_START; } - if (recipesPerPage >= 0 && handler != null) { - int recipe = guiRecipe.page * recipesPerPage + event.button.id - OVERLAY_BUTTON_ID_START; + + if (handler != null && recipeIndex >= 0 && recipeIndex < handler.numRecipes()) { final IOverlayHandler overlayHandler = handler - .getOverlayHandler(guiRecipe.firstGui, recipe); + .getOverlayHandler(guiRecipe.firstGui, recipeIndex); Minecraft.getMinecraft().displayGuiScreen(guiRecipe.firstGui); overlayHandler.overlayRecipe( guiRecipe.firstGui, (IRecipeHandler) guiRecipe.currenthandlers.get(guiRecipe.recipetype), - recipe, + recipeIndex, NEIClientUtils.shiftKey()); event.setCanceled(true); }