Search in sources :

Example 1 with URIClipboardHolder

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

the class ClipboardFormat method hold.

// FAWE start
/**
 * Sets the actor's clipboard.
 *
 * @param actor       the actor
 * @param uri         the URI of the schematic to hold
 * @param inputStream the input stream
 * @throws IOException thrown on I/O error
 */
default URIClipboardHolder hold(Actor actor, URI uri, InputStream inputStream) throws IOException {
    checkNotNull(actor);
    checkNotNull(uri);
    checkNotNull(inputStream);
    final ClipboardReader reader = getReader(inputStream);
    final Clipboard clipboard;
    LocalSession session = WorldEdit.getInstance().getSessionManager().get(actor);
    session.setClipboard(null);
    clipboard = reader.read(actor.getUniqueId());
    URIClipboardHolder holder = new URIClipboardHolder(uri, clipboard);
    session.setClipboard(holder);
    return holder;
}
Also used : LocalSession(com.sk89q.worldedit.LocalSession) Clipboard(com.sk89q.worldedit.extent.clipboard.Clipboard) URIClipboardHolder(com.fastasyncworldedit.core.extent.clipboard.URIClipboardHolder)

Example 2 with URIClipboardHolder

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

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

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

the class SchematicCommands method getFiles.

private List<File> getFiles(ClipboardHolder clipboard) {
    Collection<URI> uris = Collections.emptyList();
    if (clipboard instanceof URIClipboardHolder) {
        uris = ((URIClipboardHolder) clipboard).getURIs();
    }
    List<File> files = new ArrayList<>();
    for (URI uri : uris) {
        File file = new File(uri);
        if (file.exists()) {
            files.add(file);
        }
    }
    return files;
}
Also used : ArrayList(java.util.ArrayList) URI(java.net.URI) File(java.io.File) URIClipboardHolder(com.fastasyncworldedit.core.extent.clipboard.URIClipboardHolder)

Example 5 with URIClipboardHolder

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

Aggregations

URIClipboardHolder (com.fastasyncworldedit.core.extent.clipboard.URIClipboardHolder)8 URI (java.net.URI)7 File (java.io.File)6 LocalConfiguration (com.sk89q.worldedit.LocalConfiguration)4 CommandPermissions (com.sk89q.worldedit.command.util.CommandPermissions)4 Command (org.enginehub.piston.annotation.Command)4 MultiClipboardHolder (com.fastasyncworldedit.core.extent.clipboard.MultiClipboardHolder)3 Clipboard (com.sk89q.worldedit.extent.clipboard.Clipboard)3 ClipboardHolder (com.sk89q.worldedit.session.ClipboardHolder)3 Component (com.sk89q.worldedit.util.formatting.text.Component)2 TextComponent (com.sk89q.worldedit.util.formatting.text.TextComponent)2 IOException (java.io.IOException)2 URL (java.net.URL)2 PasteEvent (com.fastasyncworldedit.core.event.extent.PasteEvent)1 DiskOptimizedClipboard (com.fastasyncworldedit.core.extent.clipboard.DiskOptimizedClipboard)1 LazyClipboardHolder (com.fastasyncworldedit.core.extent.clipboard.LazyClipboardHolder)1 ReadOnlyClipboard (com.fastasyncworldedit.core.extent.clipboard.ReadOnlyClipboard)1 FastByteArrayOutputStream (com.fastasyncworldedit.core.internal.io.FastByteArrayOutputStream)1 RunnableVal (com.fastasyncworldedit.core.util.task.RunnableVal)1 ByteSource (com.google.common.io.ByteSource)1