diff --git a/src/main/java/net/earthcomputer/clientcommands/command/SignSearchCommand.java b/src/main/java/net/earthcomputer/clientcommands/command/SignSearchCommand.java index af08775e..9606df0c 100644 --- a/src/main/java/net/earthcomputer/clientcommands/command/SignSearchCommand.java +++ b/src/main/java/net/earthcomputer/clientcommands/command/SignSearchCommand.java @@ -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.*; @@ -30,7 +32,7 @@ public static void register(CommandDispatcher 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); } @@ -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; } } @@ -77,5 +76,4 @@ public boolean canEverMatch(BlockState state) { } }; } - } diff --git a/src/main/java/net/earthcomputer/clientcommands/command/arguments/RegexArgument.java b/src/main/java/net/earthcomputer/clientcommands/command/arguments/RegexArgument.java index 559e2fc5..20342bc5 100644 --- a/src/main/java/net/earthcomputer/clientcommands/command/arguments/RegexArgument.java +++ b/src/main/java/net/earthcomputer/clientcommands/command/arguments/RegexArgument.java @@ -17,23 +17,41 @@ public class RegexArgument implements ArgumentType { 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); } @@ -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) { @@ -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); @@ -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); @@ -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);