/
RecipeLayout.java
266 lines (231 loc) · 8.73 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
package mezz.jei.gui.recipes;
import javax.annotation.Nullable;
import java.awt.Rectangle;
import java.util.ArrayList;
import java.util.IdentityHashMap;
import java.util.List;
import java.util.Map;
import mezz.jei.Internal;
import mezz.jei.api.gui.IDrawable;
import mezz.jei.api.gui.IGuiFluidStackGroup;
import mezz.jei.api.gui.IGuiIngredientGroup;
import mezz.jei.api.gui.IRecipeLayoutDrawable;
import mezz.jei.api.ingredients.IIngredients;
import mezz.jei.api.recipe.IFocus;
import mezz.jei.api.recipe.IRecipeCategory;
import mezz.jei.api.recipe.IRecipeWrapper;
import mezz.jei.gui.Focus;
import mezz.jei.gui.TooltipRenderer;
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.LegacyUtil;
import mezz.jei.util.Log;
import net.minecraft.client.Minecraft;
import net.minecraft.client.renderer.GlStateManager;
import net.minecraft.item.ItemStack;
import net.minecraftforge.fluids.FluidStack;
public class RecipeLayout implements IRecipeLayoutDrawable {
private static final int RECIPE_BUTTON_SIZE = 13;
public static final int recipeTransferButtonIndex = 100;
private final int ingredientCycleOffset = (int) (Math.random() * 10000);
private final IRecipeCategory recipeCategory;
private final GuiItemStackGroup guiItemStackGroup;
private final GuiFluidStackGroup guiFluidStackGroup;
private final Map<Class, GuiIngredientGroup> guiIngredientGroups;
@Nullable
private final RecipeTransferButton recipeTransferButton;
private final IRecipeWrapper recipeWrapper;
@Nullable
private final IFocus<?> focus;
@Nullable
private ShapelessIcon shapelessIcon;
private int posX;
private int posY;
public <T extends IRecipeWrapper> RecipeLayout(int index, IRecipeCategory<T> recipeCategory, T recipeWrapper, @Nullable IFocus focus, int posX, int posY) {
ErrorUtil.checkNotNull(recipeCategory, "recipeCategory");
ErrorUtil.checkNotNull(recipeWrapper, "recipeWrapper");
if (focus != null) {
focus = Focus.check(focus);
}
this.recipeCategory = recipeCategory;
this.focus = focus;
IFocus<ItemStack> itemStackFocus = null;
IFocus<FluidStack> fluidStackFocus = null;
if (focus != null) {
Object focusValue = focus.getValue();
if (focusValue instanceof ItemStack) {
//noinspection unchecked
itemStackFocus = (IFocus<ItemStack>) focus;
} else if (focusValue instanceof FluidStack) {
//noinspection unchecked
fluidStackFocus = (IFocus<FluidStack>) focus;
}
}
this.guiItemStackGroup = new GuiItemStackGroup(itemStackFocus, ingredientCycleOffset);
this.guiFluidStackGroup = new GuiFluidStackGroup(fluidStackFocus, ingredientCycleOffset);
this.guiIngredientGroups = new IdentityHashMap<Class, GuiIngredientGroup>();
this.guiIngredientGroups.put(ItemStack.class, this.guiItemStackGroup);
this.guiIngredientGroups.put(FluidStack.class, this.guiFluidStackGroup);
if (index >= 0) {
IDrawable plusIcon = Internal.getHelpers().getGuiHelper().getPlusSign();
this.recipeTransferButton = new RecipeTransferButton(recipeTransferButtonIndex + index, 0, 0, RECIPE_BUTTON_SIZE, RECIPE_BUTTON_SIZE, plusIcon, this);
} else {
this.recipeTransferButton = null;
}
setPosition(posX, posY);
this.recipeWrapper = recipeWrapper;
try {
IIngredients ingredients = new Ingredients();
recipeWrapper.getIngredients(ingredients);
recipeCategory.setRecipe(this, recipeWrapper, ingredients);
} catch (RuntimeException e) {
Log.error("Error caught from Recipe Category: {}", recipeCategory.getClass().getCanonicalName(), e);
} catch (LinkageError e) {
Log.error("Error caught from Recipe Category: {}", recipeCategory.getClass().getCanonicalName(), e);
}
}
@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.xPosition = posX + width + 2;
this.recipeTransferButton.yPosition = posY + height - RECIPE_BUTTON_SIZE;
}
}
@Override
public void draw(Minecraft minecraft, final int mouseX, final int mouseY) {
IDrawable background = recipeCategory.getBackground();
GlStateManager.color(1.0F, 1.0F, 1.0F, 1.0F);
GlStateManager.disableLighting();
GlStateManager.enableAlpha();
final int recipeMouseX = mouseX - posX;
final int recipeMouseY = mouseY - posY;
GlStateManager.pushMatrix();
GlStateManager.translate(posX, posY, 0.0F);
{
background.draw(minecraft);
recipeCategory.drawExtras(minecraft);
recipeWrapper.drawInfo(minecraft, background.getWidth(), background.getHeight(), recipeMouseX, recipeMouseY);
// drawExtras and drawInfo often render text which messes with the color, this clears it
GlStateManager.color(1, 1, 1, 1);
if (shapelessIcon != null) {
shapelessIcon.draw(minecraft, background.getWidth());
}
}
GlStateManager.popMatrix();
GuiIngredient hoveredIngredient = null;
for (GuiIngredientGroup guiIngredientGroup : guiIngredientGroups.values()) {
GuiIngredient hovered = guiIngredientGroup.draw(minecraft, posX, posY, mouseX, mouseY);
if (hovered != null) {
hoveredIngredient = hovered;
}
}
if (recipeTransferButton != null) {
recipeTransferButton.drawButton(minecraft, mouseX, mouseY);
}
GlStateManager.disableBlend();
GlStateManager.disableLighting();
if (hoveredIngredient != null) {
hoveredIngredient.drawHovered(minecraft, posX, posY, recipeMouseX, recipeMouseY);
} else if (isMouseOver(mouseX, mouseY)) {
List<String> tooltipStrings = new ArrayList<String>();
List<String> categoryTooltipStrings = LegacyUtil.getTooltipStrings(recipeCategory, recipeMouseX, recipeMouseY);
tooltipStrings.addAll(categoryTooltipStrings);
List<String> wrapperTooltips = recipeWrapper.getTooltipStrings(recipeMouseX, recipeMouseY);
//noinspection ConstantConditions
if (wrapperTooltips != null) {
tooltipStrings.addAll(wrapperTooltips);
}
if (tooltipStrings.isEmpty() && shapelessIcon != null) {
tooltipStrings = shapelessIcon.getTooltipStrings(recipeMouseX, recipeMouseY);
}
if (tooltipStrings != null && !tooltipStrings.isEmpty()) {
TooltipRenderer.drawHoveringText(minecraft, tooltipStrings, mouseX, mouseY);
}
}
GlStateManager.disableAlpha();
}
@Override
public boolean isMouseOver(int mouseX, int mouseY) {
final IDrawable background = recipeCategory.getBackground();
final Rectangle backgroundRect = new Rectangle(posX, posY, background.getWidth(), background.getHeight());
return backgroundRect.contains(mouseX, mouseY) ||
(recipeTransferButton != null && recipeTransferButton.isMouseOver());
}
@Override
@Nullable
public Object getIngredientUnderMouse(int mouseX, int mouseY) {
for (GuiIngredientGroup<?> guiIngredientGroup : guiIngredientGroups.values()) {
Object clicked = guiIngredientGroup.getIngredientUnderMouse(posX, posY, mouseX, mouseY);
if (clicked != null) {
return clicked;
}
}
return null;
}
public boolean handleClick(Minecraft minecraft, int mouseX, int mouseY, int mouseButton) {
return recipeWrapper.handleClick(minecraft, mouseX - posX, mouseY - posY, mouseButton);
}
@Override
public GuiItemStackGroup getItemStacks() {
return guiItemStackGroup;
}
@Override
public IGuiFluidStackGroup getFluidStacks() {
return guiFluidStackGroup;
}
@Override
public <T> IGuiIngredientGroup<T> getIngredientsGroup(Class<T> ingredientClass) {
//noinspection unchecked
GuiIngredientGroup<T> guiIngredientGroup = guiIngredientGroups.get(ingredientClass);
if (guiIngredientGroup == null) {
IFocus<T> focus = null;
if (this.focus != null) {
Object focusValue = this.focus.getValue();
if (ingredientClass.isInstance(focusValue)) {
//noinspection unchecked
focus = (IFocus<T>) this.focus;
}
}
guiIngredientGroup = new GuiIngredientGroup<T>(ingredientClass, focus, ingredientCycleOffset);
guiIngredientGroups.put(ingredientClass, guiIngredientGroup);
}
return guiIngredientGroup;
}
@Override
public void setRecipeTransferButton(int posX, int posY) {
if (recipeTransferButton != null) {
recipeTransferButton.xPosition = posX + this.posX;
recipeTransferButton.yPosition = posY + this.posY;
}
}
@Override
public void setShapeless() {
this.shapelessIcon = new ShapelessIcon();
}
@Override
@Nullable
public IFocus<?> getFocus() {
return focus;
}
@Nullable
public RecipeTransferButton getRecipeTransferButton() {
return recipeTransferButton;
}
public IRecipeCategory getRecipeCategory() {
return recipeCategory;
}
public int getPosX() {
return posX;
}
public int getPosY() {
return posY;
}
}