/
RecipeCatalysts.java
155 lines (134 loc) · 5.3 KB
/
RecipeCatalysts.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
package mezz.jei.gui.recipes;
import com.mojang.blaze3d.matrix.MatrixStack;
import javax.annotation.Nullable;
import java.util.ArrayList;
import java.util.Collections;
import java.util.List;
import com.mojang.blaze3d.systems.RenderSystem;
import net.minecraft.client.renderer.Rectangle2d;
import mezz.jei.Internal;
import mezz.jei.api.ingredients.IIngredientHelper;
import mezz.jei.api.ingredients.IIngredientRenderer;
import mezz.jei.gui.elements.DrawableNineSliceTexture;
import mezz.jei.gui.ingredients.GuiIngredient;
import mezz.jei.gui.textures.Textures;
import mezz.jei.ingredients.IngredientManager;
import mezz.jei.input.ClickedIngredient;
import mezz.jei.input.IClickedIngredient;
import mezz.jei.input.IShowsRecipeFocuses;
import mezz.jei.util.MathUtil;
/**
* The area drawn on left side of the {@link RecipesGui} that shows which items can craft the current recipe category.
*/
public class RecipeCatalysts implements IShowsRecipeFocuses {
private static final int ingredientSize = 16;
private static final int ingredientBorderSize = 1;
private static final int borderSize = 5;
private static final int overlapSize = 6;
private final DrawableNineSliceTexture backgroundTab;
private final List<GuiIngredient<Object>> ingredients;
private final DrawableNineSliceTexture slotBackground;
private int left = 0;
private int top = 0;
private int width = 0;
private int height = 0;
public RecipeCatalysts() {
ingredients = new ArrayList<>();
Textures textures = Internal.getTextures();
backgroundTab = textures.getCatalystTab();
slotBackground = textures.getNineSliceSlot();
}
public boolean isEmpty() {
return this.ingredients.isEmpty();
}
public int getWidth() {
return width - overlapSize;
}
public void updateLayout(List<Object> ingredients, RecipesGui recipesGui) {
this.ingredients.clear();
if (!ingredients.isEmpty()) {
int availableHeight = recipesGui.getYSize() - 8;
int borderHeight = (2 * borderSize) + (2 * ingredientBorderSize);
int maxIngredientsPerColumn = (availableHeight - borderHeight) / ingredientSize;
int columnCount = MathUtil.divideCeil(ingredients.size(), maxIngredientsPerColumn);
maxIngredientsPerColumn = MathUtil.divideCeil(ingredients.size(), columnCount);
width = (2 * ingredientBorderSize) + (borderSize * 2) + (columnCount * ingredientSize);
height = (2 * ingredientBorderSize) + (borderSize * 2) + (maxIngredientsPerColumn * ingredientSize);
top = recipesGui.getGuiTop();
left = recipesGui.getGuiLeft() - width + overlapSize; // overlaps the recipe gui slightly
for (int i = 0; i < ingredients.size(); i++) {
Object ingredientForSlot = ingredients.get(i);
GuiIngredient<Object> guiIngredient = createGuiIngredient(ingredientForSlot, i, maxIngredientsPerColumn);
this.ingredients.add(guiIngredient);
}
}
}
private <T> GuiIngredient<T> createGuiIngredient(T ingredient, int index, int maxIngredientsPerColumn) {
IngredientManager ingredientManager = Internal.getIngredientManager();
IIngredientRenderer<T> ingredientRenderer = ingredientManager.getIngredientRenderer(ingredient);
IIngredientHelper<T> ingredientHelper = ingredientManager.getIngredientHelper(ingredient);
int column = index / maxIngredientsPerColumn;
int row = index % maxIngredientsPerColumn;
Rectangle2d rect = new Rectangle2d(
left + borderSize + (column * ingredientSize) + ingredientBorderSize,
top + borderSize + (row * ingredientSize) + ingredientBorderSize,
ingredientSize,
ingredientSize
);
GuiIngredient<T> guiIngredient = new GuiIngredient<>(index, true, ingredientRenderer, ingredientHelper, rect, 0, 0, 0);
guiIngredient.set(Collections.singletonList(ingredient), null);
return guiIngredient;
}
@SuppressWarnings("deprecation")
@Nullable
public GuiIngredient<?> draw(MatrixStack matrixStack, int mouseX, int mouseY) {
int ingredientCount = ingredients.size();
if (ingredientCount > 0) {
RenderSystem.color4f(1.0F, 1.0F, 1.0F, 1.0F);
RenderSystem.disableDepthTest();
RenderSystem.enableAlphaTest();
{
int slotWidth = width - (2 * borderSize);
int slotHeight = height - (2 * borderSize);
backgroundTab.draw(matrixStack, this.left, this.top, width, height);
slotBackground.draw(matrixStack, this.left + borderSize, this.top + borderSize, slotWidth, slotHeight);
}
RenderSystem.disableAlphaTest();
RenderSystem.enableDepthTest();
GuiIngredient<?> hovered = null;
for (GuiIngredient<?> guiIngredient : this.ingredients) {
if (guiIngredient.isMouseOver(0, 0, mouseX, mouseY)) {
hovered = guiIngredient;
}
guiIngredient.draw(matrixStack, 0, 0);
}
return hovered;
}
return null;
}
@Nullable
private GuiIngredient<?> getHovered(double mouseX, double mouseY) {
for (GuiIngredient<?> guiIngredient : this.ingredients) {
if (guiIngredient.isMouseOver(0, 0, mouseX, mouseY)) {
return guiIngredient;
}
}
return null;
}
@Nullable
@Override
public IClickedIngredient<?> getIngredientUnderMouse(double mouseX, double mouseY) {
GuiIngredient<?> hovered = getHovered(mouseX, mouseY);
if (hovered != null) {
Object ingredientUnderMouse = hovered.getDisplayedIngredient();
if (ingredientUnderMouse != null) {
return ClickedIngredient.create(ingredientUnderMouse, hovered.getRect());
}
}
return null;
}
@Override
public boolean canSetFocusWithMouse() {
return true;
}
}