From 262583b1abec71bcbe16c4cabe1f7fbab9d6bd50 Mon Sep 17 00:00:00 2001 From: CalMWolfs <94038482+CalMWolfs@users.noreply.github.com> Date: Wed, 25 Sep 2024 05:04:42 +1000 Subject: Backend: Add pages to /shnavigate (#2583) Co-authored-by: hannibal2 <24389977+hannibal00212@users.noreply.github.com> --- .../java/at/hannibal2/skyhanni/utils/chat/Text.kt | 77 ++++++++++++++++++++-- 1 file changed, 73 insertions(+), 4 deletions(-) (limited to 'src/main/java/at/hannibal2/skyhanni/utils') diff --git a/src/main/java/at/hannibal2/skyhanni/utils/chat/Text.kt b/src/main/java/at/hannibal2/skyhanni/utils/chat/Text.kt index 024550ec5..fe7486d0d 100644 --- a/src/main/java/at/hannibal2/skyhanni/utils/chat/Text.kt +++ b/src/main/java/at/hannibal2/skyhanni/utils/chat/Text.kt @@ -60,6 +60,7 @@ object Text { } return this } + fun IChatComponent.center(width: Int = Minecraft.getMinecraft().ingameGUI.chatGUI.chatWidth): IChatComponent { val textWidth = this.width() val spaceWidth = SPACE.width() @@ -99,10 +100,78 @@ object Text { this.command = "/shaction $token" } - - fun createDivider() = Text.HYPHEN.fitToChat().style { + fun createDivider(dividerColor: EnumChatFormatting = EnumChatFormatting.BLUE) = HYPHEN.fitToChat().style { strikethrough = true - color = EnumChatFormatting.BLUE + color = dividerColor + } + + /** + * Displays a paginated list of entries in the chat. + * + * @param title The title of the paginated list. + * @param list The list of entries to paginate and display. + * @param chatLineId The ID of the chat line for message updates. + * @param emptyMessage The message to display if the list is empty. + * @param currentPage The current page to display. + * @param maxPerPage The number of entries to display per page. + * @param dividerColor The color of the divider lines. + * @param formatter A function to format each entry into an IChatComponent. + */ + fun displayPaginatedList( + title: String, + list: List, + chatLineId: Int, + emptyMessage: String, + currentPage: Int = 1, + maxPerPage: Int = 15, + dividerColor: EnumChatFormatting = EnumChatFormatting.BLUE, + formatter: (T) -> IChatComponent, + ) { + val text = mutableListOf() + + val totalPages = (list.size + maxPerPage - 1) / maxPerPage + val page = if (totalPages == 0) 0 else currentPage + + text.add(createDivider(dividerColor)) + text.add("§6$title".asComponent().center()) + + if (totalPages > 1) { + text.add( + join( + if (page > 1) "§6§l<<".asComponent { + hover = "§eClick to view page ${page - 1}".asComponent() + onClick { + displayPaginatedList(title, list, chatLineId, emptyMessage, page - 1, maxPerPage, dividerColor, formatter) + } + } else null, + " ", + "§6(Page $page of $totalPages)", + " ", + if (page < totalPages) "§6§l>>".asComponent { + hover = "§eClick to view page ${page + 1}".asComponent() + onClick { + displayPaginatedList(title, list, chatLineId, emptyMessage, page + 1, maxPerPage, dividerColor, formatter) + } + } else null, + ).center(), + ) + } + + text.add(createDivider(dividerColor)) + + if (list.isNotEmpty()) { + val start = (page - 1) * maxPerPage + val end = (page * maxPerPage).coerceAtMost(list.size) + for (i in start until end) { + text.add(formatter(list[i])) + } + } else { + text.add(EMPTY) + text.add("§c$emptyMessage".asComponent().center()) + text.add(EMPTY) + } + + text.add(createDivider(dividerColor)) + multiline(text).send(chatLineId) } - } -- cgit