diff --git a/src/main/java/codechicken/nei/BookmarkPanel.java b/src/main/java/codechicken/nei/BookmarkPanel.java index 9ffc7b71..78c1a33a 100644 --- a/src/main/java/codechicken/nei/BookmarkPanel.java +++ b/src/main/java/codechicken/nei/BookmarkPanel.java @@ -572,7 +572,7 @@ public class BookmarkPanel extends PanelWidget { return; } else if (this.sortableGroup != null) { - final int overRowIndex = grid.getRowIndex(mousey); + final int overRowIndex = Math.min(grid.getRowIndex(mousey), grid.getRows() - 1); if (overRowIndex >= 0 && this.sortableGroup.groupId != this.grid.getRowGroupId(overRowIndex)) { this.grid.moveGroup(this.sortableGroup, overRowIndex); @@ -1105,7 +1105,7 @@ public class BookmarkPanel extends PanelWidget { } if (super.onMouseWheel(shift, mousex, mousey)) { - final int overRowIndex = grid.getRowIndex(mousey); + final int overRowIndex = Math.min(grid.getRowIndex(mousey), grid.getRows() - 1); if (this.sortableGroup != null && overRowIndex >= 0 && this.sortableGroup.groupId != this.grid.getRowGroupId(overRowIndex)) { diff --git a/src/main/java/codechicken/nei/PositionedStack.java b/src/main/java/codechicken/nei/PositionedStack.java index f72c9766..ce7d5e37 100644 --- a/src/main/java/codechicken/nei/PositionedStack.java +++ b/src/main/java/codechicken/nei/PositionedStack.java @@ -361,7 +361,7 @@ public class PositionedStack implements Cloneable { return; } - FluidDrawer.drawFluid( + FluidDrawer.drawTank( this.relx, this.rely, this.width, diff --git a/src/main/java/codechicken/nei/SubsetWidget.java b/src/main/java/codechicken/nei/SubsetWidget.java index c5bed90a..06e2d84a 100644 --- a/src/main/java/codechicken/nei/SubsetWidget.java +++ b/src/main/java/codechicken/nei/SubsetWidget.java @@ -161,7 +161,7 @@ public class SubsetWidget extends Button implements ItemFilterProvider { // 1 - partitial hidden // 2 - enabled public int state = 2; - public int calculatedWidth; + public volatile int calculatedWidth = -1; public final List items; public final List children = new ArrayList<>(); protected final SubsetSlot slot = new SubsetSlot(); @@ -207,7 +207,7 @@ public class SubsetWidget extends Button implements ItemFilterProvider { this.state = 2; this.items.clear(); this.children.clear(); - this.calculatedWidth = 0; + this.calculatedWidth = -1; } public void updateVisiblity(int mx, int my) { @@ -262,6 +262,10 @@ public class SubsetWidget extends Button implements ItemFilterProvider { } public void resize(Rectangle4i screen, Rectangle4i parent, boolean dropRight) { + if (calculatedWidth < 0) { + calculatedWidth = this.children.stream().mapToInt(SubsetTag::nameWidth).max().orElse(0); + } + int height = Math.min(slot.contentHeight(), screen.h); int width = Math.max(calculatedWidth + 2, this.items.isEmpty() ? 0 : 16 + MARGIN * 2); int scrollbarWidth = slot.scrollbarDim().width; @@ -701,12 +705,12 @@ public class SubsetWidget extends Button implements ItemFilterProvider { for (SubsetTag tag : list) { tag.children.sort(Comparator.comparing(SubsetTag::displayName)); - tag.calculatedWidth = tag.children.stream().mapToInt(SubsetTag::nameWidth).max().orElse(0); + tag.calculatedWidth = -1; } root.children.removeIf(child -> child.children.isEmpty() && child.items.isEmpty()); root.children.sort(Comparator.comparing(SubsetTag::displayName)); - root.calculatedWidth = root.children.stream().mapToInt(SubsetTag::nameWidth).max().orElse(0); + root.calculatedWidth = -1; calculateVisibility(root); } catch (Throwable e) { diff --git a/src/main/java/codechicken/nei/api/API.java b/src/main/java/codechicken/nei/api/API.java index dd5af1f1..a1804215 100644 --- a/src/main/java/codechicken/nei/api/API.java +++ b/src/main/java/codechicken/nei/api/API.java @@ -12,6 +12,7 @@ import net.minecraft.client.gui.inventory.GuiContainer; import net.minecraft.inventory.Slot; import net.minecraft.item.Item; import net.minecraft.item.ItemStack; +import net.minecraftforge.fluids.Fluid; import org.lwjgl.input.Keyboard; @@ -33,6 +34,7 @@ import codechicken.nei.SubsetWidget.SubsetTag; import codechicken.nei.api.IRecipeFilter.IRecipeFilterProvider; import codechicken.nei.api.ItemFilter.ItemFilterProvider; import codechicken.nei.config.Option; +import codechicken.nei.item.FluidDrawer; import codechicken.nei.recipe.CatalystInfo; import codechicken.nei.recipe.GuiCraftingRecipe; import codechicken.nei.recipe.GuiRecipe; @@ -375,6 +377,16 @@ public class API { StackInfo.stackStringifyHandlers.add(handler); } + /** + * Replaces the default rendering of a fluid in NEI fluid display slots and fluid tanks. + * + * @param fluid the fluid to render + * @param renderer the renderer, or null to restore the default rendering + */ + public static void registerFluidRenderer(Fluid fluid, IFluidRenderer renderer) { + FluidDrawer.registerFluidRenderer(fluid, renderer); + } + /** * Adds an association between an ingredient and what it can craft. (i.e. Furnace ItemStack -> Smelting and Fuel * Recipes) Allows players to see what ingredient they need to craft in order to make recipes from a recipe diff --git a/src/main/java/codechicken/nei/api/IFluidRenderer.java b/src/main/java/codechicken/nei/api/IFluidRenderer.java new file mode 100644 index 00000000..a9087eac --- /dev/null +++ b/src/main/java/codechicken/nei/api/IFluidRenderer.java @@ -0,0 +1,17 @@ +package codechicken.nei.api; + +import net.minecraftforge.fluids.Fluid; + +/** + * Custom renderer for a fluid, used by NEI fluid display slots and fluid tanks in recipes. + *

+ * Register with {@link API#registerFluidRenderer(Fluid, IFluidRenderer)}. + */ +public interface IFluidRenderer { + + /** + * Renders one 16x16 tile of the fluid. Partial tiles of a tank are clipped by NEI. The amount overlay is drawn by + * NEI. + */ + void renderFluid(Fluid fluid, int x, int y); +} diff --git a/src/main/java/codechicken/nei/bookmark/BookmarkGrid.java b/src/main/java/codechicken/nei/bookmark/BookmarkGrid.java index 942fcfa8..ecb7c4c1 100644 --- a/src/main/java/codechicken/nei/bookmark/BookmarkGrid.java +++ b/src/main/java/codechicken/nei/bookmark/BookmarkGrid.java @@ -799,6 +799,7 @@ public class BookmarkGrid extends ItemsGrid> 16 & 0xFF) / 255F; - final float green = (color >> 8 & 0xFF) / 255F; - final float blue = (color & 0xFF) / 255F; - - Minecraft.getMinecraft().getTextureManager().bindTexture(TextureMap.locationBlocksTexture); - GL11.glColor4f(red, green, blue, 1F); - GL11.glEnable(GL11.GL_BLEND); - OpenGlHelper - .glBlendFunc(GL11.GL_SRC_ALPHA, GL11.GL_ONE_MINUS_SRC_ALPHA, GL11.GL_ONE, GL11.GL_ONE_MINUS_SRC_ALPHA); - - final Tessellator tessellator = Tessellator.instance; - tessellator.startDrawingQuads(); - tessellator.addVertexWithUV(0, 16, 0, icon.getMinU(), icon.getMaxV()); - tessellator.addVertexWithUV(16, 16, 0, icon.getMaxU(), icon.getMaxV()); - tessellator.addVertexWithUV(16, 0, 0, icon.getMaxU(), icon.getMinV()); - tessellator.addVertexWithUV(0, 0, 0, icon.getMinU(), icon.getMinV()); - tessellator.draw(); - - GL11.glColor4f(1F, 1F, 1F, 1F); - GL11.glDisable(GL11.GL_BLEND); + private void renderIcon(ItemStack item) { + final Fluid fluid = FluidRegistry.getFluid(item.getItemDamage()); + FluidDrawer.drawFluidArea(fluid != null ? fluid : FluidRegistry.WATER, 0, 0, 16, 16, false, FillDirection.UP); } private void renderAmountOverlay(ItemStack item, ItemFluidDisplay fluidDisplay) { diff --git a/src/main/java/codechicken/nei/item/FluidDrawer.java b/src/main/java/codechicken/nei/item/FluidDrawer.java index 78886959..2e8a6bce 100644 --- a/src/main/java/codechicken/nei/item/FluidDrawer.java +++ b/src/main/java/codechicken/nei/item/FluidDrawer.java @@ -1,14 +1,21 @@ package codechicken.nei.item; +import java.util.IdentityHashMap; +import java.util.Map; + import net.minecraft.client.Minecraft; +import net.minecraft.client.renderer.OpenGlHelper; import net.minecraft.client.renderer.Tessellator; import net.minecraft.client.renderer.texture.TextureMap; import net.minecraft.util.IIcon; import net.minecraftforge.fluids.Fluid; +import net.minecraftforge.fluids.FluidRegistry; import net.minecraftforge.fluids.FluidStack; import org.lwjgl.opengl.GL11; +import codechicken.nei.api.IFluidRenderer; +import codechicken.nei.scroll.GuiHelper; import cpw.mods.fml.relauncher.Side; import cpw.mods.fml.relauncher.SideOnly; @@ -27,28 +34,32 @@ public class FluidDrawer { return this == LEFT || this == RIGHT; } - public FillDirection resolve(FluidStack fluidStack) { + public FillDirection resolve(Fluid fluid) { if (this != AUTO) return this; - return fluidStack.getFluid().isGaseous(fluidStack) ? DOWN : UP; + return fluid.isGaseous() ? DOWN : UP; } } + private static final Map fluidRenderers = new IdentityHashMap<>(); + private FluidDrawer() {} - public static void drawFluid(int x, int y, int width, int height, int capacity, FluidStack fluidStack, - boolean flowing, FillDirection direction) { - final Fluid fluid = fluidStack.getFluid(); - IIcon icon = flowing ? fluid.getFlowingIcon() : null; - - if (icon == null) { - icon = fluid.getIcon(fluidStack); + public static void registerFluidRenderer(Fluid fluid, IFluidRenderer renderer) { + if (renderer == null) { + fluidRenderers.remove(fluid); + } else { + fluidRenderers.put(fluid, renderer); } + } - if (icon == null || fluidStack.amount <= 0) { + public static void drawTank(int x, int y, int width, int height, int capacity, FluidStack fluidStack, + boolean flowing, FillDirection direction) { + if (fluidStack.amount <= 0) { return; } - direction = direction.resolve(fluidStack); + final Fluid fluid = fluidStack.getFluid(); + direction = direction.resolve(fluid); final int tankSize = direction.isHorizontal() ? width : height; final int tankCapacity = capacity > 0 ? capacity : fluidStack.amount; @@ -59,38 +70,109 @@ public class FluidDrawer { final int fillHeight = direction.isHorizontal() ? height : Math.min(fillSize, height); final int fillX = direction == FillDirection.LEFT ? x + width - fillWidth : x; final int fillY = direction == FillDirection.DOWN ? y : y + height - fillHeight; + + GL11.glDisable(GL11.GL_LIGHTING); + drawFluidArea(fluid, fillX, fillY, fillWidth, fillHeight, flowing, direction); + GL11.glEnable(GL11.GL_LIGHTING); + } + + /** + * Fills the area with fluid tiles. The tiles start from the side the fluid fills from, so a partial tile ends up at + * the fluid surface. + */ + public static void drawFluidArea(Fluid fluid, int x, int y, int width, int height, boolean flowing, + FillDirection direction) { + direction = direction.resolve(fluid); final boolean anchorRight = direction == FillDirection.LEFT; final boolean anchorBottom = direction != FillDirection.DOWN; + final IFluidRenderer renderer = fluidRenderers.get(fluid); - final int color = fluid.getColor(fluidStack); + if (renderer == null) { + drawDefaultFluid(fluid, x, y, width, height, flowing, anchorRight, anchorBottom); + } else { + drawCustomFluid(renderer, fluid, x, y, width, height, anchorRight, anchorBottom); + } + } + + private static void drawCustomFluid(IFluidRenderer renderer, Fluid fluid, int x, int y, int width, int height, + boolean anchorRight, boolean anchorBottom) { + GL11.glPushAttrib(GL11.GL_ENABLE_BIT | GL11.GL_COLOR_BUFFER_BIT | GL11.GL_LIGHTING_BIT); + GL11.glDisable(GL11.GL_LIGHTING); + + final boolean clip = width % 16 != 0 || height % 16 != 0; + + if (clip) { + GuiHelper.pushScissorFrame(x, y, width, height); + } + + try { + for (int tx = 0; tx < width; tx += 16) { + final int tileX = anchorRight ? x + width - tx - 16 : x + tx; + + for (int ty = 0; ty < height; ty += 16) { + final int tileY = anchorBottom ? y + height - ty - 16 : y + ty; + renderer.renderFluid(fluid, tileX, tileY); + } + } + } finally { + if (clip) { + GuiHelper.popScissorFrame(); + } + + GL11.glPopAttrib(); + GL11.glColor4f(1F, 1F, 1F, 1F); + } + } + + private static void drawDefaultFluid(Fluid fluid, int x, int y, int width, int height, boolean flowing, + boolean anchorRight, boolean anchorBottom) { + IIcon icon = flowing ? fluid.getFlowingIcon() : null; + + if (icon == null) { + icon = fluid.getStillIcon(); + } + + if (icon == null) { + icon = FluidRegistry.WATER.getStillIcon(); + } + + if (icon == null) { + return; + } + + final int color = fluid.getColor(); final float red = (color >> 16 & 0xFF) / 255F; final float green = (color >> 8 & 0xFF) / 255F; final float blue = (color & 0xFF) / 255F; Minecraft.getMinecraft().getTextureManager().bindTexture(TextureMap.locationBlocksTexture); - GL11.glColor3f(red, green, blue); + GL11.glColor4f(red, green, blue, 1F); GL11.glEnable(GL11.GL_BLEND); - GL11.glBlendFunc(GL11.GL_SRC_ALPHA, GL11.GL_ONE_MINUS_SRC_ALPHA); - GL11.glDisable(GL11.GL_LIGHTING); + OpenGlHelper + .glBlendFunc(GL11.GL_SRC_ALPHA, GL11.GL_ONE_MINUS_SRC_ALPHA, GL11.GL_ONE, GL11.GL_ONE_MINUS_SRC_ALPHA); + final double iconMinU = icon.getMinU(); + final double iconMaxU = icon.getMaxU(); + final double iconMinV = icon.getMinV(); + final double iconMaxV = icon.getMaxV(); final Tessellator tessellator = Tessellator.instance; tessellator.startDrawingQuads(); - for (int tx = 0; tx < fillWidth; tx += 16) { - final int tileWidth = Math.min(16, fillWidth - tx); + for (int tx = 0; tx < width; tx += 16) { + final int tileWidth = Math.min(16, width - tx); // a cropped tile keeps the texture edge that stays seamless against the neighbouring tile - final double uSize = (icon.getMaxU() - icon.getMinU()) * tileWidth / 16D; - final double uMin = anchorRight ? icon.getMaxU() - uSize : icon.getMinU(); - final double uMax = anchorRight ? icon.getMaxU() : icon.getMinU() + uSize; - final int leftX = anchorRight ? fillX + fillWidth - tx - tileWidth : fillX + tx; + final double uSize = (iconMaxU - iconMinU) * tileWidth / 16D; + final double uMin = anchorRight ? iconMaxU - uSize : iconMinU; + final double uMax = anchorRight ? iconMaxU : iconMinU + uSize; + final int leftX = anchorRight ? x + width - tx - tileWidth : x + tx; final int rightX = leftX + tileWidth; - for (int ty = 0; ty < fillHeight; ty += 16) { - final int tileHeight = Math.min(16, fillHeight - ty); - final double vSize = (icon.getMaxV() - icon.getMinV()) * tileHeight / 16D; - final double vMin = anchorBottom ? icon.getMaxV() - vSize : icon.getMinV(); - final double vMax = anchorBottom ? icon.getMaxV() : icon.getMinV() + vSize; - final int bottomY = anchorBottom ? fillY + fillHeight - ty : fillY + ty + tileHeight; + for (int ty = 0; ty < height; ty += 16) { + final int tileHeight = Math.min(16, height - ty); + final double vSize = (iconMaxV - iconMinV) * tileHeight / 16D; + final double vMin = anchorBottom ? iconMaxV - vSize : iconMinV; + final double vMax = anchorBottom ? iconMaxV : iconMinV + vSize; + final int bottomY = anchorBottom ? y + height - ty : y + ty + tileHeight; final int topY = bottomY - tileHeight; tessellator.addVertexWithUV(leftX, bottomY, 0, uMin, vMax); @@ -102,7 +184,6 @@ public class FluidDrawer { tessellator.draw(); GL11.glDisable(GL11.GL_BLEND); - GL11.glEnable(GL11.GL_LIGHTING); GL11.glColor4f(1F, 1F, 1F, 1F); } } diff --git a/src/main/java/codechicken/nei/item/ItemFluidDisplay.java b/src/main/java/codechicken/nei/item/ItemFluidDisplay.java index 72404cb1..4ca5d68d 100644 --- a/src/main/java/codechicken/nei/item/ItemFluidDisplay.java +++ b/src/main/java/codechicken/nei/item/ItemFluidDisplay.java @@ -73,7 +73,7 @@ public class ItemFluidDisplay extends Item implements IFluidContainerItem { @Override public FluidStack getFluid(ItemStack stack) { - if (stack == null || !(stack.getItem() instanceof ItemFluidDisplay) || !stack.hasTagCompound()) { + if (stack == null || !(stack.getItem() instanceof ItemFluidDisplay)) { return null; } @@ -86,7 +86,7 @@ public class ItemFluidDisplay extends Item implements IFluidContainerItem { final NBTTagCompound nbTag = stack.getTagCompound(); final FluidStack fluidStack = new FluidStack( fluid, - (int) Math.min(nbTag.getLong("neiFluidDisplayAmount"), Integer.MAX_VALUE)); + (int) Math.min(nbTag != null ? nbTag.getLong("neiFluidDisplayAmount") : 0, Integer.MAX_VALUE)); return fluidStack; } diff --git a/src/main/java/codechicken/nei/recipe/GuiRecipe.java b/src/main/java/codechicken/nei/recipe/GuiRecipe.java index 4ecea6b4..b311a449 100644 --- a/src/main/java/codechicken/nei/recipe/GuiRecipe.java +++ b/src/main/java/codechicken/nei/recipe/GuiRecipe.java @@ -8,7 +8,7 @@ import java.util.Collections; import java.util.LinkedList; import java.util.List; import java.util.Map; -import java.util.regex.Matcher; +import java.util.WeakHashMap; import net.minecraft.client.gui.GuiButton; import net.minecraft.client.gui.GuiScreen; @@ -295,73 +295,118 @@ public abstract class GuiRecipe extends GuiContainer i } /** - * Many old mods assumed a fixed NEI window height of {@code 166} pixels. Now that this is no longer the case, their - * tooltip and click zone handling is broken. This helper class fixes these old mods by hacking the {@link #height} - * property's value so that these old mods' calculations return the correct value for the new height. + * Many old mods assumed a fixed NEI window size of {@code 176x166} pixels, with exactly one recipe shown at a time, + * flush against the top-left corner of the window. Now that none of these assumptions hold (the window has a + * flexible size, several recipes can be stacked in the same scrollable list, and the recipe area is inset from the + * window's edges), these old mods' tooltip and click zone handling is broken. This helper class fixes these old + * mods by hacking the {@link #width}, {@link #height}, {@link #guiLeft} and {@link #guiTop} properties' values so + * that these old mods' calculations return the correct value for whichever recipe they are currently being asked + * about. * *

- * New and old mods often assume that the handler is only drawn when {@link GuiRecipe} is the currently active - * screen in {@link net.minecraft.client.Minecraft}. This is no longer true since the addition of recipe tooltips - * which need to render the recipe in other containers on mouse hover, this class temporarily switches the active - * screen to {@code this} while rendering them to avoid ClassCastExceptions and other similar crashes. + * Since each recipe handled by a {@link RecipeWidget} can sit at a different real position, this must be + * constructed with that widget's top-left edge every time a legacy entry point ({@code mouseClicked}, + * {@code handleTooltip}, ...) is invoked for it, rather than once per {@link GuiRecipe}. * *

* This class is an {@link AutoCloseable} so that it can be used with try-with-resources, which will ensure that - * {@link #height} and {@link net.minecraft.client.Minecraft#currentScreen} is returned to the correct value + * {@link #width}, {@link #height}, {@link #guiLeft} and {@link #guiTop} are returned to the correct value * afterwards. */ - private class CompatibilityHacks implements AutoCloseable { + public static final class CompatibilityHacks implements AutoCloseable { + // The historical NEI recipe screen's fixed size: a single top paging widget 16px tall, no horizontal inset, + // and a 176x166 box. Legacy handlers either read guiLeft/guiTop directly (expecting them to point 16px above + // their recipe, flush on the left) or recompute them from width/height with these same two constants. + private static final int LEGACY_TOP_GAP = 16; + private static final int LEGACY_BOX_WIDTH = 176; + private static final int LEGACY_BOX_HEIGHT = 166; + + // layoutWindow() places the recipe container at (guiLeft + 3, guiTop + 32); RecipePageManager then places + // each left-aligned recipe widget another 2px to the right of that. This is the inverse of both, undoing the + // container's own inset so a legacy handler sees its recipe flush with guiLeft as it expects. + private static final int CONTAINER_LEFT_INSET = 3 + 2; + + private final GuiRecipe gui; + private final int trueWidth; private final int trueHeight; + private final int trueGuiLeft; private final int trueGuiTop; - private CompatibilityHacks() { - trueHeight = height; - trueGuiTop = guiTop; + /** + * @param widget The recipe widget a legacy entry point ({@code mouseClicked}, {@code handleTooltip}, ...) is + * about to be invoked for. + */ + public CompatibilityHacks(GuiRecipe gui, RecipeWidget widget) { + this.gui = gui; + this.trueWidth = gui.width; + this.trueHeight = gui.height; + this.trueGuiLeft = gui.guiLeft; + this.trueGuiTop = gui.guiTop; - isHeightHackApplied = NEIClientConfig.heightHackHandlerRegex.stream() - .map(pattern -> pattern.matcher(handler.original.getHandlerId())).anyMatch(Matcher::matches); - if (isHeightHackApplied) { - // guiTop is the top edge of the recipe screen on the y-axis. Old recipe - // handlers expect a single paging - // widget at the top and at the bottom of the recipe screen with a height of - // 16px each, but GTNH NEI - // moved the bottom paging widget to the top following JEI's design, increasing - // the "forbidden" zone at - // the top to 32px. To fix these old recipe handlers we move guiTop down 16px so - // that they can keep - // working with the old 16px gap. - guiTop += 16; + final String handlerId = gui.handler.original.getHandlerId(); + gui.isHeightHackApplied = NEIClientConfig.heightHackHandlerRegex.stream() + .anyMatch(pattern -> pattern.matcher(handlerId).matches()); - // The old NEI recipe screen had a fixed width and height and was always - // centered on the screen. Legacy - // recipe handlers use the calculation ((height - 166) / 2) to compute the - // y-value of the top edge of - // the NEI window. GTNH NEI changes the layout so that the recipe screen length - // now has a flexible - // length and is no longer centered vertically. In order for the top-of-screen - // calculation to return the - // correct result, we have to hack the height field with an inverse of the - // calculation using the actual - // top of the recipe screen stored in guiTop. - height = (2 * guiTop) + 166; - - // For reference, in case we ever need to modify width as well: - // the legacy calculation used for width is ((width - 176) / 2), which should - // evaluate to be equal to - // guiWidth (the x-value of the left edge of the NEI recipe screen). So if we - // wanted to override width - // as well, we'd do this: - // width = (2 * guiWidth) + 176; + if (gui.isHeightHackApplied) { + gui.guiTop = recipeTop(widget) - LEGACY_TOP_GAP; + gui.guiLeft = widget.x - CONTAINER_LEFT_INSET; + gui.height = (2 * gui.guiTop) + LEGACY_BOX_HEIGHT; + gui.width = (2 * gui.guiLeft) + LEGACY_BOX_WIDTH; } } + // recipiesPerPage() == 2 is the IRecipeHandler default: a handler only ends up with this value if it never + // overrode the method, or if it overrode it and chose 2 on purpose. We only care about the latter case, so + // we require the override to actually come from the handler's own class rather than being inherited as-is. + private static final Map, Boolean> overridesRecipiesPerPage = new WeakHashMap<>(); + + /** + * The real top edge of this widget's recipe, as a legacy handler following the {@link #LEGACY_TOP_GAP} + * convention would expect to find it at guiTop. A few handlers (e.g. ImmersiveEngineering) are left over from + * the old {@code recipiesPerPage() == 2} convention, where two recipes shared one physical page: their own + * {@code mouseClicked}/{@code handleTooltip} still add a hardcoded "recipe height * (recipe % 2)" on top of + * guiTop to find the odd one of the pair. Since every recipe is its own independent widget now, we pre-subtract + * that same offset here so their own addition cancels out and lands back on this widget's real top. + * + *

+ * This only applies when the handler itself overrides {@link IRecipeHandler#recipiesPerPage()} to return + * {@code 2}: that method defaults to {@code 2} on the interface, so most height-hacked handlers return it + * without ever having implemented this pairing (e.g. BuildCraftCompat never overrides it at all, and only reads + * it elsewhere to pick a cosmetic sub-name), and applying this compensation to them would break them. A handler + * that went out of its way to declare {@code return 2;} itself is making a deliberate statement about this + * legacy convention that a handler which simply never mentions the method isn't. + */ + private static int recipeTop(RecipeWidget widget) { + final RecipeHandlerRef ref = widget.getRecipeHandlerRef(); + final HandlerInfo handlerInfo = widget.getHandlerInfo(); + final int top = widget.y + handlerInfo.getYShift(); + + if (ref.handler.recipiesPerPage() == 2 && declaresRecipiesPerPage(ref.handler)) { + return top - handlerInfo.getHeight() * (ref.recipeIndex % 2); + } + + return top; + } + + private static boolean declaresRecipiesPerPage(IRecipeHandler handler) { + return overridesRecipiesPerPage.computeIfAbsent(handler.getClass(), clazz -> { + try { + return clazz.getMethod("recipiesPerPage").getDeclaringClass() != IRecipeHandler.class; + } catch (NoSuchMethodException e) { + return false; + } + }); + } + @Override public void close() { - guiTop = trueGuiTop; - height = trueHeight; + this.gui.guiLeft = this.trueGuiLeft; + this.gui.guiTop = this.trueGuiTop; + this.gui.width = this.trueWidth; + this.gui.height = this.trueHeight; - isHeightHackApplied = false; + this.gui.isHeightHackApplied = false; } } @@ -698,10 +743,8 @@ public abstract class GuiRecipe extends GuiContainer i return; } - try (CompatibilityHacks compatibilityHacks = new CompatibilityHacks()) { - if (this.container.handleClick(mousex, mousey, button)) { - return; - } + if (this.container.handleClick(mousex, mousey, button)) { + return; } if (this.recipeTabs.mouseClicked(mousex, mousey, button)) { @@ -749,11 +792,7 @@ public abstract class GuiRecipe extends GuiContainer i public void mouseScrolled(int scroll) { // Height hacking is not necessary here since mouse scrolling is a new feature, // added in - // GTNH NEI. So no old mods will use this. Though not hacking the height here - // does mean that - // the value of the height field will be different from in other mouseover - // methods, which - // could be confusing... + // GTNH NEI. So no old mods will use this. // First, invoke scroll handling over recipe handler tabbar. Makes sure it is // not overwritten by recipe @@ -766,10 +805,8 @@ public abstract class GuiRecipe extends GuiContainer i return; } - try (CompatibilityHacks compatibilityHacks = new CompatibilityHacks()) { - if (this.container.onMouseWheel(scroll, mouse.x, mouse.y)) { - return; - } + if (this.container.onMouseWheel(scroll, mouse.x, mouse.y)) { + return; } // If shift is held, try switching to the next recipe handler. Replicates the @@ -821,9 +858,7 @@ public abstract class GuiRecipe extends GuiContainer i currenttip = this.recipeCatalyst.handleTooltip(mousex, mousey, currenttip); } - try (CompatibilityHacks compatibilityHacks = new CompatibilityHacks()) { - currenttip = this.container.handleTooltip(mousex, mousey, currenttip); - } + currenttip = this.container.handleTooltip(mousex, mousey, currenttip); this.recipeTabs.handleTooltip(mousex, mousey, currenttip); @@ -869,9 +904,7 @@ public abstract class GuiRecipe extends GuiContainer i hotkeys = this.recipeCatalyst.handleHotkeys(mousex, mousey, hotkeys); } - try (CompatibilityHacks compatibilityHacks = new CompatibilityHacks()) { - hotkeys = this.container.handleHotkeys(mousex, mousey, hotkeys); - } + hotkeys = this.container.handleHotkeys(mousex, mousey, hotkeys); return hotkeys; } diff --git a/src/main/java/codechicken/nei/recipe/widget/RecipeWidget.java b/src/main/java/codechicken/nei/recipe/widget/RecipeWidget.java index 6099e7b9..aaac733c 100644 --- a/src/main/java/codechicken/nei/recipe/widget/RecipeWidget.java +++ b/src/main/java/codechicken/nei/recipe/widget/RecipeWidget.java @@ -526,7 +526,9 @@ public class RecipeWidget extends Widget { return false; } - return this.handler.mouseClicked(guiRecipe, button, getRecipeIndex()); + try (GuiRecipe.CompatibilityHacks hack = new GuiRecipe.CompatibilityHacks(guiRecipe, this)) { + return this.handler.mouseClicked(guiRecipe, button, getRecipeIndex()); + } } @Override @@ -550,7 +552,9 @@ public class RecipeWidget extends Widget { return buttonTooltip; } - return this.handler.handleTooltip(guiRecipe, tooltip, getRecipeIndex()); + try (GuiRecipe.CompatibilityHacks hack = new GuiRecipe.CompatibilityHacks(guiRecipe, this)) { + return this.handler.handleTooltip(guiRecipe, tooltip, getRecipeIndex()); + } } @Override diff --git a/src/main/java/codechicken/nei/scroll/GuiHelper.java b/src/main/java/codechicken/nei/scroll/GuiHelper.java index eff5b869..9b2ffe45 100644 --- a/src/main/java/codechicken/nei/scroll/GuiHelper.java +++ b/src/main/java/codechicken/nei/scroll/GuiHelper.java @@ -16,6 +16,7 @@ import cpw.mods.fml.relauncher.SideOnly; public class GuiHelper { private static final Stack scissorFrameStack = new Stack<>(); + private static final FloatBuffer MATRIX_BUFFER = BufferUtils.createFloatBuffer(16); public static void useScissor(int x, int y, int width, int height, Runnable codeBlock) { pushScissorFrame(x, y, width, height); @@ -36,7 +37,7 @@ public class GuiHelper { return currentTopFrame; } - private static void pushScissorFrame(int x, int y, int width, int height) { + public static void pushScissorFrame(int x, int y, int width, int height) { int[] matrix = prepareMatrix(x, y, width, height); int[] parentScissor = peekFirstScissorOrFullScreen(); int parentX = parentScissor[0]; @@ -110,7 +111,7 @@ public class GuiHelper { private static int[] prepareMatrix(int x, int y, int w, int h) { // Get the current translation component to support the Gui being drawn at any position on the screen - final FloatBuffer matBuf = BufferUtils.createFloatBuffer(16); + final FloatBuffer matBuf = MATRIX_BUFFER; GL11.glGetFloat(GL11.GL_MODELVIEW_MATRIX, matBuf); final float xScale = matBuf.get(0); final float yScale = matBuf.get(5);