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