From d82c8cecad98759315d61228bcaf5a2bf8cae580 Mon Sep 17 00:00:00 2001 From: Quentin Legot Date: Thu, 23 Mar 2023 02:15:37 +0100 Subject: [PATCH] Added some "=" to headers and footers Signed-off-by: Quentin Legot --- .../toolbox/pagination/PaginatedContent.java | 44 ++++++++++++++----- 1 file changed, 32 insertions(+), 12 deletions(-) diff --git a/Pagination/src/main/java/fr/altarik/toolbox/pagination/PaginatedContent.java b/Pagination/src/main/java/fr/altarik/toolbox/pagination/PaginatedContent.java index e7034c5..95ac763 100644 --- a/Pagination/src/main/java/fr/altarik/toolbox/pagination/PaginatedContent.java +++ b/Pagination/src/main/java/fr/altarik/toolbox/pagination/PaginatedContent.java @@ -17,7 +17,7 @@ public class PaginatedContent { private final String header; public PaginatedContent(String header, String content) { - this.header = header; + this.header = buildHeader(header); pages = new ArrayList<>(); List secondSplit = new ArrayList<>(); for(String elem : Stream.of(content.split("\n")).collect(Collectors.toCollection(ArrayList::new))) { @@ -43,6 +43,11 @@ public class PaginatedContent { pages.add(new Page(currentPage)); } + private String buildHeader(String header) { + int numberOfEq = (50 - header.length()) / 2; + return "=".repeat(numberOfEq) + header + "=".repeat(numberOfEq); + } + public void display(ServerPlayerEntity playerEntity, int page) { if(page >= this.pages.size()) { throw new IllegalArgumentException("There's " + this.pages.size() + " paginated pages but you wanted page n°" + (page + 1)); @@ -53,20 +58,35 @@ public class PaginatedContent { for(String s : pages.get(page).lines) { playerEntity.sendMessage(Text.literal(s)); } - MutableText left = Text.literal("<").styled( - style -> style - .withColor(Formatting.YELLOW) - .withClickEvent(new ClickEvent(ClickEvent.Action.RUN_COMMAND, "/table page " + (page - 1))) - ); - MutableText right = Text.literal(">").styled( - style -> style - .withColor(Formatting.YELLOW) - .withClickEvent(new ClickEvent(ClickEvent.Action.RUN_COMMAND, "/table page " + (page + 1))) - ); - playerEntity.sendMessage(left.append(" " + page + " ").append(right)); + + playerEntity.sendMessage(buildFooter(page)); } } + private Text buildFooter(int page) { + String strPage = String.valueOf(page + 1); + int numberOfEq = (46 - strPage.length()) / 2; + MutableText left = Text.literal("<").styled( + style -> style + .withColor(Formatting.YELLOW) + .withClickEvent(new ClickEvent(ClickEvent.Action.RUN_COMMAND, "/table page " + (page - 1))) + ); + MutableText middle = Text.literal(" " + strPage + " ").styled(style -> style.withColor(Formatting.RESET)); + MutableText right = Text.literal(">").styled( + style -> style + .withColor(Formatting.YELLOW) + .withClickEvent(new ClickEvent(ClickEvent.Action.RUN_COMMAND, "/table page " + (page + 1))) + ); + return Text.literal("=".repeat(numberOfEq)) + .append(left) + .append(middle) + .append(right) + .append( + Text.literal("=".repeat(numberOfEq)) + .styled(style -> style.withColor(Formatting.RESET)) + ); + } + private record Page(List lines) { }