Skip to content

Commit

Permalink
Fixed searchString issue (Moulberry#289)
Browse files Browse the repository at this point in the history
Co-authored-by: nea <[email protected]>
Co-authored-by: Roman / Nea <[email protected]>
  • Loading branch information
3 people committed Sep 22, 2022
1 parent 2d9bf47 commit 005f9ef
Show file tree
Hide file tree
Showing 7 changed files with 140 additions and 24 deletions.
86 changes: 75 additions & 11 deletions src/main/java/io/github/moulberry/notenoughupdates/NEUManager.java
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,7 @@
import io.github.moulberry.notenoughupdates.events.RepositoryReloadEvent;
import io.github.moulberry.notenoughupdates.miscgui.GuiItemRecipe;
import io.github.moulberry.notenoughupdates.miscgui.KatSitterOverlay;
import io.github.moulberry.notenoughupdates.options.customtypes.NEUDebugFlag;
import io.github.moulberry.notenoughupdates.recipes.CraftingOverlay;
import io.github.moulberry.notenoughupdates.recipes.CraftingRecipe;
import io.github.moulberry.notenoughupdates.recipes.Ingredient;
Expand Down Expand Up @@ -394,31 +395,94 @@ public List<NeuRecipe> getAvailableUsagesFor(String internalname) {
return getUsagesFor(internalname).stream().filter(NeuRecipe::isAvailable).collect(Collectors.toList());
}

private static class DebugMatch {
int index;
String match;

DebugMatch(int index, String match) {
this.index = index;
this.match = match;
}
}


private String searchDebug(String[] searchArray, ArrayList<DebugMatch> debugMatches) {
//splitToSearch, debugMatches and query
final String ANSI_RED = "\u001B[31m";
final String ANSI_RESET = "\u001B[0m";
final String ANSI_YELLOW = "\u001B[33m";

//create debug message
StringBuilder debugBuilder = new StringBuilder();
for (int i = 0; i < searchArray.length; i++) {
final int fi = i;
Object[] matches = debugMatches.stream().filter((d) -> d.index == fi).toArray();

if (matches.length > 0) {
debugBuilder.append(ANSI_YELLOW + "[").append(((DebugMatch) matches[0]).match).append("]");
debugBuilder.append(ANSI_RED + "[").append(searchArray[i]).append("]").append(ANSI_RESET).append(" ");
} else {
debugBuilder.append(searchArray[i]).append(" ");
}
}

//yellow = query match and red = string match
return debugBuilder.toString();
}

/**
* Searches a string for a query. This method is used to mimic the behaviour of the more complex map-based search
* function. This method is used for the chest-item-search feature.
*/
public boolean searchString(String toSearch, String query) {
int lastQueryMatched = -1;
final String ANSI_RESET = "\u001B[0m";
final String ANSI_YELLOW = "\u001B[33m";

int lastStringMatch = -1;
ArrayList<DebugMatch> debugMatches = new ArrayList<>();

toSearch = clean(toSearch).toLowerCase();
query = clean(query).toLowerCase();
String[] splitToSearch = toSearch.split(" ");
String[] queryArray = query.split(" ");

out:
for (int j = 0; j < queryArray.length; j++) {
for (int i = 0; i < splitToSearch.length; i++) {
if ((queryArray.length - (lastQueryMatched != -1 ? lastQueryMatched : 0)) > (splitToSearch.length - i)) continue;
if (splitToSearch[i].startsWith(queryArray[j])) {
lastQueryMatched = j;
continue out;
{
String currentSearch = queryArray[0];
int queryIndex = 0;
boolean matchedLastQueryItem = false;

for (int k = 0; k < splitToSearch.length; k++) {
if (queryIndex - 1 != -1 && (queryArray.length - queryIndex) > (splitToSearch.length - k)) continue;
if (splitToSearch[k].startsWith(currentSearch)) {
if (((lastStringMatch != -1 ? lastStringMatch : k-1) == k-1)) {
debugMatches.add(new DebugMatch(k, currentSearch));
lastStringMatch = k;
if (queryIndex+1 != queryArray.length) {
queryIndex++;
currentSearch = queryArray[queryIndex];
} else {
matchedLastQueryItem = true;
}
}
} else if (queryIndex != 0) {
queryIndex = 0;
currentSearch = queryArray[queryIndex];
lastStringMatch = -1;
}
}
return false;
}

return true;
if (matchedLastQueryItem) {
if (NEUDebugFlag.SEARCH.isSet()) {
NotEnoughUpdates.LOGGER.info("Found match for \"" + ANSI_YELLOW + query + ANSI_RESET + "\":\n\t" + searchDebug(splitToSearch, debugMatches));
}
} else {
if (NEUDebugFlag.SEARCH.isSet() && lastStringMatch != -1) {
NotEnoughUpdates.LOGGER.info("Found partial match for \"" + ANSI_YELLOW + query + ANSI_RESET + "\":\n\t" + searchDebug(splitToSearch, debugMatches));
}
}

return matchedLastQueryItem;
}
}

/**
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -99,6 +99,8 @@
import net.minecraftforge.fml.common.event.FMLPreInitializationEvent;
import net.minecraftforge.fml.common.eventhandler.SubscribeEvent;
import net.minecraftforge.fml.common.gameevent.TickEvent;
import org.apache.logging.log4j.LogManager;
import org.apache.logging.log4j.Logger;

import java.awt.*;
import java.io.BufferedReader;
Expand All @@ -119,6 +121,8 @@ public class NotEnoughUpdates {
public static final int VERSION_ID = 20100;
public static final int PRE_VERSION_ID = 0;
public static final int HOTFIX_VERSION_ID = 0;

public static final Logger LOGGER = LogManager.getLogger("NotEnoughUpdates");
/**
* Registers the biomes for the crystal hollows here so optifine knows they exists
*/
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -58,7 +58,8 @@ public class DevTestCommand extends ClientCommandBase {
"lulonaut",
"craftyoldminer",
"eisengolem",
"whalker"
"whalker",
"ascynx"
);

private static final String[] DEV_FAIL_STRINGS = {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -41,7 +41,8 @@ public DiagCommand() {
" center=<off | on> Disable / enable using center\n" +
"/neudiag wishing Wishing Compass Solver diagnostics\n" +
"/neudiag debug\n" +
" <no sub-command> Show current flags\n" +
" <no sub-command> Show all enabled flags\n" +
" <list> Show all flags\n"+
" <enable | disable> <flag> Enable/disable flag\n";

private void showUsage(ICommandSender sender) {
Expand Down Expand Up @@ -86,14 +87,18 @@ public void processCommand(ICommandSender sender, String[] args) throws CommandE
boolean enablingFlag = true;
String action = args[1];
switch (action) {
case "list":
sender.addChatMessage(new ChatComponentText(
EnumChatFormatting.YELLOW + "Here are all flags:\n" + NEUDebugFlag.getFlagList()));
return;
case "disable":
enablingFlag = false;
// falls through
case "enable":
if (args.length != 3) {
sender.addChatMessage(new ChatComponentText(EnumChatFormatting.RED +
"You must specify a flag:\n" +
NEUDebugFlag.FLAG_LIST));
NEUDebugFlag.getFlagList()));
return;
}

Expand All @@ -108,7 +113,7 @@ public void processCommand(ICommandSender sender, String[] args) throws CommandE
} catch (IllegalArgumentException e) {
sender.addChatMessage(new ChatComponentText(EnumChatFormatting.RED +
flagName + " is invalid. Valid flags are:\n" +
NEUDebugFlag.FLAG_LIST));
NEUDebugFlag.getFlagList()));
return;
}
break;
Expand All @@ -118,8 +123,8 @@ public void processCommand(ICommandSender sender, String[] args) throws CommandE
}
}

sender.addChatMessage(new ChatComponentText(EnumChatFormatting.YELLOW + "Effective debug flags: " +
NotEnoughUpdates.INSTANCE.config.hidden.debugFlags.toString()));
sender.addChatMessage(new ChatComponentText(EnumChatFormatting.YELLOW + "Effective debug flags: \n" +
NEUDebugFlag.getEnabledFlags()));
break;
default:
showUsage(sender);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -170,7 +170,7 @@ public static void process(IChatComponent message) {
// falls through
case FOUND:
mc.thePlayer.addChatMessage(new ChatComponentText(EnumChatFormatting.YELLOW + "[NEU] Found solution."));
if (NotEnoughUpdates.INSTANCE.config.hidden.debugFlags.contains(NEUDebugFlag.METAL) &&
if (NEUDebugFlag.METAL.isSet() &&
(previousState == SolutionState.INVALID || previousState == SolutionState.FAILED)) {
NEUDebugLogger.log(
NEUDebugFlag.METAL,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -923,7 +923,7 @@ public void logDiagnosticData(boolean outputAlways) {
return;
}

boolean wishingDebugFlagSet = NotEnoughUpdates.INSTANCE.config.hidden.debugFlags.contains(NEUDebugFlag.WISHING);
boolean wishingDebugFlagSet = NEUDebugFlag.WISHING.isSet();
if (outputAlways || wishingDebugFlagSet) {
NEUDebugLogger.logAlways(getDiagnosticMessage());
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -19,13 +19,55 @@

package io.github.moulberry.notenoughupdates.options.customtypes;

import io.github.moulberry.notenoughupdates.util.NEUDebugLogger;

import java.util.Arrays;
import java.util.List;
import java.util.stream.Collectors;

public enum NEUDebugFlag {
// NOTE: Removing enum values causes gson to remove all debugFlags on load if any removed value is present
METAL,
WISHING,
METAL("Metal Detector Solver"),
WISHING("Wishing Compass Solver"),
MAP("Dungeon Map Player Information"),
SEARCH("SearchString Matches"),
;

public static final String FLAG_LIST =
"METAL - Metal Detector Solver\n" +
"WISHING - Wishing Compass Solver";
private final String description;

NEUDebugFlag(String description) {
this.description = description;
}

public String getDescription() {
return description;
}

public boolean isSet() {
return NEUDebugLogger.isFlagEnabled(this);
}

public static String getFlagList() {
return renderFlagInformation(Arrays.asList(values()));
}

public static String getEnabledFlags() {
return renderFlagInformation(Arrays.stream(values())
.filter(NEUDebugFlag::isSet)
.collect(Collectors.toList()));
}

public static String renderFlagInformation(List<NEUDebugFlag> flags) {
int maxNameLength = flags.stream()
.mapToInt(it -> it.name().length())
.max()
.orElse(0);
return flags.stream()
.map(it -> (CharSequence) String.format(
"%-" + maxNameLength + "s" + " - %s",
it.name(),
it.getDescription()
))
.collect(Collectors.joining("\n"));
}
}

0 comments on commit 005f9ef

Please sign in to comment.