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
|
|
|
|
2019-07-06 00:46:48 +00:00
|
|
|
import static com.boydti.fawe.util.ReflectionUtils.as;
|
|
|
|
import static com.google.common.base.Preconditions.checkNotNull;
|
|
|
|
|
2018-08-12 14:03:07 +00:00
|
|
|
import com.boydti.fawe.config.BBC;
|
|
|
|
import com.boydti.fawe.config.Commands;
|
|
|
|
import com.boydti.fawe.config.Settings;
|
|
|
|
import com.boydti.fawe.object.FawePlayer;
|
|
|
|
import com.boydti.fawe.object.RunnableVal3;
|
|
|
|
import com.boydti.fawe.object.clipboard.MultiClipboardHolder;
|
|
|
|
import com.boydti.fawe.object.clipboard.URIClipboardHolder;
|
|
|
|
import com.boydti.fawe.object.clipboard.remap.ClipboardRemapper;
|
|
|
|
import com.boydti.fawe.object.schematic.StructureFormat;
|
|
|
|
import com.boydti.fawe.util.MainUtil;
|
|
|
|
import com.boydti.fawe.util.chat.Message;
|
2014-06-30 00:47:08 +00:00
|
|
|
import com.sk89q.minecraft.util.commands.CommandContext;
|
|
|
|
import com.sk89q.minecraft.util.commands.CommandException;
|
|
|
|
import com.sk89q.worldedit.LocalConfiguration;
|
|
|
|
import com.sk89q.worldedit.LocalSession;
|
|
|
|
import com.sk89q.worldedit.WorldEdit;
|
|
|
|
import com.sk89q.worldedit.WorldEditException;
|
2019-07-06 00:46:48 +00:00
|
|
|
import com.sk89q.worldedit.command.util.CommandPermissions;
|
|
|
|
import com.sk89q.worldedit.command.util.CommandPermissionsConditionGenerator;
|
2014-06-28 08:01:49 +00:00
|
|
|
import com.sk89q.worldedit.entity.Player;
|
2018-08-12 14:03:07 +00:00
|
|
|
import com.sk89q.worldedit.event.extent.PlayerSaveClipboardEvent;
|
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;
|
2018-08-16 16:55:41 +00:00
|
|
|
import com.sk89q.worldedit.extent.clipboard.io.ClipboardFormats;
|
2019-07-06 00:46:48 +00:00
|
|
|
import com.sk89q.worldedit.extent.clipboard.io.ClipboardReader;
|
2014-07-10 04:08:34 +00:00
|
|
|
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;
|
2019-07-06 00:46:48 +00:00
|
|
|
import com.sk89q.worldedit.util.formatting.component.ErrorFormat;
|
|
|
|
import com.sk89q.worldedit.util.formatting.component.PaginationBox;
|
|
|
|
import com.sk89q.worldedit.util.formatting.component.SchematicPaginationBox;
|
|
|
|
import com.sk89q.worldedit.util.formatting.text.Component;
|
|
|
|
import com.sk89q.worldedit.util.formatting.text.TextComponent;
|
|
|
|
import com.sk89q.worldedit.util.io.Closer;
|
2015-12-28 20:05:16 +00:00
|
|
|
import com.sk89q.worldedit.util.io.file.FilenameException;
|
2019-07-06 00:46:48 +00:00
|
|
|
import java.io.BufferedInputStream;
|
|
|
|
import java.io.BufferedOutputStream;
|
|
|
|
import java.io.File;
|
|
|
|
import java.io.FileInputStream;
|
|
|
|
import java.io.FileOutputStream;
|
|
|
|
import java.io.IOException;
|
|
|
|
import java.io.InputStream;
|
2018-08-12 14:03:07 +00:00
|
|
|
import java.net.URI;
|
|
|
|
import java.net.URISyntaxException;
|
2019-04-03 13:25:16 +00:00
|
|
|
import java.net.URL;
|
2018-08-12 14:03:07 +00:00
|
|
|
import java.nio.channels.Channels;
|
|
|
|
import java.nio.channels.ReadableByteChannel;
|
|
|
|
import java.nio.file.Files;
|
2019-07-06 00:46:48 +00:00
|
|
|
import java.util.ArrayList;
|
|
|
|
import java.util.Arrays;
|
|
|
|
import java.util.Collection;
|
|
|
|
import java.util.Collections;
|
|
|
|
import java.util.List;
|
|
|
|
import java.util.UUID;
|
|
|
|
import java.util.concurrent.Callable;
|
2016-01-13 23:55:49 +00:00
|
|
|
import java.util.regex.Pattern;
|
2019-07-06 00:46:48 +00:00
|
|
|
import org.enginehub.piston.annotation.Command;
|
|
|
|
import org.enginehub.piston.annotation.CommandContainer;
|
|
|
|
import org.enginehub.piston.annotation.param.Arg;
|
|
|
|
import org.enginehub.piston.annotation.param.ArgFlag;
|
|
|
|
import org.enginehub.piston.annotation.param.Switch;
|
|
|
|
import org.enginehub.piston.exception.StopExecutionException;
|
|
|
|
import org.slf4j.Logger;
|
|
|
|
import org.slf4j.LoggerFactory;
|
2012-12-15 18:49:44 +00:00
|
|
|
|
2019-07-06 00:46:48 +00:00
|
|
|
//import com.boydti.fawe.object.schematic.visualizer.SchemVis;
|
2018-08-12 14:03:07 +00:00
|
|
|
|
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
|
|
|
*/
|
2019-07-06 00:46:48 +00:00
|
|
|
@CommandContainer(superTypes = CommandPermissionsConditionGenerator.Registration.class)
|
|
|
|
public class SchematicCommands {
|
2012-03-28 18:05:52 +00:00
|
|
|
|
2019-03-14 02:51:48 +00:00
|
|
|
private static final Logger log = LoggerFactory.getLogger(SchematicCommands.class);
|
2019-07-06 00:46:48 +00:00
|
|
|
private final WorldEdit worldEdit;
|
2014-06-28 08:01:49 +00:00
|
|
|
|
|
|
|
/**
|
|
|
|
* Create a new instance.
|
|
|
|
*
|
|
|
|
* @param worldEdit reference to WorldEdit
|
|
|
|
*/
|
2019-07-06 00:46:48 +00:00
|
|
|
public SchematicCommands(WorldEdit worldEdit) {
|
|
|
|
checkNotNull(worldEdit);
|
|
|
|
this.worldEdit = worldEdit;
|
2012-03-28 18:05:52 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
@Command(
|
2019-07-06 00:46:48 +00:00
|
|
|
name = "loadall",
|
|
|
|
desc = "Load multiple clipboards (paste will randomly choose one)",
|
|
|
|
descFooter = "Load multiple clipboards (paste will randomly choose one)\n" +
|
|
|
|
"The -r flag will apply random rotation"
|
2012-03-28 18:05:52 +00:00
|
|
|
)
|
2018-08-12 14:03:07 +00:00
|
|
|
@Deprecated
|
2018-10-17 12:40:52 +00:00
|
|
|
@CommandPermissions({"worldedit.clipboard.load", "worldedit.schematic.load", "worldedit.schematic.load.web", "worldedit.schematic.load.asset"})
|
2019-07-06 00:46:48 +00:00
|
|
|
public void loadall(final Player player, final LocalSession session,
|
|
|
|
@Arg(desc = "Format name.", def = "schematic")
|
|
|
|
final String formatName,
|
|
|
|
@Arg(desc = "File name.")
|
|
|
|
final String filename,
|
|
|
|
@Switch(name = 'r', desc = "Apply random rotation")
|
|
|
|
boolean randomRotate) throws FilenameException {
|
2019-01-31 15:08:58 +00:00
|
|
|
final ClipboardFormat format = ClipboardFormats.findByAlias(formatName);
|
2014-07-10 04:08:34 +00:00
|
|
|
if (format == null) {
|
2018-08-12 14:03:07 +00:00
|
|
|
BBC.CLIPBOARD_INVALID_FORMAT.send(player, formatName);
|
2014-07-10 04:08:34 +00:00
|
|
|
return;
|
|
|
|
}
|
2018-08-12 14:03:07 +00:00
|
|
|
try {
|
2019-01-31 15:08:58 +00:00
|
|
|
MultiClipboardHolder all = ClipboardFormats.loadAllFromInput(player, filename, null, true);
|
2018-08-12 14:03:07 +00:00
|
|
|
if (all != null) {
|
|
|
|
session.addClipboard(all);
|
|
|
|
BBC.SCHEMATIC_LOADED.send(player, filename);
|
|
|
|
}
|
|
|
|
} catch (IOException e) {
|
|
|
|
throw new RuntimeException(e);
|
|
|
|
}
|
|
|
|
}
|
2014-07-10 04:08:34 +00:00
|
|
|
|
2018-08-12 14:03:07 +00:00
|
|
|
@Command(
|
2019-07-06 00:46:48 +00:00
|
|
|
name = "clear",
|
|
|
|
desc = "Clear your clipboard"
|
2018-08-12 14:03:07 +00:00
|
|
|
)
|
|
|
|
@CommandPermissions({"worldedit.clipboard.clear", "worldedit.schematic.clear"})
|
|
|
|
public void clear(Player player, LocalSession session) throws WorldEditException {
|
|
|
|
session.setClipboard(null);
|
|
|
|
BBC.CLIPBOARD_CLEARED.send(player);
|
|
|
|
}
|
2014-07-10 04:08:34 +00:00
|
|
|
|
2018-08-12 14:03:07 +00:00
|
|
|
@Command(
|
2019-07-06 00:46:48 +00:00
|
|
|
name = "unload",
|
|
|
|
desc = "Remove a clipboard from your multi-clipboard"
|
2018-08-12 14:03:07 +00:00
|
|
|
)
|
|
|
|
@CommandPermissions({"worldedit.clipboard.clear", "worldedit.schematic.clear"})
|
|
|
|
public void unload(Player player, LocalSession session, String fileName) throws WorldEditException {
|
|
|
|
URI uri;
|
|
|
|
if (fileName.startsWith("file:/") || fileName.startsWith("http://") || fileName.startsWith("https://")) {
|
|
|
|
uri = URI.create(fileName);
|
|
|
|
} else {
|
|
|
|
final LocalConfiguration config = this.worldEdit.getConfiguration();
|
|
|
|
File working = this.worldEdit.getWorkingDirectoryFile(config.saveDir);
|
|
|
|
File root = Settings.IMP.PATHS.PER_PLAYER_SCHEMATICS ? new File(working, player.getUniqueId().toString()) : working;
|
|
|
|
uri = new File(root, fileName).toURI();
|
|
|
|
}
|
2014-07-10 04:08:34 +00:00
|
|
|
|
2018-08-12 14:03:07 +00:00
|
|
|
ClipboardHolder clipboard = session.getClipboard();
|
|
|
|
if (clipboard instanceof URIClipboardHolder) {
|
|
|
|
URIClipboardHolder identifiable = (URIClipboardHolder) clipboard;
|
|
|
|
if (identifiable.contains(uri)) {
|
|
|
|
if (identifiable instanceof MultiClipboardHolder) {
|
|
|
|
MultiClipboardHolder multi = (MultiClipboardHolder) identifiable;
|
|
|
|
multi.remove(uri);
|
|
|
|
if (multi.getHolders().isEmpty()) session.setClipboard(null);
|
|
|
|
} else {
|
|
|
|
session.setClipboard(null);
|
|
|
|
}
|
|
|
|
BBC.CLIPBOARD_CLEARED.send(player);
|
|
|
|
return;
|
|
|
|
}
|
2014-07-10 04:08:34 +00:00
|
|
|
}
|
2018-08-12 14:03:07 +00:00
|
|
|
BBC.CLIPBOARD_URI_NOT_FOUND.send(player, fileName);
|
2012-03-28 18:05:52 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
@Command(
|
2019-07-06 00:46:48 +00:00
|
|
|
name = "remap",
|
2018-08-23 21:18:14 +00:00
|
|
|
desc = "Remap a clipboard between MCPE/PC values"
|
2012-03-28 18:05:52 +00:00
|
|
|
)
|
2018-08-12 14:03:07 +00:00
|
|
|
@Deprecated
|
|
|
|
@CommandPermissions({"worldedit.schematic.remap"})
|
|
|
|
public void remap(final Player player, final LocalSession session) throws WorldEditException {
|
2019-07-06 00:46:48 +00:00
|
|
|
ClipboardRemapper remapper = new ClipboardRemapper(ClipboardRemapper.RemapPlatform.PE, ClipboardRemapper.RemapPlatform.PC);
|
2014-07-10 04:08:34 +00:00
|
|
|
|
2018-08-12 14:03:07 +00:00
|
|
|
for (Clipboard clip : session.getClipboard().getClipboards()) {
|
|
|
|
remapper.apply(clip);
|
|
|
|
}
|
2019-06-12 21:12:12 +00:00
|
|
|
player.print("Remapped schematic");
|
2018-08-12 14:03:07 +00:00
|
|
|
}
|
2014-07-10 04:08:34 +00:00
|
|
|
|
2019-06-08 00:58:48 +00:00
|
|
|
@Command(
|
2019-07-06 00:46:48 +00:00
|
|
|
name = "load",
|
|
|
|
desc = "Load a schematic into your clipboard"
|
2019-06-08 00:58:48 +00:00
|
|
|
)
|
2018-10-17 12:40:52 +00:00
|
|
|
@CommandPermissions({"worldedit.clipboard.load", "worldedit.schematic.load", "worldedit.schematic.load.asset", "worldedit.schematic.load.web", "worldedit.schematic.load.other"})
|
2019-07-06 00:46:48 +00:00
|
|
|
public void load(Player player, LocalSession session,
|
|
|
|
@Arg(desc = "File name.")
|
|
|
|
String filename,
|
|
|
|
@Arg(desc = "Format name.", def = "sponge")
|
|
|
|
String formatName) throws FilenameException {
|
2019-06-08 00:58:48 +00:00
|
|
|
LocalConfiguration config = worldEdit.getConfiguration();
|
2019-01-31 15:08:58 +00:00
|
|
|
ClipboardFormat format = formatName == null ? null : ClipboardFormats.findByAlias(formatName);
|
2018-08-12 14:03:07 +00:00
|
|
|
InputStream in = null;
|
|
|
|
try {
|
|
|
|
URI uri;
|
|
|
|
if (filename.startsWith("url:")) {
|
2018-10-17 12:40:52 +00:00
|
|
|
if (!player.hasPermission("worldedit.schematic.load.web")) {
|
|
|
|
BBC.NO_PERM.send(player, "worldedit.schematic.load.web");
|
2018-08-12 14:03:07 +00:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
UUID uuid = UUID.fromString(filename.substring(4));
|
|
|
|
URL base = new URL(Settings.IMP.WEB.URL);
|
2019-04-15 11:22:58 +00:00
|
|
|
URL url = new URL(base, "uploads/" + uuid + "." + format.getPrimaryFileExtension());
|
2018-08-12 14:03:07 +00:00
|
|
|
ReadableByteChannel rbc = Channels.newChannel(url.openStream());
|
|
|
|
in = Channels.newInputStream(rbc);
|
|
|
|
uri = url.toURI();
|
|
|
|
} else {
|
|
|
|
if (!player.hasPermission("worldedit.schematic.load") && !player.hasPermission("worldedit.clipboard.load")) {
|
|
|
|
BBC.NO_PERM.send(player, "worldedit.clipboard.load");
|
|
|
|
return;
|
|
|
|
}
|
2019-06-08 00:58:48 +00:00
|
|
|
File working = worldEdit.getWorkingDirectoryFile(config.saveDir);
|
2018-08-12 14:03:07 +00:00
|
|
|
File dir = Settings.IMP.PATHS.PER_PLAYER_SCHEMATICS ? new File(working, player.getUniqueId().toString()) : working;
|
|
|
|
File f;
|
|
|
|
if (filename.startsWith("#")) {
|
2018-08-16 16:55:41 +00:00
|
|
|
String[] extensions;
|
|
|
|
if (format != null) {
|
|
|
|
extensions = format.getFileExtensions().toArray(new String[0]);
|
|
|
|
} else {
|
|
|
|
extensions = ClipboardFormats.getFileExtensionArray();
|
|
|
|
}
|
|
|
|
f = player.openFileOpenDialog(extensions);
|
|
|
|
if (f == null || !f.exists()) {
|
2019-06-12 21:12:12 +00:00
|
|
|
player.printError("Schematic " + filename + " does not exist! (" + f + ")");
|
2018-08-12 14:03:07 +00:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
if (Settings.IMP.PATHS.PER_PLAYER_SCHEMATICS && Pattern.compile("[0-9a-f]{8}-[0-9a-f]{4}-[0-9a-f]{4}-[0-9a-f]{4}-[0-9a-f]{12}").matcher(filename).find() && !player.hasPermission("worldedit.schematic.load.other")) {
|
|
|
|
BBC.NO_PERM.send(player, "worldedit.schematic.load.other");
|
|
|
|
return;
|
|
|
|
}
|
2018-08-16 16:55:41 +00:00
|
|
|
if (format == null && filename.matches(".*\\.[\\w].*")) {
|
2019-07-06 00:46:48 +00:00
|
|
|
String extension = filename.substring(filename.lastIndexOf('.') + 1);
|
2019-01-31 15:08:58 +00:00
|
|
|
format = ClipboardFormats.findByExtension(extension);
|
2018-08-12 14:03:07 +00:00
|
|
|
}
|
2018-10-17 12:40:52 +00:00
|
|
|
f = MainUtil.resolve(dir, filename, format, false);
|
2018-08-12 14:03:07 +00:00
|
|
|
}
|
2018-08-16 16:55:41 +00:00
|
|
|
if (f == null || !f.exists()) {
|
2018-08-12 14:03:07 +00:00
|
|
|
if (!filename.contains("../")) {
|
|
|
|
dir = this.worldEdit.getWorkingDirectoryFile(config.saveDir);
|
2018-10-17 12:40:52 +00:00
|
|
|
f = MainUtil.resolve(dir, filename, format, false);
|
2018-08-12 14:03:07 +00:00
|
|
|
}
|
|
|
|
}
|
2018-08-16 16:55:41 +00:00
|
|
|
if (f == null || !f.exists() || !MainUtil.isInSubDirectory(working, f)) {
|
2019-07-06 00:46:48 +00:00
|
|
|
player.printError("Schematic " + filename + " does not exist! (" + (f != null && f.exists()) + "|" + f + "|" + (f != null && !MainUtil.isInSubDirectory(working, f)) + ")");
|
2018-08-12 14:03:07 +00:00
|
|
|
return;
|
|
|
|
}
|
2018-08-16 16:55:41 +00:00
|
|
|
if (format == null) {
|
2019-01-31 15:08:58 +00:00
|
|
|
format = ClipboardFormats.findByFile(f);
|
2018-08-16 16:55:41 +00:00
|
|
|
if (format == null) {
|
|
|
|
BBC.CLIPBOARD_INVALID_FORMAT.send(player, f.getName());
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
}
|
2018-08-12 14:03:07 +00:00
|
|
|
in = new FileInputStream(f);
|
|
|
|
uri = f.toURI();
|
|
|
|
}
|
|
|
|
format.hold(player, uri, in);
|
|
|
|
BBC.SCHEMATIC_LOADED.send(player, filename);
|
|
|
|
} catch (IllegalArgumentException e) {
|
2019-06-12 21:12:12 +00:00
|
|
|
player.printError("Unknown filename: " + filename);
|
2018-08-12 14:03:07 +00:00
|
|
|
} catch (URISyntaxException | IOException e) {
|
2019-06-12 21:12:12 +00:00
|
|
|
player.printError("File could not be read or it does not exist: " + e.getMessage());
|
2019-04-03 13:25:16 +00:00
|
|
|
log.warn("Failed to load a saved clipboard", e);
|
2018-08-12 14:03:07 +00:00
|
|
|
} finally {
|
|
|
|
if (in != null) {
|
|
|
|
try {
|
|
|
|
in.close();
|
|
|
|
} catch (IOException ignored) {
|
|
|
|
}
|
|
|
|
}
|
2014-07-10 05:04:17 +00:00
|
|
|
}
|
2018-08-12 14:03:07 +00:00
|
|
|
}
|
2014-07-10 04:08:34 +00:00
|
|
|
|
2019-06-08 00:58:48 +00:00
|
|
|
@Command(
|
2019-07-06 00:46:48 +00:00
|
|
|
name = "save",
|
|
|
|
desc = "Save a schematic into your clipboard"
|
2019-06-08 00:58:48 +00:00
|
|
|
)
|
2018-08-12 14:03:07 +00:00
|
|
|
@CommandPermissions({"worldedit.clipboard.save", "worldedit.schematic.save", "worldedit.schematic.save.other"})
|
2019-07-06 00:46:48 +00:00
|
|
|
public void save(Player player, LocalSession session,
|
|
|
|
@Arg(desc = "File name.")
|
|
|
|
String filename,
|
|
|
|
@Arg(desc = "Format name.", def = "sponge")
|
|
|
|
String formatName,
|
|
|
|
@Switch(name = 'f', desc = "Overwrite an existing file.")
|
|
|
|
boolean allowOverwrite,
|
|
|
|
@Switch(name = 'g', desc = "//TODO")
|
|
|
|
boolean global
|
|
|
|
) throws WorldEditException {
|
2019-06-08 00:58:48 +00:00
|
|
|
LocalConfiguration config = worldEdit.getConfiguration();
|
2019-07-06 00:46:48 +00:00
|
|
|
|
|
|
|
File dir = worldEdit.getWorkingDirectoryFile(config.saveDir);
|
|
|
|
|
|
|
|
if (!global && Settings.IMP.PATHS.PER_PLAYER_SCHEMATICS) {
|
|
|
|
dir = new File(dir, player.getUniqueId().toString());
|
|
|
|
}
|
|
|
|
|
2019-06-08 00:58:48 +00:00
|
|
|
ClipboardFormat format = ClipboardFormats.findByAlias(formatName);
|
2018-08-12 14:03:07 +00:00
|
|
|
if (format == null) {
|
2019-06-12 21:12:12 +00:00
|
|
|
player.printError("Unknown schematic format: " + formatName);
|
2018-08-12 14:03:07 +00:00
|
|
|
return;
|
|
|
|
}
|
2019-07-06 00:46:48 +00:00
|
|
|
|
2018-08-12 14:03:07 +00:00
|
|
|
if (filename.contains("../")) {
|
|
|
|
if (!player.hasPermission("worldedit.schematic.save.other")) {
|
|
|
|
BBC.NO_PERM.send(player, "worldedit.schematic.save.other");
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
if (filename.startsWith("../")) {
|
2019-07-06 00:46:48 +00:00
|
|
|
dir = worldEdit.getWorkingDirectoryFile(config.saveDir);
|
2018-08-12 14:03:07 +00:00
|
|
|
filename = filename.substring(3);
|
|
|
|
}
|
|
|
|
}
|
2019-07-06 00:46:48 +00:00
|
|
|
|
|
|
|
File f = worldEdit.getSafeSaveFile(player, dir, filename, format.getPrimaryFileExtension());
|
|
|
|
|
|
|
|
boolean overwrite = f.exists();
|
|
|
|
if (overwrite) {
|
|
|
|
if (!player.hasPermission("worldedit.schematic.delete")) {
|
|
|
|
throw new StopExecutionException(TextComponent.of("That schematic already exists!"));
|
|
|
|
}
|
|
|
|
if (!allowOverwrite) {
|
|
|
|
player.printError("That schematic already exists. Use the -f flag to overwrite it.");
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-01-31 15:08:58 +00:00
|
|
|
if (f.getName().replaceAll("." + format.getPrimaryFileExtension(), "").isEmpty()) {
|
2018-08-12 14:03:07 +00:00
|
|
|
File directory = f.getParentFile();
|
|
|
|
if (directory.exists()) {
|
|
|
|
int max = MainUtil.getMaxFileId(directory);
|
2019-01-31 15:08:58 +00:00
|
|
|
f = new File(directory, max + "." + format.getPrimaryFileExtension());
|
2018-08-12 14:03:07 +00:00
|
|
|
} else {
|
2019-01-31 15:08:58 +00:00
|
|
|
f = new File(directory, "1." + format.getPrimaryFileExtension());
|
2018-08-12 14:03:07 +00:00
|
|
|
}
|
|
|
|
}
|
2019-07-06 00:46:48 +00:00
|
|
|
|
|
|
|
// Create parent directories
|
|
|
|
File parent = f.getParentFile();
|
|
|
|
if (parent != null && !parent.exists()) {
|
2018-08-12 14:03:07 +00:00
|
|
|
if (!parent.mkdirs()) {
|
2019-07-06 00:46:48 +00:00
|
|
|
throw new StopExecutionException(TextComponent.of(
|
|
|
|
"Could not create folder for schematics!"));
|
2014-07-10 04:08:34 +00:00
|
|
|
}
|
2018-08-12 14:03:07 +00:00
|
|
|
}
|
|
|
|
try {
|
|
|
|
if (!f.exists()) {
|
|
|
|
f.createNewFile();
|
2019-04-03 05:53:34 +00:00
|
|
|
} else if (!allowOverwrite) {
|
|
|
|
BBC.SCHEMATIC_MOVE_EXISTS.send(player, f.getName());
|
2018-08-12 14:03:07 +00:00
|
|
|
}
|
2019-07-06 00:46:48 +00:00
|
|
|
|
|
|
|
ClipboardHolder holder = session.getClipboard();
|
|
|
|
|
2018-08-12 14:03:07 +00:00
|
|
|
try (FileOutputStream fos = new FileOutputStream(f)) {
|
2019-06-08 00:58:48 +00:00
|
|
|
Clipboard clipboard = holder.getClipboard();
|
|
|
|
Transform transform = holder.getTransform();
|
|
|
|
Clipboard target;
|
2018-08-12 14:03:07 +00:00
|
|
|
|
|
|
|
// If we have a transform, bake it into the copy
|
|
|
|
if (!transform.isIdentity()) {
|
2019-06-08 00:58:48 +00:00
|
|
|
FlattenedClipboardTransform result = FlattenedClipboardTransform.transform(clipboard, transform);
|
2018-08-12 14:03:07 +00:00
|
|
|
target = new BlockArrayClipboard(result.getTransformedRegion(), UUID.randomUUID());
|
|
|
|
target.setOrigin(clipboard.getOrigin());
|
|
|
|
Operations.completeLegacy(result.copyTo(target));
|
|
|
|
} else {
|
|
|
|
target = clipboard;
|
|
|
|
}
|
2014-07-10 04:08:34 +00:00
|
|
|
|
2018-08-12 14:03:07 +00:00
|
|
|
URI uri = null;
|
2019-07-06 00:46:48 +00:00
|
|
|
if (holder instanceof URIClipboardHolder) {
|
|
|
|
uri = ((URIClipboardHolder) holder).getURI(clipboard);
|
|
|
|
}
|
2018-08-12 14:03:07 +00:00
|
|
|
if (new PlayerSaveClipboardEvent(player, clipboard, uri, f.toURI()).call()) {
|
|
|
|
try (ClipboardWriter writer = format.getWriter(fos)) {
|
|
|
|
if (writer instanceof StructureFormat) {
|
|
|
|
((StructureFormat) writer).write(target, player.getName());
|
|
|
|
} else {
|
|
|
|
writer.write(target);
|
|
|
|
}
|
|
|
|
log.info(player.getName() + " saved " + f.getCanonicalPath());
|
|
|
|
BBC.SCHEMATIC_SAVED.send(player, filename);
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
BBC.WORLDEDIT_CANCEL_REASON_MANUAL.send(player);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
} catch (IllegalArgumentException e) {
|
|
|
|
e.printStackTrace();
|
2019-06-12 21:12:12 +00:00
|
|
|
player.printError("Unknown filename: " + filename);
|
2014-07-10 04:08:34 +00:00
|
|
|
} catch (IOException e) {
|
2018-08-12 14:03:07 +00:00
|
|
|
e.printStackTrace();
|
2019-06-12 21:12:12 +00:00
|
|
|
player.printError("Schematic could not written: " + e.getMessage());
|
|
|
|
log.warn("Failed to write a saved clipboard", e);
|
2014-07-10 04:08:34 +00:00
|
|
|
}
|
2012-03-28 18:05:52 +00:00
|
|
|
}
|
|
|
|
|
2019-06-08 00:58:48 +00:00
|
|
|
@Command(
|
2019-07-06 00:46:48 +00:00
|
|
|
name = "move",
|
|
|
|
aliases = {"m"},
|
|
|
|
desc = "Move your loaded schematic"
|
2019-06-08 00:58:48 +00:00
|
|
|
)
|
2018-08-12 14:03:07 +00:00
|
|
|
@CommandPermissions({"worldedit.schematic.move", "worldedit.schematic.move.other"})
|
2019-06-08 00:58:48 +00:00
|
|
|
public void move(Player player, LocalSession session, String directory) throws WorldEditException, IOException {
|
|
|
|
LocalConfiguration config = worldEdit.getConfiguration();
|
|
|
|
File working = worldEdit.getWorkingDirectoryFile(config.saveDir);
|
|
|
|
File dir = Settings.IMP.PATHS.PER_PLAYER_SCHEMATICS ? new File(working, player.getUniqueId().toString()) : working;
|
2018-08-21 17:22:37 +00:00
|
|
|
File destDir = new File(dir, directory);
|
2018-08-12 14:03:07 +00:00
|
|
|
if (!MainUtil.isInSubDirectory(working, destDir)) {
|
2019-06-12 21:12:12 +00:00
|
|
|
player.printError("Directory " + destDir + " does not exist!");
|
2018-08-12 14:03:07 +00:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
if (Settings.IMP.PATHS.PER_PLAYER_SCHEMATICS && !MainUtil.isInSubDirectory(dir, destDir) && !player.hasPermission("worldedit.schematic.move.other")) {
|
|
|
|
BBC.NO_PERM.send(player, "worldedit.schematic.move.other");
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
ClipboardHolder clipboard = session.getClipboard();
|
|
|
|
List<File> sources = getFiles(clipboard);
|
|
|
|
if (sources.isEmpty()) {
|
|
|
|
BBC.SCHEMATIC_NONE.send(player);
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
if (!destDir.exists() && !destDir.mkdirs()) {
|
2019-06-12 21:12:12 +00:00
|
|
|
player.printError("Creation of " + destDir + " failed! (check file permissions)");
|
2018-08-12 14:03:07 +00:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
for (File source : sources) {
|
|
|
|
File destFile = new File(destDir, source.getName());
|
|
|
|
if (destFile.exists()) {
|
|
|
|
BBC.SCHEMATIC_MOVE_EXISTS.send(player, destFile);
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
if (Settings.IMP.PATHS.PER_PLAYER_SCHEMATICS && (!MainUtil.isInSubDirectory(dir, destFile) || !MainUtil.isInSubDirectory(dir, source)) && !player.hasPermission("worldedit.schematic.delete.other")) {
|
|
|
|
BBC.SCHEMATIC_MOVE_FAILED.send(player, destFile, BBC.NO_PERM.f("worldedit.schematic.move.other"));
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
try {
|
|
|
|
File cached = new File(source.getParentFile(), "." + source.getName() + ".cached");
|
|
|
|
Files.move(source.toPath(), destFile.toPath());
|
|
|
|
if (cached.exists()) Files.move(cached.toPath(), destFile.toPath());
|
|
|
|
BBC.SCHEMATIC_MOVE_SUCCESS.send(player, source, destFile);
|
|
|
|
} catch (IOException e) {
|
|
|
|
throw new RuntimeException(e);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2014-05-02 01:00:59 +00:00
|
|
|
|
2019-06-08 00:58:48 +00:00
|
|
|
@Command(
|
2019-07-06 00:46:48 +00:00
|
|
|
name = "delete",
|
|
|
|
aliases = {"d"},
|
|
|
|
desc = "Delete a saved schematic"
|
2019-06-08 00:58:48 +00:00
|
|
|
)
|
2018-08-12 14:03:07 +00:00
|
|
|
@CommandPermissions({"worldedit.schematic.delete", "worldedit.schematic.delete.other"})
|
2019-07-06 00:46:48 +00:00
|
|
|
public void delete(Actor actor, LocalSession session,
|
|
|
|
@Arg(desc = "File name.")
|
|
|
|
String filename) throws WorldEditException, IOException {
|
|
|
|
LocalConfiguration config = worldEdit.getConfiguration();
|
|
|
|
File working = worldEdit.getWorkingDirectoryFile(config.saveDir);
|
|
|
|
File dir = Settings.IMP.PATHS.PER_PLAYER_SCHEMATICS ? new File(working, actor.getUniqueId().toString()) : working;
|
2018-08-12 14:03:07 +00:00
|
|
|
List<File> files = new ArrayList<>();
|
2014-05-02 01:00:59 +00:00
|
|
|
|
2018-08-12 14:03:07 +00:00
|
|
|
if (filename.equalsIgnoreCase("*")) {
|
|
|
|
files.addAll(getFiles(session.getClipboard()));
|
|
|
|
} else {
|
|
|
|
File f = MainUtil.resolveRelative(new File(dir, filename));
|
|
|
|
files.add(f);
|
|
|
|
}
|
2019-07-06 00:46:48 +00:00
|
|
|
|
2018-08-12 14:03:07 +00:00
|
|
|
if (files.isEmpty()) {
|
2019-07-06 00:46:48 +00:00
|
|
|
BBC.SCHEMATIC_NONE.send(actor);
|
2014-05-02 01:00:59 +00:00
|
|
|
return;
|
|
|
|
}
|
2018-08-12 14:03:07 +00:00
|
|
|
for (File f : files) {
|
|
|
|
if (!MainUtil.isInSubDirectory(working, f) || !f.exists()) {
|
2019-07-06 00:46:48 +00:00
|
|
|
actor.printError("Schematic " + filename + " does not exist! (" + f.exists() + "|" + f + "|" + (!MainUtil.isInSubDirectory(working, f)) + ")");
|
2018-08-12 14:03:07 +00:00
|
|
|
continue;
|
|
|
|
}
|
2019-07-06 00:46:48 +00:00
|
|
|
if (Settings.IMP.PATHS.PER_PLAYER_SCHEMATICS && !MainUtil.isInSubDirectory(dir, f) && !actor.hasPermission("worldedit.schematic.delete.other")) {
|
|
|
|
BBC.NO_PERM.send(actor, "worldedit.schematic.delete.other");
|
2018-08-12 14:03:07 +00:00
|
|
|
continue;
|
|
|
|
}
|
2019-07-06 00:46:48 +00:00
|
|
|
if (!f.delete()) {
|
|
|
|
actor.printError("Deletion of " + filename + " failed! Maybe it is read-only.");
|
2018-08-12 14:03:07 +00:00
|
|
|
continue;
|
|
|
|
}
|
2019-07-06 00:46:48 +00:00
|
|
|
BBC.FILE_DELETED.send(actor, filename);
|
2018-08-12 14:03:07 +00:00
|
|
|
}
|
|
|
|
}
|
2014-05-02 01:00:59 +00:00
|
|
|
|
2018-08-12 14:03:07 +00:00
|
|
|
private List<File> getFiles(ClipboardHolder clipboard) {
|
|
|
|
Collection<URI> uris = Collections.emptyList();
|
|
|
|
if (clipboard instanceof URIClipboardHolder) {
|
|
|
|
uris = ((URIClipboardHolder) clipboard).getURIs();
|
2014-05-02 01:00:59 +00:00
|
|
|
}
|
2019-07-06 00:46:48 +00:00
|
|
|
List<File> files = new ArrayList<>();
|
2018-08-12 14:03:07 +00:00
|
|
|
for (URI uri : uris) {
|
|
|
|
File file = new File(uri);
|
|
|
|
if (file.exists()) files.add(file);
|
|
|
|
}
|
|
|
|
return files;
|
|
|
|
}
|
2014-05-02 01:00:59 +00:00
|
|
|
|
2019-06-08 00:58:48 +00:00
|
|
|
@Command(
|
2019-07-06 00:46:48 +00:00
|
|
|
name = "formats",
|
|
|
|
aliases = {"listformats", "f"},
|
|
|
|
desc = "List available formats"
|
2019-06-08 00:58:48 +00:00
|
|
|
)
|
2012-04-01 16:21:27 +00:00
|
|
|
@CommandPermissions("worldedit.schematic.formats")
|
2019-07-06 00:46:48 +00:00
|
|
|
public void formats(Actor actor) {
|
|
|
|
actor.print("Available clipboard formats (Name: Lookup names)");
|
|
|
|
StringBuilder builder;
|
2012-03-28 18:05:52 +00:00
|
|
|
boolean first = true;
|
2019-07-06 00:46:48 +00:00
|
|
|
for (ClipboardFormat format : ClipboardFormats.getAll()) {
|
|
|
|
builder = new StringBuilder();
|
2019-01-31 15:08:58 +00:00
|
|
|
builder.append(format.getName()).append(": ");
|
2019-07-06 00:46:48 +00:00
|
|
|
for (String lookupName : format.getAliases()) {
|
2012-03-28 18:05:52 +00:00
|
|
|
if (!first) {
|
|
|
|
builder.append(", ");
|
|
|
|
}
|
|
|
|
builder.append(lookupName);
|
|
|
|
first = false;
|
|
|
|
}
|
|
|
|
first = true;
|
2019-07-06 00:46:48 +00:00
|
|
|
actor.print(builder.toString());
|
2012-03-28 18:05:52 +00:00
|
|
|
}
|
|
|
|
}
|
2012-03-28 19:30:36 +00:00
|
|
|
|
2018-08-12 14:03:07 +00:00
|
|
|
|
2019-06-08 00:58:48 +00:00
|
|
|
/*
|
2012-03-28 19:30:36 +00:00
|
|
|
@Command(
|
2019-07-17 05:43:18 +00:00
|
|
|
name = "show",
|
2018-08-12 14:03:07 +00:00
|
|
|
desc = "Show a schematic",
|
2019-07-17 05:43:18 +00:00
|
|
|
descFooter = "List all schematics in the schematics directory\n" +
|
2018-08-12 14:03:07 +00:00
|
|
|
" -f <format> restricts by format\n"
|
2012-03-28 19:30:36 +00:00
|
|
|
)
|
2018-08-12 14:03:07 +00:00
|
|
|
@CommandPermissions("worldedit.schematic.show")
|
|
|
|
public void show(Player player, CommandContext args, @Switch('f') String formatName) {
|
|
|
|
FawePlayer fp = FawePlayer.wrap(player);
|
|
|
|
if (args.argsLength() == 0) {
|
|
|
|
if (fp.getSession().getVirtualWorld() != null) fp.setVirtualWorld(null);
|
|
|
|
else {
|
|
|
|
BBC.COMMAND_SYNTAX.send(player, "/" + Commands.getAlias(SchematicCommands.class, "schematic") + " " + getCommand().aliases()[0] + " " + getCommand().usage());
|
|
|
|
}
|
2015-12-28 20:05:16 +00:00
|
|
|
return;
|
|
|
|
}
|
2018-08-12 14:03:07 +00:00
|
|
|
LocalConfiguration config = worldEdit.getConfiguration();
|
|
|
|
File dir = worldEdit.getWorkingDirectoryFile(config.saveDir);
|
|
|
|
try {
|
|
|
|
SchemVis visExtent = new SchemVis(fp);
|
|
|
|
LongAdder count = new LongAdder();
|
|
|
|
UtilityCommands.getFiles(dir, player, args, 0, Character.MAX_VALUE, formatName, Settings.IMP.PATHS.PER_PLAYER_SCHEMATICS, file -> {
|
|
|
|
if (file.isFile()) {
|
|
|
|
try {
|
|
|
|
visExtent.add(file);
|
|
|
|
count.add(1);
|
|
|
|
} catch (IOException e) {
|
|
|
|
throw new RuntimeException(e);
|
|
|
|
}
|
2016-01-13 23:55:49 +00:00
|
|
|
}
|
2018-08-12 14:03:07 +00:00
|
|
|
});
|
|
|
|
long total = count.longValue();
|
|
|
|
if (total == 0) {
|
|
|
|
if (args.getJoinedStrings(0).toLowerCase().startsWith("all")) {
|
|
|
|
BBC.SCHEMATIC_NONE.send(player);
|
|
|
|
} else {
|
|
|
|
String joined = args.getJoinedStrings(0);
|
|
|
|
String cmd = "/" + Commands.getAlias(SchematicCommands.class, "schematic") + " " + getCommand().aliases()[0] + " all " + joined;
|
|
|
|
BBC.HELP_SUGGEST.send(player, joined, cmd);
|
|
|
|
}
|
|
|
|
return;
|
2012-12-25 22:36:57 +00:00
|
|
|
}
|
2018-08-12 14:03:07 +00:00
|
|
|
visExtent.bind();
|
|
|
|
visExtent.update();
|
2012-12-25 22:36:57 +00:00
|
|
|
|
2018-08-12 14:03:07 +00:00
|
|
|
String cmdPrefix = "/" + (config.noDoubleSlash ? "" : "/");
|
|
|
|
String cmdShow = Commands.getAlias(ClipboardCommands.class, "schematic") + " " + Commands.getAlias(ClipboardCommands.class, "show");
|
|
|
|
BBC.SCHEMATIC_SHOW.send(fp, count.longValue(), args.getJoinedStrings(0), cmdShow);
|
2013-12-19 16:32:31 +00:00
|
|
|
|
2018-08-12 14:03:07 +00:00
|
|
|
if (fp.getSession().getExistingClipboard() != null) {
|
|
|
|
String cmd = cmdPrefix + Commands.getAlias(ClipboardCommands.class, "clearclipboard");
|
|
|
|
BBC.SCHEMATIC_PROMPT_CLEAR.send(fp, cmd);
|
2016-01-13 23:55:49 +00:00
|
|
|
}
|
2015-12-28 20:05:16 +00:00
|
|
|
|
2018-08-12 14:03:07 +00:00
|
|
|
} catch (Throwable e) {
|
|
|
|
fp.setVirtualWorld(null);
|
|
|
|
throw e;
|
|
|
|
}
|
2015-12-28 20:05:16 +00:00
|
|
|
}
|
2019-06-08 00:58:48 +00:00
|
|
|
*/
|
2015-12-28 20:05:16 +00:00
|
|
|
|
2018-08-12 14:03:07 +00:00
|
|
|
@Command(
|
2019-07-06 00:46:48 +00:00
|
|
|
name = "list",
|
|
|
|
aliases = {"all", "ls"},
|
|
|
|
desc = "List saved schematics",
|
|
|
|
descFooter = "Note: Format is not fully verified until loading."
|
2018-08-12 14:03:07 +00:00
|
|
|
)
|
|
|
|
@CommandPermissions("worldedit.schematic.list")
|
2019-07-06 00:46:48 +00:00
|
|
|
public void list(FawePlayer fp, Actor actor, CommandContext args,
|
|
|
|
@ArgFlag(name = 'p', desc = "Page to view.", def = "1")
|
|
|
|
int page,
|
|
|
|
@Switch(name = 'f', desc = "Restricts by format.")
|
|
|
|
String formatName) throws WorldEditException {
|
2018-08-12 14:03:07 +00:00
|
|
|
LocalConfiguration config = worldEdit.getConfiguration();
|
|
|
|
File dir = worldEdit.getWorkingDirectoryFile(config.saveDir);
|
|
|
|
|
2019-07-06 00:46:48 +00:00
|
|
|
String schemCmd = "/" + Commands.getAlias(SchematicCommands.class, "schematic");
|
2018-08-12 14:03:07 +00:00
|
|
|
String loadSingle = schemCmd + " " + Commands.getAlias(SchematicCommands.class, "load");
|
|
|
|
String loadMulti = schemCmd + " " + Commands.getAlias(SchematicCommands.class, "loadall");
|
|
|
|
String unload = schemCmd + " " + Commands.getAlias(SchematicCommands.class, "unload");
|
|
|
|
String delete = schemCmd + " " + Commands.getAlias(SchematicCommands.class, "delete");
|
|
|
|
String list = schemCmd + " " + Commands.getAlias(SchematicCommands.class, "list");
|
|
|
|
String showCmd = schemCmd + " " + Commands.getAlias(SchematicCommands.class, "show");
|
|
|
|
|
|
|
|
URIClipboardHolder multi = as(URIClipboardHolder.class, fp.getSession().getExistingClipboard());
|
|
|
|
|
|
|
|
final boolean hasShow = actor.hasPermission("worldedit.schematic.show");
|
|
|
|
UtilityCommands.list(dir, actor, args, page, -1, formatName, Settings.IMP.PATHS.PER_PLAYER_SCHEMATICS, new RunnableVal3<Message, URI, String>() {
|
|
|
|
@Override
|
|
|
|
public void run(Message msg, URI uri, String relFilePath) {
|
|
|
|
boolean isDir = false;
|
|
|
|
boolean loaded = multi != null && multi.contains(uri);
|
|
|
|
|
|
|
|
String name = relFilePath;
|
|
|
|
String uriStr = uri.toString();
|
|
|
|
if (uriStr.startsWith("file:/")) {
|
|
|
|
File file = new File(uri.getPath());
|
|
|
|
name = file.getName();
|
2018-10-17 12:57:36 +00:00
|
|
|
try {
|
|
|
|
if (!MainUtil.isInSubDirectory(dir, file)) {
|
|
|
|
throw new RuntimeException(new CommandException("Invalid path"));
|
|
|
|
}
|
|
|
|
} catch (IOException ignore) {}
|
2018-08-12 14:03:07 +00:00
|
|
|
if (file.isDirectory()) {
|
|
|
|
isDir = true;
|
2019-07-06 00:46:48 +00:00
|
|
|
} else if (name.indexOf('.') != -1) {
|
|
|
|
name = name.substring(0, name.lastIndexOf('.'));
|
2018-08-12 14:03:07 +00:00
|
|
|
}
|
2019-07-06 00:46:48 +00:00
|
|
|
} // url
|
2018-08-12 14:03:07 +00:00
|
|
|
|
2019-07-06 00:46:48 +00:00
|
|
|
msg.text(" - ");
|
2018-08-12 14:03:07 +00:00
|
|
|
|
|
|
|
if (msg.supportsInteraction()) {
|
|
|
|
if (loaded) {
|
2019-07-06 00:46:48 +00:00
|
|
|
msg.text("[-]").command(unload + " " + relFilePath).tooltip("Unload");
|
2018-08-12 14:03:07 +00:00
|
|
|
} else {
|
2019-07-06 00:46:48 +00:00
|
|
|
msg.text("[+]").command(loadMulti + " " + relFilePath).tooltip("Add to clipboard");
|
2018-08-12 14:03:07 +00:00
|
|
|
}
|
2019-07-06 00:46:48 +00:00
|
|
|
if (!isDir) msg.text("[X]").suggest("/" + delete + " " + relFilePath).tooltip("Delete");
|
|
|
|
else if (hasShow) msg.text("[O]").command(showCmd + " " + args.getJoinedStrings(0) + " " + relFilePath).tooltip("Show");
|
|
|
|
msg.text(name);
|
2018-08-12 14:03:07 +00:00
|
|
|
if (isDir) {
|
|
|
|
msg.command(list + " " + relFilePath).tooltip("List");
|
|
|
|
} else {
|
|
|
|
msg.command(loadSingle + " " + relFilePath).tooltip("Load");
|
|
|
|
}
|
|
|
|
} else {
|
2019-07-06 00:46:48 +00:00
|
|
|
msg.text(name);
|
2018-08-12 14:03:07 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
});
|
2016-01-13 23:55:49 +00:00
|
|
|
}
|
2019-07-06 00:46:48 +00:00
|
|
|
|
|
|
|
private static class SchematicLoadTask implements Callable<ClipboardHolder> {
|
|
|
|
private final Player player;
|
|
|
|
private final File file;
|
|
|
|
private final ClipboardFormat format;
|
|
|
|
|
|
|
|
SchematicLoadTask(Player player, File file, ClipboardFormat format) {
|
|
|
|
this.player = player;
|
|
|
|
this.file = file;
|
|
|
|
this.format = format;
|
|
|
|
}
|
|
|
|
|
|
|
|
@Override
|
|
|
|
public ClipboardHolder call() throws Exception {
|
|
|
|
try (Closer closer = Closer.create()) {
|
|
|
|
FileInputStream fis = closer.register(new FileInputStream(file));
|
|
|
|
BufferedInputStream bis = closer.register(new BufferedInputStream(fis));
|
|
|
|
ClipboardReader reader = closer.register(format.getReader(bis));
|
|
|
|
|
|
|
|
Clipboard clipboard = reader.read();
|
|
|
|
log.info(player.getName() + " loaded " + file.getCanonicalPath());
|
|
|
|
return new ClipboardHolder(clipboard);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
private static class SchematicSaveTask implements Callable<Void> {
|
|
|
|
private final Player player;
|
|
|
|
private final File file;
|
|
|
|
private final ClipboardFormat format;
|
|
|
|
private final ClipboardHolder holder;
|
|
|
|
private final boolean overwrite;
|
|
|
|
|
|
|
|
SchematicSaveTask(Player player, File file, ClipboardFormat format, ClipboardHolder holder, boolean overwrite) {
|
|
|
|
this.player = player;
|
|
|
|
this.file = file;
|
|
|
|
this.format = format;
|
|
|
|
this.holder = holder;
|
|
|
|
this.overwrite = overwrite;
|
|
|
|
}
|
|
|
|
|
|
|
|
@Override
|
|
|
|
public Void call() throws Exception {
|
|
|
|
Clipboard clipboard = holder.getClipboard();
|
|
|
|
Transform transform = holder.getTransform();
|
|
|
|
Clipboard target;
|
|
|
|
|
|
|
|
// If we have a transform, bake it into the copy
|
|
|
|
if (transform.isIdentity()) {
|
|
|
|
target = clipboard;
|
|
|
|
} else {
|
|
|
|
FlattenedClipboardTransform result = FlattenedClipboardTransform.transform(clipboard, transform);
|
|
|
|
target = new BlockArrayClipboard(result.getTransformedRegion());
|
|
|
|
target.setOrigin(clipboard.getOrigin());
|
|
|
|
Operations.completeLegacy(result.copyTo(target));
|
|
|
|
}
|
|
|
|
|
|
|
|
try (Closer closer = Closer.create()) {
|
|
|
|
FileOutputStream fos = closer.register(new FileOutputStream(file));
|
|
|
|
BufferedOutputStream bos = closer.register(new BufferedOutputStream(fos));
|
|
|
|
ClipboardWriter writer = closer.register(format.getWriter(bos));
|
|
|
|
writer.write(target);
|
|
|
|
|
|
|
|
log.info(player.getName() + " saved " + file.getCanonicalPath() + (overwrite ? " (overwriting previous file)" : ""));
|
|
|
|
}
|
|
|
|
return null;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
private static class SchematicListTask implements Callable<Component> {
|
|
|
|
private final String prefix;
|
|
|
|
private final int sortType;
|
|
|
|
private final int page;
|
|
|
|
private final File rootDir;
|
|
|
|
private final String pageCommand;
|
|
|
|
|
|
|
|
SchematicListTask(String prefix, int sortType, int page, String pageCommand) {
|
|
|
|
this.prefix = prefix;
|
|
|
|
this.sortType = sortType;
|
|
|
|
this.page = page;
|
|
|
|
this.rootDir = WorldEdit.getInstance().getWorkingDirectoryFile(prefix);
|
|
|
|
this.pageCommand = pageCommand;
|
|
|
|
}
|
|
|
|
|
|
|
|
@Override
|
|
|
|
public Component call() throws Exception {
|
|
|
|
List<File> fileList = allFiles(rootDir);
|
|
|
|
|
|
|
|
if (fileList == null || fileList.isEmpty()) {
|
|
|
|
return ErrorFormat.wrap("No schematics found.");
|
|
|
|
}
|
|
|
|
|
|
|
|
File[] files = new File[fileList.size()];
|
|
|
|
fileList.toArray(files);
|
|
|
|
// cleanup file list
|
|
|
|
Arrays.sort(files, (f1, f2) -> {
|
|
|
|
// http://stackoverflow.com/questions/203030/best-way-to-list-files-in-java-sorted-by-date-modified
|
|
|
|
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.compare(f1.lastModified(), 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;
|
|
|
|
});
|
|
|
|
|
|
|
|
PaginationBox paginationBox = new SchematicPaginationBox(prefix, files, pageCommand);
|
|
|
|
return paginationBox.create(page);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
private static List<File> allFiles(File root) {
|
|
|
|
File[] files = root.listFiles();
|
|
|
|
if (files == null) return null;
|
|
|
|
List<File> fileList = new ArrayList<>();
|
|
|
|
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;
|
|
|
|
}
|
2012-03-28 18:05:52 +00:00
|
|
|
}
|