diff options
author | CalMWolfs <94038482+CalMWolfs@users.noreply.github.com> | 2024-09-25 05:04:42 +1000 |
---|---|---|
committer | GitHub <noreply@github.com> | 2024-09-24 21:04:42 +0200 |
commit | 262583b1abec71bcbe16c4cabe1f7fbab9d6bd50 (patch) | |
tree | 7529286e8ff332981202ba113f652ac0c420389b /src/main/java/at/hannibal2/skyhanni/utils | |
parent | f77477f5d22e33b34d1723b0c89dd08244b40f85 (diff) | |
download | skyhanni-262583b1abec71bcbe16c4cabe1f7fbab9d6bd50.tar.gz skyhanni-262583b1abec71bcbe16c4cabe1f7fbab9d6bd50.tar.bz2 skyhanni-262583b1abec71bcbe16c4cabe1f7fbab9d6bd50.zip |
Backend: Add pages to /shnavigate (#2583)
Co-authored-by: hannibal2 <24389977+hannibal00212@users.noreply.github.com>
Diffstat (limited to 'src/main/java/at/hannibal2/skyhanni/utils')
-rw-r--r-- | src/main/java/at/hannibal2/skyhanni/utils/chat/Text.kt | 77 |
1 files changed, 73 insertions, 4 deletions
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 <T> displayPaginatedList( + title: String, + list: List<T>, + chatLineId: Int, + emptyMessage: String, + currentPage: Int = 1, + maxPerPage: Int = 15, + dividerColor: EnumChatFormatting = EnumChatFormatting.BLUE, + formatter: (T) -> IChatComponent, + ) { + val text = mutableListOf<IChatComponent>() + + 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) } - } |