aboutsummaryrefslogtreecommitdiff
path: root/src/main/java/io/github/moulberry/notenoughupdates/recipes/CraftingOverlay.java
blob: 4be3b051d69823199c12f6735aca55b23cce3418 (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
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
/*
 * Copyright (C) 2022 NotEnoughUpdates contributors
 *
 * This file is part of NotEnoughUpdates.
 *
 * NotEnoughUpdates is free software: you can redistribute it
 * and/or modify it under the terms of the GNU Lesser General Public
 * License as published by the Free Software Foundation, either
 * version 3 of the License, or (at your option) any later version.
 *
 * NotEnoughUpdates is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
 * Lesser General Public License for more details.
 *
 * You should have received a copy of the GNU Lesser General Public License
 * along with NotEnoughUpdates. If not, see <https://www.gnu.org/licenses/>.
 */

package io.github.moulberry.notenoughupdates.recipes;

import io.github.moulberry.notenoughupdates.NEUManager;
import io.github.moulberry.notenoughupdates.miscfeatures.EnchantingSolvers;
import io.github.moulberry.notenoughupdates.mixins.AccessorGuiContainer;
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 + ((AccessorGuiContainer) gui).getGuiLeft(),
				slot.yDisplayPosition + ((AccessorGuiContainer) gui).getGuiTop(),
				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,
					((AccessorGuiContainer) guiChest).getGuiTop() - 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;
		if (EnchantingSolvers.currentSolver != EnchantingSolvers.SolverType.NONE) 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());
				}
			});
		});
	}

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

	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 + ((AccessorGuiContainer) gui).getGuiLeft();
		int slotY = slot.yDisplayPosition + ((AccessorGuiContainer) gui).getGuiTop();
		Gui.drawRect(slotX, slotY, slotX + 16, slotY + 16, 0x64ff0000);
		if (item != null)
			Utils.drawItemStack(item, slotX, slotY);
	}

}