Added builder, DataTracker and KeyValue #16
@ -14,7 +14,7 @@ import java.util.stream.Stream;
|
||||
public class PaginatedContent {
|
||||
|
||||
private final List<Page> pages;
|
||||
private final String header;
|
||||
private final Text header;
|
||||
|
||||
public PaginatedContent(String header, String content) {
|
||||
this.header = buildHeader(header);
|
||||
@ -29,11 +29,11 @@ public class PaginatedContent {
|
||||
}
|
||||
}
|
||||
int line = 0;
|
||||
List<String> currentPage = new ArrayList<>();
|
||||
List<Text> currentPage = new ArrayList<>();
|
||||
for(String elem : secondSplit) {
|
||||
line++;
|
||||
if(!elem.isEmpty())
|
||||
currentPage.add(elem);
|
||||
currentPage.add(Text.literal(elem));
|
||||
if(line == 8 || elem.isEmpty()) {
|
||||
pages.add(new Page(currentPage));
|
||||
line = 0;
|
||||
@ -43,9 +43,9 @@ public class PaginatedContent {
|
||||
pages.add(new Page(currentPage));
|
||||
}
|
||||
|
||||
private String buildHeader(String header) {
|
||||
private Text buildHeader(String header) {
|
||||
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) {
|
||||
@ -54,9 +54,9 @@ public class PaginatedContent {
|
||||
} else if(page < 0) {
|
||||
throw new IllegalArgumentException("argument page is lower than 0");
|
||||
} else {
|
||||
playerEntity.sendMessage(Text.literal(header));
|
||||
for(String s : pages.get(page).lines) {
|
||||
playerEntity.sendMessage(Text.literal(s));
|
||||
playerEntity.sendMessage(header);
|
||||
for(Text s : pages.get(page).lines) {
|
||||
playerEntity.sendMessage(s);
|
||||
}
|
||||
|
||||
playerEntity.sendMessage(buildFooter(page));
|
||||
@ -87,7 +87,7 @@ public class PaginatedContent {
|
||||
);
|
||||
}
|
||||
|
||||
private record Page(List<String> lines) {
|
||||
private record Page(List<Text> lines) {
|
||||
}
|
||||
|
||||
}
|
||||
|
@ -2,7 +2,6 @@ package fr.altarik.toolbox.pagination.api;
|
||||
|
||||
import net.minecraft.server.network.ServerPlayerEntity;
|
||||
|
||||
@SuppressWarnings("unused") // Api usage
|
||||
public interface PaginationApi {
|
||||
|
||||
/**
|
||||
@ -15,13 +14,15 @@ public interface PaginationApi {
|
||||
* <p>Special values are:
|
||||
* <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>
|
||||
* @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>
|
||||
* <li><b>header</b> length is more than 50 characters</li>
|
||||
* <li><b>content</b> is empty/blank</li>
|
||||
* <li><b>playerEntity</b> or <b>content</b> are null</li>
|
||||
* </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
|
||||
|
@ -29,13 +29,14 @@ public class PaginationApiImpl implements PaginationApi {
|
||||
}
|
||||
|
||||
@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)) {
|
||||
throw new IllegalArgumentException("Preconditions aren't satisfied");
|
||||
}
|
||||
PaginatedContent paginatedContent1 = new PaginatedContent(header, content);
|
||||
paginatedContent.put(playerEntity, new Pair<>(18000, paginatedContent1));
|
||||
paginatedContent1.display(playerEntity, 0);
|
||||
if(display)
|
||||
paginatedContent1.display(playerEntity, 0);
|
||||
}
|
||||
|
||||
@Override
|
||||
|
@ -52,7 +52,7 @@ public class CommandsRegister {
|
||||
third page
|
||||
dqdqd
|
||||
dqdqd
|
||||
d""", "My header");
|
||||
d""", "My header", true);
|
||||
return 0;
|
||||
}
|
||||
|
||||
|
Loading…
Reference in New Issue
Block a user