Added builder, DataTracker and KeyValue #16

Manually merged
quentinlegot merged 40 commits from dev into master 2023-06-13 22:15:40 +02:00
4 changed files with 16 additions and 14 deletions
Showing only changes of commit 5d6cf24582 - Show all commits

View File

@ -14,7 +14,7 @@ import java.util.stream.Stream;
public class PaginatedContent { public class PaginatedContent {
private final List<Page> pages; private final List<Page> pages;
private final String header; private final Text header;
public PaginatedContent(String header, String content) { public PaginatedContent(String header, String content) {
this.header = buildHeader(header); this.header = buildHeader(header);
@ -29,11 +29,11 @@ public class PaginatedContent {
} }
} }
int line = 0; int line = 0;
List<String> currentPage = new ArrayList<>(); List<Text> currentPage = new ArrayList<>();
for(String elem : secondSplit) { for(String elem : secondSplit) {
line++; line++;
if(!elem.isEmpty()) if(!elem.isEmpty())
currentPage.add(elem); currentPage.add(Text.literal(elem));
if(line == 8 || elem.isEmpty()) { if(line == 8 || elem.isEmpty()) {
pages.add(new Page(currentPage)); pages.add(new Page(currentPage));
line = 0; line = 0;
@ -43,9 +43,9 @@ public class PaginatedContent {
pages.add(new Page(currentPage)); pages.add(new Page(currentPage));
} }
private String buildHeader(String header) { private Text buildHeader(String header) {
int numberOfEq = (50 - header.length()) / 2; int numberOfEq = (50 - header.length()) / 2;
return "=".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) {
@ -54,9 +54,9 @@ public class PaginatedContent {
} else if(page < 0) { } else if(page < 0) {
throw new IllegalArgumentException("argument page is lower than 0"); throw new IllegalArgumentException("argument page is lower than 0");
} else { } else {
playerEntity.sendMessage(Text.literal(header)); playerEntity.sendMessage(header);
for(String s : pages.get(page).lines) { for(Text s : pages.get(page).lines) {
playerEntity.sendMessage(Text.literal(s)); playerEntity.sendMessage(s);
} }
playerEntity.sendMessage(buildFooter(page)); playerEntity.sendMessage(buildFooter(page));
@ -87,7 +87,7 @@ public class PaginatedContent {
); );
} }
private record Page(List<String> lines) { private record Page(List<Text> lines) {
} }
} }

View File

@ -2,7 +2,6 @@ package fr.altarik.toolbox.pagination.api;
import net.minecraft.server.network.ServerPlayerEntity; import net.minecraft.server.network.ServerPlayerEntity;
@SuppressWarnings("unused") // Api usage
public interface PaginationApi { public interface PaginationApi {
/** /**
@ -15,13 +14,15 @@ public interface PaginationApi {
* <p>Special values are: * <p>Special values are:
* <ul><li><b>null</b> if you doesn't want to add a header</li> * <ul><li><b>null</b> if you doesn't want to add a header</li>
* <li><b>empty String</b> if you want just the header to be filled only with "="</li></ul> * <li><b>empty String</b> if you want just the header to be filled only with "="</li></ul>
* @param display true if you want the message to be displayed now, false otherwise if you want to display the
* message yourself
* @throws IllegalArgumentException if one of its conditions is met: <ol> * @throws IllegalArgumentException if one of its conditions is met: <ol>
* <li><b>header</b> length is more than 50 characters</li> * <li><b>header</b> length is more than 50 characters</li>
* <li><b>content</b> is empty/blank</li> * <li><b>content</b> is empty/blank</li>
* <li><b>playerEntity</b> or <b>content</b> are null</li> * <li><b>playerEntity</b> or <b>content</b> are null</li>
* </ol> * </ol>
*/ */
void createTable(ServerPlayerEntity playerEntity, String content, String header); void createTable(ServerPlayerEntity playerEntity, String content, String header, boolean display);
/** /**
* Display the given page for the given player * Display the given page for the given player

View File

@ -29,13 +29,14 @@ public class PaginationApiImpl implements PaginationApi {
} }
@Override @Override
public void createTable(ServerPlayerEntity playerEntity, String content, String header) { public void createTable(ServerPlayerEntity playerEntity, String content, String header, boolean display) {
if(playerCondition.test(playerEntity) || headerCondition.test(header) || contentCondition.test(content)) { if(playerCondition.test(playerEntity) || headerCondition.test(header) || contentCondition.test(content)) {
throw new IllegalArgumentException("Preconditions aren't satisfied"); throw new IllegalArgumentException("Preconditions aren't satisfied");
} }
PaginatedContent paginatedContent1 = new PaginatedContent(header, content); PaginatedContent paginatedContent1 = new PaginatedContent(header, content);
paginatedContent.put(playerEntity, new Pair<>(18000, paginatedContent1)); paginatedContent.put(playerEntity, new Pair<>(18000, paginatedContent1));
paginatedContent1.display(playerEntity, 0); if(display)
paginatedContent1.display(playerEntity, 0);
} }
@Override @Override

View File

@ -52,7 +52,7 @@ public class CommandsRegister {
third page third page
dqdqd dqdqd
dqdqd dqdqd
d""", "My header"); d""", "My header", true);
return 0; return 0;
} }