/
RecipeGuiLogic.java
180 lines (147 loc) · 4.53 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
package mezz.jei.gui;
import com.google.common.collect.ImmutableList;
import javax.annotation.Nonnull;
import java.util.ArrayList;
import java.util.List;
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 {
/* Whether this GUI is dispzzlaying input or output recipes */
private Mode mode;
/* The focus of this GUI */
@Nonnull
private Focus focus = new Focus();
/* 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 ImmutableList<Object> recipes = ImmutableList.of();
private int recipesPerPage = 0;
private int recipeCategoryIndex = 0;
private int pageIndex = 0;
@Override
public boolean setFocus(@Nonnull Focus focus, @Nonnull Mode mode) {
if (this.focus.equals(focus) && this.mode == mode) {
return true;
}
ImmutableList<IRecipeCategory> types = null;
switch (mode) {
case INPUT:
types = focus.getCategoriesWithInput();
break;
case OUTPUT:
types = focus.getCategoriesWithOutput();
break;
}
if (types.isEmpty()) {
return false;
}
this.recipeCategories = types;
this.focus = focus;
this.mode = mode;
this.recipeCategoryIndex = 0;
this.pageIndex = 0;
updateRecipes();
return true;
}
@Override
public void setRecipesPerPage(int recipesPerPage) {
if (this.recipesPerPage != recipesPerPage) {
int recipeIndex = pageIndex * this.recipesPerPage;
this.pageIndex = recipeIndex / recipesPerPage;
this.recipesPerPage = recipesPerPage;
updateRecipes();
}
}
private void updateRecipes() {
IRecipeCategory recipeCategory = getRecipeCategory();
switch (mode) {
case INPUT:
recipes = focus.getRecipesWithInput(recipeCategory);
break;
case OUTPUT:
recipes = focus.getRecipesWithOutput(recipeCategory);
break;
}
}
@Override
public IRecipeCategory getRecipeCategory() {
if (recipeCategories.size() == 0) {
return null;
}
return recipeCategories.get(recipeCategoryIndex);
}
@Override
@Nonnull
public List<RecipeWidget> getRecipeWidgets() {
List<RecipeWidget> recipeWidgets = new ArrayList<>();
IRecipeCategory recipeCategory = getRecipeCategory();
if (recipeCategory == null) {
return recipeWidgets;
}
for (int recipeIndex = 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;
}
RecipeWidget recipeWidget = new RecipeWidget(recipeCategory);
@SuppressWarnings("unchecked")
IRecipeWrapper recipeWrapper = recipeHandler.getRecipeWrapper(recipe);
recipeWidget.setRecipe(recipeWrapper, focus);
recipeWidgets.add(recipeWidget);
}
return recipeWidgets;
}
@Override
public void nextRecipeCategory() {
int recipesTypesCount = recipeCategories.size();
recipeCategoryIndex = (recipeCategoryIndex + 1) % recipesTypesCount;
pageIndex = 0;
updateRecipes();
}
@Override
public boolean hasMultiplePages() {
return recipes.size() > recipesPerPage;
}
@Override
public void previousRecipeCategory() {
int recipesTypesCount = recipeCategories.size();
recipeCategoryIndex = (recipesTypesCount + recipeCategoryIndex - 1) % recipesTypesCount;
pageIndex = 0;
updateRecipes();
}
@Override
public void nextPage() {
int pageCount = pageCount(recipesPerPage);
pageIndex = (pageIndex + 1) % pageCount;
updateRecipes();
}
@Override
public void previousPage() {
int pageCount = pageCount(recipesPerPage);
pageIndex = (pageCount + 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() {
return (pageIndex + 1) + "/" + pageCount(recipesPerPage);
}
@Override
public boolean hasMultipleCategories() {
return recipeCategories.size() > 1;
}
}