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