Add paging to //schematics.

-p <int> can be used to select a page. Defaults to page 1.

First implementation by @BangL.
This commit is contained in:
Kenzie Togami 2015-12-28 12:05:16 -08:00
parent 23d6fa7579
commit 8cd46977f3

View File

@ -24,8 +24,6 @@ import com.sk89q.minecraft.util.commands.CommandContext;
import com.sk89q.minecraft.util.commands.CommandException; import com.sk89q.minecraft.util.commands.CommandException;
import com.sk89q.minecraft.util.commands.CommandPermissions; import com.sk89q.minecraft.util.commands.CommandPermissions;
import com.sk89q.worldedit.EditSession; import com.sk89q.worldedit.EditSession;
import com.sk89q.worldedit.util.io.file.FilenameException;
import com.sk89q.worldedit.util.io.file.FilenameResolutionException;
import com.sk89q.worldedit.LocalConfiguration; import com.sk89q.worldedit.LocalConfiguration;
import com.sk89q.worldedit.LocalSession; import com.sk89q.worldedit.LocalSession;
import com.sk89q.worldedit.WorldEdit; import com.sk89q.worldedit.WorldEdit;
@ -40,8 +38,11 @@ import com.sk89q.worldedit.extent.clipboard.io.ClipboardWriter;
import com.sk89q.worldedit.function.operation.Operations; import com.sk89q.worldedit.function.operation.Operations;
import com.sk89q.worldedit.math.transform.Transform; import com.sk89q.worldedit.math.transform.Transform;
import com.sk89q.worldedit.session.ClipboardHolder; import com.sk89q.worldedit.session.ClipboardHolder;
import com.sk89q.worldedit.util.io.Closer; import com.sk89q.worldedit.util.command.binding.Switch;
import com.sk89q.worldedit.util.command.parametric.Optional; import com.sk89q.worldedit.util.command.parametric.Optional;
import com.sk89q.worldedit.util.io.Closer;
import com.sk89q.worldedit.util.io.file.FilenameException;
import com.sk89q.worldedit.util.io.file.FilenameResolutionException;
import com.sk89q.worldedit.world.registry.WorldData; import com.sk89q.worldedit.world.registry.WorldData;
import java.io.BufferedInputStream; import java.io.BufferedInputStream;
@ -51,8 +52,10 @@ import java.io.FileFilter;
import java.io.FileInputStream; import java.io.FileInputStream;
import java.io.FileOutputStream; import java.io.FileOutputStream;
import java.io.IOException; import java.io.IOException;
import java.util.ArrayList;
import java.util.Arrays; import java.util.Arrays;
import java.util.Comparator; import java.util.Comparator;
import java.util.List;
import java.util.logging.Level; import java.util.logging.Level;
import java.util.logging.Logger; import java.util.logging.Logger;
@ -63,6 +66,10 @@ import static com.google.common.base.Preconditions.checkNotNull;
*/ */
public class SchematicCommands { public class SchematicCommands {
/**
* 9 schematics per page fits in the MC chat window.
*/
private static final int SCHEMATICS_PER_PAGE = 9;
private static final Logger log = Logger.getLogger(SchematicCommands.class.getCanonicalName()); private static final Logger log = Logger.getLogger(SchematicCommands.class.getCanonicalName());
private final WorldEdit worldEdit; private final WorldEdit worldEdit;
@ -244,14 +251,16 @@ public class SchematicCommands {
@Command( @Command(
aliases = {"list", "all", "ls"}, aliases = {"list", "all", "ls"},
desc = "List saved schematics", desc = "List saved schematics",
max = 0, min = 0,
flags = "dn", max = 1,
flags = "dnp",
help = "List all schematics in the schematics directory\n" + help = "List all schematics in the schematics directory\n" +
" -d sorts by date, oldest first\n" + " -d sorts by date, oldest first\n" +
" -n sorts by date, newest first\n" " -n sorts by date, newest first\n" +
" -p <page> prints the requested page\n"
) )
@CommandPermissions("worldedit.schematic.list") @CommandPermissions("worldedit.schematic.list")
public void list(Actor actor, CommandContext args) throws WorldEditException { public void list(Actor actor, CommandContext args, @Switch('p') @Optional("1") int page) throws WorldEditException {
File dir = worldEdit.getWorkingDirectoryFile(worldEdit.getConfiguration().saveDir); File dir = worldEdit.getWorkingDirectoryFile(worldEdit.getConfiguration().saveDir);
File[] files = dir.listFiles(new FileFilter(){ File[] files = dir.listFiles(new FileFilter(){
@Override @Override
@ -266,6 +275,21 @@ public class SchematicCommands {
throw new FilenameResolutionException(dir.getPath(), "Schematics directory invalid or not found."); throw new FilenameResolutionException(dir.getPath(), "Schematics directory invalid or not found.");
} }
if (files.length == 0) {
actor.printError("No schematics found.");
return;
}
int pageCount = files.length / SCHEMATICS_PER_PAGE + 1;
if (page < 1) {
actor.printError("Page must be at least 1");
return;
}
if (page > pageCount) {
actor.printError("Page must be less than " + (pageCount + 1));
return;
}
final int sortType = args.hasFlag('d') ? -1 : args.hasFlag('n') ? 1 : 0; final int sortType = args.hasFlag('d') ? -1 : args.hasFlag('n') ? 1 : 0;
// cleanup file list // cleanup file list
Arrays.sort(files, new Comparator<File>(){ Arrays.sort(files, new Comparator<File>(){
@ -283,13 +307,23 @@ public class SchematicCommands {
} }
}); });
actor.print("Available schematics (Filename (Format)):"); List<String> schematics = listFiles("", files);
actor.print(listFiles("", files)); int offset = (page - 1) * SCHEMATICS_PER_PAGE;
actor.print("Available schematics (Filename: Format) [" + page + "/" + pageCount + "]:");
StringBuilder build = new StringBuilder();
int limit = Math.min(offset + SCHEMATICS_PER_PAGE, schematics.size());
for (int i = offset; i < limit; i++) {
build.append(schematics.get(i));
} }
private String listFiles(String prefix, File[] files) { actor.print(build.toString());
StringBuilder build = new StringBuilder(); }
private List<String> listFiles(String prefix, File[] files) {
List<String> result = new ArrayList<String>();
for (File file : files) { for (File file : files) {
StringBuilder build = new StringBuilder();
if (file.isDirectory()) { if (file.isDirectory()) {
build.append(listFiles(prefix + file.getName() + "/", file.listFiles())); build.append(listFiles(prefix + file.getName() + "/", file.listFiles()));
continue; continue;
@ -299,10 +333,16 @@ public class SchematicCommands {
continue; continue;
} }
build.append("\n\u00a79"); if (!result.isEmpty()) { // prevent an empty line
ClipboardFormat format = ClipboardFormat.findByFile(file); build.append('\n');
build.append(prefix).append(file.getName()).append(": ").append(format == null ? "Unknown" : format.name());
} }
return build.toString(); build.append("\u00a79");
ClipboardFormat format = ClipboardFormat.findByFile(file);
build.append(prefix).append(file.getName())
.append(": ").append(format == null ? "Unknown" : format.name());
result.add(build.toString());
}
return result;
} }
} }