-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathRecipeIndex.java
More file actions
188 lines (160 loc) · 6.03 KB
/
RecipeIndex.java
File metadata and controls
188 lines (160 loc) · 6.03 KB
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
package turing.tmb;
import net.minecraft.core.util.collection.Pair;
import turing.tmb.api.ingredient.ITypedIngredient;
import turing.tmb.api.recipe.*;
import turing.tmb.util.LookupContext;
import java.util.*;
import java.util.function.Function;
public class RecipeIndex implements IRecipeIndex {
private final TMBRuntime runtime;
protected final List<IRecipeCategory<?>> categories = new ArrayList<>();
protected final List<String> hiddenCategories = new ArrayList<>();
protected final Map<IRecipeCategory<?>, List<ITypedIngredient<?>>> catalysts = new HashMap<>();
protected final Map<IRecipeCategory<?>, List<IRecipeTranslator<?>>> recipeLists = new HashMap<>();
protected final Map<ILookupContext, List<Pair<IRecipeCategory<?>, IRecipeTranslator<?>>>> recipeLookupCache = new HashMap<>();
public RecipeIndex(TMBRuntime runtime) {
this.runtime = runtime;
}
protected void clear() {
catalysts.clear();
categories.clear();
recipeLists.clear();
recipeLookupCache.clear();
hiddenCategories.clear();
}
protected void loadLists() {
for (IRecipeCategory<?> category : categories) {
catalysts.computeIfAbsent(category, k -> new ArrayList<>());
recipeLists.computeIfAbsent(category, k -> new ArrayList<>());
}
}
public List<Pair<IRecipeCategory<?>, IRecipeTranslator<?>>> searchRecipes(ILookupContext context) {
List<Pair<IRecipeCategory<?>, IRecipeTranslator<?>>> existing = recipeLookupCache.get(context);
if (existing != null) {
return existing;
}
List<Pair<IRecipeCategory<?>, IRecipeTranslator<?>>> list = context.getRole() == RecipeIngredientRole.INPUT ? getRecipesCatalyst(context.getIngredient(), false) : new ArrayList<>();
for (Map.Entry<IRecipeCategory<?>, List<IRecipeTranslator<?>>> entry : recipeLists.entrySet()) {
if (!hiddenCategories.contains(entry.getKey().getName())) {
for (IRecipeTranslator<?> translator : entry.getValue()) {
boolean isIn = false;
switch (context.getRole()) {
case INPUT:
if (translator.isValidInput(context.getIngredient())) {
isIn = true;
}
break;
case OUTPUT:
if (translator.isOutput(context.getIngredient())) {
isIn = true;
}
break;
default:
break;
}
if (isIn) {
list.add(Pair.of(entry.getKey(), translator));
}
}
}
}
recipeLookupCache.put(context, list);
return list;
}
@Override
public List<Pair<IRecipeCategory<?>, IRecipeTranslator<?>>> getRecipesInput(ITypedIngredient<?> ingredient) {
return searchRecipes(new LookupContext(ingredient, RecipeIngredientRole.INPUT));
}
@Override
public List<Pair<IRecipeCategory<?>, IRecipeTranslator<?>>> getRecipesOutput(ITypedIngredient<?> ingredient) {
return searchRecipes(new LookupContext(ingredient, RecipeIngredientRole.OUTPUT));
}
@Override
public List<Pair<IRecipeCategory<?>, IRecipeTranslator<?>>> getRecipesCatalyst(ITypedIngredient<?> ingredient) {
return getRecipesCatalyst(ingredient, true);
}
public List<Pair<IRecipeCategory<?>, IRecipeTranslator<?>>> getRecipesCatalyst(ITypedIngredient<?> ingredient, boolean cache) {
ILookupContext context = new LookupContext(ingredient, RecipeIngredientRole.CATALYST);
List<Pair<IRecipeCategory<?>, IRecipeTranslator<?>>> pairList = recipeLookupCache.get(context);
if (pairList != null) {
return pairList;
} else {
pairList = new ArrayList<>();
}
List<IRecipeCategory<?>> categoryList = getCategoriesForCatalyst(ingredient);
for (IRecipeCategory<?> category : categoryList) {
if (!hiddenCategories.contains(category.getName())) {
for (IRecipeTranslator<?> translator : recipeLists.get(category)) {
pairList.add(Pair.of(category, translator));
}
}
}
if (cache) recipeLookupCache.put(context, pairList);
return pairList;
}
@Override
public List<IRecipeCategory<?>> getAllCategories() {
return Collections.unmodifiableList(categories);
}
@Override
public Map<IRecipeCategory<?>, List<IRecipeTranslator<?>>> getRecipeLists() {
return recipeLists;
}
@Override
public void registerCatalyst(IRecipeCategory<?> category, ITypedIngredient<?> catalyst) {
catalysts.get(category).add(catalyst);
}
@Override
public void hideCategory(String name) {
hiddenCategories.add(name);
}
@Override
public <T extends IRecipeCategory<?>> T registerCategory(T category) {
categories.add(category);
return category;
}
@Override
public <R, T extends IRecipeTranslator<R>> void registerRecipes(IRecipeCategory<T> category, Collection<R> recipes, Function<R, T> conv) {
for (R recipe : recipes) {
registerRecipe(category, recipe, conv);
}
}
@Override
@SuppressWarnings("unchecked")
public <R, T extends IRecipeTranslator<R>> void registerRecipe(IRecipeCategory<T> category, R recipe, Function<R, T> conv) {
List<T> list = (List<T>) recipeLists.get(category);
list.add(conv.apply(recipe));
}
@Override
public List<ITypedIngredient<?>> getCatalystsForCategory(IRecipeCategory<?> category) {
if (category == null) return Collections.emptyList();
return catalysts.get(category);
}
@Override
public List<IRecipeCategory<?>> getCategoriesForCatalyst(ITypedIngredient<?> ingredient) {
if (ingredient == null) return getAllCategories();
ILookupContext context = new LookupContext(ingredient, RecipeIngredientRole.CATALYST);
List<Pair<IRecipeCategory<?>, IRecipeTranslator<?>>> existing = recipeLookupCache.get(context);
List<IRecipeCategory<?>> list;
if (existing != null) {
list = new ArrayList<>(existing.size());
for (Pair<IRecipeCategory<?>, ?> pair : existing) {
list.add(pair.getLeft());
}
} else {
list = new ArrayList<>();
List<Pair<IRecipeCategory<?>, IRecipeTranslator<?>>> cacheList = new ArrayList<>();
for (IRecipeCategory<?> category : categories) {
List<ITypedIngredient<?>> catalystList = catalysts.get(category);
if (catalystList != null) {
if (catalystList.stream().anyMatch((i) -> i.hashCode() == ingredient.hashCode())) {
list.add(category);
cacheList.add(Pair.of(category, null));
}
}
}
recipeLookupCache.put(context, cacheList);
}
return list;
}
}