Search in sources :

Example 1 with RunnableVal

use of com.fastasyncworldedit.core.util.task.RunnableVal 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 2 with RunnableVal

use of com.fastasyncworldedit.core.util.task.RunnableVal in project FastAsyncWorldEdit by IntellectualSites.

the class PaperweightFaweWorldNativeAccess method flush.

@Override
public synchronized void flush() {
    RunnableVal<Object> runnableVal = new RunnableVal<>() {

        @Override
        public void run(Object value) {
            cachedChanges.forEach(cc -> cc.levelChunk.setBlockState(cc.blockPos, cc.blockState, sideEffectSet != null && sideEffectSet.shouldApply(SideEffect.UPDATE)));
            for (IntPair chunk : cachedChunksToSend) {
                PaperweightPlatformAdapter.sendChunk(getLevel().getWorld().getHandle(), chunk.x(), chunk.z(), false);
            }
        }
    };
    if (Fawe.isMainThread()) {
        runnableVal.run();
    } else {
        TaskManager.taskManager().sync(runnableVal);
    }
    cachedChanges.clear();
    cachedChunksToSend.clear();
}
Also used : RunnableVal(com.fastasyncworldedit.core.util.task.RunnableVal) IntPair(com.fastasyncworldedit.core.math.IntPair)

Example 3 with RunnableVal

use of com.fastasyncworldedit.core.util.task.RunnableVal in project FastAsyncWorldEdit by IntellectualSites.

the class PaperweightFaweWorldNativeAccess method flushAsync.

private synchronized void flushAsync(final boolean sendChunks) {
    final Set<CachedChange> changes = Set.copyOf(cachedChanges);
    cachedChanges.clear();
    final Set<IntPair> toSend;
    if (sendChunks) {
        toSend = Set.copyOf(cachedChunksToSend);
        cachedChunksToSend.clear();
    } else {
        toSend = Collections.emptySet();
    }
    RunnableVal<Object> runnableVal = new RunnableVal<>() {

        @Override
        public void run(Object value) {
            changes.forEach(cc -> cc.levelChunk.setBlockState(cc.blockPos, cc.blockState, sideEffectSet != null && sideEffectSet.shouldApply(SideEffect.UPDATE)));
            if (!sendChunks) {
                return;
            }
            for (IntPair chunk : toSend) {
                PaperweightPlatformAdapter.sendChunk(getLevel().getWorld().getHandle(), chunk.x(), chunk.z(), false);
            }
        }
    };
    TaskManager.taskManager().async(() -> TaskManager.taskManager().sync(runnableVal));
}
Also used : RunnableVal(com.fastasyncworldedit.core.util.task.RunnableVal) IntPair(com.fastasyncworldedit.core.math.IntPair)

Example 4 with RunnableVal

use of com.fastasyncworldedit.core.util.task.RunnableVal in project FastAsyncWorldEdit by IntellectualSites.

the class PaperweightFaweWorldNativeAccess method flushAsync.

private synchronized void flushAsync(final boolean sendChunks) {
    final Set<CachedChange> changes = Set.copyOf(cachedChanges);
    cachedChanges.clear();
    final Set<IntPair> toSend;
    if (sendChunks) {
        toSend = Set.copyOf(cachedChunksToSend);
        cachedChunksToSend.clear();
    } else {
        toSend = Collections.emptySet();
    }
    RunnableVal<Object> runnableVal = new RunnableVal<>() {

        @Override
        public void run(Object value) {
            changes.forEach(cc -> cc.levelChunk.setBlockState(cc.blockPos, cc.blockState, sideEffectSet != null && sideEffectSet.shouldApply(SideEffect.UPDATE)));
            if (!sendChunks) {
                return;
            }
            for (IntPair chunk : toSend) {
                PaperweightPlatformAdapter.sendChunk(getLevel().getWorld().getHandle(), chunk.x(), chunk.z(), false);
            }
        }
    };
    TaskManager.taskManager().async(() -> TaskManager.taskManager().sync(runnableVal));
}
Also used : RunnableVal(com.fastasyncworldedit.core.util.task.RunnableVal) IntPair(com.fastasyncworldedit.core.math.IntPair)

Example 5 with RunnableVal

use of com.fastasyncworldedit.core.util.task.RunnableVal in project FastAsyncWorldEdit by IntellectualSites.

the class PaperweightFaweWorldNativeAccess method flush.

@Override
public synchronized void flush() {
    RunnableVal<Object> runnableVal = new RunnableVal<>() {

        @Override
        public void run(Object value) {
            cachedChanges.forEach(cc -> cc.levelChunk.setBlockState(cc.blockPos, cc.blockState, sideEffectSet != null && sideEffectSet.shouldApply(SideEffect.UPDATE)));
            for (IntPair chunk : cachedChunksToSend) {
                PaperweightPlatformAdapter.sendChunk(getLevel().getWorld().getHandle(), chunk.x(), chunk.z(), false);
            }
        }
    };
    if (Fawe.isMainThread()) {
        runnableVal.run();
    } else {
        TaskManager.taskManager().sync(runnableVal);
    }
    cachedChanges.clear();
    cachedChunksToSend.clear();
}
Also used : RunnableVal(com.fastasyncworldedit.core.util.task.RunnableVal) IntPair(com.fastasyncworldedit.core.math.IntPair)

Aggregations

RunnableVal (com.fastasyncworldedit.core.util.task.RunnableVal)7 IntPair (com.fastasyncworldedit.core.math.IntPair)6 DiskOptimizedClipboard (com.fastasyncworldedit.core.extent.clipboard.DiskOptimizedClipboard)1 MultiClipboardHolder (com.fastasyncworldedit.core.extent.clipboard.MultiClipboardHolder)1 ReadOnlyClipboard (com.fastasyncworldedit.core.extent.clipboard.ReadOnlyClipboard)1 URIClipboardHolder (com.fastasyncworldedit.core.extent.clipboard.URIClipboardHolder)1 FastByteArrayOutputStream (com.fastasyncworldedit.core.internal.io.FastByteArrayOutputStream)1 LocalConfiguration (com.sk89q.worldedit.LocalConfiguration)1 CommandPermissions (com.sk89q.worldedit.command.util.CommandPermissions)1 BlockArrayClipboard (com.sk89q.worldedit.extent.clipboard.BlockArrayClipboard)1 Clipboard (com.sk89q.worldedit.extent.clipboard.Clipboard)1 BuiltInClipboardFormat (com.sk89q.worldedit.extent.clipboard.io.BuiltInClipboardFormat)1 ClipboardFormat (com.sk89q.worldedit.extent.clipboard.io.ClipboardFormat)1 ClipboardWriter (com.sk89q.worldedit.extent.clipboard.io.ClipboardWriter)1 AffineTransform (com.sk89q.worldedit.math.transform.AffineTransform)1 Transform (com.sk89q.worldedit.math.transform.Transform)1 ClipboardHolder (com.sk89q.worldedit.session.ClipboardHolder)1 File (java.io.File)1 IOException (java.io.IOException)1 OutputStream (java.io.OutputStream)1