Search in sources :

Example 1 with IridiumSkyblock

use of com.iridium.iridiumskyblock.IridiumSkyblock in project IridiumSkyblock by Iridium-Development.

the class HelpCommand method execute.

/**
 * Executes the command for the specified {@link CommandSender} with the provided arguments.
 * Not called when the command execution was invalid (no permission, no player or command disabled).
 * Shows a list of all IridiumSkyblock commands.
 *
 * @param sender    The CommandSender which executes this command
 * @param arguments The arguments used with this command. They contain the sub-command
 */
@Override
public boolean execute(CommandSender sender, String[] arguments) {
    List<Command> availableCommands = IridiumSkyblock.getInstance().getCommandManager().commands.stream().filter(command -> sender.hasPermission(command.permission) || command.permission.isEmpty()).collect(Collectors.toList());
    int page = 1;
    int maxPage = (int) Math.ceil(availableCommands.size() / 8.0);
    // Read optional page argument
    if (arguments.length > 1) {
        String pageArgument = arguments[1];
        if (pageArgument.matches("[0-9]+")) {
            page = Integer.parseInt(pageArgument);
        } else {
            return showCommandHelp(sender, arguments);
        }
    }
    // Correct requested page if it's out of bounds
    if (page > maxPage) {
        page = maxPage;
    } else if (page < 1) {
        page = 1;
    }
    // Prepare the footer
    TextComponent footerText = new TextComponent(StringUtils.color(IridiumSkyblock.getInstance().getMessages().helpCommandFooter.replace("%page%", String.valueOf(page)).replace("%max_page%", String.valueOf(maxPage))));
    TextComponent previousButton = new TextComponent(StringUtils.color(IridiumSkyblock.getInstance().getMessages().helpCommandPreviousPage));
    TextComponent nextButton = new TextComponent(StringUtils.color(IridiumSkyblock.getInstance().getMessages().helpCommandNextPage));
    if (page != 1) {
        previousButton.setClickEvent(new ClickEvent(ClickEvent.Action.RUN_COMMAND, "/is " + IridiumSkyblock.getInstance().getCommands().helpCommand.aliases.get(0) + " " + (page - 1)));
        previousButton.setHoverEvent(new HoverEvent(HoverEvent.Action.SHOW_TEXT, new ComponentBuilder(StringUtils.color(IridiumSkyblock.getInstance().getMessages().helpCommandPreviousPageHover)).create()));
    }
    if (page != maxPage) {
        nextButton.setClickEvent(new ClickEvent(ClickEvent.Action.RUN_COMMAND, "/is " + IridiumSkyblock.getInstance().getCommands().helpCommand.aliases.get(0) + " " + (page + 1)));
        nextButton.setHoverEvent(new HoverEvent(HoverEvent.Action.SHOW_TEXT, new ComponentBuilder(StringUtils.color(IridiumSkyblock.getInstance().getMessages().helpCommandNextPageHover)).create()));
    }
    // Send all messages
    sender.sendMessage(StringUtils.color(IridiumSkyblock.getInstance().getMessages().helpCommandHeader));
    availableCommands.stream().skip((page - 1) * 8L).limit(8).map(command -> StringUtils.color(IridiumSkyblock.getInstance().getMessages().helpCommandMessage.replace("%command%", command.aliases.get(0)).replace("%description%", command.description))).forEach(sender::sendMessage);
    if (sender instanceof Player) {
        ((Player) sender).spigot().sendMessage(previousButton, footerText, nextButton);
    }
    return true;
}
Also used : IntStream(java.util.stream.IntStream) HoverEvent(net.md_5.bungee.api.chat.HoverEvent) Arrays(java.util.Arrays) CommandSender(org.bukkit.command.CommandSender) ClickEvent(net.md_5.bungee.api.chat.ClickEvent) Player(org.bukkit.entity.Player) Collectors(java.util.stream.Collectors) StringUtils(com.iridium.iridiumcore.utils.StringUtils) TextComponent(net.md_5.bungee.api.chat.TextComponent) ComponentBuilder(net.md_5.bungee.api.chat.ComponentBuilder) List(java.util.List) Duration(java.time.Duration) Optional(java.util.Optional) Collections(java.util.Collections) IridiumColorAPI(com.iridium.iridiumcore.dependencies.iridiumcolorapi.IridiumColorAPI) IridiumSkyblock(com.iridium.iridiumskyblock.IridiumSkyblock) TextComponent(net.md_5.bungee.api.chat.TextComponent) HoverEvent(net.md_5.bungee.api.chat.HoverEvent) Player(org.bukkit.entity.Player) ClickEvent(net.md_5.bungee.api.chat.ClickEvent) ComponentBuilder(net.md_5.bungee.api.chat.ComponentBuilder)

Aggregations

IridiumColorAPI (com.iridium.iridiumcore.dependencies.iridiumcolorapi.IridiumColorAPI)1 StringUtils (com.iridium.iridiumcore.utils.StringUtils)1 IridiumSkyblock (com.iridium.iridiumskyblock.IridiumSkyblock)1 Duration (java.time.Duration)1 Arrays (java.util.Arrays)1 Collections (java.util.Collections)1 List (java.util.List)1 Optional (java.util.Optional)1 Collectors (java.util.stream.Collectors)1 IntStream (java.util.stream.IntStream)1 ClickEvent (net.md_5.bungee.api.chat.ClickEvent)1 ComponentBuilder (net.md_5.bungee.api.chat.ComponentBuilder)1 HoverEvent (net.md_5.bungee.api.chat.HoverEvent)1 TextComponent (net.md_5.bungee.api.chat.TextComponent)1 CommandSender (org.bukkit.command.CommandSender)1 Player (org.bukkit.entity.Player)1