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.PlotArea;
027import com.plotsquared.core.plot.PlotManager;
028import com.plotsquared.core.plot.world.PlotAreaManager;
029import net.kyori.adventure.text.minimessage.Template;
030import org.checkerframework.checker.nullness.qual.NonNull;
031
032@CommandDeclaration(command = "regenallroads",
033        aliases = {"rgar"},
034        usage = "/plot regenallroads <world> [height]",
035        category = CommandCategory.ADMINISTRATION,
036        requiredType = RequiredType.CONSOLE,
037        permission = "plots.regenallroads")
038public class RegenAllRoads extends SubCommand {
039
040    private final PlotAreaManager plotAreaManager;
041    private final HybridUtils hybridUtils;
042
043    @Inject
044    public RegenAllRoads(
045            final @NonNull PlotAreaManager plotAreaManager,
046            final @NonNull HybridUtils hybridUtils
047    ) {
048        this.plotAreaManager = plotAreaManager;
049        this.hybridUtils = hybridUtils;
050    }
051
052    @Override
053    public boolean onCommand(PlotPlayer<?> player, String[] args) {
054        int height = 0;
055        if (args.length == 2) {
056            try {
057                height = Integer.parseInt(args[1]);
058            } catch (NumberFormatException ignored) {
059                player.sendMessage(
060                        TranslatableCaption.of("invalid.not_valid_number"),
061                        Template.of("value", "(0, 256)")
062                );
063                player.sendMessage(
064                        TranslatableCaption.of("commandconfig.command_syntax"),
065                        Template.of("value", "/plot regenallroads <world> [height]")
066                );
067                return false;
068            }
069        } else if (args.length != 1) {
070            player.sendMessage(
071                    TranslatableCaption.of("commandconfig.command_syntax"),
072                    Template.of("value", "/plot regenallroads <world> [height]")
073            );
074            return false;
075        }
076        PlotArea area = this.plotAreaManager.getPlotAreaByString(args[0]);
077        if (area == null) {
078            player.sendMessage(
079                    TranslatableCaption.of("errors.not_valid_plot_world"),
080                    Template.of("value", args[0])
081            );
082            return false;
083        }
084        PlotManager manager = area.getPlotManager();
085        if (!(manager instanceof HybridPlotManager)) {
086            player.sendMessage(TranslatableCaption.of("errors.invalid_plot_world"));
087            return false;
088        }
089        player.sendMessage(
090                TranslatableCaption.of("debugroadregen.schematic"),
091                Template.of("command", "/plot createroadschematic")
092        );
093        player.sendMessage(TranslatableCaption.of("debugroadregen.regenallroads_started"));
094        boolean result = this.hybridUtils.scheduleRoadUpdate(area, height);
095        if (!result) {
096            player.sendMessage(TranslatableCaption.of("debugexec.mass_schematic_update_in_progress"));
097            return false;
098        }
099        return true;
100    }
101
102}