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;
}
Aggregations