/
GuiIngredientGroup.java
143 lines (128 loc) · 4.68 KB
/
GuiIngredientGroup.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
package mezz.jei.gui.ingredients;
import javax.annotation.Nullable;
import java.util.ArrayList;
import java.util.Collection;
import java.util.Collections;
import java.util.HashMap;
import java.util.HashSet;
import java.util.List;
import java.util.Map;
import java.util.Set;
import mezz.jei.Internal;
import mezz.jei.api.gui.IGuiIngredientGroup;
import mezz.jei.api.gui.ITooltipCallback;
import mezz.jei.api.ingredients.IIngredientHelper;
import mezz.jei.api.ingredients.IIngredientRenderer;
import mezz.jei.api.ingredients.IIngredients;
import mezz.jei.api.recipe.IFocus;
import mezz.jei.input.ClickedIngredient;
import mezz.jei.input.IClickedIngredient;
import mezz.jei.util.Log;
import net.minecraft.client.Minecraft;
public class GuiIngredientGroup<T> implements IGuiIngredientGroup<T> {
private final int itemCycleOffset = (int) (Math.random() * 1000);
private final Map<Integer, GuiIngredient<T>> guiIngredients = new HashMap<Integer, GuiIngredient<T>>();
private final Set<Integer> inputSlots = new HashSet<Integer>();
private final IIngredientHelper<T> ingredientHelper;
private final Class<T> ingredientClass;
/**
* If focus is set and any of the guiIngredients contains focus
* they will only display focus instead of rotating through all their values.
*/
private final IFocus<T> focus;
@Nullable
private ITooltipCallback<T> tooltipCallback;
public GuiIngredientGroup(Class<T> ingredientClass, IFocus<T> focus) {
this.ingredientClass = ingredientClass;
this.focus = focus;
this.ingredientHelper = Internal.getIngredientRegistry().getIngredientHelper(ingredientClass);
}
@Override
public void init(int slotIndex, boolean input, IIngredientRenderer<T> ingredientRenderer, int xPosition, int yPosition, int width, int height, int xPadding, int yPadding) {
GuiIngredient<T> guiIngredient = new GuiIngredient<T>(slotIndex, input, ingredientRenderer, ingredientHelper, xPosition, yPosition, width, height, xPadding, yPadding, itemCycleOffset);
guiIngredients.put(slotIndex, guiIngredient);
if (input) {
inputSlots.add(slotIndex);
}
}
@Override
public IFocus<T> getFocus() {
return focus;
}
@Override
public void set(IIngredients ingredients) {
List<List<T>> inputs = ingredients.getInputs(ingredientClass);
List<T> outputs = ingredients.getOutputs(ingredientClass);
int inputIndex = 0;
int outputIndex = 0;
List<Integer> slots = new ArrayList<Integer>(guiIngredients.keySet());
Collections.sort(slots);
for (Integer slot : slots) {
if (inputSlots.contains(slot)) {
if (inputIndex < inputs.size()) {
List<T> input = inputs.get(inputIndex);
inputIndex++;
set(slot, input);
}
} else {
if (outputIndex < outputs.size()) {
T output = outputs.get(outputIndex);
outputIndex++;
set(slot, output);
}
}
}
}
@Override
public void set(int slotIndex, Collection<T> values) {
set(slotIndex, new ArrayList<T>(values));
}
@Override
public void set(int slotIndex, List<T> ingredients) {
// Sanitize API input
for (T ingredient : ingredients) {
if (!ingredientClass.isInstance(ingredient) && ingredient != null) {
Log.error("Received wrong type of ingredient. Expected {}, got {}", ingredientClass, ingredient.getClass(), new IllegalArgumentException());
return;
}
}
guiIngredients.get(slotIndex).set(ingredients, focus);
}
@Override
public void set(int slotIndex, T value) {
set(slotIndex, Collections.singletonList(value));
}
@Override
public void addTooltipCallback(ITooltipCallback<T> tooltipCallback) {
this.tooltipCallback = tooltipCallback;
}
@Override
public Map<Integer, GuiIngredient<T>> getGuiIngredients() {
return guiIngredients;
}
@Nullable
public IClickedIngredient<T> getIngredientUnderMouse(int xOffset, int yOffset, int mouseX, int mouseY) {
for (GuiIngredient<T> guiIngredient : guiIngredients.values()) {
if (guiIngredient != null && guiIngredient.isMouseOver(xOffset, yOffset, mouseX, mouseY)) {
T displayedIngredient = guiIngredient.getDisplayedIngredient();
if (displayedIngredient != null) {
return new ClickedIngredient<T>(displayedIngredient);
}
}
}
return null;
}
@Nullable
public GuiIngredient<T> draw(Minecraft minecraft, int xOffset, int yOffset, int mouseX, int mouseY) {
GuiIngredient<T> hovered = null;
for (GuiIngredient<T> ingredient : guiIngredients.values()) {
if (hovered == null && ingredient.isMouseOver(xOffset, yOffset, mouseX, mouseY)) {
hovered = ingredient;
hovered.setTooltipCallback(tooltipCallback);
} else {
ingredient.draw(minecraft, xOffset, yOffset);
}
}
return hovered;
}
}