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            return false;
100        }
101        Plot plot = player.getCurrentPlot();
102        if (plot == null) {
103            player.sendMessage(TranslatableCaption.of("errors.not_in_plot"));
104        } else if (plot.isMerged()) {
105            player.sendMessage(TranslatableCaption.of("debug.requires_unmerged"));
106        } else {
107            PlotManager manager = area.getPlotManager();
108            QueueCoordinator queue = area.getQueue();
109            queue.setCompleteTask(() -> {
110                player.sendMessage(
111                        TranslatableCaption.of("debugroadregen.regen_done"),
112                        TagResolver.resolver("value", Tag.inserting(Component.text(plot.getId().toString())))
113                );
114                player.sendMessage(
115                        TranslatableCaption.of("debugroadregen.regen_all"),
116                        TagResolver.resolver("value", Tag.inserting(Component.text("/plot regenallroads")))
117                );
118            });
119            manager.createRoadEast(plot, queue);
120            manager.createRoadSouth(plot, queue);
121            manager.createRoadSouthEast(plot, queue);
122            queue.enqueue();
123        }
124        return true;
125    }
126
127    public boolean regenRegion(PlotPlayer<?> player, String[] args) {
128        int height = 0;
129        if (args.length == 1) {
130            try {
131                height = Integer.parseInt(args[0]);
132            } catch (NumberFormatException ignored) {
133                player.sendMessage(
134                        TranslatableCaption.of("invalid.not_valid_number"),
135                        TagResolver.resolver("value", Tag.inserting(Component.text("0, 256")))
136                );
137                player.sendMessage(
138                        TranslatableCaption.of("commandconfig.command_syntax"),
139                        TagResolver.resolver("value", Tag.inserting(Component.text(DebugRoadRegen.USAGE)))
140                );
141                return false;
142            }
143        } else if (args.length != 0) {
144            player.sendMessage(
145                    TranslatableCaption.of("commandconfig.command_syntax"),
146                    TagResolver.resolver("value", Tag.inserting(Component.text(DebugRoadRegen.USAGE)))
147            );
148            return false;
149        }
150
151        Location location = player.getLocation();
152        PlotArea area = location.getPlotArea();
153        if (area == null) {
154            player.sendMessage(TranslatableCaption.of("errors.not_in_plot_world"));
155        }
156        Plot plot = player.getCurrentPlot();
157        PlotManager manager = area.getPlotManager();
158        if (!(manager instanceof HybridPlotManager)) {
159            player.sendMessage(TranslatableCaption.of("errors.invalid_plot_world"));
160            return true;
161        }
162        player.sendMessage(
163                TranslatableCaption.of("debugroadregen.schematic"),
164                TagResolver.resolver("command", Tag.inserting(Component.text("/plot createroadschematic")))
165        );
166        player.sendMessage(
167                TranslatableCaption.of("debugroadregen.regenallroads"),
168                TagResolver.resolver("command", Tag.inserting(Component.text("/plot regenallroads")))
169        );
170        boolean result = this.hybridUtils.scheduleSingleRegionRoadUpdate(plot, height);
171        if (!result) {
172            player.sendMessage(TranslatableCaption.of("debugexec.mass_schematic_update_in_progress"));
173            return false;
174        }
175        return true;
176    }
177
178    @Override
179    public Collection<Command> tab(final PlotPlayer<?> player, String[] args, boolean space) {
180        return Stream.of("plot", "region")
181                .filter(value -> value.startsWith(args[0].toLowerCase(Locale.ENGLISH)))
182                .map(value -> new Command(null, false, value, "plots.debugroadregen", RequiredType.NONE, null) {
183                }).collect(Collectors.toList());
184    }
185
186}