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.util.helpmenu;
020
021import com.plotsquared.core.command.Argument;
022import com.plotsquared.core.command.Command;
023import com.plotsquared.core.configuration.caption.Templates;
024import com.plotsquared.core.configuration.caption.TranslatableCaption;
025import com.plotsquared.core.player.PlotPlayer;
026import com.plotsquared.core.util.StringMan;
027import net.kyori.adventure.text.minimessage.MiniMessage;
028import net.kyori.adventure.text.minimessage.Template;
029
030public class HelpObject {
031
032    static final MiniMessage MINI_MESSAGE = MiniMessage.builder().build();
033
034    private final String rendered;
035
036    public HelpObject(final Command command, final String label, final PlotPlayer<?> audience) {
037        rendered = MINI_MESSAGE.serialize(MINI_MESSAGE.parse(
038                TranslatableCaption.of("help.help_item").getComponent(audience),
039                Template.of("usage", command.getUsage().replace("{label}", label)),
040                Template.of("alias", command.getAliases().isEmpty() ? "" : StringMan.join(command.getAliases(), " | ")),
041                Templates.of(audience, "desc", command.getDescription()),
042                Template.of("arguments", buildArgumentList(command.getRequiredArguments())),
043                Template.of("label", label)
044        ));
045    }
046
047    @Override
048    public String toString() {
049        return rendered;
050    }
051
052    private String buildArgumentList(final Argument<?>[] arguments) {
053        if (arguments == null) {
054            return "";
055        }
056        final StringBuilder builder = new StringBuilder();
057        for (final Argument<?> argument : arguments) {
058            builder.append("[").append(argument.getName()).append(" (")
059                    .append(argument.getExample()).append(")],");
060        }
061        return arguments.length > 0 ? builder.substring(0, builder.length() - 1) : "";
062    }
063
064}