Skip to content

Commit

Permalink
Add RenderTooltipEvent Compat (#574)
Browse files Browse the repository at this point in the history
Co-authored-by: Martin Robertz <dream-master@gmx.net>
Co-authored-by: Maya <10861407+serenibyss@users.noreply.github.com>
  • Loading branch information
3 people authored Dec 29, 2024
1 parent 4613ade commit 8dd36f1
Show file tree
Hide file tree
Showing 4 changed files with 137 additions and 5 deletions.
2 changes: 2 additions & 0 deletions dependencies.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -2,5 +2,7 @@

dependencies {
api('com.github.GTNewHorizons:CodeChickenCore:1.4.0:dev')
implementation('com.github.GTNewHorizons:GTNHLib:0.6.0:dev')

shadowImplementation('org.apache.commons:commons-csv:1.10.0')
}
14 changes: 12 additions & 2 deletions src/main/java/codechicken/nei/NEIModContainer.java
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package codechicken.nei;

import java.io.File;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.HashSet;
import java.util.LinkedList;
Expand Down Expand Up @@ -38,6 +39,7 @@ public class NEIModContainer extends DummyModContainer {
public static LinkedList<IConfigureNEI> plugins = new LinkedList<>();

private static boolean gregTech5Loaded;
private static boolean gtnhLibLoaded;

public NEIModContainer() {
super(getModMetadata());
Expand All @@ -59,16 +61,23 @@ public static boolean isGT5Loaded() {
return gregTech5Loaded;
}

public static boolean isGTNHLibLoaded() {
return gtnhLibLoaded;
}

@Override
public Set<ArtifactVersion> getRequirements() {
Set<ArtifactVersion> deps = new HashSet<>();
deps.add(VersionParser.parseVersionReference("CodeChickenCore@[" + CodeChickenCorePlugin.version + ",)"));
deps.add(VersionParser.parseVersionReference("CodeChickenCore@[" + codechicken.core.asm.Tags.VERSION + ",)"));
return deps;
}

@Override
public List<ArtifactVersion> getDependencies() {
return new LinkedList<>(getRequirements());
List<ArtifactVersion> deps = new ArrayList<>();
deps.add(VersionParser.parseVersionReference("CodeChickenCore@[" + codechicken.core.asm.Tags.VERSION + ",)"));
deps.add(VersionParser.parseVersionReference("gtnhlib@[0.6.0,)"));
return deps;
}

private String description;
Expand Down Expand Up @@ -106,6 +115,7 @@ public boolean registerBus(EventBus bus, LoadController controller) {
@Subscribe
public void preInit(FMLPreInitializationEvent event) {
gregTech5Loaded = Loader.isModLoaded("gregtech") && !Loader.isModLoaded("gregapi_post");
gtnhLibLoaded = Loader.isModLoaded("gtnhlib");
if (CommonUtils.isClient()) ClientHandler.preInit();
}

Expand Down
48 changes: 45 additions & 3 deletions src/main/java/codechicken/nei/guihook/GuiContainerManager.java
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@
import java.util.Map;
import java.util.StringJoiner;
import java.util.concurrent.CopyOnWriteArrayList;
import java.util.function.Consumer;
import java.util.regex.Pattern;

import javax.annotation.Nullable;
Expand All @@ -24,6 +25,7 @@
import net.minecraft.client.gui.FontRenderer;
import net.minecraft.client.gui.GuiScreen;
import net.minecraft.client.gui.inventory.GuiContainer;
import net.minecraft.client.renderer.RenderHelper;
import net.minecraft.client.renderer.Tessellator;
import net.minecraft.client.renderer.entity.RenderItem;
import net.minecraft.client.resources.IResourceManager;
Expand All @@ -38,15 +40,18 @@
import org.lwjgl.input.Keyboard;
import org.lwjgl.input.Mouse;
import org.lwjgl.opengl.GL11;
import org.lwjgl.opengl.GL12;

import codechicken.lib.gui.GuiDraw;
import codechicken.nei.ItemStackSet;
import codechicken.nei.NEIClientConfig;
import codechicken.nei.NEIClientUtils;
import codechicken.nei.NEIModContainer;
import codechicken.nei.SearchField;
import codechicken.nei.recipe.StackInfo;
import codechicken.nei.search.TooltipFilter;
import codechicken.nei.util.ReadableNumberConverter;
import codechicken.nei.util.RenderTooltipEventHelper;

public class GuiContainerManager {

Expand Down Expand Up @@ -552,8 +557,8 @@ public void renderToolTips(int mousex, int mousey) {
tooltip = handler.handleTooltip(window, mousex, mousey, tooltip);
}

ItemStack stack = getStackMouseOver(window);
if (tooltip.isEmpty() && shouldShowTooltip(window)) { // mouseover tip, not holding an item
ItemStack stack = getStackMouseOver(window);
font = getFontRenderer(stack);
if (stack != null) {
tooltip = itemDisplayNameMultiline(stack, window, true);
Expand Down Expand Up @@ -600,7 +605,32 @@ public void renderToolTips(int mousex, int mousey) {
}
}

drawPagedTooltip(font, mousex + 12, mousey - 12, tooltip);
if (NEIModContainer.isGTNHLibLoaded() && !tooltip.isEmpty()) {
if (RenderTooltipEventHelper.post(stack, this.window, mousex, mousey, font)) {
return;
}
Consumer<List<String>> alternativeRenderer = RenderTooltipEventHelper.getAlternativeRenderer();
if (alternativeRenderer == null) {
drawPagedTooltip(
RenderTooltipEventHelper.getFont(),
RenderTooltipEventHelper.getX() + 12,
RenderTooltipEventHelper.getY() - 12,
tooltip);
} else {
GL11.glDisable(GL12.GL_RESCALE_NORMAL);
RenderHelper.disableStandardItemLighting();
GL11.glDisable(GL11.GL_DEPTH_TEST);
GuiDraw.gui.incZLevel(300.0f);
alternativeRenderer.accept(tooltip);
GuiDraw.gui.incZLevel(-300.0f);
GL11.glEnable(GL11.GL_DEPTH_TEST);
RenderHelper.enableStandardItemLighting();
GL11.glEnable(GL12.GL_RESCALE_NORMAL);
}
RenderTooltipEventHelper.flush();
} else {
drawPagedTooltip(font, mousex + 12, mousey - 12, tooltip);
}

for (IContainerDrawHandler drawHandler : drawHandlers) {
drawHandler.postRenderTooltips(window, mousex, mousey, tooltip);
Expand All @@ -625,6 +655,18 @@ public static void drawPagedTooltip(FontRenderer font, int x, int y, List<String
tooltips.size(),
NEIClientConfig.getKeyName("gui.next_tooltip")));
}
if (NEIModContainer.isGTNHLibLoaded()) {
drawMultilineTip(
font,
x,
y,
tooltips.get(tooltipPage),
RenderTooltipEventHelper.getBackgroundStart(),
RenderTooltipEventHelper.getBackgroundEnd(),
RenderTooltipEventHelper.getBorderStart(),
RenderTooltipEventHelper.getBorderEnd());
return;
}
drawMultilineTip(font, x, y, tooltips.get(tooltipPage));
}

Expand Down Expand Up @@ -713,7 +755,7 @@ public void handleKeyboardInput() {
if (Keyboard.getEventKeyState() || (k == 0 && Character.isDefined(c))) {
try {
keyTyped(c, k);
} catch (java.lang.IndexOutOfBoundsException e) {
} catch (IndexOutOfBoundsException e) {
System.err.println("Caught out of bounds exception pressing " + c + " " + k);
e.printStackTrace();
}
Expand Down
78 changes: 78 additions & 0 deletions src/main/java/codechicken/nei/util/RenderTooltipEventHelper.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,78 @@
package codechicken.nei.util;

import static com.gtnewhorizon.gtnhlib.client.event.RenderTooltipEvent.*;

import java.util.List;
import java.util.function.Consumer;

import net.minecraft.client.gui.FontRenderer;
import net.minecraft.client.gui.GuiScreen;
import net.minecraft.item.ItemStack;
import net.minecraftforge.common.MinecraftForge;

import com.gtnewhorizon.gtnhlib.client.event.RenderTooltipEvent;

public class RenderTooltipEventHelper {

private static RenderTooltipEvent event;

/**
* @return {@code true} if the event has been canceled
*/
public static boolean post(ItemStack stack, GuiScreen gui, int x, int y, FontRenderer font) {
event = new RenderTooltipEvent(
stack,
gui,
ORIGINAL_BG_START,
ORIGINAL_BG_END,
ORIGINAL_BORDER_START,
ORIGINAL_BORDER_END,
x,
y,
font);
if (stack != null) {
MinecraftForge.EVENT_BUS.post(event);
if (event.isCanceled()) {
flush();
return true;
}
}
return false;
}

public static void flush() {
event = null;
}

public static int getBackgroundStart() {
return event == null ? ORIGINAL_BG_START : event.backgroundStart;
}

public static int getBackgroundEnd() {
return event == null ? ORIGINAL_BG_END : event.backgroundEnd;
}

public static int getBorderStart() {
return event == null ? ORIGINAL_BORDER_START : event.borderStart;
}

public static int getBorderEnd() {
return event == null ? ORIGINAL_BORDER_END : event.borderEnd;
}

public static int getX() {
return event.x;
}

public static int getY() {
return event.y;
}

public static FontRenderer getFont() {
return event.font;
}

public static Consumer<List<String>> getAlternativeRenderer() {
return event.alternativeRenderer;
}
}

0 comments on commit 8dd36f1

Please sign in to comment.