Search in sources :

Example 6 with LocalConfiguration

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

the class ClipboardFormats method loadAllFromInput.

public static MultiClipboardHolder loadAllFromInput(Actor player, String input, ClipboardFormat format, boolean message) throws IOException {
    checkNotNull(player);
    checkNotNull(input);
    WorldEdit worldEdit = WorldEdit.getInstance();
    LocalConfiguration config = worldEdit.getConfiguration();
    if (input.startsWith("url:")) {
        if (!player.hasPermission("worldedit.schematic.load.web")) {
            if (message) {
                player.print(Caption.of("fawe.error.no-perm", "worldedit.schematic.load.web"));
            }
            return null;
        }
        URL base = new URL(Settings.settings().WEB.URL);
        input = new URL(base, "uploads/" + input.substring(4) + "." + format.getPrimaryFileExtension()).toString();
    }
    if (input.startsWith("http")) {
        return null;
    }
    if (Settings.settings().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(input).find() && !player.hasPermission("worldedit.schematic.load.other")) {
        player.print(Caption.of("fawe.error.no-perm", "worldedit.schematic.load.other"));
        return null;
    }
    File working = worldEdit.getWorkingDirectoryPath(config.saveDir).toFile();
    File dir = Settings.settings().PATHS.PER_PLAYER_SCHEMATICS ? new File(working, player.getUniqueId().toString()) : working;
    File f;
    if (input.startsWith("#")) {
        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()) {
            if (message) {
                player.print(Caption.of("worldedit.schematic.load.does-not-exist", TextComponent.of(input)));
            }
            return null;
        }
    } else {
        if (Settings.settings().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(input).find() && !player.hasPermission("worldedit.schematic.load.other")) {
            if (message) {
                player.print(Caption.of("fawe.error.no-perm", "worldedit.schematic.load.other"));
            }
            return null;
        }
        if (format == null && input.matches(".*\\.[\\w].*")) {
            String extension = input.substring(input.lastIndexOf('.') + 1);
            format = findByExtension(extension);
        }
        f = MainUtil.resolve(dir, input, format, true);
    }
    if (f == null || !f.exists()) {
        if (!input.contains("../")) {
            f = MainUtil.resolve(dir, input, null, true);
        }
    }
    if (f == null || !f.exists() || !MainUtil.isInSubDirectory(working, f)) {
        if (message) {
            player.printError("Schematic " + input + " does not exist! (" + ((f != null) && f.exists()) + "|" + f + "|" + (f != null && !MainUtil.isInSubDirectory(working, f)) + ")");
        }
        return null;
    }
    if (format == null && f.isFile()) {
        format = findByFile(f);
        if (format == null) {
            player.print(Caption.of("fawe.worldedit.clipboard.clipboard.invalid.format", f.getName()));
            return null;
        }
    }
    if (!f.exists()) {
        if (message) {
            player.print(Caption.of("fawe.error.schematic.not.found", input));
        }
        return null;
    }
    if (!f.isDirectory()) {
        ByteSource source = Files.asByteSource(f);
        URI uri = f.toURI();
        return new MultiClipboardHolder(uri, new LazyClipboardHolder(f.toURI(), source, format, null));
    }
    URIClipboardHolder[] clipboards = loadAllFromDirectory(f);
    if (clipboards.length < 1) {
        if (message) {
            player.print(Caption.of("fawe.error.schematic.not.found", input));
        }
        return null;
    }
    return new MultiClipboardHolder(f.toURI(), clipboards);
}
Also used : MultiClipboardHolder(com.fastasyncworldedit.core.extent.clipboard.MultiClipboardHolder) WorldEdit(com.sk89q.worldedit.WorldEdit) LazyClipboardHolder(com.fastasyncworldedit.core.extent.clipboard.LazyClipboardHolder) ByteSource(com.google.common.io.ByteSource) LocalConfiguration(com.sk89q.worldedit.LocalConfiguration) File(java.io.File) URI(java.net.URI) URL(java.net.URL) URIClipboardHolder(com.fastasyncworldedit.core.extent.clipboard.URIClipboardHolder)

Example 7 with LocalConfiguration

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

the class SchematicCommands method move.

// FAWE start
@Command(name = "move", aliases = { "m" }, desc = "Move your loaded schematic")
@CommandPermissions({ "worldedit.schematic.move", "worldedit.schematic.move.other" })
public void move(Actor actor, LocalSession session, @Arg(desc = "Directory.") String directory) throws WorldEditException, IOException {
    LocalConfiguration config = worldEdit.getConfiguration();
    File working = worldEdit.getWorkingDirectoryPath(config.saveDir).toFile();
    File dir = Settings.settings().PATHS.PER_PLAYER_SCHEMATICS ? new File(working, actor.getUniqueId().toString()) : working;
    File destDir = new File(dir, directory);
    if (!MainUtil.isInSubDirectory(working, destDir)) {
        actor.print(Caption.of("worldedit.schematic.directory-does-not-exist", TextComponent.of(String.valueOf(destDir))));
        return;
    }
    if (Settings.settings().PATHS.PER_PLAYER_SCHEMATICS && !MainUtil.isInSubDirectory(dir, destDir) && !actor.hasPermission("worldedit.schematic.move.other")) {
        actor.print(Caption.of("fawe.error.no-perm", "worldedit.schematic.move.other"));
        return;
    }
    ClipboardHolder clipboard = session.getClipboard();
    List<File> sources = getFiles(clipboard);
    if (sources.isEmpty()) {
        actor.print(Caption.of("fawe.worldedit.schematic.schematic.none"));
        return;
    }
    if (!destDir.exists() && !destDir.mkdirs()) {
        actor.print(Caption.of("worldedit.schematic.file-perm-fail", TextComponent.of(String.valueOf(destDir))));
        return;
    }
    for (File source : sources) {
        File destFile = new File(destDir, source.getName());
        if (destFile.exists()) {
            actor.print(Caption.of("fawe.worldedit.schematic.schematic.move.exists", destFile));
            continue;
        }
        if (Settings.settings().PATHS.PER_PLAYER_SCHEMATICS && (!MainUtil.isInSubDirectory(dir, destFile) || !MainUtil.isInSubDirectory(dir, source)) && !actor.hasPermission("worldedit.schematic.delete.other")) {
            actor.print(Caption.of("fawe.worldedit.schematic.schematic.move.failed", destFile, Caption.of("fawe.error.no-perm", ("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());
            }
            actor.print(Caption.of("fawe.worldedit.schematic.schematic.move.success", source, destFile));
        } catch (IOException e) {
            throw new RuntimeException(e);
        }
    }
}
Also used : MultiClipboardHolder(com.fastasyncworldedit.core.extent.clipboard.MultiClipboardHolder) URIClipboardHolder(com.fastasyncworldedit.core.extent.clipboard.URIClipboardHolder) ClipboardHolder(com.sk89q.worldedit.session.ClipboardHolder) IOException(java.io.IOException) LocalConfiguration(com.sk89q.worldedit.LocalConfiguration) File(java.io.File) Command(org.enginehub.piston.annotation.Command) CommandPermissions(com.sk89q.worldedit.command.util.CommandPermissions)

Example 8 with LocalConfiguration

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

the class SchematicCommands method unload.

@Command(name = "unload", desc = "Remove a clipboard from your multi-clipboard")
@CommandPermissions({ "worldedit.clipboard.clear", "worldedit.schematic.clear" })
public void unload(Actor actor, LocalSession session, @Arg(desc = "File name, requires extension.") 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.getWorkingDirectoryPath(config.saveDir).toFile();
        File root = Settings.settings().PATHS.PER_PLAYER_SCHEMATICS ? new File(working, actor.getUniqueId().toString()) : working;
        uri = new File(root, fileName).toURI();
    }
    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);
            }
            actor.print(Caption.of("fawe.worldedit.clipboard.clipboard.cleared"));
            return;
        }
    }
    actor.print(Caption.of("fawe.worldedit.clipboard.clipboard.uri.not.found", fileName));
}
Also used : MultiClipboardHolder(com.fastasyncworldedit.core.extent.clipboard.MultiClipboardHolder) MultiClipboardHolder(com.fastasyncworldedit.core.extent.clipboard.MultiClipboardHolder) URIClipboardHolder(com.fastasyncworldedit.core.extent.clipboard.URIClipboardHolder) ClipboardHolder(com.sk89q.worldedit.session.ClipboardHolder) URI(java.net.URI) LocalConfiguration(com.sk89q.worldedit.LocalConfiguration) File(java.io.File) URIClipboardHolder(com.fastasyncworldedit.core.extent.clipboard.URIClipboardHolder) Command(org.enginehub.piston.annotation.Command) CommandPermissions(com.sk89q.worldedit.command.util.CommandPermissions)

Example 9 with LocalConfiguration

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

the class ClipboardCommands method download.

// FAWE start
@Command(name = "download", aliases = { "/download" }, desc = "Downloads your clipboard through the configured web interface")
@Deprecated
@CommandPermissions({ "worldedit.clipboard.download" })
public void download(final Actor actor, final LocalSession session, @Arg(name = "format", desc = "String", def = "fast") final String formatName) throws WorldEditException {
    final ClipboardFormat format = ClipboardFormats.findByAlias(formatName);
    if (format == null) {
        actor.print(Caption.of("fawe.worldedit.clipboard.clipboard.invalid.format", formatName));
        return;
    }
    actor.print(Caption.of("fawe.web.generating.link", formatName));
    ClipboardHolder holder = session.getClipboard();
    URL url;
    if (holder instanceof MultiClipboardHolder) {
        MultiClipboardHolder multi = (MultiClipboardHolder) holder;
        Set<File> files = new HashSet<>();
        Set<URI> invalid = new HashSet<>();
        for (ClipboardHolder cur : multi.getHolders()) {
            if (cur instanceof URIClipboardHolder) {
                URIClipboardHolder uriHolder = (URIClipboardHolder) cur;
                URI uri = uriHolder.getUri();
                File file = new File(uri.getPath());
                if (file.exists() && file.isFile()) {
                    files.add(file.getAbsoluteFile());
                } else if (!uri.getPath().isEmpty()) {
                    invalid.add(uri);
                }
            }
        }
        final LocalConfiguration config = WorldEdit.getInstance().getConfiguration();
        final File working = WorldEdit.getInstance().getWorkingDirectoryFile(config.saveDir).getAbsoluteFile();
        url = MainUtil.upload(null, null, "zip", new RunnableVal<>() {

            @Override
            public void run(OutputStream out) {
                try (ZipOutputStream zos = new ZipOutputStream(out)) {
                    for (File file : files) {
                        String fileName = file.getName();
                        if (MainUtil.isInSubDirectory(working, file)) {
                            fileName = working.toURI().relativize(file.toURI()).getPath();
                        }
                        ZipEntry ze = new ZipEntry(fileName);
                        zos.putNextEntry(ze);
                        Files.copy(file.toPath(), zos);
                        zos.closeEntry();
                    }
                } catch (IOException e) {
                    throw new RuntimeException(e);
                }
            }
        });
    } else {
        Clipboard clipboard = holder.getClipboard();
        final Transform transform = holder.getTransform();
        final Clipboard target;
        // If we have a transform, bake it into the copy
        if (!transform.isIdentity()) {
            final FlattenedClipboardTransform result = FlattenedClipboardTransform.transform(clipboard, transform);
            target = new BlockArrayClipboard(result.getTransformedRegion(), actor.getUniqueId());
            target.setOrigin(clipboard.getOrigin());
            Operations.completeLegacy(result.copyTo(target));
        } else {
            target = clipboard;
        }
        if (format == BuiltInClipboardFormat.PNG) {
            try {
                FastByteArrayOutputStream baos = new FastByteArrayOutputStream(Short.MAX_VALUE);
                ClipboardWriter writer = format.getWriter(baos);
                writer.write(target);
                baos.flush();
                url = ImgurUtility.uploadImage(baos.toByteArray());
            } catch (IOException e) {
                e.printStackTrace();
                url = null;
            }
        } else {
            if (Settings.settings().WEB.URL.isEmpty()) {
                actor.print(Caption.of("fawe.error.setting.disable", "web.url"));
                return;
            }
            url = FaweAPI.upload(target, format);
        }
    }
    if (url == null) {
        actor.print(Caption.of("fawe.web.generating.link.failed"));
    } else {
        String urlText = url.toString();
        actor.print(Caption.of("fawe.web.download.link", urlText).clickEvent(ClickEvent.openUrl(urlText)));
    }
}
Also used : ClipboardWriter(com.sk89q.worldedit.extent.clipboard.io.ClipboardWriter) MultiClipboardHolder(com.fastasyncworldedit.core.extent.clipboard.MultiClipboardHolder) BlockArrayClipboard(com.sk89q.worldedit.extent.clipboard.BlockArrayClipboard) MultiClipboardHolder(com.fastasyncworldedit.core.extent.clipboard.MultiClipboardHolder) URIClipboardHolder(com.fastasyncworldedit.core.extent.clipboard.URIClipboardHolder) ClipboardHolder(com.sk89q.worldedit.session.ClipboardHolder) FastByteArrayOutputStream(com.fastasyncworldedit.core.internal.io.FastByteArrayOutputStream) ZipOutputStream(java.util.zip.ZipOutputStream) OutputStream(java.io.OutputStream) ZipEntry(java.util.zip.ZipEntry) IOException(java.io.IOException) BuiltInClipboardFormat(com.sk89q.worldedit.extent.clipboard.io.BuiltInClipboardFormat) ClipboardFormat(com.sk89q.worldedit.extent.clipboard.io.ClipboardFormat) URI(java.net.URI) LocalConfiguration(com.sk89q.worldedit.LocalConfiguration) URL(java.net.URL) URIClipboardHolder(com.fastasyncworldedit.core.extent.clipboard.URIClipboardHolder) FastByteArrayOutputStream(com.fastasyncworldedit.core.internal.io.FastByteArrayOutputStream) ZipOutputStream(java.util.zip.ZipOutputStream) RunnableVal(com.fastasyncworldedit.core.util.task.RunnableVal) DiskOptimizedClipboard(com.fastasyncworldedit.core.extent.clipboard.DiskOptimizedClipboard) Clipboard(com.sk89q.worldedit.extent.clipboard.Clipboard) BlockArrayClipboard(com.sk89q.worldedit.extent.clipboard.BlockArrayClipboard) ReadOnlyClipboard(com.fastasyncworldedit.core.extent.clipboard.ReadOnlyClipboard) AffineTransform(com.sk89q.worldedit.math.transform.AffineTransform) Transform(com.sk89q.worldedit.math.transform.Transform) File(java.io.File) HashSet(java.util.HashSet) Command(org.enginehub.piston.annotation.Command) CommandPermissions(com.sk89q.worldedit.command.util.CommandPermissions)

Example 10 with LocalConfiguration

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

the class GeneralCommands method timeout.

@Command(name = "/timeout", desc = "Modify evaluation timeout time.")
@CommandPermissions("worldedit.timeout")
public void timeout(Actor actor, LocalSession session, @Arg(desc = "The timeout time to set", def = "") Integer limit) {
    LocalConfiguration config = worldEdit.getConfiguration();
    boolean mayDisable = actor.hasPermission("worldedit.timeout.unrestricted");
    limit = limit == null ? config.calculationTimeout : Math.max(-1, limit);
    if (!mayDisable && config.maxCalculationTimeout > -1) {
        if (limit > config.maxCalculationTimeout) {
            actor.print(Caption.of("worldedit.timeout.too-high", TextComponent.of(config.maxCalculationTimeout)));
            return;
        }
    }
    session.setTimeout(limit);
    Component component = TextComponent.empty().append(Caption.of("worldedit.timeout.set", TextComponent.of(limit)));
    if (limit != config.calculationTimeout) {
        component.append(Caption.of("worldedit.timeout.return-to-default"));
    }
    actor.print(component);
}
Also used : Component(com.sk89q.worldedit.util.formatting.text.Component) TranslatableComponent(com.sk89q.worldedit.util.formatting.text.TranslatableComponent) TextComponent(com.sk89q.worldedit.util.formatting.text.TextComponent) LocalConfiguration(com.sk89q.worldedit.LocalConfiguration) Command(org.enginehub.piston.annotation.Command) CommandPermissions(com.sk89q.worldedit.command.util.CommandPermissions)

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