/
RecipeGuiLogic.java
265 lines (217 loc) · 6.56 KB
/
RecipeGuiLogic.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
package mezz.jei.gui;
import com.google.common.collect.ImmutableList;
import javax.annotation.Nonnull;
import javax.annotation.Nullable;
import java.util.ArrayList;
import java.util.Collections;
import java.util.List;
import java.util.Stack;
import net.minecraft.client.Minecraft;
import net.minecraft.inventory.Container;
import mezz.jei.api.JEIManager;
import mezz.jei.api.recipe.IRecipeCategory;
import mezz.jei.api.recipe.IRecipeHandler;
import mezz.jei.api.recipe.IRecipeWrapper;
import mezz.jei.util.Log;
import mezz.jei.util.MathUtil;
public class RecipeGuiLogic implements IRecipeGuiLogic {
private static class State {
/* The focus of this GUI */
@Nonnull
public final Focus focus;
public int recipeCategoryIndex;
public int pageIndex;
public State(@Nonnull Focus focus, int recipeCategoryIndex, int pageIndex) {
this.focus = focus;
this.recipeCategoryIndex = recipeCategoryIndex;
this.pageIndex = pageIndex;
}
}
/* The current state of this GUI */
@Nullable
private State state = null;
/* The previous states of this GUI */
@Nonnull
private Stack<State> history = new Stack<>();
/* List of Recipe Categories that involve the focus */
@Nonnull
private ImmutableList<IRecipeCategory> recipeCategories = ImmutableList.of();
/* List of recipes for the currently selected recipeClass */
@Nonnull
private List<Object> recipes = Collections.emptyList();
private int recipesPerPage = 0;
@Override
public boolean setFocus(@Nonnull Focus focus) {
return setFocus(focus, true);
}
@Override
public boolean back() {
if (history.empty()) {
return false;
}
State state = history.pop();
if (setFocus(state.focus, false)) {
this.state = state;
return true;
}
return false;
}
private boolean setFocus(@Nonnull Focus focus, boolean saveHistory) {
if (this.state != null && this.state.focus.equals(focus)) {
return true;
}
ImmutableList<IRecipeCategory> types = focus.getCategories();
if (types.isEmpty()) {
return false;
}
this.recipeCategories = types;
int recipeCategoryIndex = 0;
Container container = Minecraft.getMinecraft().thePlayer.openContainer;
if (container != null) {
for (int i = 0; i < recipeCategories.size(); i++) {
IRecipeCategory recipeCategory = recipeCategories.get(i);
if (JEIManager.recipeRegistry.getRecipeTransferHelper(container, recipeCategory) != null) {
recipeCategoryIndex = i;
break;
}
}
}
if (this.state != null && saveHistory) {
history.push(this.state);
}
this.state = new State(focus, recipeCategoryIndex, 0);
updateRecipes();
return true;
}
@Override
public boolean setCategoryFocus() {
IRecipeCategory recipeCategory = getRecipeCategory();
if (recipeCategory == null) {
return false;
}
if (this.state != null) {
if (this.state.focus.isBlank()) {
return false;
}
history.push(this.state);
}
this.recipeCategories = JEIManager.recipeRegistry.getRecipeCategories();
int recipeCategoryIndex = this.recipeCategories.indexOf(recipeCategory);
this.state = new State(new Focus(), recipeCategoryIndex, 0);
updateRecipes();
return true;
}
@Override
public void setRecipesPerPage(int recipesPerPage) {
if (state == null) {
return;
}
if (this.recipesPerPage != recipesPerPage) {
int recipeIndex = state.pageIndex * this.recipesPerPage;
state.pageIndex = recipeIndex / recipesPerPage;
this.recipesPerPage = recipesPerPage;
updateRecipes();
}
}
private void updateRecipes() {
if (state == null) {
return;
}
IRecipeCategory recipeCategory = getRecipeCategory();
recipes = state.focus.getRecipes(recipeCategory);
}
@Override
public IRecipeCategory getRecipeCategory() {
if (state == null || recipeCategories.size() == 0) {
return null;
}
return recipeCategories.get(state.recipeCategoryIndex);
}
@Override
@Nonnull
public List<RecipeLayout> getRecipeWidgets(int posX, int posY, int spacingY) {
if (state == null) {
return Collections.emptyList();
}
List<RecipeLayout> recipeWidgets = new ArrayList<>();
IRecipeCategory recipeCategory = getRecipeCategory();
if (recipeCategory == null) {
return recipeWidgets;
}
int recipeWidgetIndex = 0;
for (int recipeIndex = state.pageIndex * recipesPerPage; recipeIndex < recipes.size() && recipeWidgets.size() < recipesPerPage; recipeIndex++) {
Object recipe = recipes.get(recipeIndex);
IRecipeHandler recipeHandler = JEIManager.recipeRegistry.getRecipeHandler(recipe.getClass());
if (recipeHandler == null) {
Log.error("Couldn't find recipe handler for recipe: {}", recipe);
continue;
}
@SuppressWarnings("unchecked")
IRecipeWrapper recipeWrapper = recipeHandler.getRecipeWrapper(recipe);
RecipeLayout recipeWidget = new RecipeLayout(recipeWidgetIndex++, posX, posY, recipeCategory, recipeWrapper, state.focus);
recipeWidgets.add(recipeWidget);
posY += spacingY;
}
return recipeWidgets;
}
@Override
public void nextRecipeCategory() {
if (state == null) {
return;
}
int recipesTypesCount = recipeCategories.size();
state.recipeCategoryIndex = (state.recipeCategoryIndex + 1) % recipesTypesCount;
state.pageIndex = 0;
updateRecipes();
}
@Override
public boolean hasMultiplePages() {
return recipes.size() > recipesPerPage;
}
@Override
public void previousRecipeCategory() {
if (state == null) {
return;
}
int recipesTypesCount = recipeCategories.size();
state.recipeCategoryIndex = (recipesTypesCount + state.recipeCategoryIndex - 1) % recipesTypesCount;
state.pageIndex = 0;
updateRecipes();
}
@Override
public void nextPage() {
if (state == null) {
return;
}
int pageCount = pageCount(recipesPerPage);
state.pageIndex = (state.pageIndex + 1) % pageCount;
updateRecipes();
}
@Override
public void previousPage() {
if (state == null) {
return;
}
int pageCount = pageCount(recipesPerPage);
state.pageIndex = (pageCount + state.pageIndex - 1) % pageCount;
updateRecipes();
}
private int pageCount(int recipesPerPage) {
if (recipes.size() <= 1) {
return 1;
}
return MathUtil.divideCeil(recipes.size(), recipesPerPage);
}
@Override
@Nonnull
public String getPageString() {
if (state == null) {
return "1/1";
}
return (state.pageIndex + 1) + "/" + pageCount(recipesPerPage);
}
@Override
public boolean hasMultipleCategories() {
return recipeCategories.size() > 1;
}
}