Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Clear Search Filter Cache after Change Settings #569

Merged
merged 6 commits into from
Dec 17, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
42 changes: 41 additions & 1 deletion src/main/java/codechicken/nei/NEIClientConfig.java
Original file line number Diff line number Diff line change
Expand Up @@ -432,7 +432,15 @@ private static void setInventorySearchDefaults(ConfigTagParent tag) {
API.addOption(new OptionCycled("inventory.search.widgetAutofocus", 3, true));

tag.getTag("inventory.search.patternMode").setComment("Search Mode").getIntValue(1);
API.addOption(new OptionCycled("inventory.search.patternMode", 3, true));
API.addOption(new OptionCycled("inventory.search.patternMode", 3, true) {

@Override
public boolean onClick(int button) {
SearchField.searchParser.clearCache();
return super.onClick(button);
}

});

tag.getTag("inventory.search.quoteDropItemName").setComment("Quote Drop Item Name").getBooleanValue(true);
API.addOption(new OptionToggleButton("inventory.search.quoteDropItemName", true));
Expand All @@ -458,6 +466,7 @@ public boolean onClick(int button) {
SearchField.searchParser.prefixRedefinitions.clear();
SearchField.searchParser.prefixRedefinitions.put('%', '@');
SearchField.searchParser.prefixRedefinitions.put('@', '%');
SearchField.searchParser.clearCache();
} else {
NEIClientConfig.setIntSetting("inventory.search.modNameSearchMode", 1);
NEIClientConfig.setIntSetting("inventory.search.tooltipSearchMode", 0);
Expand All @@ -466,6 +475,7 @@ public boolean onClick(int button) {
NEIClientConfig.setIntSetting("inventory.search.subsetsSearchMode", 1);
tag.getTag("inventory.search.prefixRedefinitions").setValue("{}");
SearchField.searchParser.prefixRedefinitions.clear();
SearchField.searchParser.clearCache();
}

return true;
Expand All @@ -477,6 +487,12 @@ public boolean onClick(int button) {
.getIntValue(1);
API.addOption(new OptionCycled("inventory.search.modNameSearchMode", 3, true) {

@Override
public boolean onClick(int button) {
SearchField.searchParser.clearCache();
return super.onClick(button);
}

@Override
public String getButtonText() {
return translateN(
Expand All @@ -496,6 +512,12 @@ public boolean isEnabled() {
.getIntValue(0);
API.addOption(new OptionCycled("inventory.search.tooltipSearchMode", 3, true) {

@Override
public boolean onClick(int button) {
SearchField.searchParser.clearCache();
return super.onClick(button);
}

@Override
public String getButtonText() {
return translateN(
Expand All @@ -514,6 +536,12 @@ public boolean isEnabled() {
.getIntValue(0);
API.addOption(new OptionCycled("inventory.search.identifierSearchMode", 3, true) {

@Override
public boolean onClick(int button) {
SearchField.searchParser.clearCache();
return super.onClick(button);
}

@Override
public String getButtonText() {
return translateN(
Expand All @@ -532,6 +560,12 @@ public boolean isEnabled() {
.getIntValue(0);
API.addOption(new OptionCycled("inventory.search.oreDictSearchMode", 3, true) {

@Override
public boolean onClick(int button) {
SearchField.searchParser.clearCache();
return super.onClick(button);
}

@Override
public String getButtonText() {
return translateN(
Expand All @@ -550,6 +584,12 @@ public boolean isEnabled() {
.getIntValue(1);
API.addOption(new OptionCycled("inventory.search.subsetsSearchMode", 3, true) {

@Override
public boolean onClick(int button) {
SearchField.searchParser.clearCache();
return super.onClick(button);
}

@Override
public String getButtonText() {
return translateN(
Expand Down
58 changes: 14 additions & 44 deletions src/main/java/codechicken/nei/SearchTokenParser.java
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,6 @@
import java.util.LinkedHashMap;
import java.util.List;
import java.util.Map;
import java.util.concurrent.locks.ReentrantLock;
import java.util.regex.Matcher;
import java.util.regex.Pattern;
import java.util.stream.Collectors;
Expand Down Expand Up @@ -88,38 +87,7 @@ public boolean matches(ItemStack item) {
}
}

private static class ItemFilterCache implements ItemFilter {

public ItemFilter filter;
private final ItemStackMap<Boolean> states = new ItemStackMap<>();
private final ReentrantLock lock = new ReentrantLock();

public ItemFilterCache(ItemFilter filter) {
this.filter = filter;
}

@Override
public boolean matches(ItemStack item) {
lock.lock();

try {
Boolean match = states.get(item);

if (match == null) {
states.put(item, match = this.filter.matches(item));
}

return match;
} catch (Throwable th) {
th.printStackTrace();
return false;
} finally {
lock.unlock();
}
}
}

private static final LinkedHashMap<String, ItemFilter> filtersCache = new LinkedHashMap<>() {
private final LinkedHashMap<String, ItemFilter> filtersCache = new LinkedHashMap<>() {

private static final long serialVersionUID = 1042213947848622164L;

Expand All @@ -144,6 +112,11 @@ public SearchTokenParser() {
public void addProvider(ISearchParserProvider provider) {
this.searchProviders.add(provider);
this.providersCache.clear();
this.filtersCache.clear();
}

public void clearCache() {
this.filtersCache.clear();
}

protected List<ISearchParserProvider> getProviders() {
Expand Down Expand Up @@ -176,27 +149,24 @@ && getRedefinedPrefix(provider.getPrefix()) == ch)
.findFirst().orElse(null);
}

public ItemFilter getFilter(String filterText) {
public synchronized ItemFilter getFilter(String filterText) {
filterText = EnumChatFormatting.getTextWithoutFormattingCodes(filterText).toLowerCase();

if (!filtersCache.containsKey(filterText)) {
final String[] parts = EnumChatFormatting.getTextWithoutFormattingCodes(filterText).toLowerCase()
.split("\\|");
return this.filtersCache.computeIfAbsent(filterText, text -> {
final String[] parts = text.split("\\|");
final List<ItemFilter> searchTokens = Arrays.stream(parts).map(this::parseSearchText).filter(s -> s != null)
.collect(Collectors.toCollection(ArrayList::new));

if (searchTokens.isEmpty()) {
filtersCache.put(filterText, new EverythingItemFilter());
return new EverythingItemFilter();
} else if (searchTokens.size() == 1) {
filtersCache.put(filterText, new IsRegisteredItemFilter(new ItemFilterCache(searchTokens.get(0))));
return new IsRegisteredItemFilter(searchTokens.get(0));
} else {
filtersCache.put(
filterText,
new IsRegisteredItemFilter(new ItemFilterCache(new AnyMultiItemFilter(searchTokens))));
return new IsRegisteredItemFilter(new AnyMultiItemFilter(searchTokens));
}

}
});

return filtersCache.get(filterText);
}

public Pattern getSplitPattern() {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -43,6 +43,7 @@
import codechicken.nei.ItemStackSet;
import codechicken.nei.NEIClientConfig;
import codechicken.nei.NEIClientUtils;
import codechicken.nei.SearchField;
import codechicken.nei.recipe.StackInfo;
import codechicken.nei.search.TooltipFilter;
import codechicken.nei.util.ReadableNumberConverter;
Expand All @@ -55,6 +56,7 @@ private static class ResourcePackReloaded implements IResourceManagerReloadListe
public void onResourceManagerReload(IResourceManager p_110549_1_) {
renderingErrorItems.clear();
TooltipFilter.populateSearchMap();
SearchField.searchParser.clearCache();
}
}

Expand Down
36 changes: 19 additions & 17 deletions src/main/java/codechicken/nei/search/TooltipFilter.java
Original file line number Diff line number Diff line change
@@ -1,21 +1,19 @@
package codechicken.nei.search;

import java.util.List;
import java.util.concurrent.locks.ReentrantLock;
import java.util.regex.Pattern;

import net.minecraft.item.ItemStack;
import net.minecraft.util.EnumChatFormatting;

import codechicken.nei.ItemList;
import codechicken.nei.ItemStackMap;
import codechicken.nei.NEIClientUtils;
import codechicken.nei.api.ItemFilter;
import codechicken.nei.guihook.GuiContainerManager;

public class TooltipFilter implements ItemFilter {

private static final ItemStackMap<String> itemSearchNames = new ItemStackMap<>();
private static final ReentrantLock lock = new ReentrantLock();

private final Pattern pattern;

Expand All @@ -36,28 +34,32 @@ public static void populateSearchMap() {
}

protected static String getSearchTooltip(ItemStack stack) {
lock.lock();
String tooltip = itemSearchNames.get(stack);

try {
String tooltip = itemSearchNames.get(stack);
if (tooltip == null) {
tooltip = getTooltip(stack.copy());

if (tooltip == null) {
tooltip = getTooltip(stack.copy());
synchronized (itemSearchNames) {
itemSearchNames.put(stack, tooltip);
}

return tooltip;
} catch (Throwable th) {
th.printStackTrace();
return "";
} finally {
lock.unlock();
}

return tooltip;
}

private static String getTooltip(ItemStack itemstack) {
final List<String> list = GuiContainerManager.itemDisplayNameMultiline(itemstack, null, true);
return EnumChatFormatting.getTextWithoutFormattingCodes(String.join("\n", list.subList(1, list.size())));

try {
List<String> namelist = itemstack.getTooltip(NEIClientUtils.mc().thePlayer, false);

if (namelist.size() > 1) {
return EnumChatFormatting
.getTextWithoutFormattingCodes(String.join("\n", namelist.subList(1, namelist.size())));
}

} catch (Throwable ignored) {}

return "";
}

}
Loading