/
RecipeLayout.java
208 lines (176 loc) · 7.33 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
package mezz.jei.gui;
import javax.annotation.Nullable;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import mezz.jei.api.gui.IDrawable;
import mezz.jei.api.gui.IGuiFluidStackGroup;
import mezz.jei.api.gui.IGuiIngredientGroup;
import mezz.jei.api.gui.IRecipeLayout;
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.ingredients.GuiFluidStackGroup;
import mezz.jei.gui.ingredients.GuiIngredient;
import mezz.jei.gui.ingredients.GuiIngredientGroup;
import mezz.jei.gui.ingredients.GuiItemStackGroup;
import mezz.jei.input.IClickedIngredient;
import mezz.jei.util.Ingredients;
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 IRecipeLayout {
private static final int RECIPE_BUTTON_SIZE = 12;
public static final int recipeTransferButtonIndex = 100;
private final IRecipeCategory recipeCategory;
private final GuiItemStackGroup guiItemStackGroup;
private final GuiFluidStackGroup guiFluidStackGroup;
private final Map<Class, GuiIngredientGroup> guiIngredientGroups;
private final RecipeTransferButton recipeTransferButton;
private final IRecipeWrapper recipeWrapper;
private final IFocus<?> focus;
private final int posX;
private final int posY;
public <T extends IRecipeWrapper> RecipeLayout(int index, int posX, int posY, IRecipeCategory<T> recipeCategory, T recipeWrapper, IFocus focus) {
this.recipeCategory = recipeCategory;
this.focus = focus;
ItemStack itemStackFocus = null;
FluidStack fluidStackFocus = null;
Object focusValue = focus.getValue();
if (focusValue instanceof ItemStack) {
itemStackFocus = (ItemStack) focusValue;
} else if (focusValue instanceof FluidStack) {
fluidStackFocus = (FluidStack) focusValue;
}
this.guiItemStackGroup = new GuiItemStackGroup(new Focus<ItemStack>(focus.getMode(), itemStackFocus));
this.guiFluidStackGroup = new GuiFluidStackGroup(new Focus<FluidStack>(focus.getMode(), fluidStackFocus));
this.guiIngredientGroups = new HashMap<Class, GuiIngredientGroup>();
this.guiIngredientGroups.put(ItemStack.class, this.guiItemStackGroup);
this.guiIngredientGroups.put(FluidStack.class, this.guiFluidStackGroup);
int width = recipeCategory.getBackground().getWidth();
int height = recipeCategory.getBackground().getHeight();
this.recipeTransferButton = new RecipeTransferButton(recipeTransferButtonIndex + index, posX + width + 2, posY + height - RECIPE_BUTTON_SIZE, RECIPE_BUTTON_SIZE, RECIPE_BUTTON_SIZE, "+");
this.posX = posX;
this.posY = posY;
this.recipeWrapper = recipeWrapper;
try {
try {
IIngredients ingredients = new Ingredients();
recipeWrapper.getIngredients(ingredients);
recipeCategory.setRecipe(this, recipeWrapper, ingredients);
} catch (AbstractMethodError ignored) { // legacy
recipeCategory.setRecipe(this, recipeWrapper);
}
} 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);
}
}
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);
recipeCategory.drawAnimations(minecraft);
recipeWrapper.drawAnimations(minecraft, background.getWidth(), background.getHeight());
recipeWrapper.drawInfo(minecraft, background.getWidth(), background.getHeight(), recipeMouseX, recipeMouseY);
}
GlStateManager.popMatrix();
GuiIngredient hoveredIngredient = null;
for (GuiIngredientGroup guiIngredientGroup : guiIngredientGroups.values()) {
GuiIngredient hovered = guiIngredientGroup.draw(minecraft, posX, posY, mouseX, mouseY);
if (hovered != null) {
hoveredIngredient = hovered;
}
}
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 = recipeWrapper.getTooltipStrings(recipeMouseX, recipeMouseY);
if (tooltipStrings != null && !tooltipStrings.isEmpty()) {
TooltipRenderer.drawHoveringText(minecraft, tooltipStrings, mouseX, mouseY);
}
}
GlStateManager.disableAlpha();
}
public boolean isMouseOver(int mouseX, int mouseY) {
final int recipeMouseX = mouseX - posX;
final int recipeMouseY = mouseY - posY;
final IDrawable background = recipeCategory.getBackground();
return recipeMouseX >= 0 && recipeMouseX < background.getWidth() && recipeMouseY >= 0 && recipeMouseY < background.getHeight();
}
@Nullable
public IClickedIngredient<?> getIngredientUnderMouse(int mouseX, int mouseY) {
IClickedIngredient<?> clicked = guiItemStackGroup.getIngredientUnderMouse(posX, posY, mouseX, mouseY);
if (clicked == null) {
clicked = guiFluidStackGroup.getIngredientUnderMouse(posX, posY, mouseX, mouseY);
}
return clicked;
}
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) {
T value = null;
Object focusValue = this.focus.getValue();
if (ingredientClass.isInstance(focusValue)) {
//noinspection unchecked
value = (T) focusValue;
}
IFocus<T> focus = new Focus<T>(this.focus.getMode(), value);
guiIngredientGroup = new GuiIngredientGroup<T>(ingredientClass, focus);
guiIngredientGroups.put(ingredientClass, guiIngredientGroup);
}
return guiIngredientGroup;
}
@Override
public void setRecipeTransferButton(int posX, int posY) {
recipeTransferButton.xPosition = posX + this.posX;
recipeTransferButton.yPosition = posY + this.posY;
}
@Override
public IFocus<?> getFocus() {
return focus;
}
public RecipeTransferButton getRecipeTransferButton() {
return recipeTransferButton;
}
public IRecipeWrapper getRecipeWrapper() {
return recipeWrapper;
}
public IRecipeCategory getRecipeCategory() {
return recipeCategory;
}
public int getPosX() {
return posX;
}
public int getPosY() {
return posY;
}
}