From 30853dee70daad53de47914a02eda2b3fbb55127 Mon Sep 17 00:00:00 2001 From: Quentin Legot Date: Fri, 24 Mar 2023 19:38:40 +0100 Subject: [PATCH] Improve player feedback and improve table style Signed-off-by: Quentin Legot --- .../toolbox/pagination/PaginatedContent.java | 13 ++++++------ .../toolbox/pagination/Pagination.java | 3 ++- .../api/PageIndexOutOfBoundException.java | 20 +++++++++++++++++++ .../toolbox/pagination/api/PaginationApi.java | 2 +- .../pagination/api/PaginationApiImpl.java | 11 +++++++--- .../pagination/command/CommandsRegister.java | 9 +++++---- .../assets/pagination/lang/en_us.json | 4 ++++ 7 files changed, 47 insertions(+), 15 deletions(-) create mode 100644 Pagination/src/main/java/fr/altarik/toolbox/pagination/api/PageIndexOutOfBoundException.java create mode 100644 Pagination/src/main/resources/assets/pagination/lang/en_us.json 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 23fb640..6cadae2 100644 --- a/Pagination/src/main/java/fr/altarik/toolbox/pagination/PaginatedContent.java +++ b/Pagination/src/main/java/fr/altarik/toolbox/pagination/PaginatedContent.java @@ -1,5 +1,6 @@ package fr.altarik.toolbox.pagination; +import fr.altarik.toolbox.pagination.api.PageIndexOutOfBoundException; import net.minecraft.server.network.ServerPlayerEntity; import net.minecraft.text.ClickEvent; import net.minecraft.text.MutableText; @@ -45,14 +46,14 @@ public class PaginatedContent { private Text buildHeader(String header) { int numberOfEq = (50 - header.length()) / 2; - return Text.literal("=".repeat(numberOfEq) + header + "=".repeat(numberOfEq)); + return Text.literal("=".repeat(numberOfEq) + " " + header + " " + "=".repeat(numberOfEq)); } - public void display(ServerPlayerEntity playerEntity, int page) { + public void display(ServerPlayerEntity playerEntity, int page) throws PageIndexOutOfBoundException { if(page >= this.pages.size()) { - throw new IllegalArgumentException("There's " + this.pages.size() + " paginated pages but you wanted page n°" + (page + 1)); + throw new PageIndexOutOfBoundException("api.pagination.page_higher_than_expected", this.pages.size(), (page + 1)); } else if(page < 0) { - throw new IllegalArgumentException("argument page is lower than 0"); + throw new PageIndexOutOfBoundException("api.pagination.page_lower_than_0"); } else { playerEntity.sendMessage(header); for(Text s : pages.get(page).lines) { @@ -66,13 +67,13 @@ public class PaginatedContent { private Text buildFooter(int page) { String strPage = String.valueOf(page + 1); int numberOfEq = (46 - strPage.length()) / 2; - MutableText left = Text.literal("<").styled( + 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( + MutableText right = Text.literal("> ").styled( style -> style .withColor(Formatting.YELLOW) .withClickEvent(new ClickEvent(ClickEvent.Action.RUN_COMMAND, "/table page " + (page + 1))) diff --git a/Pagination/src/main/java/fr/altarik/toolbox/pagination/Pagination.java b/Pagination/src/main/java/fr/altarik/toolbox/pagination/Pagination.java index 8e4b19e..5ef76c5 100644 --- a/Pagination/src/main/java/fr/altarik/toolbox/pagination/Pagination.java +++ b/Pagination/src/main/java/fr/altarik/toolbox/pagination/Pagination.java @@ -19,13 +19,14 @@ public class Pagination implements ModInitializer { @Override public void onInitialize() { - CommandRegistrationCallback.EVENT.register((dispatcher, registryAccess, environment) -> new CommandsRegister(this).register(dispatcher, environment.dedicated)); + CommandRegistrationCallback.EVENT.register((dispatcher, registryAccess, environment) -> new CommandsRegister(this).register(dispatcher)); } public @NotNull PaginationApi getApi() { return api; } + @SuppressWarnings("unused") public static @NotNull Pagination getInstance() { return instance; } diff --git a/Pagination/src/main/java/fr/altarik/toolbox/pagination/api/PageIndexOutOfBoundException.java b/Pagination/src/main/java/fr/altarik/toolbox/pagination/api/PageIndexOutOfBoundException.java new file mode 100644 index 0000000..1d222f8 --- /dev/null +++ b/Pagination/src/main/java/fr/altarik/toolbox/pagination/api/PageIndexOutOfBoundException.java @@ -0,0 +1,20 @@ +package fr.altarik.toolbox.pagination.api; + +import net.minecraft.text.Text; + +public class PageIndexOutOfBoundException extends Exception { + + private final Text text; + + public PageIndexOutOfBoundException(String s) { + this.text = Text.translatable(s); + } + + public PageIndexOutOfBoundException(String s, int size, int currentPage) { + this.text = Text.translatable(s, size, currentPage); + } + + public Text getText() { + return text; + } +} 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 eb5297b..88f345e 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 @@ -32,6 +32,6 @@ public interface PaginationApi { * @throws NullPointerException if player is null or paginated content for the player doesn't exist (or have expired) * @see fr.altarik.toolbox.pagination.PaginatedContent#display(ServerPlayerEntity, int) */ - void display(ServerPlayerEntity player, int page); + void display(ServerPlayerEntity player, int page) throws PageIndexOutOfBoundException; } 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 5a62fb4..43c858f 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 @@ -35,12 +35,17 @@ public class PaginationApiImpl implements PaginationApi { } PaginatedContent paginatedContent1 = new PaginatedContent(header, content); paginatedContent.put(playerEntity, new Pair<>(18000, paginatedContent1)); - if(display) - paginatedContent1.display(playerEntity, 0); + if(display) { + try { + paginatedContent1.display(playerEntity, 0); + } catch (PageIndexOutOfBoundException e) { + throw new IllegalArgumentException(e); + } + } } @Override - public void display(ServerPlayerEntity player, int page) { + public void display(ServerPlayerEntity player, int page) throws PageIndexOutOfBoundException { if(player == null) throw new NullPointerException("Player is null"); Pair pair = paginatedContent.get(player); 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 58642c4..57be1f0 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 @@ -4,10 +4,11 @@ import com.mojang.brigadier.CommandDispatcher; import com.mojang.brigadier.arguments.IntegerArgumentType; import com.mojang.brigadier.context.CommandContext; import com.mojang.brigadier.exceptions.CommandSyntaxException; +import com.mojang.brigadier.exceptions.SimpleCommandExceptionType; 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 static net.minecraft.server.command.CommandManager.argument; import static net.minecraft.server.command.CommandManager.literal; @@ -20,7 +21,7 @@ public class CommandsRegister { this.api = instance.getApi(); } - public void register(CommandDispatcher dispatcher, boolean dedicated) { + public void register(CommandDispatcher dispatcher) { dispatcher.register(literal("table") .then(literal("page") .then(argument("page", IntegerArgumentType.integer()) @@ -60,8 +61,8 @@ public class CommandsRegister { try { int page = IntegerArgumentType.getInteger(context, "page"); api.display(context.getSource().getPlayerOrThrow(), page); - } catch(NullPointerException | IllegalArgumentException e) { - context.getSource().sendFeedback(Text.literal("Error: " + e.getMessage()), false); + } catch(PageIndexOutOfBoundException e) { + throw new CommandSyntaxException(new SimpleCommandExceptionType(e.getText()), e.getText()); } return 0; } diff --git a/Pagination/src/main/resources/assets/pagination/lang/en_us.json b/Pagination/src/main/resources/assets/pagination/lang/en_us.json new file mode 100644 index 0000000..94665c3 --- /dev/null +++ b/Pagination/src/main/resources/assets/pagination/lang/en_us.json @@ -0,0 +1,4 @@ +{ + "api.pagination.page_lower_than_0": "argument page is lower than 0", + "api.pagination.page_higher_than_expected": "There's %d paginated pages but you wanted page n°%d" +} \ No newline at end of file