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.CommandCategory;
022import com.plotsquared.core.configuration.caption.StaticCaption;
023import com.plotsquared.core.configuration.caption.TranslatableCaption;
024import com.plotsquared.core.player.PlotPlayer;
025import com.plotsquared.core.util.ComponentHelper;
026import net.kyori.adventure.text.Component;
027import net.kyori.adventure.text.minimessage.MiniMessage;
028import net.kyori.adventure.text.minimessage.tag.Tag;
029import net.kyori.adventure.text.minimessage.tag.resolver.TagResolver;
030
031import java.util.ArrayList;
032import java.util.List;
033
034public class HelpPage {
035
036    private static final MiniMessage MINI_MESSAGE = MiniMessage.miniMessage();
037    private final List<HelpObject> helpObjects;
038    private final TagResolver pageHeaderResolver;
039
040    public HelpPage(CommandCategory category, int currentPage, int maxPages) {
041        this.helpObjects = new ArrayList<>();
042        this.pageHeaderResolver = TagResolver.builder()
043                .tag("category", Tag.inserting(Component.text(category == null ? "ALL" : category.name())))
044                .tag("current", Tag.inserting(Component.text(currentPage + 1)))
045                .tag("max", Tag.inserting(Component.text(maxPages + 1)))
046                .build();
047    }
048
049    public void render(PlotPlayer<?> player) {
050        if (this.helpObjects.size() < 1) {
051            player.sendMessage(TranslatableCaption.of("help.no_permission"));
052        } else {
053            TagResolver contentResolver = TagResolver.builder()
054                    .tag("header", Tag.inserting(TranslatableCaption.of("help.help_header").toComponent(player)))
055                    .tag("page_header", Tag.inserting(MINI_MESSAGE.deserialize(
056                            TranslatableCaption.of("help.help_page_header").getComponent(player),
057                            pageHeaderResolver
058                    )))
059                    .tag("help_objects", Tag.inserting(ComponentHelper.join(this.helpObjects, Component.text("\n"))))
060                    .tag("footer", Tag.inserting(TranslatableCaption.of("help.help_footer").toComponent(player)))
061                    .build();
062            player.sendMessage(
063                    StaticCaption.of("<header>\n<page_header>\n<help_objects>\n<footer>"),
064                    contentResolver
065            );
066        }
067    }
068
069    public void addHelpItem(HelpObject object) {
070        this.helpObjects.add(object);
071    }
072
073}