Search in sources :

Example 96 with CommandPermissions

use of com.sk89q.worldedit.command.util.CommandPermissions 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 97 with CommandPermissions

use of com.sk89q.worldedit.command.util.CommandPermissions 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 98 with CommandPermissions

use of com.sk89q.worldedit.command.util.CommandPermissions in project FastAsyncWorldEdit by IntellectualSites.

the class SchematicCommands method formats.

@Command(name = "formats", aliases = { "listformats", "f" }, desc = "List available formats")
@CommandPermissions("worldedit.schematic.formats")
public void formats(Actor actor) {
    actor.print(Caption.of("worldedit.schematic.formats.title"));
    StringBuilder builder;
    boolean first = true;
    for (ClipboardFormat format : ClipboardFormats.getAll()) {
        builder = new StringBuilder();
        builder.append(format.getName()).append(": ");
        for (String lookupName : format.getAliases()) {
            if (!first) {
                builder.append(", ");
            }
            builder.append(lookupName);
            first = false;
        }
        first = true;
        actor.print(TextComponent.of(builder.toString()));
    }
}
Also used : ClipboardFormat(com.sk89q.worldedit.extent.clipboard.io.ClipboardFormat) Command(org.enginehub.piston.annotation.Command) CommandPermissions(com.sk89q.worldedit.command.util.CommandPermissions)

Example 99 with CommandPermissions

use of com.sk89q.worldedit.command.util.CommandPermissions 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 100 with CommandPermissions

use of com.sk89q.worldedit.command.util.CommandPermissions in project FastAsyncWorldEdit by IntellectualSites.

the class SelectionCommands method pos2.

@Command(name = "/pos2", aliases = "/2", desc = "Set position 2")
@Logging(POSITION)
@CommandPermissions("worldedit.selection.pos")
public void pos2(Actor actor, World world, LocalSession session, @Arg(desc = "Coordinates to set position 2 to", def = "") BlockVector3 coordinates) throws WorldEditException {
    Location pos;
    if (coordinates != null) {
        // FAWE start - clamp
        pos = new Location(world, coordinates.toVector3().clampY(world.getMinY(), world.getMaxY()));
    } else if (actor instanceof Locatable) {
        pos = ((Locatable) actor).getBlockLocation().clampY(world.getMinY(), world.getMaxY());
    // Fawe end
    } else {
        actor.print(Caption.of("worldedit.pos.console-require-coords"));
        return;
    }
    if (!session.getRegionSelector(world).selectSecondary(pos.toVector().toBlockPoint(), ActorSelectorLimits.forActor(actor))) {
        actor.print(Caption.of("worldedit.pos.already-set"));
        return;
    }
    session.getRegionSelector(world).explainSecondarySelection(actor, session, pos.toVector().toBlockPoint());
}
Also used : Location(com.sk89q.worldedit.util.Location) Locatable(com.sk89q.worldedit.extension.platform.Locatable) Logging(com.sk89q.worldedit.command.util.Logging) Command(org.enginehub.piston.annotation.Command) CommandPermissions(com.sk89q.worldedit.command.util.CommandPermissions)

Aggregations

CommandPermissions (com.sk89q.worldedit.command.util.CommandPermissions)133 Command (org.enginehub.piston.annotation.Command)133 Logging (com.sk89q.worldedit.command.util.Logging)47 BlockVector3 (com.sk89q.worldedit.math.BlockVector3)34 ScatterCommand (com.fastasyncworldedit.core.command.tool.brush.ScatterCommand)25 Confirm (com.sk89q.worldedit.command.util.annotation.Confirm)22 LocalConfiguration (com.sk89q.worldedit.LocalConfiguration)20 Region (com.sk89q.worldedit.regions.Region)19 Preload (com.sk89q.worldedit.command.util.annotation.Preload)17 File (java.io.File)17 ClipboardHolder (com.sk89q.worldedit.session.ClipboardHolder)16 URIClipboardHolder (com.fastasyncworldedit.core.extent.clipboard.URIClipboardHolder)15 MultiClipboardHolder (com.fastasyncworldedit.core.extent.clipboard.MultiClipboardHolder)14 MaskTraverser (com.fastasyncworldedit.core.util.MaskTraverser)13 Mask (com.sk89q.worldedit.function.mask.Mask)11 IOException (java.io.IOException)11 BrushTool (com.sk89q.worldedit.command.tool.BrushTool)10 Player (com.sk89q.worldedit.entity.Player)10 Clipboard (com.sk89q.worldedit.extent.clipboard.Clipboard)10 Location (com.sk89q.worldedit.util.Location)10