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.common.base.Charsets;
022import com.google.inject.Inject;
023import com.plotsquared.core.PlotSquared;
024import com.plotsquared.core.configuration.caption.TranslatableCaption;
025import com.plotsquared.core.player.PlotPlayer;
026import com.plotsquared.core.plot.PlotId;
027import com.plotsquared.core.plot.world.PlotAreaManager;
028import com.plotsquared.core.plot.world.SinglePlotArea;
029import com.plotsquared.core.plot.world.SinglePlotAreaManager;
030import com.plotsquared.core.util.WorldUtil;
031import com.plotsquared.core.util.task.RunnableVal2;
032import com.plotsquared.core.util.task.RunnableVal3;
033import org.checkerframework.checker.nullness.qual.NonNull;
034
035import java.io.File;
036import java.util.UUID;
037import java.util.concurrent.CompletableFuture;
038
039@CommandDeclaration(command = "debugimportworlds",
040        permission = "plots.admin",
041        requiredType = RequiredType.CONSOLE,
042        category = CommandCategory.TELEPORT)
043public class DebugImportWorlds extends Command {
044
045    private final PlotAreaManager plotAreaManager;
046    private final WorldUtil worldUtil;
047
048    @Inject
049    public DebugImportWorlds(
050            final @NonNull PlotAreaManager plotAreaManager,
051            final @NonNull WorldUtil worldUtil
052    ) {
053        super(MainCommand.getInstance(), true);
054        this.plotAreaManager = plotAreaManager;
055        this.worldUtil = worldUtil;
056    }
057
058    @Override
059    public CompletableFuture<Boolean> execute(
060            PlotPlayer<?> player, String[] args,
061            RunnableVal3<Command, Runnable, Runnable> confirm,
062            RunnableVal2<Command, CommandResult> whenDone
063    ) throws CommandException {
064        // UUID.nameUUIDFromBytes(("OfflinePlayer:" + player.getName()).getBytes(Charsets.UTF_8))
065        if (!(this.plotAreaManager instanceof SinglePlotAreaManager)) {
066            player.sendMessage(TranslatableCaption.of("debugimportworlds.single_plot_area"));
067            return CompletableFuture.completedFuture(false);
068        }
069        SinglePlotArea area = ((SinglePlotAreaManager) this.plotAreaManager).getArea();
070        PlotId id = PlotId.of(0, 0);
071        File container = PlotSquared.platform().worldContainer();
072        if (container.equals(new File("."))) {
073            player.sendMessage(TranslatableCaption.of("debugimportworlds.world_container"));
074            return CompletableFuture.completedFuture(false);
075        }
076        for (File folder : container.listFiles()) {
077            String name = folder.getName();
078            if (!this.worldUtil.isWorld(name) && PlotId.fromStringOrNull(name) == null) {
079                UUID uuid;
080                if (name.length() > 16) {
081                    uuid = UUID.fromString(name);
082                } else {
083                    player.sendMessage(TranslatableCaption.of("players.fetching_player"));
084                    uuid = PlotSquared.get().getImpromptuUUIDPipeline().getSingle(name, 60000L);
085                }
086                if (uuid == null) {
087                    uuid =
088                            UUID.nameUUIDFromBytes(("OfflinePlayer:" + name).getBytes(Charsets.UTF_8));
089                }
090                while (new File(container, id.toCommaSeparatedString()).exists()) {
091                    id = id.getNextId();
092                }
093                File newDir = new File(container, id.toCommaSeparatedString());
094                if (folder.renameTo(newDir)) {
095                    area.getPlot(id).setOwner(uuid);
096                }
097            }
098        }
099        player.sendMessage(TranslatableCaption.of("players.done"));
100        return CompletableFuture.completedFuture(true);
101    }
102
103}