Added -d and -n flags for //schem list, allowing sorting by date by oldest or newest, respectively. By default the command should now sort by file name alphabetically.

This commit is contained in:
Wizjany 2012-12-25 17:36:57 -05:00
parent 2aab0369b5
commit a7b4913f6c

View File

@ -20,6 +20,8 @@ package com.sk89q.worldedit.commands;
import java.io.File; import java.io.File;
import java.io.IOException; import java.io.IOException;
import java.util.Arrays;
import java.util.Comparator;
import com.sk89q.minecraft.util.commands.Command; import com.sk89q.minecraft.util.commands.Command;
import com.sk89q.minecraft.util.commands.CommandContext; import com.sk89q.minecraft.util.commands.CommandContext;
@ -207,7 +209,11 @@ public class SchematicCommands {
@Command( @Command(
aliases = {"list", "all", "ls"}, aliases = {"list", "all", "ls"},
desc = "List available schematics", desc = "List available schematics",
max = 0 max = 0,
flags = "dn",
help = "List all schematics in the schematics directory\n" +
" -d sorts by date, oldest first\n" +
" -n sorts by date, newest first\n"
) )
@Console @Console
@CommandPermissions("worldedit.schematic.list") @CommandPermissions("worldedit.schematic.list")
@ -221,14 +227,25 @@ public class SchematicCommands {
StringBuilder build = new StringBuilder("Available schematics (Filename (Format)): "); StringBuilder build = new StringBuilder("Available schematics (Filename (Format)): ");
boolean first = true; boolean first = true;
final int sortType = args.hasFlag('d') ? -1 : args.hasFlag('n') ? 1 : 0;
// cleanup file list
Arrays.sort(files, new Comparator<File>(){
@Override
public int compare(File f1, File f2) {
if (!f1.isFile() || !f2.isFile()) return -1; // don't care, will get removed
// http://stackoverflow.com/questions/203030/best-way-to-list-files-in-java-sorted-by-date-modified
int result = sortType == 0 ? f1.getName().compareToIgnoreCase(f2.getName()) : // use name by default
Long.valueOf(f1.lastModified()).compareTo(f2.lastModified()); // use date if there is a flag
if (sortType == 1) result = -result; // flip date for newest first instead of oldest first
return result;
}
});
for (File file : files) { for (File file : files) {
if (!file.isFile()) { if (!file.isFile()) {
continue; continue;
} }
build.append("\n\u00a79");
if (!first) {
build.append(", ");
}
SchematicFormat format = SchematicFormat.getFormat(file); SchematicFormat format = SchematicFormat.getFormat(file);
build.append(file.getName()).append(": ").append(format == null ? "Unknown" : format.getName()); build.append(file.getName()).append(": ").append(format == null ? "Unknown" : format.getName());
first = false; first = false;