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.PlotSquared;
023import com.plotsquared.core.configuration.caption.StaticCaption;
024import com.plotsquared.core.configuration.caption.TranslatableCaption;
025import com.plotsquared.core.generator.GeneratorWrapper;
026import com.plotsquared.core.player.MetaDataAccess;
027import com.plotsquared.core.player.PlayerMetaDataKeys;
028import com.plotsquared.core.player.PlotPlayer;
029import com.plotsquared.core.setup.SetupProcess;
030import com.plotsquared.core.setup.SetupStep;
031import com.plotsquared.core.util.SetupUtils;
032import net.kyori.adventure.text.Component;
033import net.kyori.adventure.text.minimessage.tag.Tag;
034import net.kyori.adventure.text.minimessage.tag.resolver.TagResolver;
035import org.checkerframework.checker.nullness.qual.NonNull;
036
037import java.util.ArrayList;
038import java.util.Collection;
039import java.util.Collections;
040import java.util.List;
041import java.util.Map.Entry;
042
043@CommandDeclaration(command = "setup",
044        permission = "plots.admin.command.setup",
045        usage = "/plot setup",
046        aliases = {"create"},
047        category = CommandCategory.ADMINISTRATION)
048public class Setup extends SubCommand {
049
050    private final SetupUtils setupUtils;
051
052    @Inject
053    public Setup(final @NonNull SetupUtils setupUtils) {
054        this.setupUtils = setupUtils;
055    }
056
057    public void displayGenerators(PlotPlayer<?> player) {
058        StringBuilder message = new StringBuilder();
059        message.append(TranslatableCaption.of("setup.choose_generator").getComponent(player));
060        for (Entry<String, GeneratorWrapper<?>> entry : SetupUtils.generators.entrySet()) {
061            if (entry.getKey().equals(PlotSquared.platform().pluginName())) {
062                message.append("\n<dark_gray> - </dark_gray><dark_green>").append(entry.getKey()).append(
063                        " (Default Generator)</dark_green>");
064            } else if (entry.getValue().isFull()) {
065                message.append("\n<dark_gray> - </dark_gray><gray>").append(entry.getKey()).append(" (Plot Generator)</gray>");
066            } else {
067                message.append("\n<dark_gray> - </dark_gray><gray>").append(entry.getKey()).append(" (Unknown structure)</gray>");
068            }
069        }
070        player.sendMessage(StaticCaption.of(message.toString()));
071    }
072
073    @Override
074    public boolean onCommand(PlotPlayer<?> player, String[] args) {
075        try (final MetaDataAccess<SetupProcess> metaDataAccess =
076                     player.accessTemporaryMetaData(PlayerMetaDataKeys.TEMPORARY_SETUP)) {
077            SetupProcess process = metaDataAccess.get().orElse(null);
078            if (process == null) {
079                if (args.length > 0) {
080                    player.sendMessage(TranslatableCaption.of("setup.setup_not_started"));
081                    player.sendMessage(
082                            TranslatableCaption.of("commandconfig.command_syntax"),
083                            TagResolver.resolver(
084                                    "value",
085                                    Tag.inserting(Component.text("Use /plot setup to start a setup process."))
086                            )
087                    );
088                    return true;
089                }
090                process = new SetupProcess();
091                metaDataAccess.set(process);
092                this.setupUtils.updateGenerators(false);
093                SetupStep step = process.getCurrentStep();
094                step.announce(player);
095                displayGenerators(player);
096                return true;
097            }
098            if (args.length == 1) {
099                if ("back".equalsIgnoreCase(args[0])) {
100                    process.back();
101                    process.getCurrentStep().announce(player);
102                } else if ("cancel".equalsIgnoreCase(args[0])) {
103                    metaDataAccess.remove();
104                    player.sendMessage(TranslatableCaption.of("setup.setup_cancelled"));
105                } else {
106                    process.handleInput(player, args[0]);
107                    if (process.getCurrentStep() != null) {
108                        process.getCurrentStep().announce(player);
109                    }
110                }
111            } else {
112                process.getCurrentStep().announce(player);
113            }
114            return true;
115        }
116    }
117
118    @Override
119    public Collection<Command> tab(PlotPlayer<?> player, String[] args, boolean space) {
120        SetupProcess process;
121        try (final MetaDataAccess<SetupProcess> metaDataAccess =
122                     player.accessTemporaryMetaData(PlayerMetaDataKeys.TEMPORARY_SETUP)) {
123            process = metaDataAccess.get().orElse(null);
124        }
125        if (process == null) {
126            return Collections.emptyList();
127        }
128        // player already provided too many arguments
129        if (args.length > 1 || (args.length == 1 && space)) {
130            return Collections.emptyList();
131        }
132        SetupStep setupStep = process.getCurrentStep();
133        List<Command> commands = new ArrayList<>(setupStep.createSuggestions(player, space ? "" : args[0]));
134        tryAddSubCommand("back", args[0], commands);
135        tryAddSubCommand("cancel", args[0], commands);
136        return commands;
137    }
138
139    private void tryAddSubCommand(String subCommand, String argument, List<Command> suggestions) {
140        if (!argument.isEmpty() && subCommand.startsWith(argument)) {
141            suggestions.add(new Command(null, false, subCommand, "", RequiredType.NONE, null) {
142            });
143        }
144    }
145
146}