aboutsummaryrefslogtreecommitdiff
path: root/src/main/java/io/github/moulberry/notenoughupdates/recipes/CraftingOverlay.java
diff options
context:
space:
mode:
Diffstat (limited to 'src/main/java/io/github/moulberry/notenoughupdates/recipes/CraftingOverlay.java')
-rw-r--r--src/main/java/io/github/moulberry/notenoughupdates/recipes/CraftingOverlay.java242
1 files changed, 126 insertions, 116 deletions
diff --git a/src/main/java/io/github/moulberry/notenoughupdates/recipes/CraftingOverlay.java b/src/main/java/io/github/moulberry/notenoughupdates/recipes/CraftingOverlay.java
index 21823792..d0b464f6 100644
--- a/src/main/java/io/github/moulberry/notenoughupdates/recipes/CraftingOverlay.java
+++ b/src/main/java/io/github/moulberry/notenoughupdates/recipes/CraftingOverlay.java
@@ -20,121 +20,131 @@ import java.util.function.BiConsumer;
public class CraftingOverlay {
- private final NEUManager manager;
- private CraftingRecipe currentRecipe = null;
-
- public CraftingOverlay(NEUManager manager) {
- this.manager = manager;
- MinecraftForge.EVENT_BUS.register(this);
- }
-
- public void setShownRecipe(CraftingRecipe recipe) {
- currentRecipe = recipe;
- }
-
- private void forEachSlot(ContainerChest chest, BiConsumer<Ingredient, Slot> block) {
- for (int i = 0; i < 9; i++) {
- Ingredient recipeIngredient = currentRecipe.getInputs()[i];
- Slot slot = chest.inventorySlots.get(10 + 9 * (i / 3) + (i % 3));
- block.accept(recipeIngredient, slot);
- }
- }
-
- private void forEachHoveredSlot(GuiChest gui, ContainerChest chest, int mouseX, int mouseY, BiConsumer<Ingredient, Slot> block) {
- forEachSlot(chest, (recipeIngredient, slot) -> {
- if (Utils.isWithinRect(
- mouseX, mouseY,
- slot.xDisplayPosition + gui.guiLeft,
- slot.yDisplayPosition + gui.guiTop,
- 16, 16))
- block.accept(recipeIngredient, slot);
- });
- }
-
- private void runIfCraftingOverlayIsPresent(Gui gui, BiConsumer<GuiChest, ContainerChest> block) {
- if (currentRecipe == null) return;
- if (!(gui instanceof GuiChest)) return;
- GuiChest guiChest = (GuiChest) gui;
- ContainerChest chest = (ContainerChest) guiChest.inventorySlots;
- IInventory chestInventory = chest.getLowerChestInventory();
- if (!"Craft Item".equals(chestInventory.getDisplayName().getUnformattedText())) return;
- block.accept(guiChest, chest);
- }
-
- @SubscribeEvent
- public void onGuiOpen(GuiOpenEvent event) {
- currentRecipe = null;
- }
-
- @SubscribeEvent
- public void onRender(GuiScreenEvent.DrawScreenEvent.Post event) {
- runIfCraftingOverlayIsPresent(event.gui, (guiChest, chest) -> {
- renderSlots(guiChest, chest);
- if (currentRecipe.getCraftText() != null) {
- FontRenderer fontRenderer = Minecraft.getMinecraft().fontRendererObj;
- fontRenderer.drawStringWithShadow(
- currentRecipe.getCraftText(),
- Utils.peekGuiScale().getScaledWidth() / 2f - fontRenderer.getStringWidth(currentRecipe.getCraftText()) / 2f,
- guiChest.guiTop - 15f, 0x808080);
- }
- renderTooltip(guiChest, chest);
- });
- }
-
- @SubscribeEvent
- public void onKeyDown(GuiScreenEvent.KeyboardInputEvent.Pre event) {
- if (!Keyboard.getEventKeyState() || (Keyboard.getEventKey() != Keyboard.KEY_U && Keyboard.getEventKey() != Keyboard.KEY_R))
- return;
- runIfCraftingOverlayIsPresent(event.gui, (guiChest, chest) -> {
- int mouseX = Utils.getMouseX();
- int mouseY = Utils.getMouseY();
-
- forEachHoveredSlot(guiChest, chest, mouseX, mouseY, (recipeIngredient, slot) -> {
- if (slot.getStack() == null && recipeIngredient != null) {
- if (Keyboard.getEventKey() == Keyboard.KEY_R)
- manager.showRecipe(recipeIngredient.getInternalItemId());
- if (Keyboard.getEventKey() == Keyboard.KEY_U)
- manager.displayGuiItemRecipe(recipeIngredient.getInternalItemId(), null);
- }
- });
- });
- }
-
- private void renderTooltip(GuiChest guiChest, ContainerChest chest) {
- int mouseX = Utils.getMouseX();
- int mouseY = Utils.getMouseY();
- forEachHoveredSlot(guiChest, chest, mouseX, mouseY, (recipeIngredient, slot) -> {
- ItemStack actualItem = slot.getStack();
- if (actualItem == null && recipeIngredient != null) {
- Utils.drawHoveringText(
- recipeIngredient.getItemStack().getTooltip(Minecraft.getMinecraft().thePlayer, false),
- mouseX, mouseY,
- Utils.peekGuiScale().getScaledWidth(), Utils.peekGuiScale().getScaledHeight(), -1,
- Minecraft.getMinecraft().fontRendererObj);
- }
- });
- }
-
- private void renderSlots(GuiChest guiChest, ContainerChest chest) {
- forEachSlot(chest, (recipeIngredient, slot) -> {
- ItemStack actualItem = slot.getStack();
- if (actualItem != null && (recipeIngredient == null ||
- !recipeIngredient.getInternalItemId().equals(manager.getInternalNameForItem(actualItem)) ||
- actualItem.stackSize < recipeIngredient.getCount())) {
- drawItemStack(guiChest, slot, actualItem);
- }
- if (recipeIngredient != null && actualItem == null) {
- drawItemStack(guiChest, slot, recipeIngredient.getItemStack());
- }
- });
- }
-
- private void drawItemStack(GuiChest gui, Slot slot, ItemStack item) {
- int slotX = slot.xDisplayPosition + gui.guiLeft;
- int slotY = slot.yDisplayPosition + gui.guiTop;
- Gui.drawRect(slotX, slotY, slotX + 16, slotY + 16, 0x64ff0000);
- if (item != null)
- Utils.drawItemStack(item, slotX, slotY);
- }
+ private final NEUManager manager;
+ private CraftingRecipe currentRecipe = null;
+
+ public CraftingOverlay(NEUManager manager) {
+ this.manager = manager;
+ MinecraftForge.EVENT_BUS.register(this);
+ }
+
+ public void setShownRecipe(CraftingRecipe recipe) {
+ currentRecipe = recipe;
+ }
+
+ private void forEachSlot(ContainerChest chest, BiConsumer<Ingredient, Slot> block) {
+ for (int i = 0; i < 9; i++) {
+ Ingredient recipeIngredient = currentRecipe.getInputs()[i];
+ Slot slot = chest.inventorySlots.get(10 + 9 * (i / 3) + (i % 3));
+ block.accept(recipeIngredient, slot);
+ }
+ }
+
+ private void forEachHoveredSlot(
+ GuiChest gui,
+ ContainerChest chest,
+ int mouseX,
+ int mouseY,
+ BiConsumer<Ingredient, Slot> block
+ ) {
+ forEachSlot(chest, (recipeIngredient, slot) -> {
+ if (Utils.isWithinRect(
+ mouseX, mouseY,
+ slot.xDisplayPosition + gui.guiLeft,
+ slot.yDisplayPosition + gui.guiTop,
+ 16, 16
+ ))
+ block.accept(recipeIngredient, slot);
+ });
+ }
+
+ private void runIfCraftingOverlayIsPresent(Gui gui, BiConsumer<GuiChest, ContainerChest> block) {
+ if (currentRecipe == null) return;
+ if (!(gui instanceof GuiChest)) return;
+ GuiChest guiChest = (GuiChest) gui;
+ ContainerChest chest = (ContainerChest) guiChest.inventorySlots;
+ IInventory chestInventory = chest.getLowerChestInventory();
+ if (!"Craft Item".equals(chestInventory.getDisplayName().getUnformattedText())) return;
+ block.accept(guiChest, chest);
+ }
+
+ @SubscribeEvent
+ public void onGuiOpen(GuiOpenEvent event) {
+ currentRecipe = null;
+ }
+
+ @SubscribeEvent
+ public void onRender(GuiScreenEvent.DrawScreenEvent.Post event) {
+ runIfCraftingOverlayIsPresent(event.gui, (guiChest, chest) -> {
+ renderSlots(guiChest, chest);
+ if (currentRecipe.getCraftText() != null) {
+ FontRenderer fontRenderer = Minecraft.getMinecraft().fontRendererObj;
+ fontRenderer.drawStringWithShadow(
+ currentRecipe.getCraftText(),
+ Utils.peekGuiScale().getScaledWidth() / 2f - fontRenderer.getStringWidth(currentRecipe.getCraftText()) / 2f,
+ guiChest.guiTop - 15f, 0x808080
+ );
+ }
+ renderTooltip(guiChest, chest);
+ });
+ }
+
+ @SubscribeEvent
+ public void onKeyDown(GuiScreenEvent.KeyboardInputEvent.Pre event) {
+ if (!Keyboard.getEventKeyState() ||
+ (Keyboard.getEventKey() != Keyboard.KEY_U && Keyboard.getEventKey() != Keyboard.KEY_R))
+ return;
+ runIfCraftingOverlayIsPresent(event.gui, (guiChest, chest) -> {
+ int mouseX = Utils.getMouseX();
+ int mouseY = Utils.getMouseY();
+
+ forEachHoveredSlot(guiChest, chest, mouseX, mouseY, (recipeIngredient, slot) -> {
+ if (slot.getStack() == null && recipeIngredient != null) {
+ if (Keyboard.getEventKey() == Keyboard.KEY_R)
+ manager.showRecipe(recipeIngredient.getInternalItemId());
+ if (Keyboard.getEventKey() == Keyboard.KEY_U)
+ manager.displayGuiItemRecipe(recipeIngredient.getInternalItemId(), null);
+ }
+ });
+ });
+ }
+
+ private void renderTooltip(GuiChest guiChest, ContainerChest chest) {
+ int mouseX = Utils.getMouseX();
+ int mouseY = Utils.getMouseY();
+ forEachHoveredSlot(guiChest, chest, mouseX, mouseY, (recipeIngredient, slot) -> {
+ ItemStack actualItem = slot.getStack();
+ if (actualItem == null && recipeIngredient != null) {
+ Utils.drawHoveringText(
+ recipeIngredient.getItemStack().getTooltip(Minecraft.getMinecraft().thePlayer, false),
+ mouseX, mouseY,
+ Utils.peekGuiScale().getScaledWidth(), Utils.peekGuiScale().getScaledHeight(), -1,
+ Minecraft.getMinecraft().fontRendererObj
+ );
+ }
+ });
+ }
+
+ private void renderSlots(GuiChest guiChest, ContainerChest chest) {
+ forEachSlot(chest, (recipeIngredient, slot) -> {
+ ItemStack actualItem = slot.getStack();
+ if (actualItem != null && (recipeIngredient == null ||
+ !recipeIngredient.getInternalItemId().equals(manager.getInternalNameForItem(actualItem)) ||
+ actualItem.stackSize < recipeIngredient.getCount())) {
+ drawItemStack(guiChest, slot, actualItem);
+ }
+ if (recipeIngredient != null && actualItem == null) {
+ drawItemStack(guiChest, slot, recipeIngredient.getItemStack());
+ }
+ });
+ }
+
+ private void drawItemStack(GuiChest gui, Slot slot, ItemStack item) {
+ int slotX = slot.xDisplayPosition + gui.guiLeft;
+ int slotY = slot.yDisplayPosition + gui.guiTop;
+ Gui.drawRect(slotX, slotY, slotX + 16, slotY + 16, 0x64ff0000);
+ if (item != null)
+ Utils.drawItemStack(item, slotX, slotY);
+ }
}