Skip to content

Commit

Permalink
Merge branch 'master' into new_search_settings
Browse files Browse the repository at this point in the history
  • Loading branch information
boubou19 authored Dec 23, 2024
2 parents e4464cf + b32f25c commit 02e3cdf
Show file tree
Hide file tree
Showing 9 changed files with 108 additions and 88 deletions.
2 changes: 1 addition & 1 deletion dependencies.gradle
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
// Add your dependencies here

dependencies {
api('com.github.GTNewHorizons:CodeChickenCore:1.3.10:dev')
api('com.github.GTNewHorizons:CodeChickenCore:1.4.0:dev')
shadowImplementation('org.apache.commons:commons-csv:1.10.0')
}
Binary file modified gradle/wrapper/gradle-wrapper.jar
Binary file not shown.
2 changes: 1 addition & 1 deletion gradle/wrapper/gradle-wrapper.properties
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
distributionBase=GRADLE_USER_HOME
distributionPath=wrapper/dists
distributionUrl=https\://services.gradle.org/distributions/gradle-8.9-bin.zip
distributionUrl=https\://services.gradle.org/distributions/gradle-8.11.1-bin.zip
networkTimeout=10000
validateDistributionUrl=true
zipStoreBase=GRADLE_USER_HOME
Expand Down
2 changes: 1 addition & 1 deletion settings.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ pluginManagement {
}

plugins {
id 'com.gtnewhorizons.gtnhsettingsconvention' version '1.0.29'
id 'com.gtnewhorizons.gtnhsettingsconvention' version '1.0.30'
}


Expand Down
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 @@ -459,6 +467,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.spaceMode", 0);
NEIClientConfig.setIntSetting("inventory.search.modNameSearchMode", 1);
Expand All @@ -468,6 +477,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 Down Expand Up @@ -495,6 +505,12 @@ public boolean isEnabled() {
.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 @@ -514,6 +530,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 @@ -532,6 +554,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 @@ -550,6 +578,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 @@ -568,6 +602,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 @@ -7,7 +7,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 @@ -101,38 +100,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 @@ -157,6 +125,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 @@ -189,27 +162,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 List<SearchToken> splitSearchText(String filterText) {
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
52 changes: 29 additions & 23 deletions src/main/java/codechicken/nei/recipe/GuiRecipe.java
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,7 @@
import codechicken.lib.gui.GuiDraw;
import codechicken.nei.Button;
import codechicken.nei.GuiNEIButton;
import codechicken.nei.ItemStackSet;
import codechicken.nei.ItemsTooltipLineHandler;
import codechicken.nei.LayoutManager;
import codechicken.nei.NEICPH;
Expand Down Expand Up @@ -135,7 +136,13 @@ public static PermutationTooltipLineHandler getInstance(PositionedStack pStack)
items = Arrays.asList(pStack.items);
}

return new PermutationTooltipLineHandler(pStack, items);
items = new ItemStackSet().addAll(items).keys();

if (items.size() > 1) {
return new PermutationTooltipLineHandler(pStack, items);
}

return null;
}

}
Expand Down Expand Up @@ -869,41 +876,40 @@ && new Rectangle(searchField.x + searchField.w, 15, 44, 16)
public List<String> handleItemTooltip(GuiContainer gui, ItemStack itemstack, int mousex, int mousey,
List<String> currenttip) {

try (CompatibilityHacks compatibilityHacks = new CompatibilityHacks()) {
List<Integer> indices = getRecipeIndices();
List<Integer> indices = getRecipeIndices();

try (CompatibilityHacks compatibilityHacks = new CompatibilityHacks()) {
for (int recipeIndex : indices) {
currenttip = handler.original.handleItemTooltip(this, itemstack, currenttip, recipeIndex);
}
}

if (NEIClientConfig.showCycledIngredientsTooltip()) {
PositionedStack focused = null;
if (NEIClientConfig.showCycledIngredientsTooltip()) {
PositionedStack focused = null;

for (int refIndex = 0; refIndex < indices.size(); refIndex++) {
final int recipeIndex = indices.get(refIndex);
for (int refIndex = 0; refIndex < indices.size(); refIndex++) {
final int recipeIndex = indices.get(refIndex);

if (itemstack != null && focused == null) {
final List<PositionedStack> stacks = handler.original.getIngredientStacks(recipeIndex);
if (itemstack != null && focused == null) {
final List<PositionedStack> stacks = handler.original.getIngredientStacks(recipeIndex);

for (PositionedStack pStack : stacks) {
if (isMouseOver(pStack, refIndex)) {
focused = pStack;
break;
}
for (PositionedStack pStack : stacks) {
if (isMouseOver(pStack, refIndex)) {
focused = pStack;
break;
}
}
}

if (focused == null || focused.items.length <= 1) {
this.permutationTooltipLineHandler = null;
} else if (this.permutationTooltipLineHandler == null
|| this.permutationTooltipLineHandler.pStack != focused) {
this.permutationTooltipLineHandler = PermutationTooltipLineHandler.getInstance(focused);
}
} else if (this.permutationTooltipLineHandler != null) {
this.permutationTooltipLineHandler = null;
}

if (focused == null || focused.items.length <= 1) {
this.permutationTooltipLineHandler = null;
} else if (this.permutationTooltipLineHandler == null
|| this.permutationTooltipLineHandler.pStack != focused) {
this.permutationTooltipLineHandler = PermutationTooltipLineHandler.getInstance(focused);
}
} else if (this.permutationTooltipLineHandler != null) {
this.permutationTooltipLineHandler = null;
}

if (this.permutationTooltipLineHandler != null) {
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 "";
}

}

0 comments on commit 02e3cdf

Please sign in to comment.