Skip to content

Commit

Permalink
Concatenate sign lines to allow for multiline search (#706)
Browse files Browse the repository at this point in the history
* Concatenate sign lines to allow for multiline search

* Add option for multiline search to RegexArgument and apply it for sign searching
  • Loading branch information
xpple authored Jan 10, 2025
1 parent 00a34cc commit 55b46c4
Show file tree
Hide file tree
Showing 2 changed files with 32 additions and 16 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,8 @@
import net.minecraft.world.level.block.state.BlockState;

import java.util.regex.Pattern;
import java.util.stream.Collectors;
import java.util.stream.IntStream;

import static com.mojang.brigadier.arguments.StringArgumentType.*;
import static net.earthcomputer.clientcommands.command.arguments.RegexArgument.*;
Expand All @@ -30,7 +32,7 @@ public static void register(CommandDispatcher<FabricClientCommandSource> dispatc
.then(argument("query", greedyString())
.executes(ctx -> FindBlockCommand.findBlock(ctx, Component.translatable("commands.csignsearch.starting"), predicate(getString(ctx, "query"))))))
.then(literal("regex")
.then(argument("query", greedyRegex())
.then(argument("query", greedyRegex(true))
.executes(ctx -> FindBlockCommand.findBlock(ctx, Component.translatable("commands.csignsearch.starting"), predicate(getRegex(ctx, "query")))))));
FindBlockCommand.FLAG_KEEP_SEARCHING.addToCommand(dispatcher, csignsearch, ctx -> true);
}
Expand All @@ -55,15 +57,12 @@ public boolean test(HolderLookup.Provider holderLookupProvider, BlockGetter bloc
return false;
}

SignText frontText = sign.getFrontText();
SignText backText = sign.getBackText();
for (int i = 0; i < SignText.LINES; i++) {
String line = frontText.getMessage(i, Minecraft.getInstance().isTextFilteringEnabled()).getString();
if (linePredicate.test(line)) {
return true;
}
line = backText.getMessage(i, Minecraft.getInstance().isTextFilteringEnabled()).getString();
if (linePredicate.test(line)) {
boolean textFilteringEnabled = Minecraft.getInstance().isTextFilteringEnabled();
for (SignText text : new SignText[]{sign.getFrontText(), sign.getBackText()}) {
String string = IntStream.range(0, SignText.LINES)
.mapToObj(i -> text.getMessage(i, textFilteringEnabled).getString())
.collect(Collectors.joining("\n"));
if (linePredicate.test(string)) {
return true;
}
}
Expand All @@ -77,5 +76,4 @@ public boolean canEverMatch(BlockState state) {
}
};
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -17,23 +17,41 @@ public class RegexArgument implements ArgumentType<Pattern> {
private static final DynamicCommandExceptionType EXPECTED_REGEX_EXCEPTION = new DynamicCommandExceptionType(arg -> Component.translatable("commands.client.expectedRegex", arg));

private final RegexType type;
private final boolean multiline;

private RegexArgument(RegexType type) {
this(type, false);
}

private RegexArgument(RegexType type, boolean multiline) {
this.type = type;
this.multiline = multiline;
}

public static RegexArgument wordRegex() {
return new RegexArgument(RegexType.SINGLE_WORD);
}

public static RegexArgument wordRegex(boolean multiline) {
return new RegexArgument(RegexType.SINGLE_WORD, multiline);
}

public static RegexArgument slashyRegex() {
return new RegexArgument(RegexType.SLASHY_PHRASE);
}

public static RegexArgument slashyRegex(boolean multiline) {
return new RegexArgument(RegexType.SLASHY_PHRASE, multiline);
}

public static RegexArgument greedyRegex() {
return new RegexArgument(RegexType.GREEDY_PHRASE);
}

public static RegexArgument greedyRegex(boolean multiline) {
return new RegexArgument(RegexType.GREEDY_PHRASE, multiline);
}

public static Pattern getRegex(CommandContext<?> context, String name) {
return context.getArgument(name, Pattern.class);
}
Expand All @@ -44,7 +62,7 @@ public Pattern parse(StringReader reader) throws CommandSyntaxException {
if (type == RegexType.GREEDY_PHRASE) {
String text = reader.getRemaining();
try {
Pattern pattern = Pattern.compile(text);
Pattern pattern = Pattern.compile(text, multiline ? Pattern.MULTILINE : 0);
reader.setCursor(reader.getTotalLength());
return pattern;
} catch (PatternSyntaxException e) {
Expand All @@ -54,7 +72,7 @@ public Pattern parse(StringReader reader) throws CommandSyntaxException {
} else if (type == RegexType.SINGLE_WORD) {
String text = reader.readUnquotedString();
try {
return Pattern.compile(text);
return Pattern.compile(text, multiline ? Pattern.MULTILINE : 0);
} catch (PatternSyntaxException e) {
reader.setCursor(start);
throw EXPECTED_REGEX_EXCEPTION.createWithContext(reader, text);
Expand All @@ -64,14 +82,14 @@ public Pattern parse(StringReader reader) throws CommandSyntaxException {
}
}

public static Pattern parseSlashyRegex(StringReader reader) throws CommandSyntaxException {
private Pattern parseSlashyRegex(StringReader reader) throws CommandSyntaxException {
final int start = reader.getCursor();

boolean slashy = reader.canRead() && reader.peek() == '/';
if (!slashy) {
String text = reader.readUnquotedString();
try {
return Pattern.compile(text);
return Pattern.compile(text, multiline ? Pattern.MULTILINE : 0);
} catch (PatternSyntaxException e) {
reader.setCursor(start);
throw EXPECTED_REGEX_EXCEPTION.createWithContext(reader, text);
Expand All @@ -92,7 +110,7 @@ public static Pattern parseSlashyRegex(StringReader reader) throws CommandSyntax
if (!escaped) {
reader.skip();
try {
return Pattern.compile(regex.toString());
return Pattern.compile(regex.toString(), multiline ? Pattern.MULTILINE : 0);
} catch (PatternSyntaxException e) {
int end = reader.getCursor();
reader.setCursor(start);
Expand Down

0 comments on commit 55b46c4

Please sign in to comment.