Added support of Text for Pagination
All checks were successful
build (17, ubuntu-latest)

Signed-off-by: Quentin Legot <legotquentin@gmail.com>
This commit is contained in:
Quentin Legot 2023-03-24 21:58:05 +01:00
parent 351b8aaf84
commit 48e53e0edb
4 changed files with 75 additions and 14 deletions

View File

@ -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<Text> 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<Text> content) {
this.header = buildHeader(header);
this.pages = new ArrayList<>();
List<Text> 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));

View File

@ -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 {
* </ol>
* @see Text#empty()
*/
void createTable(ServerPlayerEntity playerEntity, List<Text> content, Text header, boolean display);
void createTable(ServerPlayerEntity playerEntity, List<Text> content, @Nullable Text header, boolean display);
/**

View File

@ -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<Text> 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<Text> content, Text header, boolean display) {
throw new UnsupportedOperationException("Not yet implemented");
}
@Override
public void display(ServerPlayerEntity player, int page) throws PageIndexOutOfBoundException {
if(player == null)

View File

@ -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<ServerCommandSource> 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<ServerCommandSource> context) {
List<Text> 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<ServerCommandSource> context) throws CommandSyntaxException {
try {
int page = IntegerArgumentType.getInteger(context, "page");
@ -67,4 +94,9 @@ public class CommandsRegister {
return 0;
}
private enum TestType {
String,
Text;
}
}