001/*
002 * PlotSquared, a land and world management plugin for Minecraft.
003 * Copyright (C) IntellectualSites <https://intellectualsites.com>
004 * Copyright (C) IntellectualSites team and contributors
005 *
006 * This program is free software: you can redistribute it and/or modify
007 * it under the terms of the GNU General Public License as published by
008 * the Free Software Foundation, either version 3 of the License, or
009 * (at your option) any later version.
010 *
011 * This program is distributed in the hope that it will be useful,
012 * but WITHOUT ANY WARRANTY; without even the implied warranty of
013 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
014 * GNU General Public License for more details.
015 *
016 * You should have received a copy of the GNU General Public License
017 * along with this program.  If not, see <https://www.gnu.org/licenses/>.
018 */
019package com.plotsquared.core.command;
020
021import com.google.inject.Inject;
022import com.plotsquared.core.configuration.caption.TranslatableCaption;
023import com.plotsquared.core.location.Location;
024import com.plotsquared.core.permissions.Permission;
025import com.plotsquared.core.player.MetaDataAccess;
026import com.plotsquared.core.player.PlayerMetaDataKeys;
027import com.plotsquared.core.player.PlotPlayer;
028import com.plotsquared.core.plot.Plot;
029import com.plotsquared.core.plot.PlotId;
030import com.plotsquared.core.plot.world.PlotAreaManager;
031import com.plotsquared.core.util.SchematicHandler;
032import com.plotsquared.core.util.task.RunnableVal;
033import com.plotsquared.core.util.task.TaskManager;
034import net.kyori.adventure.text.minimessage.Template;
035import org.checkerframework.checker.nullness.qual.NonNull;
036
037import java.net.URL;
038import java.util.List;
039import java.util.UUID;
040
041/**
042 * @deprecated In favor of "/plot download" (Arkitektonika) and scheduled
043 *         for removal within the next major release.
044 */
045@Deprecated(forRemoval = true, since = "6.0.9")
046@CommandDeclaration(command = "save",
047        category = CommandCategory.SCHEMATIC,
048        requiredType = RequiredType.NONE,
049        permission = "plots.save")
050public class Save extends SubCommand {
051
052    private final PlotAreaManager plotAreaManager;
053    private final SchematicHandler schematicHandler;
054
055    @Inject
056    public Save(
057            final @NonNull PlotAreaManager plotAreaManager,
058            final @NonNull SchematicHandler schematicHandler
059    ) {
060        this.plotAreaManager = plotAreaManager;
061        this.schematicHandler = schematicHandler;
062    }
063
064    @Override
065    public boolean onCommand(final PlotPlayer<?> player, final String[] args) {
066        final String world = player.getLocation().getWorldName();
067        if (!this.plotAreaManager.hasPlotArea(world)) {
068            player.sendMessage(TranslatableCaption.of("errors.not_in_plot_world"));
069            return false;
070        }
071        final Plot plot = player.getCurrentPlot();
072        if (plot == null) {
073            player.sendMessage(TranslatableCaption.of("errors.not_in_plot"));
074            return false;
075        }
076        if (!plot.hasOwner()) {
077            player.sendMessage(TranslatableCaption.of("info.plot_unowned"));
078            return false;
079        }
080        if (plot.getVolume() > Integer.MAX_VALUE) {
081            player.sendMessage(TranslatableCaption.of("schematics.schematic_too_large"));
082            return false;
083        }
084        if (!plot.isOwner(player.getUUID()) && !player.hasPermission(Permission.PERMISSION_ADMIN_COMMAND_SAVE)) {
085            player.sendMessage(TranslatableCaption.of("permission.no_plot_perms"));
086            return false;
087        }
088        if (plot.getRunning() > 0) {
089            player.sendMessage(TranslatableCaption.of("errors.wait_for_timer"));
090            return false;
091        }
092        plot.addRunning();
093        this.schematicHandler.getCompoundTag(plot)
094                .whenComplete((compoundTag, throwable) -> {
095                    TaskManager.runTaskAsync(() -> {
096                        String time = (System.currentTimeMillis() / 1000) + "";
097                        Location[] corners = plot.getCorners();
098                        corners[0] = corners[0].withY(plot.getArea().getMinBuildHeight());
099                        corners[1] = corners[1].withY(plot.getArea().getMaxBuildHeight());
100                        int size = (corners[1].getX() - corners[0].getX()) + 1;
101                        PlotId id = plot.getId();
102                        String world1 = plot.getArea().toString().replaceAll(";", "-")
103                                .replaceAll("[^A-Za-z0-9]", "");
104                        final String file = time + '_' + world1 + '_' + id.getX() + '_' + id.getY() + '_' + size;
105                        UUID uuid = player.getUUID();
106                        schematicHandler.upload(compoundTag, uuid, file, new RunnableVal<>() {
107                            @Override
108                            public void run(URL url) {
109                                plot.removeRunning();
110                                if (url == null) {
111                                    player.sendMessage(TranslatableCaption.of("backups.backup_save_failed"));
112                                    return;
113                                }
114                                player.sendMessage(TranslatableCaption.of("web.save_success"));
115                                player.sendMessage(
116                                        TranslatableCaption.of("errors.deprecated_commands"),
117                                        Template.of("replacement", "/plot download")
118                                );
119                                try (final MetaDataAccess<List<String>> schematicAccess =
120                                             player.accessTemporaryMetaData(PlayerMetaDataKeys.TEMPORARY_SCHEMATICS)) {
121                                    schematicAccess.get().ifPresent(schematics -> schematics.add(file + ".schem"));
122                                }
123                            }
124                        });
125                    });
126                });
127        return true;
128    }
129
130}