From 48e53e0edb6b716cf341a21905fab0c5b32fc88f Mon Sep 17 00:00:00 2001 From: Quentin Legot Date: Fri, 24 Mar 2023 21:58:05 +0100 Subject: [PATCH] Added support of Text for Pagination Signed-off-by: Quentin Legot --- .../toolbox/pagination/PaginatedContent.java | 33 ++++++++++++++---- .../toolbox/pagination/api/PaginationApi.java | 3 +- .../pagination/api/PaginationApiImpl.java | 19 ++++++++--- .../pagination/command/CommandsRegister.java | 34 ++++++++++++++++++- 4 files changed, 75 insertions(+), 14 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 6cadae2..f6868c4 100644 --- a/Pagination/src/main/java/fr/altarik/toolbox/pagination/PaginatedContent.java +++ b/Pagination/src/main/java/fr/altarik/toolbox/pagination/PaginatedContent.java @@ -6,6 +6,7 @@ import net.minecraft.text.ClickEvent; import net.minecraft.text.MutableText; import net.minecraft.text.Text; import net.minecraft.util.Formatting; +import org.jetbrains.annotations.Nullable; import java.util.ArrayList; import java.util.List; @@ -29,26 +30,44 @@ public class PaginatedContent { secondSplit.add(elem); } } - int line = 0; List currentPage = new ArrayList<>(); for(String elem : secondSplit) { - line++; - if(!elem.isEmpty()) + if(!elem.isEmpty()) { currentPage.add(Text.literal(elem)); - if(line == 8 || elem.isEmpty()) { + } + if(currentPage.size() == 8 || elem.isEmpty()) { pages.add(new Page(currentPage)); - line = 0; currentPage = new ArrayList<>(); } } pages.add(new Page(currentPage)); } - private Text buildHeader(String header) { - int numberOfEq = (50 - header.length()) / 2; + public PaginatedContent(@Nullable Text header, List content) { + this.header = buildHeader(header); + this.pages = new ArrayList<>(); + List currentPage = new ArrayList<>(); + for(Text elem : content) { + if(elem != null) + currentPage.add(elem); + if(currentPage.size() == 8 || elem == null) { + pages.add(new Page(currentPage)); + currentPage = new ArrayList<>(); + } + } + pages.add(new Page(currentPage)); + } + + private Text buildHeader(@Nullable String header) { + int numberOfEq = (50 - (header != null ? header.length() : 0)) / 2; return Text.literal("=".repeat(numberOfEq) + " " + header + " " + "=".repeat(numberOfEq)); } + private Text buildHeader(@Nullable Text header) { + int numberOfEq = (50 - (header != null ? header.getString().length() : 0)) / 2; + return Text.literal("=".repeat(numberOfEq) + " ").append(header).append(" " + "=".repeat(numberOfEq)); + } + public void display(ServerPlayerEntity playerEntity, int page) throws PageIndexOutOfBoundException { if(page >= this.pages.size()) { throw new PageIndexOutOfBoundException("api.pagination.page_higher_than_expected", this.pages.size(), (page + 1)); diff --git a/Pagination/src/main/java/fr/altarik/toolbox/pagination/api/PaginationApi.java b/Pagination/src/main/java/fr/altarik/toolbox/pagination/api/PaginationApi.java index c7bea9a..cee4fcf 100644 --- a/Pagination/src/main/java/fr/altarik/toolbox/pagination/api/PaginationApi.java +++ b/Pagination/src/main/java/fr/altarik/toolbox/pagination/api/PaginationApi.java @@ -2,6 +2,7 @@ package fr.altarik.toolbox.pagination.api; import net.minecraft.server.network.ServerPlayerEntity; import net.minecraft.text.Text; +import org.jetbrains.annotations.Nullable; import java.util.List; @@ -48,7 +49,7 @@ public interface PaginationApi { * * @see Text#empty() */ - void createTable(ServerPlayerEntity playerEntity, List content, Text header, boolean display); + void createTable(ServerPlayerEntity playerEntity, List content, @Nullable Text header, boolean display); /** diff --git a/Pagination/src/main/java/fr/altarik/toolbox/pagination/api/PaginationApiImpl.java b/Pagination/src/main/java/fr/altarik/toolbox/pagination/api/PaginationApiImpl.java index a95946c..700997c 100644 --- a/Pagination/src/main/java/fr/altarik/toolbox/pagination/api/PaginationApiImpl.java +++ b/Pagination/src/main/java/fr/altarik/toolbox/pagination/api/PaginationApiImpl.java @@ -9,6 +9,7 @@ import net.minecraft.server.MinecraftServer; import net.minecraft.server.network.ServerPlayerEntity; import net.minecraft.text.Text; import net.minecraft.util.Pair; +import org.jetbrains.annotations.Nullable; import java.util.ArrayList; import java.util.HashMap; @@ -35,6 +36,19 @@ public class PaginationApiImpl implements PaginationApi { throw new IllegalArgumentException("Preconditions aren't satisfied"); } PaginatedContent paginatedContent1 = new PaginatedContent(header, content); + storeAndDisplay(playerEntity, paginatedContent1, display); + } + + @Override + public void createTable(ServerPlayerEntity playerEntity, List content, @Nullable Text header, boolean display) { + if(playerCondition.test(playerEntity)) { + throw new IllegalArgumentException("Preconditions aren't satisfied"); + } + PaginatedContent paginatedContent1 = new PaginatedContent(header, content); + storeAndDisplay(playerEntity, paginatedContent1, display); + } + + private void storeAndDisplay(ServerPlayerEntity playerEntity, PaginatedContent paginatedContent1, boolean display) { paginatedContent.put(playerEntity, new Pair<>(18000, paginatedContent1)); if(display) { try { @@ -45,11 +59,6 @@ public class PaginationApiImpl implements PaginationApi { } } - @Override - public void createTable(ServerPlayerEntity playerEntity, List content, Text header, boolean display) { - throw new UnsupportedOperationException("Not yet implemented"); - } - @Override public void display(ServerPlayerEntity player, int page) throws PageIndexOutOfBoundException { if(player == null) diff --git a/Pagination/src/main/java/fr/altarik/toolbox/pagination/command/CommandsRegister.java b/Pagination/src/main/java/fr/altarik/toolbox/pagination/command/CommandsRegister.java index 57be1f0..e46d715 100644 --- a/Pagination/src/main/java/fr/altarik/toolbox/pagination/command/CommandsRegister.java +++ b/Pagination/src/main/java/fr/altarik/toolbox/pagination/command/CommandsRegister.java @@ -9,6 +9,10 @@ import fr.altarik.toolbox.pagination.Pagination; import fr.altarik.toolbox.pagination.api.PageIndexOutOfBoundException; import fr.altarik.toolbox.pagination.api.PaginationApi; import net.minecraft.server.command.ServerCommandSource; +import net.minecraft.text.Text; + +import java.util.ArrayList; +import java.util.List; import static net.minecraft.server.command.CommandManager.argument; import static net.minecraft.server.command.CommandManager.literal; @@ -30,6 +34,9 @@ public class CommandsRegister { ).then(literal("test") .requires(source -> source.isExecutedByPlayer() && source.hasPermissionLevel(3)) .executes(this::testPageCommand) + ).then(literal("testText") + .requires(source -> source.isExecutedByPlayer() && source.hasPermissionLevel(3)) + .executes(this::testPageTextCommand) ) ); } @@ -39,7 +46,7 @@ public class CommandsRegister { */ private int testPageCommand(CommandContext context) { api.createTable(context.getSource().getPlayer(), """ - first line + first line, string version Second line second page @@ -57,6 +64,26 @@ public class CommandsRegister { return 0; } + private int testPageTextCommand(CommandContext context) { + List content = new ArrayList<>(); + content.add(Text.literal("first line, text version")); + content.add(Text.literal("Second line")); + content.add(null); + content.add(Text.literal("second page")); + content.add(Text.literal("dqdq")); + content.add(Text.literal("dqdqd")); + content.add(Text.literal("dqdqd")); + content.add(Text.literal("dqdq")); + content.add(Text.literal("dqdq")); + content.add(Text.literal("dqdq")); + content.add(Text.literal("dqdqd")); + content.add(Text.literal("third page")); + content.add(Text.literal("dqdqd")); + content.add(Text.literal("dqdqd")); + api.createTable(context.getSource().getPlayer(), content, Text.literal("My Text Header"), true); + return 0; + } + private int selectPageCommand(CommandContext context) throws CommandSyntaxException { try { int page = IntegerArgumentType.getInteger(context, "page"); @@ -67,4 +94,9 @@ public class CommandsRegister { return 0; } + private enum TestType { + String, + Text; + } + }