/
ItemFilter.java
173 lines (148 loc) · 5.92 KB
/
ItemFilter.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
package mezz.jei;
import javax.annotation.Nullable;
import java.util.ArrayList;
import java.util.List;
import com.google.common.base.Predicate;
import com.google.common.cache.CacheBuilder;
import com.google.common.cache.CacheLoader;
import com.google.common.cache.LoadingCache;
import com.google.common.cache.Weigher;
import com.google.common.collect.ImmutableList;
import mezz.jei.config.Config;
import mezz.jei.gui.ingredients.IIngredientListElement;
import net.minecraft.item.ItemStack;
public class ItemFilter {
/**
* A cache for fast searches while typing or using backspace. Maps filterText to filteredItemMaps
*/
private final LoadingCache<String, ImmutableList<IIngredientListElement>> filteredItemMapsCache = CacheBuilder.newBuilder()
.maximumWeight(16)
.weigher(new OneWeigher())
.concurrencyLevel(1)
.build(new ItemFilterCacheLoader());
private ImmutableList<IIngredientListElement> baseList;
public ItemFilter() {
this.baseList = IngredientBaseListFactory.create();
}
public void rebuild() {
this.baseList = IngredientBaseListFactory.create();
this.filteredItemMapsCache.invalidateAll();
}
public ImmutableList<IIngredientListElement> getIngredientList() {
String[] filters = Config.getFilterText().split("\\|");
if (filters.length == 1) {
String filter = filters[0];
return filteredItemMapsCache.getUnchecked(filter);
} else {
ImmutableList.Builder<IIngredientListElement> ingredientList = ImmutableList.builder();
for (String filter : filters) {
List<IIngredientListElement> ingredients = filteredItemMapsCache.getUnchecked(filter);
ingredientList.addAll(ingredients);
}
return ingredientList.build();
}
}
/**
* {@link #getItemStacks()} is slow, so cache the previous value in case someone requests it often.
*/
private ImmutableList<ItemStack> itemStacksCached = ImmutableList.of();
@Nullable
private String filterCached;
public ImmutableList<ItemStack> getItemStacks() {
if (!Config.getFilterText().equals(filterCached)) {
ImmutableList.Builder<ItemStack> filteredStacks = ImmutableList.builder();
for (IIngredientListElement element : getIngredientList()) {
Object ingredient = element.getIngredient();
if (ingredient instanceof ItemStack) {
filteredStacks.add((ItemStack) ingredient);
}
}
itemStacksCached = filteredStacks.build();
filterCached = Config.getFilterText();
}
return itemStacksCached;
}
public int size() {
return getIngredientList().size();
}
private static class OneWeigher implements Weigher<String, ImmutableList<IIngredientListElement>> {
public int weigh(String key, ImmutableList<IIngredientListElement> value) {
return 1;
}
}
private class ItemFilterCacheLoader extends CacheLoader<String, ImmutableList<IIngredientListElement>> {
@Override
public ImmutableList<IIngredientListElement> load(final String filterText) throws Exception {
if (filterText.length() == 0) {
return baseList;
}
// Recursive.
// Find a cached filter that is before the one we want, so we don't have to filter the full item list.
// For example, the "", "i", "ir", and "iro" filters contain everything in the "iron" filter and more.
String prevFilterText = filterText.substring(0, filterText.length() - 1);
ImmutableList<IIngredientListElement> baseItemSet = filteredItemMapsCache.get(prevFilterText);
FilterPredicate filterPredicate = new FilterPredicate(filterText);
ImmutableList.Builder<IIngredientListElement> builder = ImmutableList.builder();
for (IIngredientListElement itemStackElement : baseItemSet) {
if (filterPredicate.apply(itemStackElement)) {
builder.add(itemStackElement);
}
}
return builder.build();
}
}
private static class FilterPredicate implements Predicate<IIngredientListElement> {
private final List<String> searchTokens = new ArrayList<String>();
private final List<String> modNameTokens = new ArrayList<String>();
private final List<String> tooltipTokens = new ArrayList<String>();
private final List<String> oreDictTokens = new ArrayList<String>();
private final List<String> creativeTabTokens = new ArrayList<String>();
private final List<String> colorTokens = new ArrayList<String>();
public FilterPredicate(String filterText) {
String[] tokens = filterText.split(" ");
for (String token : tokens) {
if (token.isEmpty()) {
continue;
}
if (token.startsWith("@")) {
addTokenWithoutPrefix(token, modNameTokens);
} else if (token.startsWith("#")) {
addTokenWithoutPrefix(token, tooltipTokens);
} else if (token.startsWith("$")) {
addTokenWithoutPrefix(token, oreDictTokens);
} else if (token.startsWith("%")) {
addTokenWithoutPrefix(token, creativeTabTokens);
} else if (token.startsWith("^")) {
addTokenWithoutPrefix(token, colorTokens);
} else {
searchTokens.add(token);
}
}
}
private static void addTokenWithoutPrefix(String token, List<String> tokensList) {
if (token.length() < 2) {
return;
}
String tokenText = token.substring(1);
tokensList.add(tokenText);
}
@Override
public boolean apply(@Nullable IIngredientListElement input) {
return input != null &&
stringContainsTokens(input.getModNameString(), modNameTokens) &&
stringContainsTokens(input.getTooltipString(), tooltipTokens) &&
stringContainsTokens(input.getOreDictString(), oreDictTokens) &&
stringContainsTokens(input.getCreativeTabsString(), creativeTabTokens) &&
stringContainsTokens(input.getColorString(), colorTokens) &&
stringContainsTokens(input.getSearchString(), searchTokens);
}
private static boolean stringContainsTokens(String comparisonString, List<String> tokens) {
for (String token : tokens) {
if (!comparisonString.contains(token)) {
return false;
}
}
return true;
}
}
}