aboutsummaryrefslogtreecommitdiff
path: root/src/main/java/io/github/moulberry/notenoughupdates/recipes/CraftingOverlay.java
blob: 218237927edb41f21c5abca455930afd830001df (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
package io.github.moulberry.notenoughupdates.recipes;

import io.github.moulberry.notenoughupdates.NEUManager;
import io.github.moulberry.notenoughupdates.util.Utils;
import net.minecraft.client.Minecraft;
import net.minecraft.client.gui.FontRenderer;
import net.minecraft.client.gui.Gui;
import net.minecraft.client.gui.inventory.GuiChest;
import net.minecraft.inventory.ContainerChest;
import net.minecraft.inventory.IInventory;
import net.minecraft.inventory.Slot;
import net.minecraft.item.ItemStack;
import net.minecraftforge.client.event.GuiOpenEvent;
import net.minecraftforge.client.event.GuiScreenEvent;
import net.minecraftforge.common.MinecraftForge;
import net.minecraftforge.fml.common.eventhandler.SubscribeEvent;
import org.lwjgl.input.Keyboard;

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);
    }

}