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