Fixed listing schematics with directories in the folder.

This commit is contained in:
wizjany 2014-04-25 23:45:07 -04:00
parent e69e72c410
commit 5ed9239e14

View File

@ -20,6 +20,7 @@
package com.sk89q.worldedit.command; package com.sk89q.worldedit.command;
import java.io.File; import java.io.File;
import java.io.FileFilter;
import java.io.IOException; import java.io.IOException;
import java.util.Arrays; import java.util.Arrays;
import java.util.Comparator; import java.util.Comparator;
@ -221,7 +222,15 @@ public class SchematicCommands {
public void list(CommandContext args, LocalSession session, LocalPlayer player, public void list(CommandContext args, LocalSession session, LocalPlayer player,
EditSession editSession) throws WorldEditException { EditSession editSession) throws WorldEditException {
File dir = we.getWorkingDirectoryFile(we.getConfiguration().saveDir); File dir = we.getWorkingDirectoryFile(we.getConfiguration().saveDir);
File[] files = dir.listFiles(); File[] files = dir.listFiles(new FileFilter(){
@Override
public boolean accept(File file) {
// sort out directories from the schematic list
// if WE supports sub-directories in the future,
// this will have to be changed
return file.isFile();
}
});
if (files == null) { if (files == null) {
throw new FilenameResolutionException(dir.getPath(), "Schematics directory invalid or not found."); throw new FilenameResolutionException(dir.getPath(), "Schematics directory invalid or not found.");
} }
@ -232,7 +241,10 @@ public class SchematicCommands {
Arrays.sort(files, new Comparator<File>(){ Arrays.sort(files, new Comparator<File>(){
@Override @Override
public int compare(File f1, File f2) { public int compare(File f1, File f2) {
if (!f1.isFile() || !f2.isFile()) return -1; // don't care, will get removed // this should no longer happen, as directory-ness is checked before
// however, if a directory slips through, this will break the contract
// of comparator transitivity
if (!f1.isFile() || !f2.isFile()) return -1;
// http://stackoverflow.com/questions/203030/best-way-to-list-files-in-java-sorted-by-date-modified // 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 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 Long.valueOf(f1.lastModified()).compareTo(f2.lastModified()); // use date if there is a flag
@ -242,9 +254,6 @@ public class SchematicCommands {
}); });
for (File file : files) { for (File file : files) {
if (!file.isFile()) {
continue;
}
build.append("\n\u00a79"); build.append("\n\u00a79");
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());