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.generator.HybridPlotManager;
024import com.plotsquared.core.generator.HybridUtils;
025import com.plotsquared.core.location.Location;
026import com.plotsquared.core.player.PlotPlayer;
027import com.plotsquared.core.plot.Plot;
028import com.plotsquared.core.plot.PlotArea;
029import com.plotsquared.core.plot.PlotManager;
030import com.plotsquared.core.queue.QueueCoordinator;
031import net.kyori.adventure.text.minimessage.Template;
032import org.checkerframework.checker.nullness.qual.NonNull;
033
034import java.util.Arrays;
035import java.util.Collection;
036import java.util.Locale;
037import java.util.stream.Collectors;
038import java.util.stream.Stream;
039
040@CommandDeclaration(command = "debugroadregen",
041        usage = DebugRoadRegen.USAGE,
042        requiredType = RequiredType.NONE,
043        category = CommandCategory.DEBUG,
044        permission = "plots.debugroadregen")
045public class DebugRoadRegen extends SubCommand {
046
047    public static final String USAGE = "/plot debugroadregen <plot | region [height]>";
048
049    private final HybridUtils hybridUtils;
050
051    @Inject
052    public DebugRoadRegen(final @NonNull HybridUtils hybridUtils) {
053        this.hybridUtils = hybridUtils;
054    }
055
056    @Override
057    public boolean onCommand(PlotPlayer<?> player, String[] args) {
058        Location location = player.getLocation();
059        Plot plot = location.getPlotAbs();
060        if (args.length < 1) {
061            player.sendMessage(
062                    TranslatableCaption.of("commandconfig.command_syntax"),
063                    Template.of("value", DebugRoadRegen.USAGE)
064            );
065            return false;
066        }
067
068        PlotArea area = player.getPlotAreaAbs();
069        check(area, TranslatableCaption.of("errors.not_in_plot_world"));
070        if (plot.getVolume() > Integer.MAX_VALUE) {
071            player.sendMessage(TranslatableCaption.of("schematics.schematic_too_large"));
072            return false;
073        }
074        String kind = args[0].toLowerCase();
075        switch (kind) {
076            case "plot":
077                return regenPlot(player);
078            case "region":
079                return regenRegion(player, Arrays.copyOfRange(args, 1, args.length));
080            default:
081                player.sendMessage(
082                        TranslatableCaption.of("commandconfig.command_syntax"),
083                        Template.of("value", DebugRoadRegen.USAGE)
084                );
085                return false;
086        }
087    }
088
089    public boolean regenPlot(PlotPlayer<?> player) {
090        Location location = player.getLocation();
091        PlotArea area = location.getPlotArea();
092        if (area == null) {
093            player.sendMessage(TranslatableCaption.of("errors.not_in_plot_world"));
094        }
095        Plot plot = player.getCurrentPlot();
096        if (plot == null) {
097            player.sendMessage(TranslatableCaption.of("errors.not_in_plot"));
098        } else if (plot.isMerged()) {
099            player.sendMessage(TranslatableCaption.of("debug.requires_unmerged"));
100        } else {
101            PlotManager manager = area.getPlotManager();
102            QueueCoordinator queue = area.getQueue();
103            queue.setCompleteTask(() -> {
104                player.sendMessage(
105                        TranslatableCaption.of("debugroadregen.regen_done"),
106                        Template.of("value", plot.getId().toString())
107                );
108                player.sendMessage(
109                        TranslatableCaption.of("debugroadregen.regen_all"),
110                        Template.of("value", "/plot regenallroads")
111                );
112            });
113            manager.createRoadEast(plot, queue);
114            manager.createRoadSouth(plot, queue);
115            manager.createRoadSouthEast(plot, queue);
116            queue.enqueue();
117        }
118        return true;
119    }
120
121    public boolean regenRegion(PlotPlayer<?> player, String[] args) {
122        int height = 0;
123        if (args.length == 1) {
124            try {
125                height = Integer.parseInt(args[0]);
126            } catch (NumberFormatException ignored) {
127                player.sendMessage(
128                        TranslatableCaption.of("invalid.not_valid_number"),
129                        Template.of("value", "0, 256")
130                );
131                player.sendMessage(
132                        TranslatableCaption.of("commandconfig.command_syntax"),
133                        Template.of("value", DebugRoadRegen.USAGE)
134                );
135                return false;
136            }
137        } else if (args.length != 0) {
138            player.sendMessage(
139                    TranslatableCaption.of("commandconfig.command_syntax"),
140                    Template.of("value", DebugRoadRegen.USAGE)
141            );
142            return false;
143        }
144
145        Location location = player.getLocation();
146        PlotArea area = location.getPlotArea();
147        if (area == null) {
148            player.sendMessage(TranslatableCaption.of("errors.not_in_plot_world"));
149        }
150        Plot plot = player.getCurrentPlot();
151        PlotManager manager = area.getPlotManager();
152        if (!(manager instanceof HybridPlotManager)) {
153            player.sendMessage(TranslatableCaption.of("errors.invalid_plot_world"));
154            return true;
155        }
156        player.sendMessage(
157                TranslatableCaption.of("debugroadregen.schematic"),
158                Template.of("command", "/plot createroadschematic")
159        );
160        player.sendMessage(
161                TranslatableCaption.of("debugroadregen.regenallroads"),
162                Template.of("command", "/plot regenallroads")
163        );
164        boolean result = this.hybridUtils.scheduleSingleRegionRoadUpdate(plot, height);
165        if (!result) {
166            player.sendMessage(TranslatableCaption.of("debugexec.mass_schematic_update_in_progress"));
167            return false;
168        }
169        return true;
170    }
171
172    @Override
173    public Collection<Command> tab(final PlotPlayer<?> player, String[] args, boolean space) {
174        return Stream.of("plot", "region")
175                .filter(value -> value.startsWith(args[0].toLowerCase(Locale.ENGLISH)))
176                .map(value -> new Command(null, false, value, "plots.debugroadregen", RequiredType.NONE, null) {
177                }).collect(Collectors.toList());
178    }
179
180}