Fix schematics in directories.

Dieser Commit ist enthalten in:
wizjany 2016-01-13 18:55:49 -05:00
Ursprung 629b7d56dc
Commit 62befe7594

Datei anzeigen

@ -42,13 +42,11 @@ 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.Closer;
import com.sk89q.worldedit.util.io.file.FilenameException; 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;
import java.io.BufferedOutputStream; import java.io.BufferedOutputStream;
import java.io.File; import java.io.File;
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;
@ -58,6 +56,7 @@ import java.util.Comparator;
import java.util.List; import java.util.List;
import java.util.logging.Level; import java.util.logging.Level;
import java.util.logging.Logger; import java.util.logging.Logger;
import java.util.regex.Pattern;
import static com.google.common.base.Preconditions.checkNotNull; import static com.google.common.base.Preconditions.checkNotNull;
@ -262,18 +261,9 @@ public class SchematicCommands {
@CommandPermissions("worldedit.schematic.list") @CommandPermissions("worldedit.schematic.list")
public void list(Actor actor, CommandContext args, @Switch('p') @Optional("1") int page) 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(){ List<File> fileList = allFiles(dir);
@Override File[] files = new File[fileList.size()];
public boolean accept(File file) { fileList.toArray(files);
// 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) {
throw new FilenameResolutionException(dir.getPath(), "Schematics directory invalid or not found.");
}
if (files.length == 0) { if (files.length == 0) {
actor.printError("No schematics found."); actor.printError("No schematics found.");
@ -295,52 +285,66 @@ 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) {
// 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 res;
Long.valueOf(f1.lastModified()).compareTo(f2.lastModified()); // use date if there is a flag if (sortType == 0) { // use name by default
if (sortType == 1) result = -result; // flip date for newest first instead of oldest first int p = f1.getParent().compareTo(f2.getParent());
return result; if (p == 0) { // same parent, compare names
res = f1.getName().compareTo(f2.getName());
} else { // different parent, sort by that
res = p;
}
} else {
res = Long.valueOf(f1.lastModified()).compareTo(f2.lastModified()); // use date if there is a flag
if (sortType == 1) res = -res; // flip date for newest first instead of oldest first
}
return res;
} }
}); });
List<String> schematics = listFiles("", files); List<String> schematics = listFiles(worldEdit.getConfiguration().saveDir, files);
int offset = (page - 1) * SCHEMATICS_PER_PAGE; int offset = (page - 1) * SCHEMATICS_PER_PAGE;
actor.print("Available schematics (Filename: Format) [" + page + "/" + pageCount + "]:"); actor.print("Available schematics (Filename: Format) [" + page + "/" + pageCount + "]:");
StringBuilder build = new StringBuilder(); StringBuilder build = new StringBuilder();
int limit = Math.min(offset + SCHEMATICS_PER_PAGE, schematics.size()); int limit = Math.min(offset + SCHEMATICS_PER_PAGE, schematics.size());
for (int i = offset; i < limit; i++) { for (int i = offset; i < limit;) {
build.append(schematics.get(i)); build.append(schematics.get(i));
if (++i != limit) {
build.append("\n");
}
} }
actor.print(build.toString()); actor.print(build.toString());
} }
private List<File> allFiles(File root) {
File[] files = root.listFiles();
if (files == null) return null;
List<File> fileList = new ArrayList<File>();
for (File f : files) {
if (f.isDirectory()) {
List<File> subFiles = allFiles(f);
if (subFiles == null) continue; // empty subdir
fileList.addAll(subFiles);
} else {
fileList.add(f);
}
}
return fileList;
}
private List<String> listFiles(String prefix, File[] files) { private List<String> listFiles(String prefix, File[] files) {
if (prefix == null) prefix = "";
List<String> result = new ArrayList<String>(); List<String> result = new ArrayList<String>();
for (File file : files) { for (File file : files) {
StringBuilder build = new StringBuilder(); StringBuilder build = new StringBuilder();
if (file.isDirectory()) {
build.append(listFiles(prefix + file.getName() + "/", file.listFiles()));
continue;
}
if (!file.isFile()) { build.append("\u00a72");
continue;
}
if (!result.isEmpty()) { // prevent an empty line
build.append('\n');
}
build.append("\u00a79");
ClipboardFormat format = ClipboardFormat.findByFile(file); ClipboardFormat format = ClipboardFormat.findByFile(file);
build.append(prefix).append(file.getName()) boolean inRoot = file.getParentFile().getName().equals(prefix);
.append(": ").append(format == null ? "Unknown" : format.name()); build.append(inRoot ? file.getName() : file.getPath().split(Pattern.quote(prefix + File.separator))[1])
.append(": ").append(format == null ? "Unknown" : format.name());
result.add(build.toString()); result.add(build.toString());
} }
return result; return result;