From e2365d498d2e93b4ec8d40f1b01af28c741cce75 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Florian=20N=C3=BCcke?= Date: Thu, 28 Aug 2014 08:27:42 +0200 Subject: [PATCH] Also highlight slots for items hovered in NEI. --- .../oc/client/gui/DynamicGuiContainer.scala | 34 +++++++++++++------ src/main/scala/li/cil/oc/util/mods/NEI.scala | 20 +++++++++-- 2 files changed, 40 insertions(+), 14 deletions(-) diff --git a/src/main/scala/li/cil/oc/client/gui/DynamicGuiContainer.scala b/src/main/scala/li/cil/oc/client/gui/DynamicGuiContainer.scala index 36b251e3a..0cd279c9b 100644 --- a/src/main/scala/li/cil/oc/client/gui/DynamicGuiContainer.scala +++ b/src/main/scala/li/cil/oc/client/gui/DynamicGuiContainer.scala @@ -4,9 +4,11 @@ import li.cil.oc.client.Textures import li.cil.oc.common.Tier import li.cil.oc.common.container.{ComponentSlot, Player} import li.cil.oc.util.RenderState +import li.cil.oc.util.mods.NEI import net.minecraft.client.renderer.Tessellator import net.minecraft.client.renderer.texture.TextureMap import net.minecraft.inventory.{Container, Slot} +import net.minecraft.item.ItemStack import net.minecraft.util.StatCollector import org.lwjgl.opengl.GL11 @@ -15,6 +17,8 @@ import scala.collection.convert.WrapAsScala._ abstract class DynamicGuiContainer(container: Container) extends CustomGuiContainer(container) { protected var hoveredSlot: Option[Slot] = None + protected var hoveredStackNEI: Option[ItemStack] = None + override def drawGuiContainerForegroundLayer(mouseX: Int, mouseY: Int) { fontRenderer.drawString( StatCollector.translateToLocal("container.inventory"), @@ -30,6 +34,7 @@ abstract class DynamicGuiContainer(container: Container) extends CustomGuiContai hoveredSlot = (inventorySlots.inventorySlots collect { case slot: Slot if isPointInRegion(slot.xDisplayPosition, slot.yDisplayPosition, 16, 16, mouseX, mouseY) => slot }).headOption + hoveredStackNEI = NEI.hoveredStack(this, mouseX, mouseY) super.drawScreen(mouseX, mouseY, dt) } @@ -59,20 +64,27 @@ abstract class DynamicGuiContainer(container: Container) extends CustomGuiContai case _ => } - if (mc.thePlayer.inventory.getItemStack == null) hoveredSlot.foreach(hovered => { + if (mc.thePlayer.inventory.getItemStack == null) { val currentIsInPlayerInventory = isInPlayerInventory(slot) - val hoveredIsInPlayerInventory = isInPlayerInventory(hovered) - if (currentIsInPlayerInventory != hoveredIsInPlayerInventory) { - if ((currentIsInPlayerInventory && slot.getHasStack && hovered.isItemValid(slot.getStack)) || - (hoveredIsInPlayerInventory && hovered.getHasStack && slot.isItemValid(hovered.getStack))) { - GL11.glDisable(GL11.GL_DEPTH_TEST) - GL11.glDisable(GL11.GL_LIGHTING) - drawGradientRect(slot.xDisplayPosition, slot.yDisplayPosition, slot.xDisplayPosition + 16, slot.yDisplayPosition + 16, 0x80FFFFFF, 0x80FFFFFF) - GL11.glEnable(GL11.GL_LIGHTING) - GL11.glEnable(GL11.GL_DEPTH_TEST) + val drawHighlight = hoveredSlot match { + case Some(hovered) => + val hoveredIsInPlayerInventory = isInPlayerInventory(hovered) + (currentIsInPlayerInventory != hoveredIsInPlayerInventory) && + ((currentIsInPlayerInventory && slot.getHasStack && hovered.isItemValid(slot.getStack)) || + (hoveredIsInPlayerInventory && hovered.getHasStack && slot.isItemValid(hovered.getStack))) + case _ => hoveredStackNEI match { + case Some(stack) => !currentIsInPlayerInventory && slot.isItemValid(stack) + case _ => false } } - }) + if (drawHighlight) { + GL11.glDisable(GL11.GL_DEPTH_TEST) + GL11.glDisable(GL11.GL_LIGHTING) + drawGradientRect(slot.xDisplayPosition, slot.yDisplayPosition, slot.xDisplayPosition + 16, slot.yDisplayPosition + 16, 0x80FFFFFF, 0x80FFFFFF) + GL11.glEnable(GL11.GL_LIGHTING) + GL11.glEnable(GL11.GL_DEPTH_TEST) + } + } } } diff --git a/src/main/scala/li/cil/oc/util/mods/NEI.scala b/src/main/scala/li/cil/oc/util/mods/NEI.scala index 0a0ade2d5..7cd773da8 100644 --- a/src/main/scala/li/cil/oc/util/mods/NEI.scala +++ b/src/main/scala/li/cil/oc/util/mods/NEI.scala @@ -1,11 +1,17 @@ package li.cil.oc.util.mods +import net.minecraft.client.gui.inventory.GuiContainer +import net.minecraft.item.ItemStack + object NEI { - private lazy val layoutManagerClass = try { - Class.forName("codechicken.nei.LayoutManager") + private lazy val (layoutManagerClass, getInstance, getStackUnderMouse) = try { + val layoutManager = Class.forName("codechicken.nei.LayoutManager") + val getInstance = layoutManager.getMethod("instance") + val getStackUnderMouse = layoutManager.getMethod("getStackUnderMouse", classOf[GuiContainer], classOf[Int], classOf[Int]) + (layoutManager, getInstance, getStackUnderMouse) } catch { - case _: Throwable => null + case _: Throwable => (null, null, null) } def isInputFocused = @@ -15,4 +21,12 @@ object NEI { catch { case _: Throwable => false }) + + def hoveredStack(container: GuiContainer, mouseX: Int, mouseY: Int): Option[ItemStack] = { + if (Mods.NotEnoughItems.isAvailable && layoutManagerClass != null && getInstance != null && getStackUnderMouse != null) + try return Option(getStackUnderMouse.invoke(getInstance.invoke(null), container, mouseX.underlying(), mouseY.underlying()).asInstanceOf[ItemStack]) catch { + case t: Throwable => println(t) + } + None + } }