Add -f to //schem save to confirm overwriting.

Overwriting existing schematics now checks delete perm.
Also allow delete to be run from console.

Fixes WORLDEDIT-3868.
Dieser Commit ist enthalten in:
wizjany 2019-03-15 17:08:11 -04:00
Ursprung 18414fe3b5
Commit 9d2d43f0db

Datei anzeigen

@ -27,7 +27,6 @@ import com.sk89q.minecraft.util.commands.Command;
import com.sk89q.minecraft.util.commands.CommandContext; import com.sk89q.minecraft.util.commands.CommandContext;
import com.sk89q.minecraft.util.commands.CommandException; import com.sk89q.minecraft.util.commands.CommandException;
import com.sk89q.minecraft.util.commands.CommandPermissions; import com.sk89q.minecraft.util.commands.CommandPermissions;
import com.sk89q.worldedit.EditSession;
import com.sk89q.worldedit.LocalConfiguration; import com.sk89q.worldedit.LocalConfiguration;
import com.sk89q.worldedit.LocalSession; import com.sk89q.worldedit.LocalSession;
import com.sk89q.worldedit.WorldEdit; import com.sk89q.worldedit.WorldEdit;
@ -129,12 +128,15 @@ public class SchematicCommands {
@Command( @Command(
aliases = { "save" }, aliases = { "save" },
flags = "f",
usage = "[<format>] <filename>", usage = "[<format>] <filename>",
desc = "Save a schematic into your clipboard", desc = "Save a schematic into your clipboard",
help = "-f is required to overwrite an existing file",
min = 1, max = 2 min = 1, max = 2
) )
@CommandPermissions({ "worldedit.clipboard.save", "worldedit.schematic.save" }) @CommandPermissions({ "worldedit.clipboard.save", "worldedit.schematic.save" })
public void save(Player player, LocalSession session, @Optional("sponge") String formatName, String filename) throws CommandException, WorldEditException { public void save(Player player, LocalSession session, @Optional("sponge") String formatName,
String filename, @Switch('f') boolean allowOverwrite) throws CommandException, WorldEditException {
LocalConfiguration config = worldEdit.getConfiguration(); LocalConfiguration config = worldEdit.getConfiguration();
File dir = worldEdit.getWorkingDirectoryFile(config.saveDir); File dir = worldEdit.getWorkingDirectoryFile(config.saveDir);
@ -147,6 +149,17 @@ public class SchematicCommands {
File f = worldEdit.getSafeSaveFile(player, dir, filename, 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 CommandException("That schematic already exists!");
}
if (!allowOverwrite) {
player.printError("That schematic already exists. Use the -f flag to overwrite it.");
return;
}
}
ClipboardHolder holder = session.getClipboard(); ClipboardHolder holder = session.getClipboard();
Clipboard clipboard = holder.getClipboard(); Clipboard clipboard = holder.getClipboard();
Transform transform = holder.getTransform(); Transform transform = holder.getTransform();
@ -162,7 +175,6 @@ public class SchematicCommands {
target = clipboard; target = clipboard;
} }
try (Closer closer = Closer.create()) {
// Create parent directories // Create parent directories
File parent = f.getParentFile(); File parent = f.getParentFile();
if (parent != null && !parent.exists()) { if (parent != null && !parent.exists()) {
@ -171,12 +183,14 @@ public class SchematicCommands {
} }
} }
try (Closer closer = Closer.create()) {
FileOutputStream fos = closer.register(new FileOutputStream(f)); FileOutputStream fos = closer.register(new FileOutputStream(f));
BufferedOutputStream bos = closer.register(new BufferedOutputStream(fos)); BufferedOutputStream bos = closer.register(new BufferedOutputStream(fos));
ClipboardWriter writer = closer.register(format.getWriter(bos)); ClipboardWriter writer = closer.register(format.getWriter(bos));
writer.write(target); writer.write(target);
log.info(player.getName() + " saved " + f.getCanonicalPath());
player.print(filename + " saved."); log.info(player.getName() + " saved " + f.getCanonicalPath() + (overwrite ? " (overwriting previous file)" : ""));
player.print(filename + " saved" + (overwrite ? " (overwriting previous file)." : "."));
} catch (IOException e) { } catch (IOException e) {
player.printError("Schematic could not written: " + e.getMessage()); player.printError("Schematic could not written: " + e.getMessage());
log.log(Level.WARNING, "Failed to write a saved clipboard", e); log.log(Level.WARNING, "Failed to write a saved clipboard", e);
@ -192,29 +206,28 @@ public class SchematicCommands {
max = 1 max = 1
) )
@CommandPermissions("worldedit.schematic.delete") @CommandPermissions("worldedit.schematic.delete")
public void delete(Player player, LocalSession session, EditSession editSession, CommandContext args) throws WorldEditException { public void delete(Actor actor, String filename) throws WorldEditException {
LocalConfiguration config = worldEdit.getConfiguration(); LocalConfiguration config = worldEdit.getConfiguration();
String filename = args.getString(0);
File dir = worldEdit.getWorkingDirectoryFile(config.saveDir); File dir = worldEdit.getWorkingDirectoryFile(config.saveDir);
File f = worldEdit.getSafeOpenFile(player, dir, filename, "schematic", ClipboardFormats.getFileExtensionArray());
File f = worldEdit.getSafeOpenFile(actor instanceof Player ? ((Player) actor) : null,
dir, filename, "schematic", ClipboardFormats.getFileExtensionArray());
if (!f.exists()) { if (!f.exists()) {
player.printError("Schematic " + filename + " does not exist!"); actor.printError("Schematic " + filename + " does not exist!");
return; return;
} }
if (!f.delete()) { if (!f.delete()) {
player.printError("Deletion of " + filename + " failed! Maybe it is read-only."); actor.printError("Deletion of " + filename + " failed! Maybe it is read-only.");
return; return;
} }
player.print(filename + " has been deleted."); actor.print(filename + " has been deleted.");
try { try {
log.info(player.getName() + " deleted " + f.getCanonicalPath()); log.info(actor.getName() + " deleted " + f.getCanonicalPath());
} catch (IOException e) { } catch (IOException e) {
log.info(player.getName() + " deleted " + f.getAbsolutePath()); log.info(actor.getName() + " deleted " + f.getAbsolutePath());
} }
} }
@ -246,7 +259,6 @@ public class SchematicCommands {
@Command( @Command(
aliases = {"list", "all", "ls"}, aliases = {"list", "all", "ls"},
desc = "List saved schematics", desc = "List saved schematics",
min = 0,
max = 1, max = 1,
flags = "dnp", flags = "dnp",
help = "List all schematics in the schematics directory\n" + help = "List all schematics in the schematics directory\n" +