All Downloads are FREE. Search and download functionalities are using the official Maven repository.

com.plotsquared.core.command.Help Maven / Gradle / Ivy

There is a newer version: 6.11.2
Show newest version
/*
 *       _____  _       _    _____                                _
 *      |  __ \| |     | |  / ____|                              | |
 *      | |__) | | ___ | |_| (___   __ _ _   _  __ _ _ __ ___  __| |
 *      |  ___/| |/ _ \| __|\___ \ / _` | | | |/ _` | '__/ _ \/ _` |
 *      | |    | | (_) | |_ ____) | (_| | |_| | (_| | | |  __/ (_| |
 *      |_|    |_|\___/ \__|_____/ \__, |\__,_|\__,_|_|  \___|\__,_|
 *                                    | |
 *                                    |_|
 *            PlotSquared plot management system for Minecraft
 *               Copyright (C) 2014 - 2022 IntellectualSites
 *
 *     This program is free software: you can redistribute it and/or modify
 *     it under the terms of the GNU General Public License as published by
 *     the Free Software Foundation, either version 3 of the License, or
 *     (at your option) any later version.
 *
 *     This program is distributed in the hope that it will be useful,
 *     but WITHOUT ANY WARRANTY; without even the implied warranty of
 *     MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 *     GNU General Public License for more details.
 *
 *     You should have received a copy of the GNU General Public License
 *     along with this program.  If not, see .
 */
package com.plotsquared.core.command;

import com.plotsquared.core.configuration.caption.StaticCaption;
import com.plotsquared.core.configuration.caption.TranslatableCaption;
import com.plotsquared.core.player.PlotPlayer;
import com.plotsquared.core.util.MathMan;
import com.plotsquared.core.util.StringMan;
import com.plotsquared.core.util.helpmenu.HelpMenu;
import com.plotsquared.core.util.task.RunnableVal2;
import com.plotsquared.core.util.task.RunnableVal3;
import net.kyori.adventure.text.Component;
import net.kyori.adventure.text.TextComponent;
import net.kyori.adventure.text.minimessage.Template;

import java.util.Collection;
import java.util.Locale;
import java.util.concurrent.CompletableFuture;
import java.util.stream.Collectors;
import java.util.stream.Stream;

@CommandDeclaration(command = "help",
        aliases = "?",
        category = CommandCategory.INFO,
        usage = "help [category | #]",
        permission = "plots.use")
public class Help extends Command {

    public Help(Command parent) {
        super(parent, true);
    }

    @Override
    public boolean canExecute(PlotPlayer player, boolean message) {
        return true;
    }

    @Override
    public CompletableFuture execute(
            PlotPlayer player, String[] args,
            RunnableVal3 confirm,
            RunnableVal2 whenDone
    ) {
        switch (args.length) {
            case 0:
                return displayHelp(player, null, 0);
            case 1:
                if (MathMan.isInteger(args[0])) {
                    try {
                        return displayHelp(player, null, Integer.parseInt(args[0]));
                    } catch (NumberFormatException ignored) {
                        return displayHelp(player, null, 1);
                    }
                } else {
                    return displayHelp(player, args[0], 1);
                }
            case 2:
                if (MathMan.isInteger(args[1])) {
                    try {
                        return displayHelp(player, args[0], Integer.parseInt(args[1]));
                    } catch (NumberFormatException ignored) {
                        return displayHelp(player, args[0], 1);
                    }
                }
                return CompletableFuture.completedFuture(false);
            default:
                sendUsage(player);
        }
        return CompletableFuture.completedFuture(true);
    }

    public CompletableFuture displayHelp(
            final PlotPlayer player, final String catRaw,
            final int page
    ) {
        return CompletableFuture.supplyAsync(() -> {
            String cat = catRaw;

            CommandCategory catEnum = null;
            if (cat != null) {
                if (!"all".equalsIgnoreCase(cat)) {
                    for (CommandCategory c : CommandCategory.values()) {
                        if (StringMan.isEqualIgnoreCaseToAny(cat, c.name(), c.toString())) {
                            catEnum = c;
                            cat = c.name();
                            break;
                        }
                    }
                    if (catEnum == null) {
                        cat = null;
                    }
                }
            }
            if (cat == null && page == 0) {
                TextComponent.Builder builder = Component.text();
                builder.append(MINI_MESSAGE.parse(TranslatableCaption.of("help.help_header").getComponent(player)));
                for (CommandCategory c : CommandCategory.values()) {
                    builder.append(Component.newline()).append(MINI_MESSAGE
                            .parse(
                                    TranslatableCaption.of("help.help_info_item").getComponent(player),
                                    Template.of("command", "/plot help"),
                                    Template.of("category", c.name().toLowerCase()),
                                    Template.of("category_desc", c.getComponent(player))
                            ));
                }
                builder.append(Component.newline()).append(MINI_MESSAGE
                        .parse(
                                TranslatableCaption.of("help.help_info_item").getComponent(player),
                                Template.of("command", "/plot help"),
                                Template.of("category", "all"),
                                Template.of("category_desc", "Display all commands")
                        ));
                builder.append(Component.newline()).append(MINI_MESSAGE.parse(TranslatableCaption
                        .of("help.help_footer")
                        .getComponent(player)));
                player.sendMessage(StaticCaption.of(MINI_MESSAGE.serialize(builder.asComponent())));
                return true;
            }
            new HelpMenu(player).setCategory(catEnum).getCommands().generateMaxPages().generatePage(
                            page - 1,
                            getParent().toString(),
                            player
                    )
                    .render();
            return true;
        });
    }

    @Override
    public Collection tab(PlotPlayer player, String[] args, boolean space) {
        return Stream.of("claiming", "teleport", "settings", "chat", "schematic", "appearance", "info", "debug",
                        "administration", "all"
                )
                .filter(value -> value.startsWith(args[0].toLowerCase(Locale.ENGLISH)))
                .map(value -> new Command(null, false, value, "", RequiredType.NONE, null) {
                }).collect(Collectors.toList());
    }

}




© 2015 - 2025 Weber Informatics LLC | Privacy Policy