aboutsummaryrefslogtreecommitdiff
path: root/src/main/java/io/github/moulberry/notenoughupdates/overlays/CraftingOverlay.java
blob: eb4749db7fd9eb655a242fde4d4bf3ea9aee0e11 (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
package io.github.moulberry.notenoughupdates.overlays;

import com.google.gson.JsonObject;
import io.github.moulberry.notenoughupdates.NEUManager;
import io.github.moulberry.notenoughupdates.NotEnoughUpdates;
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.Slot;
import net.minecraft.item.ItemStack;
import org.lwjgl.input.Keyboard;
import org.lwjgl.input.Mouse;

import java.util.List;

public class CraftingOverlay {
    private static ItemStack[] items = new ItemStack[9];
    private static final NEUManager manager = NotEnoughUpdates.INSTANCE.manager;
    public static boolean shouldRender = false;
    private static String text = null;


    public static void render() {
        if (shouldRender) {
            ContainerChest container = (ContainerChest) Minecraft.getMinecraft().thePlayer.openContainer;
            GuiChest gc = (GuiChest) Minecraft.getMinecraft().currentScreen;
            FontRenderer ft = Minecraft.getMinecraft().fontRendererObj;
            int width = Utils.peekGuiScale().getScaledWidth();
            int height = Utils.peekGuiScale().getScaledHeight();
            int mouseX = Mouse.getX() * width / Minecraft.getMinecraft().displayWidth;
            int mouseY = height - Mouse.getY() * height / Minecraft.getMinecraft().displayHeight - 1;
            List<String> tooltip = null;
            for (int i = 0; i < 9; i++) {
                if (items[i] != null) {
                    int slotIndex = (int) (10 + 9 * Math.floor(i / 3f) + (i % 3));
                    Slot slot = container.inventorySlots.get(slotIndex);
                    int x = slot.xDisplayPosition + gc.guiLeft;
                    int y = slot.yDisplayPosition + gc.guiTop;
                    if (!slot.getHasStack() || !manager.getInternalNameForItem(items[i]).equals(manager.getInternalNameForItem(slot.getStack())) ||
                            slot.getStack().stackSize < items[i].stackSize)
                        Gui.drawRect(x, y, x + 16, y + 16, 0x64ff0000);
                    if (!slot.getHasStack())
                        Utils.drawItemStack(items[i], x, y);
                    if (!slot.getHasStack() && mouseX >= x && mouseX < x + 16 && mouseY >= y && mouseY < y + 16)
                        tooltip = items[i].getTooltip(Minecraft.getMinecraft().thePlayer, false);
                }
            }
            if (text != null)
                ft.drawStringWithShadow(text,
                        Utils.peekGuiScale().getScaledWidth() / 2f - ft.getStringWidth(text) / 2f,
                        gc.guiTop - 15f, 0x808080);
            if (tooltip != null)
                Utils.drawHoveringText(tooltip, mouseX, mouseY, width, height, -1, ft);
        }
    }

    public static void updateItem(JsonObject item) {
        items = new ItemStack[9];
        text = null;
        String[] x = {"1", "2", "3"};
        String[] y = {"A", "B", "C"};
        for (int i = 0; i < 9; i++) {
            String name = y[i / 3] + x[i % 3];
            String itemS = item.getAsJsonObject("recipe").get(name).getAsString();
            if (itemS != null && !itemS.equals("")) {
                int count = 1;
                if (itemS.split(":").length == 2) {
                    count = Integer.parseInt(itemS.split(":")[1]);
                    itemS = itemS.split(":")[0];
                }
                JsonObject craft = manager.getItemInformation().get(itemS);
                if (craft != null) {
                    ItemStack stack = manager.jsonToStack(craft);
                    stack.stackSize = count;
                    items[i] = stack;
                }
            }
        }
        if (item.has("crafttext")) {
            text = item.get("crafttext").getAsString();
        }
        shouldRender = true;
    }

    public static void keyInput() {
        if (!Keyboard.getEventKeyState() || Keyboard.getEventKey() != Keyboard.KEY_U && Keyboard.getEventKey() != Keyboard.KEY_R)
            return;
        int width = Utils.peekGuiScale().getScaledWidth();
        int height = Utils.peekGuiScale().getScaledHeight();
        int mouseX = Mouse.getX() * width / Minecraft.getMinecraft().displayWidth;
        int mouseY = height - Mouse.getY() * height / Minecraft.getMinecraft().displayHeight - 1;
        ContainerChest container = (ContainerChest) Minecraft.getMinecraft().thePlayer.openContainer;
        GuiChest gc = (GuiChest) Minecraft.getMinecraft().currentScreen;
        for (int i = 0; i < 9; i++) {
            if (items[i] != null) {
                int slotIndex = (int) (10 + 9 * Math.floor(i / 3f) + (i % 3));
                Slot slot = container.inventorySlots.get(slotIndex);
                int x = slot.xDisplayPosition + gc.guiLeft;
                int y = slot.yDisplayPosition + gc.guiTop;
                if (mouseX >= x && mouseX < x + 16 && mouseY >= y && mouseY < y + 16) {
                    if (!slot.getHasStack()) {
                        String internalName = manager.getInternalNameForItem(items[i]);
                        if (Keyboard.getEventKey() == Keyboard.KEY_U && internalName != null) {
                            manager.displayGuiItemUsages(internalName);
                        } else if (Keyboard.getEventKey() == Keyboard.KEY_R && internalName != null && manager.getItemInformation().containsKey(internalName)) {
                            JsonObject item = manager.getItemInformation().get(internalName);
                            manager.showRecipe(item);
                        }
                    }
                    break;
                }
            }
        }
    }
}