Search in sources :

Example 6 with MultiClipboardHolder

use of com.fastasyncworldedit.core.extent.clipboard.MultiClipboardHolder 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 7 with MultiClipboardHolder

use of com.fastasyncworldedit.core.extent.clipboard.MultiClipboardHolder in project FastAsyncWorldEdit by IntellectualSites.

the class BrushCommands method scatterSchemBrush.

@Command(name = "populateschematic", aliases = { "populateschem", "popschem", "pschem", "ps" }, desc = "Scatter a schematic on a surface")
@CommandPermissions("worldedit.brush.populateschematic")
public void scatterSchemBrush(Player player, InjectedValueAccess context, @Arg(desc = "Mask") Mask mask, @Arg(name = "clipboard", desc = "Clipboard uri") String clipboardStr, @Arg(desc = "Expression", def = "30") Expression radius, @Arg(desc = "double", def = "50") double density, @Switch(name = 'r', desc = "Apply random rotation") boolean rotate) throws WorldEditException {
    worldEdit.checkMaxBrushRadius(radius);
    try {
        MultiClipboardHolder clipboards = ClipboardFormats.loadAllFromInput(player, clipboardStr, null, true);
        if (clipboards == null) {
            player.print(Caption.of("fawe.error.schematic.not.found", clipboardStr));
            return;
        }
        List<ClipboardHolder> holders = clipboards.getHolders();
        if (holders == null) {
            player.print(Caption.of("fawe.error.schematic.not.found", clipboardStr));
            return;
        }
        set(context, new PopulateSchem(mask, holders, (int) density, rotate), "worldedit.brush.populateschematic").setSize(radius);
    } catch (IOException e) {
        throw new RuntimeException(e);
    }
}
Also used : MultiClipboardHolder(com.fastasyncworldedit.core.extent.clipboard.MultiClipboardHolder) PopulateSchem(com.fastasyncworldedit.core.command.tool.brush.PopulateSchem) MultiClipboardHolder(com.fastasyncworldedit.core.extent.clipboard.MultiClipboardHolder) ClipboardHolder(com.sk89q.worldedit.session.ClipboardHolder) IOException(java.io.IOException) ScatterCommand(com.fastasyncworldedit.core.command.tool.brush.ScatterCommand) Command(org.enginehub.piston.annotation.Command) CommandPermissions(com.sk89q.worldedit.command.util.CommandPermissions)

Aggregations

MultiClipboardHolder (com.fastasyncworldedit.core.extent.clipboard.MultiClipboardHolder)7 ClipboardHolder (com.sk89q.worldedit.session.ClipboardHolder)4 IOException (java.io.IOException)4 URIClipboardHolder (com.fastasyncworldedit.core.extent.clipboard.URIClipboardHolder)3 LocalConfiguration (com.sk89q.worldedit.LocalConfiguration)3 CommandPermissions (com.sk89q.worldedit.command.util.CommandPermissions)3 File (java.io.File)3 URI (java.net.URI)3 Command (org.enginehub.piston.annotation.Command)3 LazyClipboardHolder (com.fastasyncworldedit.core.extent.clipboard.LazyClipboardHolder)2 FastByteArrayOutputStream (com.fastasyncworldedit.core.internal.io.FastByteArrayOutputStream)2 ByteSource (com.google.common.io.ByteSource)2 URL (java.net.URL)2 ZipEntry (java.util.zip.ZipEntry)2 PopulateSchem (com.fastasyncworldedit.core.command.tool.brush.PopulateSchem)1 ScatterCommand (com.fastasyncworldedit.core.command.tool.brush.ScatterCommand)1 DiskOptimizedClipboard (com.fastasyncworldedit.core.extent.clipboard.DiskOptimizedClipboard)1 ReadOnlyClipboard (com.fastasyncworldedit.core.extent.clipboard.ReadOnlyClipboard)1 RandomFullClipboardPattern (com.fastasyncworldedit.core.function.pattern.RandomFullClipboardPattern)1 RunnableVal (com.fastasyncworldedit.core.util.task.RunnableVal)1