/
IngredientLookupMemory.java
291 lines (255 loc) · 10.1 KB
/
IngredientLookupMemory.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
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
package mezz.jei.gui.ingredients;
import java.io.File;
import java.io.IOException;
import java.io.InputStreamReader;
import java.io.OutputStreamWriter;
import java.util.Collections;
import java.util.HashSet;
import java.util.List;
import java.util.Map;
import java.util.Set;
import java.util.zip.ZipInputStream;
import java.util.zip.ZipOutputStream;
import com.google.common.collect.HashBasedTable;
import com.google.common.collect.ImmutableCollection;
import com.google.common.collect.Table;
import com.google.gson.stream.JsonReader;
import com.google.gson.stream.JsonWriter;
import mezz.jei.api.ingredients.IIngredientHelper;
import mezz.jei.api.ingredients.IIngredientRegistry;
import mezz.jei.api.recipe.IFocus;
import mezz.jei.api.recipe.IRecipeCategory;
import mezz.jei.config.Config;
import mezz.jei.gui.Focus;
import mezz.jei.gui.recipes.RecipeClickableArea;
import mezz.jei.gui.recipes.RecipesGui;
import mezz.jei.recipes.RecipeRegistry;
import mezz.jei.util.ErrorUtil;
import mezz.jei.util.FileUtil;
import mezz.jei.util.Log;
import net.minecraft.client.Minecraft;
import net.minecraft.client.entity.EntityPlayerSP;
import net.minecraft.client.gui.GuiScreen;
import net.minecraft.client.gui.inventory.GuiContainer;
import net.minecraft.inventory.Container;
/**
* Keeps track of what the player was viewing at last time they looked up the recipes or uses of an ingredient.
*/
public class IngredientLookupMemory {
private final Table<IFocus.Mode, String, IngredientLookupState> mostRecentLookups = HashBasedTable.create();
private final Table<IFocus.Mode, String, RecipeLookupFromFile> mostRecentLookupsFromFile = HashBasedTable.create();
private final RecipeRegistry recipeRegistry;
private final IIngredientRegistry ingredientRegistry;
private final Object saveLock = new Object();
private boolean needsSaving = false;
private boolean saving = false;
public IngredientLookupMemory(RecipeRegistry recipeRegistry, IIngredientRegistry ingredientRegistry) {
this.recipeRegistry = recipeRegistry;
this.ingredientRegistry = ingredientRegistry;
readFromFile();
}
public <T> IngredientLookupState getState(IFocus<T> focus, List<IRecipeCategory> recipeCategories) {
ErrorUtil.checkNotEmpty(recipeCategories, "recipeCategories");
focus = Focus.check(focus);
final IFocus.Mode focusMode = focus.getMode();
final T value = focus.getValue();
final IIngredientHelper<T> ingredientHelper = this.ingredientRegistry.getIngredientHelper(value);
final String uniqueId = ingredientHelper.getUniqueId(value);
IngredientLookupState state = this.mostRecentLookups.get(focusMode, uniqueId);
if (state == null) {
RecipeLookupFromFile stateFromFile = this.mostRecentLookupsFromFile.get(focusMode, uniqueId);
if (stateFromFile != null) {
final int categoryIndex = getRecipeCategoryIndex(recipeCategories, stateFromFile.categoryUid);
if (categoryIndex >= 0) {
IRecipeCategory recipeCategory = recipeCategories.get(categoryIndex);
if (stateFromFile.recipeIndex < this.recipeRegistry.getRecipeWrappers(recipeCategory, focus).size()) {
state = new IngredientLookupState(focus, recipeCategories, categoryIndex, stateFromFile.recipeIndex);
}
}
}
}
if (state == null) {
state = new IngredientLookupState(focus, recipeCategories, 0, 0);
}
final Set<Integer> indexesForOpenContainer = getRecipeCategoryIndexesForOpenContainer(state.getRecipeCategories());
if (!indexesForOpenContainer.isEmpty() && !indexesForOpenContainer.contains(state.getRecipeCategoryIndex())) {
Integer index = Collections.min(indexesForOpenContainer);
state.setRecipeCategoryIndex(index);
}
if (this.mostRecentLookups.put(focusMode, uniqueId, state) != state) {
markDirty();
}
if (this.mostRecentLookupsFromFile.remove(focusMode, uniqueId) != null) {
markDirty();
}
return state;
}
private Set<Integer> getRecipeCategoryIndexesForOpenContainer(List<IRecipeCategory> recipeCategories) {
final Set<Integer> indexes = new HashSet<Integer>();
final Minecraft minecraft = Minecraft.getMinecraft();
final EntityPlayerSP player = minecraft.player;
if (player != null) {
final Container container = player.openContainer;
if (container != null) {
for (int i = 0; i < recipeCategories.size(); i++) {
IRecipeCategory recipeCategory = recipeCategories.get(i);
if (this.recipeRegistry.getRecipeTransferHandler(container, recipeCategory) != null) {
indexes.add(i);
}
}
}
GuiScreen screen = minecraft.currentScreen;
if (screen instanceof RecipesGui) {
RecipesGui recipesGui = (RecipesGui) screen;
screen = recipesGui.getParentScreen();
}
if (screen instanceof GuiContainer) {
final GuiContainer guiContainer = (GuiContainer) screen;
final ImmutableCollection<RecipeClickableArea> clickableAreas = this.recipeRegistry.getAllRecipeClickableAreas(guiContainer);
final Set<String> recipeCategoryUids = new HashSet<String>();
for (RecipeClickableArea clickableArea : clickableAreas) {
recipeCategoryUids.addAll(clickableArea.getRecipeCategoryUids());
}
for (int i = 0; i < recipeCategories.size(); i++) {
IRecipeCategory recipeCategory = recipeCategories.get(i);
if (recipeCategoryUids.contains(recipeCategory.getUid())) {
indexes.add(i);
}
}
}
}
return indexes;
}
private int getRecipeCategoryIndex(List<IRecipeCategory> recipeCategories, String recipeCategoryUid) {
for (int i = 0; i < recipeCategories.size(); i++) {
IRecipeCategory recipeCategory = recipeCategories.get(i);
if (recipeCategory.getUid().equals(recipeCategoryUid)) {
return i;
}
}
return -1;
}
private void readFromFile() {
final File file = new File(Config.getJeiConfigurationDir(), "lookupHistory.zip");
FileUtil.readZipFileSafely(file, "lookupHistory.json", new FileUtil.ZipInputFileOperation() {
@Override
public void handle(ZipInputStream zipInputStream) throws IOException {
final JsonReader jsonReader = new JsonReader(new InputStreamReader(zipInputStream));
jsonReader.beginObject();
while (jsonReader.hasNext()) {
final String name = jsonReader.nextName();
if (name.equals("lookupStates")) {
readLookupStatesObject(jsonReader);
}
}
jsonReader.endObject();
}
});
}
private void readLookupStatesObject(JsonReader jsonReader) throws IOException {
jsonReader.beginObject();
while (jsonReader.hasNext()) {
final String focusModeName = jsonReader.nextName();
final IFocus.Mode focusMode = IFocus.Mode.valueOf(focusModeName);
jsonReader.beginArray();
while (jsonReader.hasNext()) {
jsonReader.beginArray();
String ingredientString = jsonReader.nextString();
String category = jsonReader.nextString();
int recipeIndex = jsonReader.nextInt();
jsonReader.endArray();
RecipeLookupFromFile recipeLookupFromFile = new RecipeLookupFromFile(category, recipeIndex);
this.mostRecentLookupsFromFile.put(focusMode, ingredientString, recipeLookupFromFile);
}
jsonReader.endArray();
}
jsonReader.endObject();
}
public void markDirty() {
this.needsSaving = true;
}
public void saveToFile() {
synchronized (this.saveLock) {
if (this.needsSaving && !this.saving) {
this.saving = true;
this.needsSaving = false;
saveToFileAsync();
}
}
}
private void saveToFileAsync() {
final Table<IFocus.Mode, String, IngredientLookupState> mostRecentLookups = HashBasedTable.create(this.mostRecentLookups);
final Table<IFocus.Mode, String, RecipeLookupFromFile> mostRecentLookupsFromFile = HashBasedTable.create(this.mostRecentLookupsFromFile);
new Thread(new Runnable() {
@Override
public void run() {
saveToFileSync(mostRecentLookups, mostRecentLookupsFromFile);
synchronized (saveLock) {
saving = false;
}
}
}).start();
}
private static void saveToFileSync(
final Table<IFocus.Mode, String, IngredientLookupState> mostRecentLookups,
final Table<IFocus.Mode, String, RecipeLookupFromFile> mostRecentLookupsFromFile) {
final File file = new File(Config.getJeiConfigurationDir(), "lookupHistory.zip");
final boolean write = FileUtil.writeZipFileSafely(file, "lookupHistory.json", new FileUtil.ZipOutputFileOperation() {
@Override
public void handle(ZipOutputStream zipOutputStream) throws IOException {
JsonWriter jsonWriter = new JsonWriter(new OutputStreamWriter(zipOutputStream));
jsonWriter.beginObject();
{
writeLookupStatesObject(jsonWriter, mostRecentLookups, mostRecentLookupsFromFile);
}
jsonWriter.endObject();
jsonWriter.flush();
}
});
if (write) {
Log.debug("Saved IngredientLookupMemory to {}.", file.getAbsoluteFile());
}
}
private static void writeLookupStatesObject(
JsonWriter jsonWriter,
Table<IFocus.Mode, String, IngredientLookupState> mostRecentLookups,
Table<IFocus.Mode, String, RecipeLookupFromFile> mostRecentLookupsFromFile
) throws IOException {
jsonWriter.name("lookupStates")
.beginObject();
for (IFocus.Mode focusMode : IFocus.Mode.values()) {
jsonWriter.name(focusMode.toString())
.beginArray();
for (Map.Entry<String, IngredientLookupState> lookups : mostRecentLookups.row(focusMode).entrySet()) {
String ingredientString = lookups.getKey();
IngredientLookupState state = lookups.getValue();
int recipeCategoryIndex = state.getRecipeCategoryIndex();
String categoryUid = state.getRecipeCategories().get(recipeCategoryIndex).getUid();
jsonWriter.beginArray()
.value(ingredientString)
.value(categoryUid)
.value(state.getRecipeIndex())
.endArray();
}
for (Map.Entry<String, IngredientLookupMemory.RecipeLookupFromFile> lookups : mostRecentLookupsFromFile.row(focusMode).entrySet()) {
String ingredientString = lookups.getKey();
IngredientLookupMemory.RecipeLookupFromFile state = lookups.getValue();
jsonWriter.beginArray()
.value(ingredientString)
.value(state.categoryUid)
.value(state.recipeIndex)
.endArray();
}
jsonWriter.endArray();
}
jsonWriter.endObject();
}
private static class RecipeLookupFromFile {
private final String categoryUid;
private final int recipeIndex;
public RecipeLookupFromFile(String categoryUid, int recipeIndex) {
this.categoryUid = categoryUid;
this.recipeIndex = recipeIndex;
}
}
}