Skip to content

Commit

Permalink
Merge branch 'nea89o-highlightthingy'
Browse files Browse the repository at this point in the history
  • Loading branch information
Ekwav committed Nov 27, 2023
2 parents 185a00d + 4166331 commit 6d64af9
Show file tree
Hide file tree
Showing 3 changed files with 73 additions and 0 deletions.
38 changes: 38 additions & 0 deletions src/main/java/de/torui/coflsky/handlers/DescriptionHandler.java
Original file line number Diff line number Diff line change
Expand Up @@ -3,8 +3,11 @@
import de.torui.coflsky.Config;
import de.torui.coflsky.network.QueryServerCommands;
import de.torui.coflsky.network.WSClient;
import de.torui.coflsky.utils.ReflectionUtil;
import net.minecraft.client.gui.Gui;
import net.minecraft.client.gui.inventory.GuiChest;
import net.minecraft.client.gui.inventory.GuiContainer;
import net.minecraft.client.renderer.GlStateManager;
import net.minecraft.inventory.ContainerChest;
import net.minecraft.inventory.IInventory;
import net.minecraft.inventory.Slot;
Expand All @@ -13,10 +16,12 @@
import net.minecraft.nbt.NBTTagCompound;
import net.minecraft.nbt.NBTTagList;
import net.minecraftforge.client.event.GuiOpenEvent;
import net.minecraftforge.client.event.GuiScreenEvent;
import net.minecraftforge.event.entity.player.ItemTooltipEvent;

import java.io.ByteArrayOutputStream;
import java.io.IOException;
import java.lang.invoke.MethodHandle;
import java.util.ArrayList;
import java.util.Base64;
import java.util.HashMap;
Expand Down Expand Up @@ -248,6 +253,39 @@ public void setTooltips(ItemTooltipEvent event) {
}
}

public static MethodHandle xSizeField = ReflectionUtil.getField(GuiContainer.class, "xSize", "field_146999_f", "f");
public static MethodHandle ySizeField = ReflectionUtil.getField(GuiContainer.class, "ySize", "field_147000_g", "g");

public void highlightSlots(GuiScreenEvent.BackgroundDrawnEvent event) {
if (!(event.gui instanceof GuiContainer)) {
return;
}
GuiContainer containerGui = (GuiContainer) event.gui;
for (Slot inventorySlot : containerGui.inventorySlots.inventorySlots) {
if (!inventorySlot.getHasStack()) continue;
DescModification[] tooltipData = getTooltipData(inventorySlot.getStack());
for (DescModification modification : tooltipData) {
if ("HIGHLIGHT".equals(modification.type)) {
int color = (int) (Long.parseLong(modification.value, 16) & 0xFFFFFFFFL);
try {
int guiTop = (containerGui.height - (int) ySizeField.invokeExact(containerGui)) / 2;
int guiLeft = (containerGui.width - (int) xSizeField.invokeExact(containerGui)) / 2;
int slotX = inventorySlot.xDisplayPosition + guiLeft;
int slotY = inventorySlot.yDisplayPosition + guiTop;
GlStateManager.pushMatrix();
GlStateManager.translate(0, 0, 0.1);
Gui.drawRect(slotX, slotY, slotX + 16, slotY + 16,
modification.value.length() > 6 ? color : (color | 0xFF000000));
GlStateManager.popMatrix();
} catch (Throwable e) {
throw new RuntimeException(e);
}
}
}
}
}


/**
* Called when the inventory is closed
*/
Expand Down
6 changes: 6 additions & 0 deletions src/main/java/de/torui/coflsky/handlers/EventRegistry.java
Original file line number Diff line number Diff line change
Expand Up @@ -287,6 +287,12 @@ public void onGuiOpen(GuiOpenEvent event) {
}).start();
}

@SubscribeEvent
public void onBackgroundRenderDone(GuiScreenEvent.BackgroundDrawnEvent event) {
if(descriptionHandler != null)
descriptionHandler.highlightSlots(event);
}

@SubscribeEvent(priority = EventPriority.HIGHEST)
public void onItemTooltipEvent(ItemTooltipEvent event) {
if (!config.extendedtooltips)
Expand Down
29 changes: 29 additions & 0 deletions src/main/java/de/torui/coflsky/utils/ReflectionUtil.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
package de.torui.coflsky.utils;

import java.lang.invoke.MethodHandle;
import java.lang.invoke.MethodHandles;
import java.lang.reflect.Field;
import java.util.Arrays;

public class ReflectionUtil {

public static MethodHandle getField(Class<?> clazz, String... names) {
Field f = null;
for (String name : names) {
try {
f = clazz.getDeclaredField(name);
break;
} catch (NoSuchFieldException e) {
}
}
if (f == null)
throw new RuntimeException("Could not find any of fields " + Arrays.toString(names) + " on class " + clazz);
f.setAccessible(true);
try {
return MethodHandles.publicLookup().unreflectGetter(f);
} catch (IllegalAccessException e) {
throw new RuntimeException(e);
}
}

}

0 comments on commit 6d64af9

Please sign in to comment.