Search in sources :

Example 1 with RunnableVal

use of com.plotsquared.core.util.task.RunnableVal in project PlotSquared by IntellectualSites.

the class WorldUtil method upload.

public void upload(@NonNull final Plot plot, @Nullable final UUID uuid, @Nullable final String file, @NonNull final RunnableVal<URL> whenDone) {
    plot.getHome(home -> SchematicHandler.upload(uuid, file, "zip", new RunnableVal<>() {

        @Override
        public void run(OutputStream output) {
            try (final ZipOutputStream zos = new ZipOutputStream(output)) {
                File dat = getDat(plot.getWorldName());
                Location spawn = getSpawn(plot.getWorldName());
                if (dat != null) {
                    ZipEntry ze = new ZipEntry("world" + File.separator + dat.getName());
                    zos.putNextEntry(ze);
                    try (NBTInputStream nis = new NBTInputStream(new GZIPInputStream(new FileInputStream(dat)))) {
                        Map<String, Tag> tag = ((CompoundTag) nis.readNamedTag().getTag()).getValue();
                        Map<String, Tag> newMap = new HashMap<>();
                        for (Map.Entry<String, Tag> entry : tag.entrySet()) {
                            if (!entry.getKey().equals("Data")) {
                                newMap.put(entry.getKey(), entry.getValue());
                                continue;
                            }
                            Map<String, Tag> data = new HashMap<>(((CompoundTag) entry.getValue()).getValue());
                            data.put("SpawnX", new IntTag(home.getX()));
                            data.put("SpawnY", new IntTag(home.getY()));
                            data.put("SpawnZ", new IntTag(home.getZ()));
                            newMap.put("Data", new CompoundTag(data));
                        }
                        try (ByteArrayOutputStream baos = new ByteArrayOutputStream()) {
                            try (NBTOutputStream out = new NBTOutputStream(new GZIPOutputStream(baos, true))) {
                                // TODO Find what this should be called
                                out.writeNamedTag("Schematic????", new CompoundTag(newMap));
                            }
                            zos.write(baos.toByteArray());
                        }
                    }
                }
                setSpawn(spawn);
                byte[] buffer = new byte[1024];
                Set<BlockVector2> added = new HashSet<>();
                for (Plot current : plot.getConnectedPlots()) {
                    Location bot = current.getBottomAbs();
                    Location top = current.getTopAbs();
                    int brx = bot.getX() >> 9;
                    int brz = bot.getZ() >> 9;
                    int trx = top.getX() >> 9;
                    int trz = top.getZ() >> 9;
                    Set<BlockVector2> files = getChunkChunks(bot.getWorldName());
                    for (BlockVector2 mca : files) {
                        if (mca.getX() >= brx && mca.getX() <= trx && mca.getZ() >= brz && mca.getZ() <= trz && !added.contains(mca)) {
                            final File file = getMcr(plot.getWorldName(), mca.getX(), mca.getZ());
                            if (file != null) {
                                // final String name = "r." + (x - cx) + "." + (z - cz) + ".mca";
                                String name = file.getName();
                                final ZipEntry ze = new ZipEntry("world" + File.separator + "region" + File.separator + name);
                                zos.putNextEntry(ze);
                                added.add(mca);
                                try (FileInputStream in = new FileInputStream(file)) {
                                    int len;
                                    while ((len = in.read(buffer)) > 0) {
                                        zos.write(buffer, 0, len);
                                    }
                                }
                                zos.closeEntry();
                            }
                        }
                    }
                }
                zos.closeEntry();
                zos.flush();
                zos.finish();
            } catch (IOException e) {
                e.printStackTrace();
            }
        }
    }, whenDone));
}
Also used : HashMap(java.util.HashMap) ZipOutputStream(java.util.zip.ZipOutputStream) ByteArrayOutputStream(java.io.ByteArrayOutputStream) OutputStream(java.io.OutputStream) NBTOutputStream(com.sk89q.jnbt.NBTOutputStream) GZIPOutputStream(java.util.zip.GZIPOutputStream) ZipEntry(java.util.zip.ZipEntry) GZIPInputStream(java.util.zip.GZIPInputStream) GZIPOutputStream(java.util.zip.GZIPOutputStream) CompoundTag(com.sk89q.jnbt.CompoundTag) IntTag(com.sk89q.jnbt.IntTag) HashSet(java.util.HashSet) Plot(com.plotsquared.core.plot.Plot) ByteArrayOutputStream(java.io.ByteArrayOutputStream) IOException(java.io.IOException) NBTOutputStream(com.sk89q.jnbt.NBTOutputStream) BlockVector2(com.sk89q.worldedit.math.BlockVector2) FileInputStream(java.io.FileInputStream) ZipOutputStream(java.util.zip.ZipOutputStream) RunnableVal(com.plotsquared.core.util.task.RunnableVal) NBTInputStream(com.sk89q.jnbt.NBTInputStream) IntTag(com.sk89q.jnbt.IntTag) CompoundTag(com.sk89q.jnbt.CompoundTag) Tag(com.sk89q.jnbt.Tag) File(java.io.File) HashMap(java.util.HashMap) Map(java.util.Map) Location(com.plotsquared.core.location.Location)

Example 2 with RunnableVal

use of com.plotsquared.core.util.task.RunnableVal in project PlotSquared by IntellectualSites.

the class Save method onCommand.

@Override
public boolean onCommand(final PlotPlayer<?> player, final String[] args) {
    final String world = player.getLocation().getWorldName();
    if (!this.plotAreaManager.hasPlotArea(world)) {
        player.sendMessage(TranslatableCaption.of("errors.not_in_plot_world"));
        return false;
    }
    final Plot plot = player.getCurrentPlot();
    if (plot == null) {
        player.sendMessage(TranslatableCaption.of("errors.not_in_plot"));
        return false;
    }
    if (!plot.hasOwner()) {
        player.sendMessage(TranslatableCaption.of("info.plot_unowned"));
        return false;
    }
    if (plot.getVolume() > Integer.MAX_VALUE) {
        player.sendMessage(TranslatableCaption.of("schematics.schematic_too_large"));
        return false;
    }
    if (!plot.isOwner(player.getUUID()) && !Permissions.hasPermission(player, Permission.PERMISSION_ADMIN_COMMAND_SAVE)) {
        player.sendMessage(TranslatableCaption.of("permission.no_plot_perms"));
        return false;
    }
    if (plot.getRunning() > 0) {
        player.sendMessage(TranslatableCaption.of("errors.wait_for_timer"));
        return false;
    }
    plot.addRunning();
    this.schematicHandler.getCompoundTag(plot).whenComplete((compoundTag, throwable) -> {
        TaskManager.runTaskAsync(() -> {
            String time = (System.currentTimeMillis() / 1000) + "";
            Location[] corners = plot.getCorners();
            corners[0] = corners[0].withY(plot.getArea().getMinBuildHeight());
            corners[1] = corners[1].withY(plot.getArea().getMaxBuildHeight());
            int size = (corners[1].getX() - corners[0].getX()) + 1;
            PlotId id = plot.getId();
            String world1 = plot.getArea().toString().replaceAll(";", "-").replaceAll("[^A-Za-z0-9]", "");
            final String file = time + '_' + world1 + '_' + id.getX() + '_' + id.getY() + '_' + size;
            UUID uuid = player.getUUID();
            schematicHandler.upload(compoundTag, uuid, file, new RunnableVal<>() {

                @Override
                public void run(URL url) {
                    plot.removeRunning();
                    if (url == null) {
                        player.sendMessage(TranslatableCaption.of("backups.backup_save_failed"));
                        return;
                    }
                    player.sendMessage(TranslatableCaption.of("web.save_success"));
                    player.sendMessage(TranslatableCaption.of("errors.deprecated_commands"), Template.of("replacement", "/plot download"));
                    try (final MetaDataAccess<List<String>> schematicAccess = player.accessTemporaryMetaData(PlayerMetaDataKeys.TEMPORARY_SCHEMATICS)) {
                        schematicAccess.get().ifPresent(schematics -> schematics.add(file + ".schem"));
                    }
                }
            });
        });
    });
    return true;
}
Also used : Location(com.plotsquared.core.location.Location) MetaDataAccess(com.plotsquared.core.player.MetaDataAccess) Permissions(com.plotsquared.core.util.Permissions) NonNull(org.checkerframework.checker.nullness.qual.NonNull) Plot(com.plotsquared.core.plot.Plot) PlotAreaManager(com.plotsquared.core.plot.world.PlotAreaManager) RunnableVal(com.plotsquared.core.util.task.RunnableVal) URL(java.net.URL) Inject(com.google.inject.Inject) TaskManager(com.plotsquared.core.util.task.TaskManager) PlotId(com.plotsquared.core.plot.PlotId) Permission(com.plotsquared.core.permissions.Permission) UUID(java.util.UUID) List(java.util.List) PlotPlayer(com.plotsquared.core.player.PlotPlayer) SchematicHandler(com.plotsquared.core.util.SchematicHandler) TranslatableCaption(com.plotsquared.core.configuration.caption.TranslatableCaption) PlayerMetaDataKeys(com.plotsquared.core.player.PlayerMetaDataKeys) Template(net.kyori.adventure.text.minimessage.Template) PlotId(com.plotsquared.core.plot.PlotId) Plot(com.plotsquared.core.plot.Plot) UUID(java.util.UUID) MetaDataAccess(com.plotsquared.core.player.MetaDataAccess) URL(java.net.URL) Location(com.plotsquared.core.location.Location)

Example 3 with RunnableVal

use of com.plotsquared.core.util.task.RunnableVal in project PlotSquared by IntellectualSites.

the class Download method onCommand.

@Override
public boolean onCommand(final PlotPlayer<?> player, String[] args) {
    String world = player.getLocation().getWorldName();
    if (!this.plotAreaManager.hasPlotArea(world)) {
        player.sendMessage(TranslatableCaption.of("errors.not_in_plot_world"));
        return false;
    }
    final Plot plot = player.getCurrentPlot();
    if (plot == null) {
        player.sendMessage(TranslatableCaption.of("errors.not_in_plot"));
        return false;
    }
    if (!plot.hasOwner()) {
        player.sendMessage(TranslatableCaption.of("info.plot_unowned"));
        return false;
    }
    if ((Settings.Done.REQUIRED_FOR_DOWNLOAD && !DoneFlag.isDone(plot)) && !Permissions.hasPermission(player, Permission.PERMISSION_ADMIN_COMMAND_DOWNLOAD)) {
        player.sendMessage(TranslatableCaption.of("done.done_not_done"));
        return false;
    }
    if (!plot.isOwner(player.getUUID()) && !Permissions.hasPermission(player, Permission.PERMISSION_ADMIN.toString())) {
        player.sendMessage(TranslatableCaption.of("permission.no_plot_perms"));
        return false;
    }
    if (plot.getRunning() > 0) {
        player.sendMessage(TranslatableCaption.of("errors.wait_for_timer"));
        return false;
    }
    if (args.length == 0 || (args.length == 1 && StringMan.isEqualIgnoreCaseToAny(args[0], "sch", "schem", "schematic"))) {
        if (plot.getVolume() > Integer.MAX_VALUE) {
            player.sendMessage(TranslatableCaption.of("schematics.schematic_too_large"));
            return false;
        }
        plot.addRunning();
        upload(player, plot);
    } else if (args.length == 1 && StringMan.isEqualIgnoreCaseToAny(args[0], "mcr", "world", "mca")) {
        if (!Permissions.hasPermission(player, Permission.PERMISSION_DOWNLOAD_WORLD)) {
            player.sendMessage(TranslatableCaption.of("permission.no_permission"), Template.of("node", Permission.PERMISSION_DOWNLOAD_WORLD.toString()));
            return false;
        }
        player.sendMessage(TranslatableCaption.of("schematics.mca_file_size"));
        plot.addRunning();
        this.worldUtil.saveWorld(world);
        this.worldUtil.upload(plot, null, null, new RunnableVal<>() {

            @Override
            public void run(URL url) {
                plot.removeRunning();
                if (url == null) {
                    player.sendMessage(TranslatableCaption.of("web.generating_link_failed"), Template.of("plot", plot.getId().toString()));
                    return;
                }
                player.sendMessage(TranslatableCaption.of("web.generation_link_success_legacy_world"), Template.of("url", url.toString()));
            }
        });
    } else {
        sendUsage(player);
        return false;
    }
    player.sendMessage(TranslatableCaption.of("web.generating_link"), Template.of("plot", plot.getId().toString()));
    return true;
}
Also used : Plot(com.plotsquared.core.plot.Plot) RunnableVal(com.plotsquared.core.util.task.RunnableVal) URL(java.net.URL)

Example 4 with RunnableVal

use of com.plotsquared.core.util.task.RunnableVal in project PlotSquared by IntellectualSites.

the class Inbox method onCommand.

@Override
public boolean onCommand(final PlotPlayer<?> player, String[] args) {
    final Plot plot = player.getCurrentPlot();
    if (plot == null) {
        player.sendMessage(TranslatableCaption.of("errors.not_in_plot"));
        return false;
    }
    if (!plot.hasOwner()) {
        player.sendMessage(TranslatableCaption.of("info.plot_unowned"));
        return false;
    }
    if (args.length == 0) {
        sendUsage(player);
        for (final CommentInbox inbox : CommentManager.inboxes.values()) {
            if (inbox.canRead(plot, player)) {
                if (!inbox.getComments(plot, new RunnableVal<>() {

                    @Override
                    public void run(List<PlotComment> value) {
                        if (value != null) {
                            int total = 0;
                            int unread = 0;
                            for (PlotComment comment : value) {
                                total++;
                                if (comment.timestamp > CommentManager.getTimestamp(player, inbox.toString())) {
                                    unread++;
                                }
                            }
                            if (total != 0) {
                                player.sendMessage(TranslatableCaption.of("comment.inbox_item"), Template.of("value", inbox + " (" + total + '/' + unread + ')'));
                                return;
                            }
                        }
                        player.sendMessage(TranslatableCaption.of("comment.inbox_item"), Template.of("value", inbox.toString()));
                    }
                })) {
                    player.sendMessage(TranslatableCaption.of("comment.inbox_item"), Template.of("value", inbox.toString()));
                }
            }
        }
        return false;
    }
    final CommentInbox inbox = CommentManager.inboxes.get(args[0].toLowerCase());
    if (inbox == null) {
        player.sendMessage(TranslatableCaption.of("comment.invalid_inbox"), Template.of("list", StringMan.join(CommentManager.inboxes.keySet(), ", ")));
        return false;
    }
    final MetaDataKey<Long> metaDataKey = MetaDataKey.of(String.format("inbox:%s", inbox), new TypeLiteral<>() {
    });
    try (final MetaDataAccess<Long> metaDataAccess = player.accessTemporaryMetaData(metaDataKey)) {
        metaDataAccess.set(System.currentTimeMillis());
    }
    final int page;
    if (args.length > 1) {
        switch(args[1].toLowerCase()) {
            case "delete":
                if (!inbox.canModify(plot, player)) {
                    player.sendMessage(TranslatableCaption.of("comment.no_perm_inbox_modify"));
                    return false;
                }
                if (args.length != 3) {
                    player.sendMessage(TranslatableCaption.of("commandconfig.command_syntax"), Template.of("value", "/plot inbox " + inbox + " delete <index>"));
                    return true;
                }
                final int index;
                try {
                    index = Integer.parseInt(args[2]);
                    if (index < 1) {
                        player.sendMessage(TranslatableCaption.of("comment.not_valid_inbox_index"), Templates.of("number", index));
                        return false;
                    }
                } catch (NumberFormatException ignored) {
                    player.sendMessage(TranslatableCaption.of("commandconfig.command_syntax"), Template.of("value", "/plot inbox " + inbox + " delete <index>"));
                    return false;
                }
                if (!inbox.getComments(plot, new RunnableVal<>() {

                    @Override
                    public void run(List<PlotComment> value) {
                        if (index > value.size()) {
                            player.sendMessage(TranslatableCaption.of("comment.not_valid_inbox_index"), Templates.of("number", index));
                            return;
                        }
                        PlotComment comment = value.get(index - 1);
                        inbox.removeComment(plot, comment);
                        boolean success = plot.getPlotCommentContainer().removeComment(comment);
                        if (success) {
                            player.sendMessage(TranslatableCaption.of("comment.comment_removed_success"), Template.of("value", comment.comment));
                        } else {
                            player.sendMessage(TranslatableCaption.of("comment.comment_removed_failure"));
                        }
                    }
                })) {
                    player.sendMessage(TranslatableCaption.of("errors.not_in_plot"));
                    return false;
                }
                return true;
            case "clear":
                if (!inbox.canModify(plot, player)) {
                    player.sendMessage(TranslatableCaption.of("comment.no_perm_inbox_modify"));
                }
                inbox.clearInbox(plot);
                List<PlotComment> comments = plot.getPlotCommentContainer().getComments(inbox.toString());
                if (!comments.isEmpty()) {
                    player.sendMessage(TranslatableCaption.of("comment.comment_removed_success"), Template.of("value", String.valueOf(comments)));
                    plot.getPlotCommentContainer().removeComments(comments);
                }
                return true;
            default:
                try {
                    page = Integer.parseInt(args[1]);
                } catch (NumberFormatException ignored) {
                    sendUsage(player);
                    return false;
                }
        }
    } else {
        page = 1;
    }
    if (!inbox.canRead(plot, player)) {
        player.sendMessage(TranslatableCaption.of("comment.no_perm_inbox"));
        return false;
    }
    if (!inbox.getComments(plot, new RunnableVal<>() {

        @Override
        public void run(List<PlotComment> value) {
            displayComments(player, value, page);
        }
    })) {
        player.sendMessage(TranslatableCaption.of("info.plot_unowned"));
        return false;
    }
    return true;
}
Also used : Plot(com.plotsquared.core.plot.Plot) RunnableVal(com.plotsquared.core.util.task.RunnableVal) PlotComment(com.plotsquared.core.plot.comment.PlotComment) CommentInbox(com.plotsquared.core.plot.comment.CommentInbox) List(java.util.List) LinkedList(java.util.LinkedList)

Example 5 with RunnableVal

use of com.plotsquared.core.util.task.RunnableVal in project PlotSquared by IntellectualSites.

the class Auto method claimSingle.

private void claimSingle(@NonNull final PlotPlayer<?> player, @NonNull final Plot plot, @NonNull final PlotArea plotArea, @Nullable final String schematic) {
    try (final MetaDataAccess<Boolean> metaDataAccess = player.accessTemporaryMetaData(PlayerMetaDataKeys.TEMPORARY_AUTO)) {
        metaDataAccess.set(true);
    }
    plot.setOwnerAbs(player.getUUID());
    final RunnableVal<Plot> runnableVal = new RunnableVal<>() {

        {
            this.value = plot;
        }

        @Override
        public void run(final Plot plot) {
            try {
                TaskManager.getPlatformImplementation().sync(new AutoClaimFinishTask(player, plot, plotArea, schematic, PlotSquared.get().getEventDispatcher()));
            } catch (final Exception e) {
                e.printStackTrace();
            }
        }
    };
    DBFunc.createPlotSafe(plot, runnableVal, () -> claimSingle(player, plot, plotArea, schematic));
}
Also used : Plot(com.plotsquared.core.plot.Plot) RunnableVal(com.plotsquared.core.util.task.RunnableVal) AutoClaimFinishTask(com.plotsquared.core.util.task.AutoClaimFinishTask)

Aggregations

RunnableVal (com.plotsquared.core.util.task.RunnableVal)9 Plot (com.plotsquared.core.plot.Plot)7 Location (com.plotsquared.core.location.Location)6 QueueCoordinator (com.plotsquared.core.queue.QueueCoordinator)3 Inject (com.google.inject.Inject)2 TranslatableCaption (com.plotsquared.core.configuration.caption.TranslatableCaption)2 Permission (com.plotsquared.core.permissions.Permission)2 PlotPlayer (com.plotsquared.core.player.PlotPlayer)2 Permissions (com.plotsquared.core.util.Permissions)2 BlockVector2 (com.sk89q.worldedit.math.BlockVector2)2 CuboidRegion (com.sk89q.worldedit.regions.CuboidRegion)2 URL (java.net.URL)2 HashSet (java.util.HashSet)2 List (java.util.List)2 ImmutableSet (com.google.common.collect.ImmutableSet)1 Lists (com.google.common.collect.Lists)1 Sets (com.google.common.collect.Sets)1 PlotSquared (com.plotsquared.core.PlotSquared)1 Like (com.plotsquared.core.command.Like)1 Settings (com.plotsquared.core.configuration.Settings)1