Search in sources :

Example 1 with MultiClipboardHolder

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

the class Scroll method fromArguments.

public static Scroll fromArguments(BrushTool tool, Player player, LocalSession session, Action mode, List<String> arguments, boolean message) throws InputParseException {
    ParserContext parserContext = new ParserContext();
    parserContext.setActor(player);
    parserContext.setWorld(player.getWorld());
    parserContext.setSession(session);
    switch(mode) {
        case CLIPBOARD:
            if (arguments.size() != 2) {
                if (message) {
                    player.print(Caption.of("fawe.error.command.syntax", "clipboard [file]"));
                }
                return null;
            }
            String filename = arguments.get(1);
            try {
                MultiClipboardHolder multi = ClipboardFormats.loadAllFromInput(player, filename, null, message);
                if (multi == null) {
                    return null;
                }
                return (new ScrollClipboard(tool, session, multi.getHolders()));
            } catch (IOException e) {
                throw new RuntimeException(e);
            }
        case MASK:
            if (arguments.size() < 2) {
                if (message) {
                    player.print(Caption.of("fawe.error.command.syntax", "mask [mask 1] [mask 2] [mask 3]..."));
                }
                return null;
            }
            Mask[] masks = new Mask[arguments.size() - 1];
            for (int i = 1; i < arguments.size(); i++) {
                String arg = arguments.get(i);
                masks[i - 1] = WorldEdit.getInstance().getMaskFactory().parseFromInput(arg, parserContext);
            }
            return (new ScrollMask(tool, masks));
        case PATTERN:
            if (arguments.size() < 2) {
                if (message) {
                    player.print(Caption.of("fawe.error.command.syntax", "pattern [pattern 1] [pattern 2] [pattern 3]..."));
                }
                return null;
            }
            Pattern[] patterns = new Pattern[arguments.size() - 1];
            for (int i = 1; i < arguments.size(); i++) {
                String arg = arguments.get(i);
                patterns[i - 1] = WorldEdit.getInstance().getPatternFactory().parseFromInput(arg, parserContext);
            }
            return (new ScrollPattern(tool, patterns));
        case TARGET_OFFSET:
            return (new ScrollTargetOffset(tool));
        case RANGE:
            return (new ScrollRange(tool));
        case SIZE:
            return (new ScrollSize(tool));
        case TARGET:
            return (new ScrollTarget(tool));
        default:
            return null;
    }
}
Also used : Pattern(com.sk89q.worldedit.function.pattern.Pattern) MultiClipboardHolder(com.fastasyncworldedit.core.extent.clipboard.MultiClipboardHolder) Mask(com.sk89q.worldedit.function.mask.Mask) IOException(java.io.IOException) ParserContext(com.sk89q.worldedit.extension.input.ParserContext)

Example 2 with MultiClipboardHolder

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

the class RandomFullClipboardPatternParser method parseFromInput.

@Override
protected Pattern parseFromInput(@Nonnull String[] arguments, ParserContext context) throws InputParseException {
    if (arguments.length == 0 || arguments.length > 3) {
        throw new InputParseException(Caption.of("fawe.error.command.syntax", TextComponent.of(getPrefix() + "[pattern] (e.g. " + getPrefix() + "[#copy][true][false])")));
    }
    try {
        boolean rotate = arguments.length >= 2 && Boolean.getBoolean(arguments[1]);
        boolean flip = arguments.length == 3 && Boolean.getBoolean(arguments[2]);
        List<ClipboardHolder> clipboards;
        if ("#copy".startsWith(arguments[0].toUpperCase(Locale.ROOT)) || "#clipboard".startsWith(arguments[0].toUpperCase(Locale.ROOT))) {
            ClipboardHolder clipboard = context.requireSession().getExistingClipboard();
            if (clipboard == null) {
                throw new InputParseException(Caption.of("fawe.error.parse.no-clipboard", getPrefix()));
            }
            clipboards = Collections.singletonList(clipboard);
        } else {
            Actor player = context.requireActor();
            MultiClipboardHolder multi = ClipboardFormats.loadAllFromInput(player, arguments[0], ClipboardFormats.findByAlias("fast"), false);
            if (multi == null) {
                multi = ClipboardFormats.loadAllFromInput(player, arguments[0], ClipboardFormats.findByAlias("mcedit"), false);
            }
            if (multi == null) {
                multi = ClipboardFormats.loadAllFromInput(player, arguments[0], ClipboardFormats.findByAlias("sponge"), false);
            }
            if (multi == null) {
                throw new InputParseException(Caption.of("fawe.error.parse.no-clipboard-source", arguments[0]));
            }
            clipboards = multi.getHolders();
        }
        return new RandomFullClipboardPattern(clipboards, rotate, flip);
    } catch (IOException e) {
        throw new InputParseException(TextComponent.of(e.getMessage()), e);
    }
}
Also used : RandomFullClipboardPattern(com.fastasyncworldedit.core.function.pattern.RandomFullClipboardPattern) InputParseException(com.sk89q.worldedit.extension.input.InputParseException) MultiClipboardHolder(com.fastasyncworldedit.core.extent.clipboard.MultiClipboardHolder) ClipboardHolder(com.sk89q.worldedit.session.ClipboardHolder) MultiClipboardHolder(com.fastasyncworldedit.core.extent.clipboard.MultiClipboardHolder) Actor(com.sk89q.worldedit.extension.platform.Actor) IOException(java.io.IOException)

Example 3 with MultiClipboardHolder

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

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

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

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