mirror of
https://github.com/boy0001/FastAsyncWorldedit.git
synced 2024-11-28 13:45:36 +01:00
Fixes #209
This commit is contained in:
parent
600219eb08
commit
9519fde6bd
@ -113,9 +113,11 @@ public enum BBC {
|
||||
|
||||
SCHEMATIC_DELETE("%s0 has been deleted.", "Worldedit.Schematic"),
|
||||
SCHEMATIC_FORMAT("Available clipboard formats (Name: Lookup names)", "Worldedit.Schematic"),
|
||||
SCHEMATIC_LIST("Available schematics (Filename (Format)):", "Worldedit.Schematic"),
|
||||
SCHEMATIC_LOADED("%s0 loaded. Paste it with //paste", "Worldedit.Schematic"),
|
||||
SCHEMATIC_SAVED("%s0 saved.", "Worldedit.Schematic"),
|
||||
SCHEMATIC_PAGE("Page must be %s", "WorldEdit.Schematic"),
|
||||
SCHEMATIC_NONE("No schematics found.", "WorldEdit.Schematic"),
|
||||
SCHEMATIC_LIST("Available schematics (Filename: Format) [%s0/%s1]:", "Worldedit.Schematic"),
|
||||
|
||||
CLIPBOARD_CLEARED("Clipboard cleared", "WorldEdit.Clipboard"),
|
||||
|
||||
|
@ -42,12 +42,11 @@ import com.sk89q.worldedit.extent.clipboard.io.SchematicReader;
|
||||
import com.sk89q.worldedit.function.operation.Operations;
|
||||
import com.sk89q.worldedit.math.transform.Transform;
|
||||
import com.sk89q.worldedit.session.ClipboardHolder;
|
||||
import com.sk89q.worldedit.util.command.binding.Switch;
|
||||
import com.sk89q.worldedit.util.command.parametric.Optional;
|
||||
import com.sk89q.worldedit.util.io.file.FilenameException;
|
||||
import com.sk89q.worldedit.util.io.file.FilenameResolutionException;
|
||||
import com.sk89q.worldedit.world.registry.WorldData;
|
||||
import java.io.File;
|
||||
import java.io.FileFilter;
|
||||
import java.io.FileInputStream;
|
||||
import java.io.FileOutputStream;
|
||||
import java.io.IOException;
|
||||
@ -55,11 +54,14 @@ import java.io.InputStream;
|
||||
import java.net.URL;
|
||||
import java.nio.channels.Channels;
|
||||
import java.nio.channels.ReadableByteChannel;
|
||||
import java.util.ArrayList;
|
||||
import java.util.Arrays;
|
||||
import java.util.Comparator;
|
||||
import java.util.List;
|
||||
import java.util.UUID;
|
||||
import java.util.logging.Level;
|
||||
import java.util.logging.Logger;
|
||||
import java.util.regex.Pattern;
|
||||
|
||||
|
||||
import static com.google.common.base.Preconditions.checkNotNull;
|
||||
@ -69,6 +71,10 @@ import static com.google.common.base.Preconditions.checkNotNull;
|
||||
*/
|
||||
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 final WorldEdit worldEdit;
|
||||
|
||||
@ -238,66 +244,108 @@ public class SchematicCommands {
|
||||
}
|
||||
}
|
||||
|
||||
@Command(aliases = { "list", "all", "ls" }, desc = "List saved schematics", 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")
|
||||
@Command(
|
||||
aliases = {"list", "all", "ls"},
|
||||
desc = "List saved schematics",
|
||||
min = 0,
|
||||
max = 1,
|
||||
flags = "dnp",
|
||||
help = "List all schematics in the schematics directory\n" +
|
||||
" -d sorts by date, oldest first\n" +
|
||||
" -n sorts by date, newest first\n" +
|
||||
" -p <page> prints the requested page\n"
|
||||
)
|
||||
@CommandPermissions("worldedit.schematic.list")
|
||||
public void list(final Actor actor, final CommandContext args) throws WorldEditException {
|
||||
final File dir = this.worldEdit.getWorkingDirectoryFile(this.worldEdit.getConfiguration().saveDir);
|
||||
final File[] files = dir.listFiles(new FileFilter() {
|
||||
@Override
|
||||
public boolean accept(final 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();
|
||||
public void list(Actor actor, CommandContext args, @Switch('p') @Optional("1") int page) throws WorldEditException {
|
||||
File dir = worldEdit.getWorkingDirectoryFile(worldEdit.getConfiguration().saveDir);
|
||||
List<File> fileList = allFiles(dir);
|
||||
|
||||
if (fileList == null || fileList.isEmpty()) {
|
||||
BBC.SCHEMATIC_NONE.send(actor);
|
||||
return;
|
||||
}
|
||||
});
|
||||
if (files == null) {
|
||||
throw new FilenameResolutionException(dir.getPath(), "Schematics directory invalid or not found.");
|
||||
|
||||
File[] files = new File[fileList.size()];
|
||||
fileList.toArray(files);
|
||||
|
||||
int pageCount = files.length / SCHEMATICS_PER_PAGE + 1;
|
||||
if (page < 1) {
|
||||
BBC.SCHEMATIC_PAGE.send(actor, ">0");
|
||||
return;
|
||||
}
|
||||
if (page > pageCount) {
|
||||
BBC.SCHEMATIC_PAGE.send(actor, "<" + (pageCount + 1));
|
||||
return;
|
||||
}
|
||||
|
||||
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(final File f1, final 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;
|
||||
public int compare(File f1, File f2) {
|
||||
int res;
|
||||
if (sortType == 0) { // use name by default
|
||||
int p = f1.getParent().compareTo(f2.getParent());
|
||||
if (p == 0) { // same parent, compare names
|
||||
res = f1.getName().compareTo(f2.getName());
|
||||
} else { // different parent, sort by that
|
||||
res = p;
|
||||
}
|
||||
// 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
|
||||
} 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 result;
|
||||
return res;
|
||||
}
|
||||
});
|
||||
BBC.SCHEMATIC_LIST.send(actor);
|
||||
actor.print(this.listFiles("", files));
|
||||
|
||||
List<String> schematics = listFiles(worldEdit.getConfiguration().saveDir, files);
|
||||
int offset = (page - 1) * SCHEMATICS_PER_PAGE;
|
||||
|
||||
BBC.SCHEMATIC_LIST.send(actor, page, pageCount);
|
||||
StringBuilder build = new StringBuilder();
|
||||
int limit = Math.min(offset + SCHEMATICS_PER_PAGE, schematics.size());
|
||||
for (int i = offset; i < limit;) {
|
||||
build.append(schematics.get(i));
|
||||
if (++i != limit) {
|
||||
build.append("\n");
|
||||
}
|
||||
}
|
||||
|
||||
private String listFiles(final String prefix, final File[] files) {
|
||||
final StringBuilder build = new StringBuilder();
|
||||
for (final File file : files) {
|
||||
if (file.isDirectory()) {
|
||||
build.append(this.listFiles(prefix + file.getName() + "/", file.listFiles()));
|
||||
continue;
|
||||
actor.print(build.toString());
|
||||
}
|
||||
|
||||
if (!file.isFile()) {
|
||||
continue;
|
||||
|
||||
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;
|
||||
}
|
||||
|
||||
build.append("\n\u00a79");
|
||||
final ClipboardFormat format = ClipboardFormat.findByFile(file);
|
||||
build.append(prefix).append(file.getName()).append(": ").append(format == null ? "Unknown" : format.name());
|
||||
private List<String> listFiles(String prefix, File[] files) {
|
||||
if (prefix == null) prefix = "";
|
||||
List<String> result = new ArrayList<String>();
|
||||
for (File file : files) {
|
||||
StringBuilder build = new StringBuilder();
|
||||
|
||||
build.append("\u00a72");
|
||||
ClipboardFormat format = ClipboardFormat.findByFile(file);
|
||||
boolean inRoot = file.getParentFile().getName().equals(prefix);
|
||||
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());
|
||||
}
|
||||
return build.toString();
|
||||
return result;
|
||||
}
|
||||
|
||||
public static Class<?> inject() {
|
||||
|
Loading…
Reference in New Issue
Block a user