/
RecipeLayout.java
293 lines (253 loc) · 10 KB
/
RecipeLayout.java
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
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
package mezz.jei.gui.recipes;
import com.mojang.blaze3d.matrix.MatrixStack;
import com.mojang.blaze3d.systems.RenderSystem;
import mezz.jei.Internal;
import mezz.jei.api.constants.VanillaTypes;
import mezz.jei.api.gui.IRecipeLayoutDrawable;
import mezz.jei.api.gui.drawable.IDrawable;
import mezz.jei.api.gui.ingredient.IGuiFluidStackGroup;
import mezz.jei.api.gui.ingredient.IGuiIngredientGroup;
import mezz.jei.api.ingredients.IIngredientType;
import mezz.jei.api.ingredients.IIngredients;
import mezz.jei.api.recipe.category.IRecipeCategory;
import mezz.jei.gui.Focus;
import mezz.jei.gui.TooltipRenderer;
import mezz.jei.gui.elements.DrawableNineSliceTexture;
import mezz.jei.gui.ingredients.GuiFluidStackGroup;
import mezz.jei.gui.ingredients.GuiIngredient;
import mezz.jei.gui.ingredients.GuiIngredientGroup;
import mezz.jei.gui.ingredients.GuiItemStackGroup;
import mezz.jei.ingredients.Ingredients;
import mezz.jei.util.ErrorUtil;
import mezz.jei.util.MathUtil;
import net.minecraft.client.Minecraft;
import net.minecraft.client.renderer.Rectangle2d;
import net.minecraft.item.ItemStack;
import net.minecraft.util.text.ITextComponent;
import net.minecraftforge.fluids.FluidStack;
import org.apache.logging.log4j.LogManager;
import org.apache.logging.log4j.Logger;
import javax.annotation.Nullable;
import java.util.IdentityHashMap;
import java.util.List;
import java.util.Map;
public class RecipeLayout<T> implements IRecipeLayoutDrawable {
private static final Logger LOGGER = LogManager.getLogger();
private static final int HIGHLIGHT_COLOR = 0x7FFFFFFF;
private static final int RECIPE_BUTTON_SIZE = 13;
private static final int RECIPE_BORDER_PADDING = 4;
private final int ingredientCycleOffset = (int) ((Math.random() * 10000) % Integer.MAX_VALUE);
private final IRecipeCategory<T> recipeCategory;
private final GuiItemStackGroup guiItemStackGroup;
private final GuiFluidStackGroup guiFluidStackGroup;
private final Map<IIngredientType<?>, GuiIngredientGroup<?>> guiIngredientGroups;
@Nullable
private final RecipeTransferButton recipeTransferButton;
private final T recipe;
@Nullable
private final Focus<?> focus;
@Nullable
private ShapelessIcon shapelessIcon;
private final DrawableNineSliceTexture recipeBorder;
private int posX;
private int posY;
@Nullable
public static <T> RecipeLayout<T> create(int index, IRecipeCategory<T> recipeCategory, T recipe, @Nullable Focus<?> focus, int posX, int posY) {
RecipeLayout<T> recipeLayout = new RecipeLayout<>(index, recipeCategory, recipe, focus, posX, posY);
try {
IIngredients ingredients = new Ingredients();
recipeCategory.setIngredients(recipe, ingredients);
recipeCategory.setRecipe(recipeLayout, recipe, ingredients);
return recipeLayout;
} catch (RuntimeException | LinkageError e) {
LOGGER.error("Error caught from Recipe Category: {}", recipeCategory.getClass().getCanonicalName(), e);
}
return null;
}
private RecipeLayout(int index, IRecipeCategory<T> recipeCategory, T recipe, @Nullable Focus<?> focus, int posX, int posY) {
ErrorUtil.checkNotNull(recipeCategory, "recipeCategory");
ErrorUtil.checkNotNull(recipe, "recipe");
this.recipeCategory = recipeCategory;
this.focus = focus;
Focus<ItemStack> itemStackFocus = Focus.cast(focus, VanillaTypes.ITEM);
Focus<FluidStack> fluidStackFocus = Focus.cast(focus, VanillaTypes.FLUID);
this.guiItemStackGroup = new GuiItemStackGroup(itemStackFocus, ingredientCycleOffset);
this.guiFluidStackGroup = new GuiFluidStackGroup(fluidStackFocus, ingredientCycleOffset);
this.guiIngredientGroups = new IdentityHashMap<>();
this.guiIngredientGroups.put(VanillaTypes.ITEM, this.guiItemStackGroup);
this.guiIngredientGroups.put(VanillaTypes.FLUID, this.guiFluidStackGroup);
if (index >= 0) {
IDrawable icon = Internal.getTextures().getRecipeTransfer();
this.recipeTransferButton = new RecipeTransferButton(0, 0, RECIPE_BUTTON_SIZE, RECIPE_BUTTON_SIZE, icon, this);
} else {
this.recipeTransferButton = null;
}
setPosition(posX, posY);
this.recipe = recipe;
this.recipeBorder = Internal.getTextures().getRecipeBackground();
}
@Override
public void setPosition(int posX, int posY) {
this.posX = posX;
this.posY = posY;
if (this.recipeTransferButton != null) {
int width = recipeCategory.getBackground().getWidth();
int height = recipeCategory.getBackground().getHeight();
this.recipeTransferButton.x = posX + width + RECIPE_BORDER_PADDING + 2;
this.recipeTransferButton.y = posY + height - RECIPE_BUTTON_SIZE;
}
}
@Override
@SuppressWarnings("deprecation")
public void drawRecipe(MatrixStack matrixStack, int mouseX, int mouseY) {
IDrawable background = recipeCategory.getBackground();
RenderSystem.color4f(1.0F, 1.0F, 1.0F, 1.0F);
RenderSystem.disableLighting();
RenderSystem.enableAlphaTest();
final int recipeMouseX = mouseX - posX;
final int recipeMouseY = mouseY - posY;
matrixStack.push();
matrixStack.translate(posX, posY, 0);
{
IDrawable categoryBackground = recipeCategory.getBackground();
int width = categoryBackground.getWidth() + (2 * RECIPE_BORDER_PADDING);
int height = categoryBackground.getHeight() + (2 * RECIPE_BORDER_PADDING);
recipeBorder.draw(matrixStack, -RECIPE_BORDER_PADDING, -RECIPE_BORDER_PADDING, width, height);
background.draw(matrixStack);
recipeCategory.draw(recipe, matrixStack, recipeMouseX, recipeMouseY);
// drawExtras and drawInfo often render text which messes with the color, this clears it
RenderSystem.color4f(1, 1, 1, 1);
if (shapelessIcon != null) {
shapelessIcon.draw(matrixStack, background.getWidth());
}
}
matrixStack.pop();
for (GuiIngredientGroup<?> guiIngredientGroup : guiIngredientGroups.values()) {
guiIngredientGroup.draw(matrixStack, posX, posY, HIGHLIGHT_COLOR, mouseX, mouseY);
}
if (recipeTransferButton != null) {
Minecraft minecraft = Minecraft.getInstance();
float partialTicks = minecraft.getRenderPartialTicks();
recipeTransferButton.render(matrixStack, mouseX, mouseY, partialTicks);
}
RenderSystem.disableBlend();
RenderSystem.disableLighting();
RenderSystem.disableAlphaTest();
}
@Override
@SuppressWarnings("deprecation")
public void drawOverlays(MatrixStack matrixStack, int mouseX, int mouseY) {
RenderSystem.color4f(1.0F, 1.0F, 1.0F, 1.0F);
RenderSystem.disableLighting();
RenderSystem.enableAlphaTest();
final int recipeMouseX = mouseX - posX;
final int recipeMouseY = mouseY - posY;
GuiIngredient<?> hoveredIngredient = null;
for (GuiIngredientGroup<?> guiIngredientGroup : guiIngredientGroups.values()) {
hoveredIngredient = guiIngredientGroup.getHoveredIngredient(posX, posY, mouseX, mouseY);
if (hoveredIngredient != null) {
break;
}
}
if (recipeTransferButton != null) {
recipeTransferButton.drawToolTip(matrixStack, mouseX, mouseY);
}
RenderSystem.disableBlend();
RenderSystem.disableLighting();
if (hoveredIngredient != null) {
hoveredIngredient.drawOverlays(matrixStack, posX, posY, recipeMouseX, recipeMouseY);
} else if (isMouseOver(mouseX, mouseY)) {
List<ITextComponent> tooltipStrings = recipeCategory.getTooltipStrings(recipe, recipeMouseX, recipeMouseY);
if (tooltipStrings.isEmpty() && shapelessIcon != null) {
tooltipStrings = shapelessIcon.getTooltipStrings(recipeMouseX, recipeMouseY);
}
if (tooltipStrings != null && !tooltipStrings.isEmpty()) {
TooltipRenderer.drawHoveringText(tooltipStrings, mouseX, mouseY, matrixStack);
}
}
RenderSystem.disableAlphaTest();
}
@Override
public boolean isMouseOver(double mouseX, double mouseY) {
final IDrawable background = recipeCategory.getBackground();
final Rectangle2d backgroundRect = new Rectangle2d(posX, posY, background.getWidth(), background.getHeight());
return MathUtil.contains(backgroundRect, mouseX, mouseY) ||
(recipeTransferButton != null && recipeTransferButton.isMouseOver(mouseX, mouseY));
}
@Override
@Nullable
public Object getIngredientUnderMouse(int mouseX, int mouseY) {
GuiIngredient<?> guiIngredient = getGuiIngredientUnderMouse(mouseX, mouseY);
if (guiIngredient != null) {
return guiIngredient.getDisplayedIngredient();
}
return null;
}
@Nullable
public GuiIngredient<?> getGuiIngredientUnderMouse(double mouseX, double mouseY) {
for (GuiIngredientGroup<?> guiIngredientGroup : guiIngredientGroups.values()) {
GuiIngredient<?> clicked = guiIngredientGroup.getHoveredIngredient(posX, posY, mouseX, mouseY);
if (clicked != null) {
return clicked;
}
}
return null;
}
public boolean handleClick(double mouseX, double mouseY, int mouseButton) {
return recipeCategory.handleClick(recipe, mouseX - posX, mouseY - posY, mouseButton);
}
@Override
public GuiItemStackGroup getItemStacks() {
return guiItemStackGroup;
}
@Override
public IGuiFluidStackGroup getFluidStacks() {
return guiFluidStackGroup;
}
@Override
public <V> IGuiIngredientGroup<V> getIngredientsGroup(IIngredientType<V> ingredientType) {
@SuppressWarnings("unchecked")
GuiIngredientGroup<V> guiIngredientGroup = (GuiIngredientGroup<V>) guiIngredientGroups.get(ingredientType);
if (guiIngredientGroup == null) {
Focus<V> focus = getFocus(ingredientType);
guiIngredientGroup = new GuiIngredientGroup<>(ingredientType, focus, ingredientCycleOffset);
guiIngredientGroups.put(ingredientType, guiIngredientGroup);
}
return guiIngredientGroup;
}
@Override
public void moveRecipeTransferButton(int posX, int posY) {
if (recipeTransferButton != null) {
recipeTransferButton.x = posX + this.posX;
recipeTransferButton.y = posY + this.posY;
}
}
@Override
public void setShapeless() {
this.shapelessIcon = new ShapelessIcon();
}
@Override
@Nullable
public Focus<?> getFocus() {
return focus;
}
@Nullable
@Override
public <V> Focus<V> getFocus(IIngredientType<V> ingredientType) {
return Focus.cast(this.focus, ingredientType);
}
@Nullable
public RecipeTransferButton getRecipeTransferButton() {
return recipeTransferButton;
}
@Override
public IRecipeCategory<?> getRecipeCategory() {
return recipeCategory;
}
public int getPosX() {
return posX;
}
public int getPosY() {
return posY;
}
}