Search in sources :

Example 16 with LocalConfiguration

use of com.sk89q.worldedit.LocalConfiguration in project FastAsyncWorldEdit by IntellectualSites.

the class SchematicCommands method save.

@Command(name = "save", desc = "Save your clipboard into a schematic file")
@CommandPermissions({ "worldedit.clipboard.save", "worldedit.schematic.save", "worldedit.schematic.save.other", "worldedit.schematic.save.global" })
public void save(Actor actor, LocalSession session, @Arg(desc = "File name.") String filename, @Arg(desc = "Format name.", def = "fast") String formatName, @Switch(name = 'f', desc = "Overwrite an existing file.") boolean allowOverwrite, // FAWE start
@Switch(name = 'g', desc = "Bypasses per-player-schematic folders") boolean global) throws WorldEditException {
    if (global && !actor.hasPermission("worldedit.schematic.save.global")) {
        actor.print(Caption.of("fawe.error.no-perm", "worldedit.schematic.save.global"));
        return;
    }
    // FAWE end
    if (worldEdit.getPlatformManager().queryCapability(Capability.GAME_HOOKS).getDataVersion() == -1) {
        actor.print(TranslatableComponent.of("worldedit.schematic.unsupported-minecraft-version"));
        return;
    }
    LocalConfiguration config = worldEdit.getConfiguration();
    File dir = worldEdit.getWorkingDirectoryPath(config.saveDir).toFile();
    // FAWE start
    if (!global && Settings.settings().PATHS.PER_PLAYER_SCHEMATICS) {
        dir = new File(dir, actor.getUniqueId().toString());
    }
    ClipboardFormat format = ClipboardFormats.findByAlias(formatName);
    if (format == null) {
        actor.print(Caption.of("worldedit.schematic.unknown-format", TextComponent.of(formatName)));
        return;
    }
    boolean other = false;
    if (filename.contains("../")) {
        other = true;
        if (!actor.hasPermission("worldedit.schematic.save.other")) {
            actor.print(Caption.of("fawe.error.no-perm", "worldedit.schematic.save.other"));
            return;
        }
        if (filename.startsWith("../")) {
            dir = worldEdit.getWorkingDirectoryPath(config.saveDir).toFile();
            filename = filename.substring(3);
        }
    }
    // FAWE end
    File f = worldEdit.getSafeSaveFile(actor, dir, filename, format.getPrimaryFileExtension());
    boolean overwrite = f.exists();
    if (overwrite) {
        if (!actor.hasPermission("worldedit.schematic.delete")) {
            throw new StopExecutionException(Caption.of("worldedit.schematic.already-exists"));
        }
        if (other) {
            if (!actor.hasPermission("worldedit.schematic.delete.other")) {
                actor.print(Caption.of("fawe.error.no-perm", "worldedit.schematic.delete.other"));
                return;
            }
        }
        if (!allowOverwrite) {
            actor.print(Caption.of("worldedit.schematic.save.already-exists"));
            return;
        }
    }
    // Create parent directories
    File parent = f.getParentFile();
    if (parent != null && !parent.exists()) {
        if (!parent.mkdirs()) {
            throw new StopExecutionException(Caption.of("worldedit.schematic.save.failed-directory"));
        }
    }
    ClipboardHolder holder = session.getClipboard();
    SchematicSaveTask task = new SchematicSaveTask(actor, f, dir, format, holder, overwrite);
    AsyncCommandBuilder.wrap(task, actor).registerWithSupervisor(worldEdit.getSupervisor(), "Saving schematic " + filename).setDelayMessage(Caption.of("worldedit.schematic.save.saving")).onSuccess(filename + " saved" + (overwrite ? " (overwriting previous file)." : "."), null).onFailure(Caption.of("worldedit.schematic.failed-to-save"), worldEdit.getPlatformManager().getPlatformCommandManager().getExceptionConverter()).buildAndExec(worldEdit.getExecutorService());
}
Also used : StopExecutionException(org.enginehub.piston.exception.StopExecutionException) MultiClipboardHolder(com.fastasyncworldedit.core.extent.clipboard.MultiClipboardHolder) URIClipboardHolder(com.fastasyncworldedit.core.extent.clipboard.URIClipboardHolder) ClipboardHolder(com.sk89q.worldedit.session.ClipboardHolder) LocalConfiguration(com.sk89q.worldedit.LocalConfiguration) File(java.io.File) ClipboardFormat(com.sk89q.worldedit.extent.clipboard.io.ClipboardFormat) Command(org.enginehub.piston.annotation.Command) CommandPermissions(com.sk89q.worldedit.command.util.CommandPermissions)

Example 17 with LocalConfiguration

use of com.sk89q.worldedit.LocalConfiguration in project FastAsyncWorldEdit by IntellectualSites.

the class SchematicCommands method load.

// FAWE end
@Command(name = "load", desc = "Load a schematic into your clipboard")
@CommandPermissions({ "worldedit.clipboard.load", "worldedit.schematic.load", "worldedit.schematic.load.asset", "worldedit.schematic.load.web", "worldedit.schematic.load.other" })
public void load(Actor actor, LocalSession session, @Arg(desc = "File name.") String filename, @Arg(desc = "Format name.", def = "fast") String formatName) throws FilenameException {
    LocalConfiguration config = worldEdit.getConfiguration();
    // FAWE start
    ClipboardFormat format = null;
    InputStream in = null;
    try {
        URI uri;
        if (formatName.startsWith("url:")) {
            String t = filename;
            filename = formatName;
            formatName = t;
        }
        if (filename.startsWith("url:")) {
            if (!actor.hasPermission("worldedit.schematic.load.web")) {
                actor.print(Caption.of("fawe.error.no-perm", "worldedit.schematic.load.web"));
                return;
            }
            UUID uuid = UUID.fromString(filename.substring(4));
            URL webUrl = new URL(Settings.settings().WEB.URL);
            format = ClipboardFormats.findByAlias(formatName);
            URL url = new URL(webUrl, "uploads/" + uuid + "." + format.getPrimaryFileExtension());
            ReadableByteChannel byteChannel = Channels.newChannel(url.openStream());
            in = Channels.newInputStream(byteChannel);
            uri = url.toURI();
        } else {
            File saveDir = worldEdit.getWorkingDirectoryPath(config.saveDir).toFile();
            File dir = Settings.settings().PATHS.PER_PLAYER_SCHEMATICS ? new File(saveDir, actor.getUniqueId().toString()) : saveDir;
            File file;
            if (filename.startsWith("#")) {
                format = ClipboardFormats.findByAlias(formatName);
                String[] extensions;
                if (format != null) {
                    extensions = format.getFileExtensions().toArray(new String[0]);
                } else {
                    extensions = ClipboardFormats.getFileExtensionArray();
                }
                file = actor.openFileOpenDialog(extensions);
                if (file == null || !file.exists()) {
                    actor.print(Caption.of("worldedit.schematic.load.does-not-exist", TextComponent.of(filename)));
                    return;
                }
            } else {
                if (Settings.settings().PATHS.PER_PLAYER_SCHEMATICS && !actor.hasPermission("worldedit.schematic.load.other") && 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()) {
                    actor.print(Caption.of("fawe.error.no-perm", "worldedit.schematic.load.other"));
                    return;
                }
                if (filename.matches(".*\\.[\\w].*")) {
                    format = ClipboardFormats.findByExtension(filename.substring(filename.lastIndexOf('.') + 1));
                } else {
                    format = ClipboardFormats.findByAlias(formatName);
                }
                file = MainUtil.resolve(dir, filename, format, false);
            }
            if (file == null || !file.exists()) {
                if (!filename.contains("../")) {
                    dir = this.worldEdit.getWorkingDirectoryPath(config.saveDir).toFile();
                    file = MainUtil.resolve(dir, filename, format, false);
                }
            }
            if (file == null || !file.exists() || !MainUtil.isInSubDirectory(saveDir, file)) {
                actor.printError(TextComponent.of("Schematic " + filename + " does not exist! (" + (file != null && file.exists()) + "|" + file + "|" + (file != null && !MainUtil.isInSubDirectory(saveDir, file)) + ")"));
                return;
            }
            if (format == null) {
                format = ClipboardFormats.findByFile(file);
                if (format == null) {
                    actor.print(Caption.of("worldedit.schematic.unknown-format", TextComponent.of(formatName)));
                    return;
                }
            }
            in = new FileInputStream(file);
            uri = file.toURI();
        }
        format.hold(actor, uri, in);
        actor.print(Caption.of("fawe.worldedit.schematic.schematic.loaded", filename));
    } catch (IllegalArgumentException e) {
        actor.print(Caption.of("worldedit.schematic.unknown-filename", TextComponent.of(filename)));
    } catch (URISyntaxException | IOException e) {
        actor.print(Caption.of("worldedit.schematic.file-not-exist", TextComponent.of(e.getMessage())));
        LOGGER.warn("Failed to load a saved clipboard", e);
    } finally {
        if (in != null) {
            try {
                in.close();
            } catch (IOException ignored) {
            }
        }
    }
// FAWE end
}
Also used : ReadableByteChannel(java.nio.channels.ReadableByteChannel) BufferedInputStream(java.io.BufferedInputStream) FileInputStream(java.io.FileInputStream) InputStream(java.io.InputStream) URISyntaxException(java.net.URISyntaxException) IOException(java.io.IOException) LocalConfiguration(com.sk89q.worldedit.LocalConfiguration) ClipboardFormat(com.sk89q.worldedit.extent.clipboard.io.ClipboardFormat) URI(java.net.URI) URL(java.net.URL) FileInputStream(java.io.FileInputStream) UUID(java.util.UUID) File(java.io.File) Command(org.enginehub.piston.annotation.Command) CommandPermissions(com.sk89q.worldedit.command.util.CommandPermissions)

Example 18 with LocalConfiguration

use of com.sk89q.worldedit.LocalConfiguration in project FastAsyncWorldEdit by IntellectualSites.

the class SchematicCommands method delete.

@Command(name = "delete", aliases = { "d" }, desc = "Delete a saved schematic")
@CommandPermissions("worldedit.schematic.delete")
public void delete(Actor actor, LocalSession session, @Arg(desc = "File name.") String filename) throws WorldEditException, IOException {
    LocalConfiguration config = worldEdit.getConfiguration();
    File working = worldEdit.getWorkingDirectoryPath(config.saveDir).toFile();
    // FAWE start
    File dir = Settings.settings().PATHS.PER_PLAYER_SCHEMATICS ? new File(working, actor.getUniqueId().toString()) : working;
    List<File> files = new ArrayList<>();
    if (filename.equalsIgnoreCase("*")) {
        files.addAll(getFiles(session.getClipboard()));
    } else {
        File f = MainUtil.resolveRelative(new File(dir, filename));
        files.add(f);
    }
    if (files.isEmpty()) {
        actor.print(Caption.of("worldedit.schematic.delete.does-not-exist", TextComponent.of(filename)));
        return;
    }
    for (File f : files) {
        if (!MainUtil.isInSubDirectory(working, f) || !f.exists()) {
            actor.print(Caption.of("worldedit.schematic.delete.does-not-exist", TextComponent.of(filename)));
            continue;
        }
        if (Settings.settings().PATHS.PER_PLAYER_SCHEMATICS && !MainUtil.isInSubDirectory(dir, f) && !actor.hasPermission("worldedit.schematic.delete.other")) {
            actor.print(Caption.of("fawe.error.no-perm", "worldedit.schematic.delete.other"));
            continue;
        }
        if (!deleteFile(f)) {
            actor.print(Caption.of("worldedit.schematic.delete.failed", TextComponent.of(filename)));
            continue;
        }
        actor.print(Caption.of("worldedit.schematic.delete.deleted", filename));
    }
// FAWE end
}
Also used : ArrayList(java.util.ArrayList) LocalConfiguration(com.sk89q.worldedit.LocalConfiguration) File(java.io.File) Command(org.enginehub.piston.annotation.Command) CommandPermissions(com.sk89q.worldedit.command.util.CommandPermissions)

Example 19 with LocalConfiguration

use of com.sk89q.worldedit.LocalConfiguration in project FastAsyncWorldEdit by IntellectualSites.

the class LegacySnapshotCommands method before.

void before(Actor actor, World world, LocalSession session, ZonedDateTime date) {
    LocalConfiguration config = we.getConfiguration();
    try {
        Snapshot snapshot = config.snapshotRepo.getSnapshotBefore(date, world.getName());
        if (snapshot == null) {
            actor.print(Caption.of("worldedit.snapshot.none-before", TextComponent.of(dateFormat.withZone(session.getTimeZone()).format(date))));
        } else {
            session.setSnapshot(snapshot);
            actor.print(Caption.of("worldedit.snapshot.use", TextComponent.of(snapshot.getName())));
        }
    } catch (MissingWorldException ex) {
        actor.print(Caption.of("worldedit.restore.none-for-world"));
    }
}
Also used : Snapshot(com.sk89q.worldedit.world.snapshot.Snapshot) LocalConfiguration(com.sk89q.worldedit.LocalConfiguration) MissingWorldException(com.sk89q.worldedit.world.storage.MissingWorldException)

Example 20 with LocalConfiguration

use of com.sk89q.worldedit.LocalConfiguration in project FastAsyncWorldEdit by IntellectualSites.

the class LegacySnapshotCommands method after.

void after(Actor actor, World world, LocalSession session, ZonedDateTime date) {
    LocalConfiguration config = we.getConfiguration();
    try {
        Snapshot snapshot = config.snapshotRepo.getSnapshotAfter(date, world.getName());
        if (snapshot == null) {
            actor.print(Caption.of("worldedit.snapshot.none-after", TextComponent.of(dateFormat.withZone(session.getTimeZone()).format(date))));
        } else {
            session.setSnapshot(snapshot);
            actor.print(Caption.of("worldedit.snapshot.use", TextComponent.of(snapshot.getName())));
        }
    } catch (MissingWorldException ex) {
        actor.print(Caption.of("worldedit.restore.none-for-world"));
    }
}
Also used : Snapshot(com.sk89q.worldedit.world.snapshot.Snapshot) LocalConfiguration(com.sk89q.worldedit.LocalConfiguration) MissingWorldException(com.sk89q.worldedit.world.storage.MissingWorldException)

Aggregations

LocalConfiguration (com.sk89q.worldedit.LocalConfiguration)31 CommandPermissions (com.sk89q.worldedit.command.util.CommandPermissions)20 Command (org.enginehub.piston.annotation.Command)20 File (java.io.File)12 IOException (java.io.IOException)9 URI (java.net.URI)7 URIClipboardHolder (com.fastasyncworldedit.core.extent.clipboard.URIClipboardHolder)6 Snapshot (com.sk89q.worldedit.world.snapshot.Snapshot)6 Snapshot (com.sk89q.worldedit.world.snapshot.experimental.Snapshot)6 MissingWorldException (com.sk89q.worldedit.world.storage.MissingWorldException)6 MultiClipboardHolder (com.fastasyncworldedit.core.extent.clipboard.MultiClipboardHolder)5 ClipboardHolder (com.sk89q.worldedit.session.ClipboardHolder)4 Logging (com.sk89q.worldedit.command.util.Logging)3 ClipboardFormat (com.sk89q.worldedit.extent.clipboard.io.ClipboardFormat)3 Component (com.sk89q.worldedit.util.formatting.text.Component)3 TextComponent (com.sk89q.worldedit.util.formatting.text.TextComponent)3 TranslatableComponent (com.sk89q.worldedit.util.formatting.text.TranslatableComponent)3 Region (com.sk89q.worldedit.regions.Region)2 InvalidSnapshotException (com.sk89q.worldedit.world.snapshot.InvalidSnapshotException)2 URL (java.net.URL)2