mirror of
https://github.com/plexusorg/Plex-FAWE.git
synced 2025-07-16 07:14:04 +00:00
Major command changes that don't work yet.
This commit is contained in:
@@ -19,6 +19,9 @@
|
||||
|
||||
package com.sk89q.worldedit.command;
|
||||
|
||||
import static com.boydti.fawe.util.ReflectionUtils.as;
|
||||
import static com.google.common.base.Preconditions.checkNotNull;
|
||||
|
||||
import com.boydti.fawe.config.BBC;
|
||||
import com.boydti.fawe.config.Commands;
|
||||
import com.boydti.fawe.config.Settings;
|
||||
@@ -28,79 +31,101 @@ 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.object.schematic.visualizer.SchemVis;
|
||||
import com.boydti.fawe.util.MainUtil;
|
||||
import com.boydti.fawe.util.chat.Message;
|
||||
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;
|
||||
import com.sk89q.worldedit.LocalConfiguration;
|
||||
import com.sk89q.worldedit.LocalSession;
|
||||
import com.sk89q.worldedit.WorldEdit;
|
||||
import com.sk89q.worldedit.WorldEditException;
|
||||
import com.sk89q.worldedit.command.util.CommandPermissions;
|
||||
import com.sk89q.worldedit.command.util.CommandPermissionsConditionGenerator;
|
||||
import com.sk89q.worldedit.entity.Player;
|
||||
import com.sk89q.worldedit.event.extent.PlayerSaveClipboardEvent;
|
||||
import com.sk89q.worldedit.extension.platform.Actor;
|
||||
import com.sk89q.worldedit.extent.clipboard.BlockArrayClipboard;
|
||||
import com.sk89q.worldedit.extent.clipboard.Clipboard;
|
||||
import com.sk89q.worldedit.extent.clipboard.io.BuiltInClipboardFormat;
|
||||
import com.sk89q.worldedit.extent.clipboard.io.ClipboardFormat;
|
||||
import com.sk89q.worldedit.extent.clipboard.io.ClipboardFormats;
|
||||
import com.sk89q.worldedit.extent.clipboard.io.ClipboardReader;
|
||||
import com.sk89q.worldedit.extent.clipboard.io.ClipboardWriter;
|
||||
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.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;
|
||||
import com.sk89q.worldedit.util.io.file.FilenameException;
|
||||
import org.slf4j.Logger;
|
||||
import org.slf4j.LoggerFactory;
|
||||
|
||||
import java.io.*;
|
||||
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;
|
||||
import java.net.URI;
|
||||
import java.net.URISyntaxException;
|
||||
import java.net.URL;
|
||||
import java.nio.channels.Channels;
|
||||
import java.nio.channels.ReadableByteChannel;
|
||||
import java.nio.file.Files;
|
||||
import java.util.*;
|
||||
import java.util.concurrent.atomic.LongAdder;
|
||||
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;
|
||||
import java.util.regex.Pattern;
|
||||
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;
|
||||
|
||||
import static com.boydti.fawe.util.ReflectionUtils.as;
|
||||
//import com.boydti.fawe.object.schematic.visualizer.SchemVis;
|
||||
|
||||
/**
|
||||
* Commands that work with schematic files.
|
||||
*/
|
||||
@Command(aliases = {"schematic", "schem", "/schematic", "/schem", "clipboard", "/clipboard"}, desc = "Commands that work with schematic files")
|
||||
public class SchematicCommands extends MethodCommands {
|
||||
@CommandContainer(superTypes = CommandPermissionsConditionGenerator.Registration.class)
|
||||
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 = LoggerFactory.getLogger(SchematicCommands.class);
|
||||
private final WorldEdit worldEdit;
|
||||
|
||||
/**
|
||||
* Create a new instance.
|
||||
*
|
||||
* @param worldEdit reference to WorldEdit
|
||||
*/
|
||||
public SchematicCommands(final WorldEdit worldEdit) {
|
||||
super(worldEdit);
|
||||
public SchematicCommands(WorldEdit worldEdit) {
|
||||
checkNotNull(worldEdit);
|
||||
this.worldEdit = worldEdit;
|
||||
}
|
||||
|
||||
@Command(
|
||||
aliases = {"loadall"},
|
||||
usage = "[<format>] <filename|url>",
|
||||
help = "Load multiple clipboards\n" +
|
||||
"The -r flag will apply random rotation",
|
||||
desc = "Load multiple clipboards (paste will randomly choose one)"
|
||||
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"
|
||||
)
|
||||
@Deprecated
|
||||
@CommandPermissions({"worldedit.clipboard.load", "worldedit.schematic.load", "worldedit.schematic.load.web", "worldedit.schematic.load.asset"})
|
||||
public void loadall(final Player player, final LocalSession session, @Optional("schematic") final String formatName, final String filename, @Switch('r') boolean randomRotate) throws FilenameException {
|
||||
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 {
|
||||
final ClipboardFormat format = ClipboardFormats.findByAlias(formatName);
|
||||
if (format == null) {
|
||||
BBC.CLIPBOARD_INVALID_FORMAT.send(player, formatName);
|
||||
@@ -118,9 +143,8 @@ public class SchematicCommands extends MethodCommands {
|
||||
}
|
||||
|
||||
@Command(
|
||||
aliases = {"clear"},
|
||||
desc = "Clear your clipboard",
|
||||
max = 0
|
||||
name = "clear",
|
||||
desc = "Clear your clipboard"
|
||||
)
|
||||
@CommandPermissions({"worldedit.clipboard.clear", "worldedit.schematic.clear"})
|
||||
public void clear(Player player, LocalSession session) throws WorldEditException {
|
||||
@@ -129,11 +153,8 @@ public class SchematicCommands extends MethodCommands {
|
||||
}
|
||||
|
||||
@Command(
|
||||
aliases = {"unload"},
|
||||
usage = "[file]",
|
||||
desc = "Remove a clipboard from your multi-clipboard",
|
||||
min = 1,
|
||||
max = 1
|
||||
name = "unload",
|
||||
desc = "Remove a clipboard from your multi-clipboard"
|
||||
)
|
||||
@CommandPermissions({"worldedit.clipboard.clear", "worldedit.schematic.clear"})
|
||||
public void unload(Player player, LocalSession session, String fileName) throws WorldEditException {
|
||||
@@ -166,19 +187,13 @@ public class SchematicCommands extends MethodCommands {
|
||||
}
|
||||
|
||||
@Command(
|
||||
aliases = {"remap"},
|
||||
help = "Remap a clipboard between MCPE/PC values",
|
||||
name = "remap",
|
||||
desc = "Remap a clipboard between MCPE/PC values"
|
||||
)
|
||||
@Deprecated
|
||||
@CommandPermissions({"worldedit.schematic.remap"})
|
||||
public void remap(final Player player, final LocalSession session) throws WorldEditException {
|
||||
ClipboardRemapper remapper;
|
||||
if (false) {
|
||||
remapper = new ClipboardRemapper(ClipboardRemapper.RemapPlatform.PC, ClipboardRemapper.RemapPlatform.PE);
|
||||
} else {
|
||||
remapper = new ClipboardRemapper(ClipboardRemapper.RemapPlatform.PE, ClipboardRemapper.RemapPlatform.PC);
|
||||
}
|
||||
ClipboardRemapper remapper = new ClipboardRemapper(ClipboardRemapper.RemapPlatform.PE, ClipboardRemapper.RemapPlatform.PC);
|
||||
|
||||
for (Clipboard clip : session.getClipboard().getClipboards()) {
|
||||
remapper.apply(clip);
|
||||
@@ -187,13 +202,15 @@ public class SchematicCommands extends MethodCommands {
|
||||
}
|
||||
|
||||
@Command(
|
||||
aliases = {"load"},
|
||||
usage = "[<format>] <filename>",
|
||||
desc = "Load a schematic into your clipboard"
|
||||
name = "load",
|
||||
desc = "Load a schematic into your clipboard"
|
||||
)
|
||||
@Deprecated
|
||||
@CommandPermissions({"worldedit.clipboard.load", "worldedit.schematic.load", "worldedit.schematic.load.asset", "worldedit.schematic.load.web", "worldedit.schematic.load.other"})
|
||||
public void load(Player player, LocalSession session, @Optional() String formatName, String filename) throws FilenameException {
|
||||
public void load(Player player, LocalSession session,
|
||||
@Arg(desc = "File name.")
|
||||
String filename,
|
||||
@Arg(desc = "Format name.", def = "sponge")
|
||||
String formatName) throws FilenameException {
|
||||
LocalConfiguration config = worldEdit.getConfiguration();
|
||||
ClipboardFormat format = formatName == null ? null : ClipboardFormats.findByAlias(formatName);
|
||||
InputStream in = null;
|
||||
@@ -236,7 +253,7 @@ public class SchematicCommands extends MethodCommands {
|
||||
return;
|
||||
}
|
||||
if (format == null && filename.matches(".*\\.[\\w].*")) {
|
||||
String extension = filename.substring(filename.lastIndexOf('.') + 1, filename.length());
|
||||
String extension = filename.substring(filename.lastIndexOf('.') + 1);
|
||||
format = ClipboardFormats.findByExtension(extension);
|
||||
}
|
||||
f = MainUtil.resolve(dir, filename, format, false);
|
||||
@@ -248,7 +265,7 @@ public class SchematicCommands extends MethodCommands {
|
||||
}
|
||||
}
|
||||
if (f == null || !f.exists() || !MainUtil.isInSubDirectory(working, f)) {
|
||||
player.printError("Schematic " + filename + " does not exist! (" + ((f != null) && f.exists()) + "|" + f + "|" + (f != null && !MainUtil.isInSubDirectory(working, f)) + ")");
|
||||
player.printError("Schematic " + filename + " does not exist! (" + (f != null && f.exists()) + "|" + f + "|" + (f != null && !MainUtil.isInSubDirectory(working, f)) + ")");
|
||||
return;
|
||||
}
|
||||
if (format == null) {
|
||||
@@ -279,33 +296,58 @@ public class SchematicCommands extends MethodCommands {
|
||||
}
|
||||
|
||||
@Command(
|
||||
aliases = {"save"},
|
||||
usage = "[format] <filename>",
|
||||
desc = "Save a schematic into your clipboard",
|
||||
help = "The default format for 1.13 is schem"
|
||||
name = "save",
|
||||
desc = "Save a schematic into your clipboard"
|
||||
)
|
||||
@Deprecated
|
||||
@CommandPermissions({"worldedit.clipboard.save", "worldedit.schematic.save", "worldedit.schematic.save.other"})
|
||||
public void save(Player player, LocalSession session, @Optional("schem") String formatName, String filename, @Switch('g') boolean global, @Switch('f') boolean allowOverwrite) throws CommandException, WorldEditException {
|
||||
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 {
|
||||
LocalConfiguration config = worldEdit.getConfiguration();
|
||||
|
||||
File dir = worldEdit.getWorkingDirectoryFile(config.saveDir);
|
||||
|
||||
if (!global && Settings.IMP.PATHS.PER_PLAYER_SCHEMATICS) {
|
||||
dir = new File(dir, player.getUniqueId().toString());
|
||||
}
|
||||
|
||||
ClipboardFormat format = ClipboardFormats.findByAlias(formatName);
|
||||
if (format == null) {
|
||||
player.printError("Unknown schematic format: " + formatName);
|
||||
return;
|
||||
}
|
||||
File working = worldEdit.getWorkingDirectoryFile(config.saveDir);
|
||||
File dir = !global && Settings.IMP.PATHS.PER_PLAYER_SCHEMATICS ? new File(working, player.getUniqueId().toString()) : working;
|
||||
|
||||
if (filename.contains("../")) {
|
||||
if (!player.hasPermission("worldedit.schematic.save.other")) {
|
||||
BBC.NO_PERM.send(player, "worldedit.schematic.save.other");
|
||||
return;
|
||||
}
|
||||
if (filename.startsWith("../")) {
|
||||
dir = working;
|
||||
dir = worldEdit.getWorkingDirectoryFile(config.saveDir);
|
||||
filename = filename.substring(3);
|
||||
}
|
||||
}
|
||||
File f = worldEdit.getSafeSaveFile(player, dir, filename, format.getPrimaryFileExtension(), format.getPrimaryFileExtension());
|
||||
|
||||
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;
|
||||
}
|
||||
}
|
||||
|
||||
if (f.getName().replaceAll("." + format.getPrimaryFileExtension(), "").isEmpty()) {
|
||||
File directory = f.getParentFile();
|
||||
if (directory.exists()) {
|
||||
@@ -315,16 +357,13 @@ public class SchematicCommands extends MethodCommands {
|
||||
f = new File(directory, "1." + format.getPrimaryFileExtension());
|
||||
}
|
||||
}
|
||||
final File parent = f.getParentFile();
|
||||
if ((parent != null) && !parent.exists()) {
|
||||
|
||||
// Create parent directories
|
||||
File parent = f.getParentFile();
|
||||
if (parent != null && !parent.exists()) {
|
||||
if (!parent.mkdirs()) {
|
||||
try {
|
||||
Files.createDirectories(parent.toPath());
|
||||
} catch (IOException e) {
|
||||
e.printStackTrace();
|
||||
log.info("Could not create folder for schematics!");
|
||||
return;
|
||||
}
|
||||
throw new StopExecutionException(TextComponent.of(
|
||||
"Could not create folder for schematics!"));
|
||||
}
|
||||
}
|
||||
try {
|
||||
@@ -333,8 +372,10 @@ public class SchematicCommands extends MethodCommands {
|
||||
} else if (!allowOverwrite) {
|
||||
BBC.SCHEMATIC_MOVE_EXISTS.send(player, f.getName());
|
||||
}
|
||||
|
||||
ClipboardHolder holder = session.getClipboard();
|
||||
|
||||
try (FileOutputStream fos = new FileOutputStream(f)) {
|
||||
ClipboardHolder holder = session.getClipboard();
|
||||
Clipboard clipboard = holder.getClipboard();
|
||||
Transform transform = holder.getTransform();
|
||||
Clipboard target;
|
||||
@@ -350,7 +391,9 @@ public class SchematicCommands extends MethodCommands {
|
||||
}
|
||||
|
||||
URI uri = null;
|
||||
if (holder instanceof URIClipboardHolder) uri = ((URIClipboardHolder) holder).getURI(clipboard);
|
||||
if (holder instanceof URIClipboardHolder) {
|
||||
uri = ((URIClipboardHolder) holder).getURI(clipboard);
|
||||
}
|
||||
if (new PlayerSaveClipboardEvent(player, clipboard, uri, f.toURI()).call()) {
|
||||
try (ClipboardWriter writer = format.getWriter(fos)) {
|
||||
if (writer instanceof StructureFormat) {
|
||||
@@ -376,12 +419,9 @@ public class SchematicCommands extends MethodCommands {
|
||||
}
|
||||
|
||||
@Command(
|
||||
aliases = {"move", "m"},
|
||||
usage = "<directory>",
|
||||
desc = "Move your loaded schematic",
|
||||
help = "Move your currently loaded schematics",
|
||||
min = 1,
|
||||
max = 1
|
||||
name = "move",
|
||||
aliases = {"m"},
|
||||
desc = "Move your loaded schematic"
|
||||
)
|
||||
@CommandPermissions({"worldedit.schematic.move", "worldedit.schematic.move.other"})
|
||||
public void move(Player player, LocalSession session, String directory) throws WorldEditException, IOException {
|
||||
@@ -429,55 +469,53 @@ public class SchematicCommands extends MethodCommands {
|
||||
}
|
||||
|
||||
@Command(
|
||||
aliases = {"delete", "d"},
|
||||
usage = "<filename|*>",
|
||||
desc = "Delete a saved schematic",
|
||||
help = "Delete a schematic from the schematic list",
|
||||
min = 1,
|
||||
max = 1
|
||||
name = "delete",
|
||||
aliases = {"d"},
|
||||
desc = "Delete a saved schematic"
|
||||
)
|
||||
@CommandPermissions({"worldedit.schematic.delete", "worldedit.schematic.delete.other"})
|
||||
public void delete(final Player player, final LocalSession session, final CommandContext args) throws WorldEditException, IOException {
|
||||
final LocalConfiguration config = this.worldEdit.getConfiguration();
|
||||
final File working = this.worldEdit.getWorkingDirectoryFile(config.saveDir);
|
||||
final File dir = Settings.IMP.PATHS.PER_PLAYER_SCHEMATICS ? new File(working, player.getUniqueId().toString()) : working;
|
||||
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;
|
||||
List<File> files = new ArrayList<>();
|
||||
|
||||
final String filename = args.getString(0);
|
||||
|
||||
if (filename.equalsIgnoreCase("*")) {
|
||||
files.addAll(getFiles(session.getClipboard()));
|
||||
} else {
|
||||
File f = MainUtil.resolveRelative(new File(dir, filename));
|
||||
files.add(f);
|
||||
}
|
||||
|
||||
if (files.isEmpty()) {
|
||||
BBC.SCHEMATIC_NONE.send(player);
|
||||
BBC.SCHEMATIC_NONE.send(actor);
|
||||
return;
|
||||
}
|
||||
for (File f : files) {
|
||||
if (!MainUtil.isInSubDirectory(working, f) || !f.exists()) {
|
||||
player.printError("Schematic " + filename + " does not exist! (" + f.exists() + "|" + f + "|" + (!MainUtil.isInSubDirectory(working, f)) + ")");
|
||||
actor.printError("Schematic " + filename + " does not exist! (" + f.exists() + "|" + f + "|" + (!MainUtil.isInSubDirectory(working, f)) + ")");
|
||||
continue;
|
||||
}
|
||||
if (Settings.IMP.PATHS.PER_PLAYER_SCHEMATICS && !MainUtil.isInSubDirectory(dir, f) && !player.hasPermission("worldedit.schematic.delete.other")) {
|
||||
BBC.NO_PERM.send(player, "worldedit.schematic.delete.other");
|
||||
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");
|
||||
continue;
|
||||
}
|
||||
if (!delete(f)) {
|
||||
player.printError("Deletion of " + filename + " failed! Maybe it is read-only.");
|
||||
if (!f.delete()) {
|
||||
actor.printError("Deletion of " + filename + " failed! Maybe it is read-only.");
|
||||
continue;
|
||||
}
|
||||
BBC.FILE_DELETED.send(player, filename);
|
||||
BBC.FILE_DELETED.send(actor, filename);
|
||||
}
|
||||
}
|
||||
|
||||
private List<File> getFiles(ClipboardHolder clipboard) {
|
||||
List<File> files = new ArrayList<>();
|
||||
Collection<URI> uris = Collections.emptyList();
|
||||
if (clipboard instanceof URIClipboardHolder) {
|
||||
uris = ((URIClipboardHolder) clipboard).getURIs();
|
||||
}
|
||||
List<File> files = new ArrayList<>();
|
||||
for (URI uri : uris) {
|
||||
File file = new File(uri);
|
||||
if (file.exists()) files.add(file);
|
||||
@@ -485,40 +523,29 @@ public class SchematicCommands extends MethodCommands {
|
||||
return files;
|
||||
}
|
||||
|
||||
private boolean delete(File file) {
|
||||
if (file.delete()) {
|
||||
new File(file.getParentFile(), "." + file.getName() + ".cached").delete();
|
||||
return true;
|
||||
}
|
||||
return false;
|
||||
}
|
||||
|
||||
@Command(
|
||||
aliases = {"formats", "listformats", "f"},
|
||||
desc = "List available formats",
|
||||
max = 0
|
||||
name = "formats",
|
||||
aliases = {"listformats", "f"},
|
||||
desc = "List available formats"
|
||||
)
|
||||
@CommandPermissions("worldedit.schematic.formats")
|
||||
public void formats(final Actor actor) throws WorldEditException {
|
||||
BBC.SCHEMATIC_FORMAT.send(actor);
|
||||
Message m = new Message(BBC.SCHEMATIC_FORMAT).newline();
|
||||
String baseCmd = Commands.getAlias(SchematicCommands.class, "schematic") + " " + Commands.getAlias(SchematicCommands.class, "save");
|
||||
public void formats(Actor actor) {
|
||||
actor.print("Available clipboard formats (Name: Lookup names)");
|
||||
StringBuilder builder;
|
||||
boolean first = true;
|
||||
for (final ClipboardFormat format : ClipboardFormats.getAll()) {
|
||||
StringBuilder builder = new StringBuilder();
|
||||
for (ClipboardFormat format : ClipboardFormats.getAll()) {
|
||||
builder = new StringBuilder();
|
||||
builder.append(format.getName()).append(": ");
|
||||
for (final String lookupName : format.getAliases()) {
|
||||
for (String lookupName : format.getAliases()) {
|
||||
if (!first) {
|
||||
builder.append(", ");
|
||||
}
|
||||
builder.append(lookupName);
|
||||
first = false;
|
||||
}
|
||||
String cmd = baseCmd + " " + format.getName() + " <filename>";
|
||||
m.text(builder).suggestTip(cmd).newline();
|
||||
first = true;
|
||||
actor.print(builder.toString());
|
||||
}
|
||||
m.send(actor);
|
||||
}
|
||||
|
||||
|
||||
@@ -587,25 +614,21 @@ public class SchematicCommands extends MethodCommands {
|
||||
*/
|
||||
|
||||
@Command(
|
||||
aliases = {"list", "ls", "all"},
|
||||
desc = "List saved schematics",
|
||||
usage = "[global|mine|<filter>] [page=1]",
|
||||
flags = "dnp",
|
||||
help = "List all schematics in the schematics directory\n" +
|
||||
" -p <page> prints the requested page\n" +
|
||||
" -f <format> restricts by format\n"
|
||||
name = "list",
|
||||
aliases = {"all", "ls"},
|
||||
desc = "List saved schematics",
|
||||
descFooter = "Note: Format is not fully verified until loading."
|
||||
)
|
||||
@CommandPermissions("worldedit.schematic.list")
|
||||
public void list(FawePlayer fp, Actor actor, CommandContext args, @Switch('p') @Optional("1") int page, @Switch('f') String formatName) throws WorldEditException {
|
||||
if (args.argsLength() == 0) {
|
||||
BBC.COMMAND_SYNTAX.send(fp, getCommand().usage());
|
||||
return;
|
||||
}
|
||||
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 {
|
||||
LocalConfiguration config = worldEdit.getConfiguration();
|
||||
String prefix = config.noDoubleSlash ? "" : "/";
|
||||
File dir = worldEdit.getWorkingDirectoryFile(config.saveDir);
|
||||
|
||||
String schemCmd = prefix + Commands.getAlias(SchematicCommands.class, "schematic");
|
||||
String schemCmd = "/" + Commands.getAlias(SchematicCommands.class, "schematic");
|
||||
String loadSingle = schemCmd + " " + Commands.getAlias(SchematicCommands.class, "load");
|
||||
String loadMulti = schemCmd + " " + Commands.getAlias(SchematicCommands.class, "loadall");
|
||||
String unload = schemCmd + " " + Commands.getAlias(SchematicCommands.class, "unload");
|
||||
@@ -623,7 +646,6 @@ public class SchematicCommands extends MethodCommands {
|
||||
boolean loaded = multi != null && multi.contains(uri);
|
||||
|
||||
String name = relFilePath;
|
||||
String color;
|
||||
String uriStr = uri.toString();
|
||||
if (uriStr.startsWith("file:/")) {
|
||||
File file = new File(uri.getPath());
|
||||
@@ -635,38 +657,163 @@ public class SchematicCommands extends MethodCommands {
|
||||
} catch (IOException ignore) {}
|
||||
if (file.isDirectory()) {
|
||||
isDir = true;
|
||||
color = "&6";
|
||||
} else {
|
||||
color = "&a";
|
||||
if (name.indexOf('.') != -1) name = name.substring(0, name.lastIndexOf('.'));
|
||||
} else if (name.indexOf('.') != -1) {
|
||||
name = name.substring(0, name.lastIndexOf('.'));
|
||||
}
|
||||
} else if (uriStr.startsWith("http://") || uriStr.startsWith("https://")) {
|
||||
// url
|
||||
color = "&9";
|
||||
} else {
|
||||
color = "&7";
|
||||
}
|
||||
} // url
|
||||
|
||||
msg.text("&8 - ");
|
||||
msg.text(" - ");
|
||||
|
||||
if (msg.supportsInteraction()) {
|
||||
if (loaded) {
|
||||
msg.text("&7[&c-&7]").command(unload + " " + relFilePath).tooltip("Unload");
|
||||
msg.text("[-]").command(unload + " " + relFilePath).tooltip("Unload");
|
||||
} else {
|
||||
msg.text("&7[&a+&7]").command(loadMulti + " " + relFilePath).tooltip("Add to clipboard");
|
||||
msg.text("[+]").command(loadMulti + " " + relFilePath).tooltip("Add to clipboard");
|
||||
}
|
||||
if (!isDir) msg.text("&7[&cX&7]").suggest("/" + delete + " " + relFilePath).tooltip("Delete");
|
||||
else if (hasShow) msg.text("&7[&3O&7]").command(showCmd + " " + args.getJoinedStrings(0) + " " + relFilePath).tooltip("Show");
|
||||
msg.text(color + name);
|
||||
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);
|
||||
if (isDir) {
|
||||
msg.command(list + " " + relFilePath).tooltip("List");
|
||||
} else {
|
||||
msg.command(loadSingle + " " + relFilePath).tooltip("Load");
|
||||
}
|
||||
} else {
|
||||
msg.text(color).text(name);
|
||||
msg.text(name);
|
||||
}
|
||||
}
|
||||
});
|
||||
}
|
||||
|
||||
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;
|
||||
}
|
||||
}
|
||||
|
Reference in New Issue
Block a user