Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Concatenate sign lines to allow for multiline search #706

Open
wants to merge 2 commits into
base: fabric
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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
Loading