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.plotsquared.core.configuration.caption.StaticCaption;
022import com.plotsquared.core.configuration.caption.TranslatableCaption;
023import com.plotsquared.core.player.PlotPlayer;
024import com.plotsquared.core.util.MathMan;
025import com.plotsquared.core.util.StringMan;
026import com.plotsquared.core.util.helpmenu.HelpMenu;
027import com.plotsquared.core.util.task.RunnableVal2;
028import com.plotsquared.core.util.task.RunnableVal3;
029import net.kyori.adventure.text.Component;
030import net.kyori.adventure.text.TextComponent;
031import net.kyori.adventure.text.minimessage.Template;
032
033import java.util.ArrayList;
034import java.util.Collection;
035import java.util.List;
036import java.util.Locale;
037import java.util.concurrent.CompletableFuture;
038
039@CommandDeclaration(command = "help",
040        aliases = "?",
041        category = CommandCategory.INFO,
042        usage = "help [category | #]",
043        permission = "plots.use")
044public class Help extends Command {
045
046    public Help(Command parent) {
047        super(parent, true);
048    }
049
050    @Override
051    public boolean canExecute(PlotPlayer<?> player, boolean message) {
052        return true;
053    }
054
055    @Override
056    public CompletableFuture<Boolean> execute(
057            PlotPlayer<?> player, String[] args,
058            RunnableVal3<Command, Runnable, Runnable> confirm,
059            RunnableVal2<Command, CommandResult> whenDone
060    ) {
061        switch (args.length) {
062            case 0:
063                return displayHelp(player, null, 0);
064            case 1:
065                if (MathMan.isInteger(args[0])) {
066                    try {
067                        return displayHelp(player, null, Integer.parseInt(args[0]));
068                    } catch (NumberFormatException ignored) {
069                        return displayHelp(player, null, 1);
070                    }
071                } else {
072                    return displayHelp(player, args[0], 1);
073                }
074            case 2:
075                if (MathMan.isInteger(args[1])) {
076                    try {
077                        return displayHelp(player, args[0], Integer.parseInt(args[1]));
078                    } catch (NumberFormatException ignored) {
079                        return displayHelp(player, args[0], 1);
080                    }
081                }
082                return CompletableFuture.completedFuture(false);
083            default:
084                sendUsage(player);
085        }
086        return CompletableFuture.completedFuture(true);
087    }
088
089    public CompletableFuture<Boolean> displayHelp(
090            final PlotPlayer<?> player, final String catRaw,
091            final int page
092    ) {
093        return CompletableFuture.supplyAsync(() -> {
094            String cat = catRaw;
095
096            CommandCategory catEnum = null;
097            if (cat != null) {
098                if (!"all".equalsIgnoreCase(cat)) {
099                    for (CommandCategory c : CommandCategory.values()) {
100                        if (StringMan.isEqualIgnoreCaseToAny(cat, c.name(), c.toString())) {
101                            catEnum = c;
102                            cat = c.name();
103                            break;
104                        }
105                    }
106                    if (catEnum == null) {
107                        cat = null;
108                    }
109                }
110            }
111            if (cat == null && page == 0) {
112                TextComponent.Builder builder = Component.text();
113                builder.append(MINI_MESSAGE.parse(TranslatableCaption.of("help.help_header").getComponent(player)));
114                for (CommandCategory c : CommandCategory.values()) {
115                    if (!c.canAccess(player)) {
116                        continue;
117                    }
118                    builder.append(Component.newline()).append(MINI_MESSAGE
119                            .parse(
120                                    TranslatableCaption.of("help.help_info_item").getComponent(player),
121                                    Template.of("command", "/plot help"),
122                                    Template.of("category", c.name().toLowerCase()),
123                                    Template.of("category_desc", c.getComponent(player))
124                            ));
125                }
126                builder.append(Component.newline()).append(MINI_MESSAGE
127                        .parse(
128                                TranslatableCaption.of("help.help_info_item").getComponent(player),
129                                Template.of("command", "/plot help"),
130                                Template.of("category", "all"),
131                                Template.of("category_desc", "Display all commands")
132                        ));
133                builder.append(Component.newline()).append(MINI_MESSAGE.parse(TranslatableCaption
134                        .of("help.help_footer")
135                        .getComponent(player)));
136                player.sendMessage(StaticCaption.of(MINI_MESSAGE.serialize(builder.asComponent())));
137                return true;
138            }
139            new HelpMenu(player).setCategory(catEnum).getCommands().generateMaxPages().generatePage(
140                            page - 1,
141                            getParent().toString(),
142                            player
143                    )
144                    .render();
145            return true;
146        });
147    }
148
149    @Override
150    public Collection<Command> tab(PlotPlayer<?> player, String[] args, boolean space) {
151        final String argument = args[0].toLowerCase(Locale.ENGLISH);
152        List<Command> result = new ArrayList<>();
153
154        for (final CommandCategory category : CommandCategory.values()) {
155            if (!category.canAccess(player)) {
156                continue;
157            }
158            String name = category.name().toLowerCase();
159            if (!name.startsWith(argument)) {
160                continue;
161            }
162            result.add(new Command(null, false, name, "", RequiredType.NONE, null) {
163            });
164        }
165        // add the category "all"
166        if ("all".startsWith(argument)) {
167            result.add(new Command(null, false, "all", "", RequiredType.NONE, null) {
168            });
169        }
170        return result;
171    }
172
173}