Skip to content

Commit

Permalink
Change read recipeIndex from GuiRecipe (#38)
Browse files Browse the repository at this point in the history
Co-authored-by: slprime <[email protected]>
  • Loading branch information
slprime and slprime authored Jan 26, 2024
1 parent b4ecadf commit b60727c
Show file tree
Hide file tree
Showing 2 changed files with 25 additions and 13 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -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;
}
Expand Down Expand Up @@ -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<Integer> 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();
}
Expand Down
22 changes: 15 additions & 7 deletions src/main/java/com/github/vfyjxf/nee/nei/NEECraftingHelper.java
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -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<Integer> 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);
}
Expand Down

0 comments on commit b60727c

Please sign in to comment.