2012-03-28 18:05:52 +00:00
|
|
|
/*
|
2014-04-04 22:03:18 +00:00
|
|
|
* WorldEdit, a Minecraft world manipulation toolkit
|
|
|
|
* Copyright (C) sk89q <http://www.sk89q.com>
|
|
|
|
* Copyright (C) WorldEdit team and contributors
|
2012-03-28 18:05:52 +00:00
|
|
|
*
|
2014-04-04 22:03:18 +00:00
|
|
|
* This program is free software: you can redistribute it and/or modify it
|
|
|
|
* under the terms of the GNU Lesser General Public License as published by the
|
|
|
|
* Free Software Foundation, either version 3 of the License, or
|
2012-03-28 18:05:52 +00:00
|
|
|
* (at your option) any later version.
|
|
|
|
*
|
2014-04-04 22:03:18 +00:00
|
|
|
* This program is distributed in the hope that it will be useful, but WITHOUT
|
|
|
|
* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
|
|
|
|
* FITNESS FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public License
|
|
|
|
* for more details.
|
2012-03-28 18:05:52 +00:00
|
|
|
*
|
2014-04-04 22:03:18 +00:00
|
|
|
* You should have received a copy of the GNU Lesser General Public License
|
2012-03-28 18:05:52 +00:00
|
|
|
* along with this program. If not, see <http://www.gnu.org/licenses/>.
|
|
|
|
*/
|
|
|
|
|
2014-04-03 02:08:50 +00:00
|
|
|
package com.sk89q.worldedit.command;
|
2012-03-28 18:05:52 +00:00
|
|
|
|
2014-06-30 00:47:08 +00:00
|
|
|
import com.sk89q.minecraft.util.commands.Command;
|
|
|
|
import com.sk89q.minecraft.util.commands.CommandContext;
|
|
|
|
import com.sk89q.minecraft.util.commands.CommandException;
|
|
|
|
import com.sk89q.minecraft.util.commands.CommandPermissions;
|
2017-06-09 16:44:54 +00:00
|
|
|
import com.sk89q.worldedit.EditSession;
|
2014-06-30 00:47:08 +00:00
|
|
|
import com.sk89q.worldedit.LocalConfiguration;
|
|
|
|
import com.sk89q.worldedit.LocalSession;
|
|
|
|
import com.sk89q.worldedit.WorldEdit;
|
|
|
|
import com.sk89q.worldedit.WorldEditException;
|
2014-06-28 08:01:49 +00:00
|
|
|
import com.sk89q.worldedit.entity.Player;
|
2014-06-28 00:55:39 +00:00
|
|
|
import com.sk89q.worldedit.extension.platform.Actor;
|
2014-07-10 05:04:17 +00:00
|
|
|
import com.sk89q.worldedit.extent.clipboard.BlockArrayClipboard;
|
2014-07-10 04:08:34 +00:00
|
|
|
import com.sk89q.worldedit.extent.clipboard.Clipboard;
|
|
|
|
import com.sk89q.worldedit.extent.clipboard.io.ClipboardFormat;
|
|
|
|
import com.sk89q.worldedit.extent.clipboard.io.ClipboardReader;
|
|
|
|
import com.sk89q.worldedit.extent.clipboard.io.ClipboardWriter;
|
2014-07-10 05:04:17 +00:00
|
|
|
import com.sk89q.worldedit.function.operation.Operations;
|
|
|
|
import com.sk89q.worldedit.math.transform.Transform;
|
2014-07-10 04:08:34 +00:00
|
|
|
import com.sk89q.worldedit.session.ClipboardHolder;
|
2015-12-28 20:05:16 +00:00
|
|
|
import com.sk89q.worldedit.util.command.binding.Switch;
|
2014-07-10 04:08:34 +00:00
|
|
|
import com.sk89q.worldedit.util.command.parametric.Optional;
|
2015-12-28 20:05:16 +00:00
|
|
|
import com.sk89q.worldedit.util.io.Closer;
|
|
|
|
import com.sk89q.worldedit.util.io.file.FilenameException;
|
2014-07-10 04:08:34 +00:00
|
|
|
import com.sk89q.worldedit.world.registry.WorldData;
|
2014-05-02 01:00:59 +00:00
|
|
|
|
2014-07-10 04:08:34 +00:00
|
|
|
import java.io.BufferedInputStream;
|
|
|
|
import java.io.BufferedOutputStream;
|
2012-12-15 18:49:44 +00:00
|
|
|
import java.io.File;
|
2014-07-10 04:08:34 +00:00
|
|
|
import java.io.FileInputStream;
|
|
|
|
import java.io.FileOutputStream;
|
|
|
|
import java.io.IOException;
|
2015-12-28 20:05:16 +00:00
|
|
|
import java.util.ArrayList;
|
2012-12-25 22:36:57 +00:00
|
|
|
import java.util.Arrays;
|
|
|
|
import java.util.Comparator;
|
2015-12-28 20:05:16 +00:00
|
|
|
import java.util.List;
|
2014-07-10 04:08:34 +00:00
|
|
|
import java.util.logging.Level;
|
|
|
|
import java.util.logging.Logger;
|
2016-01-13 23:55:49 +00:00
|
|
|
import java.util.regex.Pattern;
|
2012-12-15 18:49:44 +00:00
|
|
|
|
2014-06-28 08:01:49 +00:00
|
|
|
import static com.google.common.base.Preconditions.checkNotNull;
|
|
|
|
|
2012-03-28 18:05:52 +00:00
|
|
|
/**
|
2014-06-28 08:01:49 +00:00
|
|
|
* Commands that work with schematic files.
|
2012-03-28 18:05:52 +00:00
|
|
|
*/
|
|
|
|
public class SchematicCommands {
|
|
|
|
|
2015-12-28 20:05:16 +00:00
|
|
|
/**
|
|
|
|
* 9 schematics per page fits in the MC chat window.
|
|
|
|
*/
|
|
|
|
private static final int SCHEMATICS_PER_PAGE = 9;
|
2014-07-10 04:08:34 +00:00
|
|
|
private static final Logger log = Logger.getLogger(SchematicCommands.class.getCanonicalName());
|
2014-06-28 08:01:49 +00:00
|
|
|
private final WorldEdit worldEdit;
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Create a new instance.
|
|
|
|
*
|
|
|
|
* @param worldEdit reference to WorldEdit
|
|
|
|
*/
|
|
|
|
public SchematicCommands(WorldEdit worldEdit) {
|
|
|
|
checkNotNull(worldEdit);
|
|
|
|
this.worldEdit = worldEdit;
|
2012-03-28 18:05:52 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
@Command(
|
2014-07-10 04:08:34 +00:00
|
|
|
aliases = { "load" },
|
|
|
|
usage = "[<format>] <filename>",
|
2015-06-19 22:27:20 +00:00
|
|
|
desc = "Load a schematic into your clipboard",
|
|
|
|
min = 1, max = 2
|
2012-03-28 18:05:52 +00:00
|
|
|
)
|
2014-07-10 04:08:34 +00:00
|
|
|
@Deprecated
|
|
|
|
@CommandPermissions({ "worldedit.clipboard.load", "worldedit.schematic.load" })
|
|
|
|
public void load(Player player, LocalSession session, @Optional("schematic") String formatName, String filename) throws FilenameException {
|
|
|
|
LocalConfiguration config = worldEdit.getConfiguration();
|
|
|
|
|
|
|
|
File dir = worldEdit.getWorkingDirectoryFile(config.saveDir);
|
|
|
|
File f = worldEdit.getSafeOpenFile(player, dir, filename, "schematic", "schematic");
|
|
|
|
|
|
|
|
if (!f.exists()) {
|
|
|
|
player.printError("Schematic " + filename + " does not exist!");
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
ClipboardFormat format = ClipboardFormat.findByAlias(formatName);
|
|
|
|
if (format == null) {
|
|
|
|
player.printError("Unknown schematic format: " + formatName);
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
Closer closer = Closer.create();
|
|
|
|
try {
|
2015-06-19 22:27:20 +00:00
|
|
|
FileInputStream fis = closer.register(new FileInputStream(f));
|
|
|
|
BufferedInputStream bis = closer.register(new BufferedInputStream(fis));
|
|
|
|
ClipboardReader reader = format.getReader(bis);
|
2014-07-10 04:08:34 +00:00
|
|
|
|
2015-06-19 22:27:20 +00:00
|
|
|
WorldData worldData = player.getWorld().getWorldData();
|
|
|
|
Clipboard clipboard = reader.read(player.getWorld().getWorldData());
|
|
|
|
session.setClipboard(new ClipboardHolder(clipboard, worldData));
|
2014-07-10 04:08:34 +00:00
|
|
|
|
2015-06-19 22:27:20 +00:00
|
|
|
log.info(player.getName() + " loaded " + f.getCanonicalPath());
|
|
|
|
player.print(filename + " loaded. Paste it with //paste");
|
2014-07-10 04:08:34 +00:00
|
|
|
} catch (IOException e) {
|
|
|
|
player.printError("Schematic could not read or it does not exist: " + e.getMessage());
|
|
|
|
log.log(Level.WARNING, "Failed to load a saved clipboard", e);
|
|
|
|
} finally {
|
|
|
|
try {
|
|
|
|
closer.close();
|
|
|
|
} catch (IOException ignored) {
|
|
|
|
}
|
|
|
|
}
|
2012-03-28 18:05:52 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
@Command(
|
2014-07-10 04:08:34 +00:00
|
|
|
aliases = { "save" },
|
|
|
|
usage = "[<format>] <filename>",
|
2015-06-19 22:27:20 +00:00
|
|
|
desc = "Save a schematic into your clipboard",
|
|
|
|
min = 1, max = 2
|
2012-03-28 18:05:52 +00:00
|
|
|
)
|
2014-07-10 04:08:34 +00:00
|
|
|
@Deprecated
|
|
|
|
@CommandPermissions({ "worldedit.clipboard.save", "worldedit.schematic.save" })
|
2014-07-10 05:04:17 +00:00
|
|
|
public void save(Player player, LocalSession session, @Optional("schematic") String formatName, String filename) throws CommandException, WorldEditException {
|
2014-07-10 04:08:34 +00:00
|
|
|
LocalConfiguration config = worldEdit.getConfiguration();
|
|
|
|
|
|
|
|
File dir = worldEdit.getWorkingDirectoryFile(config.saveDir);
|
|
|
|
File f = worldEdit.getSafeSaveFile(player, dir, filename, "schematic", "schematic");
|
|
|
|
|
|
|
|
ClipboardFormat format = ClipboardFormat.findByAlias(formatName);
|
|
|
|
if (format == null) {
|
|
|
|
player.printError("Unknown schematic format: " + formatName);
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
ClipboardHolder holder = session.getClipboard();
|
2014-07-10 05:04:17 +00:00
|
|
|
Clipboard clipboard = holder.getClipboard();
|
|
|
|
Transform transform = holder.getTransform();
|
|
|
|
Clipboard target;
|
|
|
|
|
2014-07-10 05:51:17 +00:00
|
|
|
// If we have a transform, bake it into the copy
|
2014-07-10 05:04:17 +00:00
|
|
|
if (!transform.isIdentity()) {
|
2014-07-10 05:51:17 +00:00
|
|
|
FlattenedClipboardTransform result = FlattenedClipboardTransform.transform(clipboard, transform, holder.getWorldData());
|
2014-07-10 05:04:17 +00:00
|
|
|
target = new BlockArrayClipboard(result.getTransformedRegion());
|
2014-07-10 05:51:17 +00:00
|
|
|
target.setOrigin(clipboard.getOrigin());
|
2014-07-10 05:04:17 +00:00
|
|
|
Operations.completeLegacy(result.copyTo(target));
|
|
|
|
} else {
|
|
|
|
target = clipboard;
|
|
|
|
}
|
2014-07-10 04:08:34 +00:00
|
|
|
|
|
|
|
Closer closer = Closer.create();
|
|
|
|
try {
|
|
|
|
// Create parent directories
|
|
|
|
File parent = f.getParentFile();
|
|
|
|
if (parent != null && !parent.exists()) {
|
|
|
|
if (!parent.mkdirs()) {
|
|
|
|
throw new CommandException("Could not create folder for schematics!");
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
FileOutputStream fos = closer.register(new FileOutputStream(f));
|
|
|
|
BufferedOutputStream bos = closer.register(new BufferedOutputStream(fos));
|
|
|
|
ClipboardWriter writer = closer.register(format.getWriter(bos));
|
2014-07-10 05:04:17 +00:00
|
|
|
writer.write(target, holder.getWorldData());
|
2014-07-10 04:08:34 +00:00
|
|
|
log.info(player.getName() + " saved " + f.getCanonicalPath());
|
|
|
|
player.print(filename + " saved.");
|
|
|
|
} catch (IOException e) {
|
|
|
|
player.printError("Schematic could not written: " + e.getMessage());
|
|
|
|
log.log(Level.WARNING, "Failed to write a saved clipboard", e);
|
|
|
|
} finally {
|
|
|
|
try {
|
|
|
|
closer.close();
|
|
|
|
} catch (IOException ignored) {
|
|
|
|
}
|
|
|
|
}
|
2012-03-28 18:05:52 +00:00
|
|
|
}
|
|
|
|
|
2014-05-02 01:00:59 +00:00
|
|
|
@Command(
|
|
|
|
aliases = { "delete", "d" },
|
|
|
|
usage = "<filename>",
|
2014-07-01 04:48:26 +00:00
|
|
|
desc = "Delete a saved schematic",
|
2014-05-02 01:00:59 +00:00
|
|
|
help = "Delete a schematic from the schematic list",
|
|
|
|
min = 1,
|
|
|
|
max = 1
|
|
|
|
)
|
|
|
|
@CommandPermissions("worldedit.schematic.delete")
|
2017-06-09 16:44:54 +00:00
|
|
|
public void delete(Player player, LocalSession session, EditSession editSession, CommandContext args) throws WorldEditException {
|
2014-05-02 01:00:59 +00:00
|
|
|
|
2014-06-28 08:01:49 +00:00
|
|
|
LocalConfiguration config = worldEdit.getConfiguration();
|
2014-05-02 01:00:59 +00:00
|
|
|
String filename = args.getString(0);
|
|
|
|
|
2014-06-28 08:01:49 +00:00
|
|
|
File dir = worldEdit.getWorkingDirectoryFile(config.saveDir);
|
|
|
|
File f = worldEdit.getSafeSaveFile(player, dir, filename, "schematic", "schematic");
|
2014-05-02 01:00:59 +00:00
|
|
|
|
|
|
|
if (!f.exists()) {
|
|
|
|
player.printError("Schematic " + filename + " does not exist!");
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (!f.delete()) {
|
|
|
|
player.printError("Deletion of " + filename + " failed! Maybe it is read-only.");
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
player.print(filename + " has been deleted.");
|
|
|
|
}
|
|
|
|
|
2012-03-28 18:05:52 +00:00
|
|
|
@Command(
|
2012-04-01 16:21:27 +00:00
|
|
|
aliases = {"formats", "listformats", "f"},
|
2014-07-01 04:48:26 +00:00
|
|
|
desc = "List available formats",
|
2012-03-28 18:05:52 +00:00
|
|
|
max = 0
|
|
|
|
)
|
2012-04-01 16:21:27 +00:00
|
|
|
@CommandPermissions("worldedit.schematic.formats")
|
2014-06-28 00:55:39 +00:00
|
|
|
public void formats(Actor actor) throws WorldEditException {
|
2014-07-10 04:08:34 +00:00
|
|
|
actor.print("Available clipboard formats (Name: Lookup names)");
|
2012-03-28 18:05:52 +00:00
|
|
|
StringBuilder builder;
|
|
|
|
boolean first = true;
|
2014-07-10 04:08:34 +00:00
|
|
|
for (ClipboardFormat format : ClipboardFormat.values()) {
|
2012-03-28 18:05:52 +00:00
|
|
|
builder = new StringBuilder();
|
2014-07-10 04:08:34 +00:00
|
|
|
builder.append(format.name()).append(": ");
|
|
|
|
for (String lookupName : format.getAliases()) {
|
2012-03-28 18:05:52 +00:00
|
|
|
if (!first) {
|
|
|
|
builder.append(", ");
|
|
|
|
}
|
|
|
|
builder.append(lookupName);
|
|
|
|
first = false;
|
|
|
|
}
|
|
|
|
first = true;
|
2014-06-28 00:55:39 +00:00
|
|
|
actor.print(builder.toString());
|
2012-03-28 18:05:52 +00:00
|
|
|
}
|
|
|
|
}
|
2012-03-28 19:30:36 +00:00
|
|
|
|
|
|
|
@Command(
|
2012-04-01 16:21:27 +00:00
|
|
|
aliases = {"list", "all", "ls"},
|
2014-07-01 04:48:26 +00:00
|
|
|
desc = "List saved schematics",
|
2015-12-28 20:05:16 +00:00
|
|
|
min = 0,
|
|
|
|
max = 1,
|
|
|
|
flags = "dnp",
|
2012-12-25 22:36:57 +00:00
|
|
|
help = "List all schematics in the schematics directory\n" +
|
|
|
|
" -d sorts by date, oldest first\n" +
|
2015-12-28 20:05:16 +00:00
|
|
|
" -n sorts by date, newest first\n" +
|
|
|
|
" -p <page> prints the requested page\n"
|
2012-03-28 19:30:36 +00:00
|
|
|
)
|
2012-04-01 16:21:27 +00:00
|
|
|
@CommandPermissions("worldedit.schematic.list")
|
2015-12-28 20:05:16 +00:00
|
|
|
public void list(Actor actor, CommandContext args, @Switch('p') @Optional("1") int page) throws WorldEditException {
|
2014-06-28 08:01:49 +00:00
|
|
|
File dir = worldEdit.getWorkingDirectoryFile(worldEdit.getConfiguration().saveDir);
|
2016-01-13 23:55:49 +00:00
|
|
|
List<File> fileList = allFiles(dir);
|
|
|
|
|
2016-05-18 16:44:22 +00:00
|
|
|
if (fileList == null || fileList.isEmpty()) {
|
2015-12-28 20:05:16 +00:00
|
|
|
actor.printError("No schematics found.");
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2016-03-08 17:42:23 +00:00
|
|
|
File[] files = new File[fileList.size()];
|
|
|
|
fileList.toArray(files);
|
|
|
|
|
2015-12-28 20:05:16 +00:00
|
|
|
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;
|
|
|
|
}
|
2012-12-15 18:49:44 +00:00
|
|
|
|
2012-12-25 22:36:57 +00:00
|
|
|
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) {
|
|
|
|
// http://stackoverflow.com/questions/203030/best-way-to-list-files-in-java-sorted-by-date-modified
|
2016-01-13 23:55:49 +00:00
|
|
|
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;
|
|
|
|
}
|
|
|
|
} 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;
|
2012-12-25 22:36:57 +00:00
|
|
|
}
|
|
|
|
});
|
|
|
|
|
2016-01-13 23:55:49 +00:00
|
|
|
List<String> schematics = listFiles(worldEdit.getConfiguration().saveDir, files);
|
2015-12-28 20:05:16 +00:00
|
|
|
int offset = (page - 1) * SCHEMATICS_PER_PAGE;
|
2013-12-19 16:32:31 +00:00
|
|
|
|
2015-12-28 20:05:16 +00:00
|
|
|
actor.print("Available schematics (Filename: Format) [" + page + "/" + pageCount + "]:");
|
2013-12-19 16:32:31 +00:00
|
|
|
StringBuilder build = new StringBuilder();
|
2015-12-28 20:05:16 +00:00
|
|
|
int limit = Math.min(offset + SCHEMATICS_PER_PAGE, schematics.size());
|
2016-01-13 23:55:49 +00:00
|
|
|
for (int i = offset; i < limit;) {
|
2015-12-28 20:05:16 +00:00
|
|
|
build.append(schematics.get(i));
|
2016-01-13 23:55:49 +00:00
|
|
|
if (++i != limit) {
|
|
|
|
build.append("\n");
|
|
|
|
}
|
2015-12-28 20:05:16 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
actor.print(build.toString());
|
|
|
|
}
|
|
|
|
|
2016-01-13 23:55:49 +00:00
|
|
|
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;
|
|
|
|
}
|
|
|
|
|
2015-12-28 20:05:16 +00:00
|
|
|
private List<String> listFiles(String prefix, File[] files) {
|
2016-01-13 23:55:49 +00:00
|
|
|
if (prefix == null) prefix = "";
|
2015-12-28 20:05:16 +00:00
|
|
|
List<String> result = new ArrayList<String>();
|
2012-12-15 18:49:44 +00:00
|
|
|
for (File file : files) {
|
2015-12-28 20:05:16 +00:00
|
|
|
StringBuilder build = new StringBuilder();
|
2013-12-19 16:32:31 +00:00
|
|
|
|
2016-01-13 23:55:49 +00:00
|
|
|
build.append("\u00a72");
|
2014-07-10 04:08:34 +00:00
|
|
|
ClipboardFormat format = ClipboardFormat.findByFile(file);
|
2016-01-13 23:55:49 +00:00
|
|
|
boolean inRoot = file.getParentFile().getName().equals(prefix);
|
2017-06-09 16:44:54 +00:00
|
|
|
build.append(inRoot ? file.getName() : file.getPath().split(Pattern.quote(prefix + File.separator))[1])
|
|
|
|
.append(": ").append(format == null ? "Unknown" : format.name());
|
2015-12-28 20:05:16 +00:00
|
|
|
result.add(build.toString());
|
2012-03-28 19:30:36 +00:00
|
|
|
}
|
2015-12-28 20:05:16 +00:00
|
|
|
return result;
|
2012-03-28 19:30:36 +00:00
|
|
|
}
|
2012-03-28 18:05:52 +00:00
|
|
|
}
|