Files
NotEnoughItems/src/main/java/codechicken/nei/SearchTextFormatter.java
T

Ignoring revisions in .git-blame-ignore-revs. Click here to bypass and see the normal blame view.

96 lines
3.8 KiB
Java
Raw Normal View History

2024-06-27 18:03:50 +03:00
package codechicken.nei;
import net.minecraft.util.EnumChatFormatting;
import codechicken.nei.FormattedTextField.TextFormatter;
import codechicken.nei.search.SearchExpressionFormatVisitor;
import codechicken.nei.search.SearchExpressionUtils;
2025-09-04 16:59:51 +03:00
import codechicken.nei.search.SearchToken;
2024-06-27 18:03:50 +03:00
public class SearchTextFormatter implements TextFormatter {
protected final SearchTokenParser searchParser;
2025-09-04 16:59:51 +03:00
protected String contextToken = "";
2024-06-27 18:03:50 +03:00
public SearchTextFormatter(SearchTokenParser searchParser) {
this.searchParser = searchParser;
}
2025-09-04 16:59:51 +03:00
public void setContextToken(String contextToken) {
this.contextToken = contextToken;
}
@Override
2024-06-27 18:03:50 +03:00
public String format(String text) {
final int patternMode = NEIClientConfig.getIntSetting("inventory.search.patternMode");
if (patternMode == 3) {
final int spaceModeEnabled = NEIClientConfig.getIntSetting("inventory.search.spaceMode");
if (spaceModeEnabled == 1) {
text = SearchTokenParser.SPACE_PATTERN.matcher(text).replaceAll("\\\\ ");
}
final SearchExpressionFormatVisitor visitor = new SearchExpressionFormatVisitor(searchParser);
return SearchExpressionUtils.visitRecipeSearchExpression(text, visitor);
} else {
2025-09-04 16:59:51 +03:00
final StringBuilder formattedText = new StringBuilder();
2025-09-04 16:59:51 +03:00
for (String[] orQuery : searchParser.splitByDelimiters(text, "|", false)) {
final String filterText = orQuery[1];
2025-09-04 16:59:51 +03:00
formattedText.append(SearchExpressionUtils.HIGHLIGHTS.OR + orQuery[0]);
2025-09-04 16:59:51 +03:00
for (String[] contextPart : searchParser.splitByDelimiters(filterText, this.contextToken, false)) {
2025-09-04 16:59:51 +03:00
if (!contextPart[0].isEmpty()) {
formattedText.append(
2025-09-06 23:32:14 +03:00
SearchExpressionUtils.HIGHLIGHTS.RECIPE + contextPart[0].substring(0, 1)
+ EnumChatFormatting.RESET);
contextPart[1] = contextPart[0].substring(1) + contextPart[1];
}
2025-09-04 16:59:51 +03:00
if (contextPart[1].isEmpty()) {
continue;
}
2025-09-04 16:59:51 +03:00
int startIndex = 0;
2025-09-04 16:59:51 +03:00
for (SearchToken token : searchParser.splitSearchText(contextPart[1])) {
formattedText.append(contextPart[1].substring(startIndex, token.start));
EnumChatFormatting tokenColor = EnumChatFormatting.RESET;
if (token.firstChar != null) {
tokenColor = searchParser.getProvider(token.firstChar).getHighlightedColor();
}
if (token.ignore != null) {
formattedText
.append(SearchExpressionUtils.HIGHLIGHTS.NEGATE + String.valueOf(token.ignore));
}
if (token.firstChar != null) {
formattedText.append(tokenColor + String.valueOf(token.firstChar));
}
if (token.quotes) {
formattedText.append(SearchExpressionUtils.HIGHLIGHTS.QUOTED + "\"");
}
if (!token.rawText.isEmpty()) {
formattedText.append(tokenColor + token.rawText);
}
if (token.quotes) {
formattedText.append(SearchExpressionUtils.HIGHLIGHTS.QUOTED + "\"");
}
2025-09-04 16:59:51 +03:00
startIndex = token.end;
}
2025-09-04 16:59:51 +03:00
formattedText.append(contextPart[1].substring(startIndex, contextPart[1].length()));
2024-06-27 18:03:50 +03:00
}
}
return formattedText.toString();
2024-06-27 18:03:50 +03:00
}
}
}