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.Command;
022import com.plotsquared.core.command.CommandCategory;
023import com.plotsquared.core.command.MainCommand;
024import com.plotsquared.core.player.PlotPlayer;
025
026import java.util.List;
027
028public class HelpMenu {
029
030    private static final int PER_PAGE = 5;
031
032    private final PlotPlayer<?> commandCaller;
033    private HelpPage page = new HelpPage(CommandCategory.INFO, 0, 0);
034    private int maxPage;
035    private CommandCategory commandCategory;
036    private List<Command> commands;
037
038    public HelpMenu(PlotPlayer<?> commandCaller) {
039        this.commandCaller = commandCaller;
040    }
041
042    public HelpMenu setCategory(CommandCategory commandCategory) {
043        this.commandCategory = commandCategory;
044        return this;
045    }
046
047    public HelpMenu getCommands() {
048        this.commands = MainCommand.getInstance().getCommands(this.commandCategory, this.commandCaller);
049        return this;
050    }
051
052    public HelpMenu setCommands(final List<Command> commands) {
053        this.commands = commands;
054        return this;
055    }
056
057    public HelpMenu generateMaxPages() {
058        this.maxPage = Math.max((this.commands.size() - 1) / PER_PAGE, 0);
059        return this;
060    }
061
062    public HelpMenu generatePage(int currentPage, String label, PlotPlayer<?> audience) {
063        if (currentPage > this.maxPage) {
064            currentPage = this.maxPage;
065        }
066        if (currentPage < 0) {
067            currentPage = 0;
068        }
069        this.page = new HelpPage(this.commandCategory, currentPage, this.maxPage);
070        int max = Math.min((currentPage * PER_PAGE) + (PER_PAGE - 1), this.commands.size());
071        for (int i = currentPage * PER_PAGE; i < max; i++) {
072            this.page.addHelpItem(new HelpObject(this.commands.get(i), label, audience));
073        }
074        return this;
075    }
076
077    public void render() {
078        this.page.render(this.commandCaller);
079    }
080
081}