Search in sources :

Example 1 with ADPExecutableCommand

use of com.alessiodp.core.common.commands.utils.ADPExecutableCommand in project Parties by AlessioDP.

the class CommandHelp method onCommand.

@Override
public void onCommand(@NotNull CommandData commandData) {
    User sender = commandData.getSender();
    PartyPlayerImpl partyPlayer = ((PartiesCommandData) commandData).getPartyPlayer();
    // Command starts
    // Get all allowed commands
    LinkedList<String> list = new LinkedList<>();
    Set<ADPCommand> allowedCommands = partyPlayer.getAllowedCommands();
    for (Map.Entry<ADPCommand, ADPExecutableCommand> e : plugin.getCommandManager().getOrderedCommands().entrySet()) {
        if (allowedCommands.contains(e.getKey()) && e.getValue().isListedInHelp()) {
            list.add(e.getValue().getHelp().replace("%syntax%", e.getValue().getSyntaxForUser(sender)).replace("%description%", e.getValue().getDescription() != null ? e.getValue().getDescription() : "").replace("%run_command%", e.getValue().getRunCommand()).replace("%perform_command%", Messages.HELP_PERFORM_COMMAND));
        }
    }
    // Split commands per page
    int page = 1;
    int maxpages;
    if (list.size() == 0)
        maxpages = 1;
    else if ((list.size() % ConfigMain.COMMANDS_HELP_PERPAGE) == 0)
        maxpages = list.size() / ConfigMain.COMMANDS_HELP_PERPAGE;
    else
        maxpages = (list.size() / ConfigMain.COMMANDS_HELP_PERPAGE) + 1;
    if (commandData.getArgs().length > 1) {
        try {
            page = Integer.parseInt(commandData.getArgs()[1]);
        } catch (NumberFormatException ignored) {
        }
        if (page > maxpages || page < 1)
            page = maxpages;
    }
    // Start printing
    int commandNumber = 0;
    sendMessage(sender, partyPlayer, Messages.HELP_HEADER.replace("%page%", Integer.toString(page)).replace("%maxpages%", Integer.toString(maxpages)));
    for (String string : list) {
        int currentChoosenPage = (page - 1) * ConfigMain.COMMANDS_HELP_PERPAGE;
        if (commandNumber >= currentChoosenPage && commandNumber < currentChoosenPage + ConfigMain.COMMANDS_HELP_PERPAGE) {
            sendMessage(sender, partyPlayer, string);
        }
        commandNumber++;
    }
    sendMessage(sender, partyPlayer, Messages.HELP_FOOTER.replace("%page%", Integer.toString(page)).replace("%maxpages%", Integer.toString(maxpages)));
}
Also used : User(com.alessiodp.core.common.user.User) PartyPlayerImpl(com.alessiodp.parties.common.players.objects.PartyPlayerImpl) PartiesCommandData(com.alessiodp.parties.common.commands.utils.PartiesCommandData) LinkedList(java.util.LinkedList) ADPCommand(com.alessiodp.core.common.commands.list.ADPCommand) ADPExecutableCommand(com.alessiodp.core.common.commands.utils.ADPExecutableCommand) Map(java.util.Map)

Aggregations

ADPCommand (com.alessiodp.core.common.commands.list.ADPCommand)1 ADPExecutableCommand (com.alessiodp.core.common.commands.utils.ADPExecutableCommand)1 User (com.alessiodp.core.common.user.User)1 PartiesCommandData (com.alessiodp.parties.common.commands.utils.PartiesCommandData)1 PartyPlayerImpl (com.alessiodp.parties.common.players.objects.PartyPlayerImpl)1 LinkedList (java.util.LinkedList)1 Map (java.util.Map)1