/
ItemFilter.java
213 lines (172 loc) · 6.49 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
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
package mezz.jei;
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.Collections2;
import com.google.common.collect.ImmutableList;
import javax.annotation.Nonnull;
import javax.annotation.Nullable;
import java.util.Collection;
import java.util.List;
import net.minecraft.client.Minecraft;
import net.minecraft.client.renderer.ItemModelMesher;
import net.minecraft.client.resources.model.IBakedModel;
import net.minecraft.item.ItemStack;
import mezz.jei.api.IItemBlacklist;
import mezz.jei.api.IItemRegistry;
import mezz.jei.config.Config;
import mezz.jei.util.ItemStackElement;
import mezz.jei.util.Log;
public class ItemFilter {
/** The currently active filter text */
@Nonnull
private static String filterText = "";
/** A cache for fast searches while typing or using backspace. Maps filterText to filteredItemMaps */
private final LoadingCache<String, ImmutableList<ItemStackElement>> filteredItemMapsCache;
public ItemFilter(final IItemRegistry itemRegistry) {
filteredItemMapsCache = CacheBuilder.newBuilder()
.maximumWeight(16)
.weigher(new SearchFilterWeigher())
.build(new ItemFilterCacheLoader(itemRegistry));
}
public void reset() {
this.filteredItemMapsCache.invalidateAll();
}
public static boolean setFilterText(@Nonnull String filterText) {
String lowercaseFilterText = filterText.toLowerCase();
if (ItemFilter.filterText.equals(lowercaseFilterText)) {
return false;
}
ItemFilter.filterText = lowercaseFilterText;
return true;
}
@Nonnull
public String getFilterText() {
return filterText;
}
@Nonnull
public ImmutableList<ItemStackElement> getItemList() {
ImmutableList.Builder<ItemStackElement> itemList = ImmutableList.builder();
String[] filters = filterText.split("\\|");
for (String filter : filters) {
List<ItemStackElement> itemStackElements = filteredItemMapsCache.getUnchecked(filter);
itemList.addAll(itemStackElements);
}
return itemList.build();
}
public int size() {
return getItemList().size();
}
private static ImmutableList<ItemStackElement> createBaseList(IItemRegistry itemRegistry) {
ItemStackChecker itemStackChecker = new ItemStackChecker();
ImmutableList.Builder<ItemStackElement> baseList = ImmutableList.builder();
for (ItemStack itemStack : itemRegistry.getItemList()) {
if (itemStack == null) {
continue;
}
if (itemStackChecker.isItemStackHidden(itemStack)) {
continue;
}
ItemStackElement itemStackElement = ItemStackElement.create(itemStack);
if (itemStackElement != null) {
baseList.add(itemStackElement);
}
}
return baseList.build();
}
private static class SearchFilterWeigher implements Weigher<String, ImmutableList<ItemStackElement>> {
public int weigh(@Nonnull String key, @Nonnull ImmutableList<ItemStackElement> value) {
// The CacheLoader is recursive, so keep the base value in the cache permanently by setting its weight to 0
return (key.length() == 0) ? 0 : 1;
}
}
private class ItemFilterCacheLoader extends CacheLoader<String, ImmutableList<ItemStackElement>> {
private final IItemRegistry itemRegistry;
public ItemFilterCacheLoader(IItemRegistry itemRegistry) {
this.itemRegistry = itemRegistry;
}
@Override
public ImmutableList<ItemStackElement> load(@Nonnull final String filterText) throws Exception {
if (filterText.length() == 0) {
return createBaseList(itemRegistry);
}
// 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<ItemStackElement> baseItemSet = filteredItemMapsCache.get(prevFilterText);
FilterPredicate filterPredicate = new FilterPredicate(filterText);
Collection<ItemStackElement> filteredItemList = Collections2.filter(baseItemSet, filterPredicate);
return ImmutableList.copyOf(filteredItemList);
}
}
private static class ItemStackChecker {
private IItemBlacklist itemBlacklist;
private ItemModelMesher itemModelMesher;
@SuppressWarnings("deprecation")
private IBakedModel missingModel;
public ItemStackChecker() {
itemBlacklist = Internal.getHelpers().getItemBlacklist();
itemModelMesher = Minecraft.getMinecraft().getRenderItem().getItemModelMesher();
missingModel = itemModelMesher.getModelManager().getMissingModel();
}
public boolean isItemStackHidden(@Nonnull ItemStack itemStack) {
if (isItemStackHiddenByMissingModel(itemStack)) {
return true;
}
return isItemHiddenByBlacklist(itemStack);
}
private boolean isItemStackHiddenByMissingModel(@Nonnull ItemStack itemStack) {
if (!Config.isHideMissingModelsEnabled()) {
return false;
}
try {
if (itemModelMesher.getItemModel(itemStack) == missingModel) {
return true;
}
} catch (Exception e) {
Log.error("Couldn't get ItemModel for itemStack {}.", itemStack.getClass(), e);
return true;
}
return false;
}
private boolean isItemHiddenByBlacklist(@Nonnull ItemStack itemStack) {
if (!itemBlacklist.isItemBlacklisted(itemStack)) {
return false;
}
if (Config.isEditModeEnabled()) {
// edit mode can only change the config blacklist, not things blacklisted through the API
return !Config.isItemOnConfigBlacklist(itemStack, true) && !Config.isItemOnConfigBlacklist(itemStack, false);
}
return true;
}
}
private static class FilterPredicate implements Predicate<ItemStackElement> {
private final String filterText;
public FilterPredicate(String filterText) {
this.filterText = filterText;
}
@Override
public boolean apply(@Nullable ItemStackElement input) {
if (input == null) {
return false;
}
String[] tokens = filterText.split(" ");
for (String token : tokens) {
if (token.startsWith("@")) {
String modNameFilter = token.substring(1);
if (modNameFilter.length() > 0 && !input.getModName().contains(modNameFilter)) {
return false;
}
} else {
if (!input.getLocalizedName().contains(token)) {
return false;
}
}
}
return true;
}
}
}