Skip to content
Open
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 @@ -79,6 +79,7 @@
import java.util.List;
import java.util.concurrent.Callable;
import java.util.function.Consumer;
import java.util.regex.Pattern;

import static com.google.common.base.Preconditions.checkArgument;
import static com.google.common.base.Preconditions.checkNotNull;
Expand Down Expand Up @@ -311,6 +312,8 @@ public void formats(Actor actor) {
public void list(Actor actor,
@ArgFlag(name = 'p', desc = "Page to view.", def = "1")
int page,
@ArgFlag(name = 'f', desc = "Filter to apply to relative schematic paths. Use '?' to match any one character, and '*' to match any characters multiple times. If neither '?' nor '*' are present, '*' is added to both ends.", def = "")
String filter,
@Switch(name = 'd', desc = "Sort by date, oldest first")
boolean oldFirst,
@Switch(name = 'n', desc = "Sort by date, newest first")
Expand All @@ -330,11 +333,14 @@ public void list(Actor actor,
pathComparator = Comparator.naturalOrder();
flag = "";
}
if (filter != null && !filter.isEmpty()) {
flag += " -f \"" + filter + "\"";
}
final String pageCommand = actor.isPlayer()
? "//schem list -p %page%" + flag : null;

WorldEditAsyncCommandBuilder.createAndSendMessage(actor,
new SchematicListTask(pathComparator, page, pageCommand),
new SchematicListTask(pathComparator, page, filter, pageCommand),
SubtleFormat.wrap("(Please wait... gathering schematic list.)"));
}

Expand Down Expand Up @@ -443,11 +449,23 @@ public Consumer<Actor> call() throws Exception {
private static class SchematicListTask implements Callable<Component> {
private final Comparator<Path> pathComparator;
private final int page;
private final Pattern filter;
private final String pageCommand;

SchematicListTask(Comparator<Path> pathComparator, int page, String pageCommand) {
SchematicListTask(Comparator<Path> pathComparator, int page, String filter, String pageCommand) {
this.pathComparator = pathComparator;
this.page = page;
if (filter == null || filter.isEmpty()) {
this.filter = null;
} else {
String matcherQuery = filter;
if (!matcherQuery.contains("*") && !matcherQuery.contains("?")) {
matcherQuery = "*" + matcherQuery + "*";
}
this.filter = Pattern.compile(Pattern.quote(matcherQuery)
Comment thread
Brokkonaut marked this conversation as resolved.
Resolved
.replace("*", "\\E.*\\Q")
.replace("?", "\\E.\\Q"));
}
this.pageCommand = pageCommand;
}

Expand All @@ -456,6 +474,10 @@ public Component call() throws Exception {
SchematicsManager schematicsManager = WorldEdit.getInstance().getSchematicsManager();
// Copy this to a mutable list, we're sorting it below.
List<Path> fileList = new ArrayList<>(schematicsManager.getSchematicPaths());
if (filter != null) {
Path root = schematicsManager.getRoot();
fileList.removeIf(path -> !filter.matcher(root.relativize(path).toString()).matches());
}

if (fileList.isEmpty()) {
return ErrorFormat.wrap("No schematics found.");
Expand Down
Loading