Search in sources :

Example 1 with LazyClipboardHolder

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

the class ClipboardFormats method loadAllFromUrl.

public static MultiClipboardHolder loadAllFromUrl(URL url) throws IOException {
    List<LazyClipboardHolder> clipboards = new ArrayList<>();
    try (ReadableByteChannel rbc = Channels.newChannel(url.openStream())) {
        try (InputStream in = Channels.newInputStream(rbc)) {
            try (ZipInputStream zip = new ZipInputStream(in)) {
                ZipEntry entry;
                byte[] buffer = new byte[8192];
                while ((entry = zip.getNextEntry()) != null) {
                    String filename = entry.getName();
                    ClipboardFormat format = findByExtension(filename);
                    if (format != null) {
                        FastByteArrayOutputStream out = new FastByteArrayOutputStream();
                        int len;
                        while ((len = zip.read(buffer)) > 0) {
                            out.write(buffer, 0, len);
                        }
                        byte[] array = out.toByteArray();
                        ByteSource source = ByteSource.wrap(array);
                        LazyClipboardHolder clipboard = new LazyClipboardHolder(url.toURI(), source, format, null);
                        clipboards.add(clipboard);
                    }
                }
            } catch (URISyntaxException e) {
                e.printStackTrace();
            }
        }
    }
    LazyClipboardHolder[] arr = clipboards.toArray(new LazyClipboardHolder[0]);
    try {
        MultiClipboardHolder multi = new MultiClipboardHolder(url.toURI());
        for (LazyClipboardHolder h : arr) {
            multi.add(h);
        }
        return multi;
    } catch (URISyntaxException e) {
        throw new RuntimeException(e);
    }
}
Also used : ReadableByteChannel(java.nio.channels.ReadableByteChannel) MultiClipboardHolder(com.fastasyncworldedit.core.extent.clipboard.MultiClipboardHolder) LazyClipboardHolder(com.fastasyncworldedit.core.extent.clipboard.LazyClipboardHolder) ZipInputStream(java.util.zip.ZipInputStream) InputStream(java.io.InputStream) ZipEntry(java.util.zip.ZipEntry) ArrayList(java.util.ArrayList) URISyntaxException(java.net.URISyntaxException) ZipInputStream(java.util.zip.ZipInputStream) FastByteArrayOutputStream(com.fastasyncworldedit.core.internal.io.FastByteArrayOutputStream) ByteSource(com.google.common.io.ByteSource)

Example 2 with LazyClipboardHolder

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

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

the class ClipboardFormats method loadAllFromDirectory.

public static URIClipboardHolder[] loadAllFromDirectory(File dir) {
    HashSet<String> extensions = new HashSet<>(Arrays.asList(ClipboardFormats.getFileExtensionArray()));
    File[] files = dir.listFiles(pathname -> {
        String input = pathname.getName();
        String extension = input.substring(input.lastIndexOf('.') + 1);
        return (extensions.contains(extension.toLowerCase(Locale.ENGLISH)));
    });
    LazyClipboardHolder[] clipboards = new LazyClipboardHolder[files.length];
    for (int i = 0; i < files.length; i++) {
        File file = files[i];
        ByteSource source = Files.asByteSource(file);
        ClipboardFormat format = findByFile(file);
        clipboards[i] = new LazyClipboardHolder(file.toURI(), source, format, null);
    }
    return clipboards;
}
Also used : LazyClipboardHolder(com.fastasyncworldedit.core.extent.clipboard.LazyClipboardHolder) ByteSource(com.google.common.io.ByteSource) File(java.io.File) HashSet(java.util.HashSet)

Aggregations

LazyClipboardHolder (com.fastasyncworldedit.core.extent.clipboard.LazyClipboardHolder)3 ByteSource (com.google.common.io.ByteSource)3 MultiClipboardHolder (com.fastasyncworldedit.core.extent.clipboard.MultiClipboardHolder)2 File (java.io.File)2 URIClipboardHolder (com.fastasyncworldedit.core.extent.clipboard.URIClipboardHolder)1 FastByteArrayOutputStream (com.fastasyncworldedit.core.internal.io.FastByteArrayOutputStream)1 LocalConfiguration (com.sk89q.worldedit.LocalConfiguration)1 WorldEdit (com.sk89q.worldedit.WorldEdit)1 InputStream (java.io.InputStream)1 URI (java.net.URI)1 URISyntaxException (java.net.URISyntaxException)1 URL (java.net.URL)1 ReadableByteChannel (java.nio.channels.ReadableByteChannel)1 ArrayList (java.util.ArrayList)1 HashSet (java.util.HashSet)1 ZipEntry (java.util.zip.ZipEntry)1 ZipInputStream (java.util.zip.ZipInputStream)1