Added builder, DataTracker and KeyValue #16
@ -1,5 +1,6 @@
|
|||||||
package fr.altarik.toolbox.pagination;
|
package fr.altarik.toolbox.pagination;
|
||||||
|
|
||||||
|
import fr.altarik.toolbox.pagination.api.PageIndexOutOfBoundException;
|
||||||
import net.minecraft.server.network.ServerPlayerEntity;
|
import net.minecraft.server.network.ServerPlayerEntity;
|
||||||
import net.minecraft.text.ClickEvent;
|
import net.minecraft.text.ClickEvent;
|
||||||
import net.minecraft.text.MutableText;
|
import net.minecraft.text.MutableText;
|
||||||
@ -45,14 +46,14 @@ public class PaginatedContent {
|
|||||||
|
|
||||||
private Text buildHeader(String header) {
|
private Text buildHeader(String header) {
|
||||||
int numberOfEq = (50 - header.length()) / 2;
|
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()) {
|
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) {
|
} else if(page < 0) {
|
||||||
throw new IllegalArgumentException("argument page is lower than 0");
|
throw new PageIndexOutOfBoundException("api.pagination.page_lower_than_0");
|
||||||
} else {
|
} else {
|
||||||
playerEntity.sendMessage(header);
|
playerEntity.sendMessage(header);
|
||||||
for(Text s : pages.get(page).lines) {
|
for(Text s : pages.get(page).lines) {
|
||||||
@ -66,13 +67,13 @@ public class PaginatedContent {
|
|||||||
private Text buildFooter(int page) {
|
private Text buildFooter(int page) {
|
||||||
String strPage = String.valueOf(page + 1);
|
String strPage = String.valueOf(page + 1);
|
||||||
int numberOfEq = (46 - strPage.length()) / 2;
|
int numberOfEq = (46 - strPage.length()) / 2;
|
||||||
MutableText left = Text.literal("<").styled(
|
MutableText left = Text.literal(" <").styled(
|
||||||
style -> style
|
style -> style
|
||||||
.withColor(Formatting.YELLOW)
|
.withColor(Formatting.YELLOW)
|
||||||
.withClickEvent(new ClickEvent(ClickEvent.Action.RUN_COMMAND, "/table page " + (page - 1)))
|
.withClickEvent(new ClickEvent(ClickEvent.Action.RUN_COMMAND, "/table page " + (page - 1)))
|
||||||
);
|
);
|
||||||
MutableText middle = Text.literal(" " + strPage + " ").styled(style -> style.withColor(Formatting.RESET));
|
MutableText middle = Text.literal(" " + strPage + " ").styled(style -> style.withColor(Formatting.RESET));
|
||||||
MutableText right = Text.literal(">").styled(
|
MutableText right = Text.literal("> ").styled(
|
||||||
style -> style
|
style -> style
|
||||||
.withColor(Formatting.YELLOW)
|
.withColor(Formatting.YELLOW)
|
||||||
.withClickEvent(new ClickEvent(ClickEvent.Action.RUN_COMMAND, "/table page " + (page + 1)))
|
.withClickEvent(new ClickEvent(ClickEvent.Action.RUN_COMMAND, "/table page " + (page + 1)))
|
||||||
|
@ -19,13 +19,14 @@ public class Pagination implements ModInitializer {
|
|||||||
|
|
||||||
@Override
|
@Override
|
||||||
public void onInitialize() {
|
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() {
|
public @NotNull PaginationApi getApi() {
|
||||||
return api;
|
return api;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@SuppressWarnings("unused")
|
||||||
public static @NotNull Pagination getInstance() {
|
public static @NotNull Pagination getInstance() {
|
||||||
return instance;
|
return instance;
|
||||||
}
|
}
|
||||||
|
@ -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;
|
||||||
|
}
|
||||||
|
}
|
@ -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)
|
* @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)
|
* @see fr.altarik.toolbox.pagination.PaginatedContent#display(ServerPlayerEntity, int)
|
||||||
*/
|
*/
|
||||||
void display(ServerPlayerEntity player, int page);
|
void display(ServerPlayerEntity player, int page) throws PageIndexOutOfBoundException;
|
||||||
|
|
||||||
}
|
}
|
||||||
|
@ -35,12 +35,17 @@ public class PaginationApiImpl implements PaginationApi {
|
|||||||
}
|
}
|
||||||
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));
|
||||||
if(display)
|
if(display) {
|
||||||
|
try {
|
||||||
paginatedContent1.display(playerEntity, 0);
|
paginatedContent1.display(playerEntity, 0);
|
||||||
|
} catch (PageIndexOutOfBoundException e) {
|
||||||
|
throw new IllegalArgumentException(e);
|
||||||
|
}
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
public void display(ServerPlayerEntity player, int page) {
|
public void display(ServerPlayerEntity player, int page) throws PageIndexOutOfBoundException {
|
||||||
if(player == null)
|
if(player == null)
|
||||||
throw new NullPointerException("Player is null");
|
throw new NullPointerException("Player is null");
|
||||||
Pair<Integer, PaginatedContent> pair = paginatedContent.get(player);
|
Pair<Integer, PaginatedContent> pair = paginatedContent.get(player);
|
||||||
|
@ -4,10 +4,11 @@ import com.mojang.brigadier.CommandDispatcher;
|
|||||||
import com.mojang.brigadier.arguments.IntegerArgumentType;
|
import com.mojang.brigadier.arguments.IntegerArgumentType;
|
||||||
import com.mojang.brigadier.context.CommandContext;
|
import com.mojang.brigadier.context.CommandContext;
|
||||||
import com.mojang.brigadier.exceptions.CommandSyntaxException;
|
import com.mojang.brigadier.exceptions.CommandSyntaxException;
|
||||||
|
import com.mojang.brigadier.exceptions.SimpleCommandExceptionType;
|
||||||
import fr.altarik.toolbox.pagination.Pagination;
|
import fr.altarik.toolbox.pagination.Pagination;
|
||||||
|
import fr.altarik.toolbox.pagination.api.PageIndexOutOfBoundException;
|
||||||
import fr.altarik.toolbox.pagination.api.PaginationApi;
|
import fr.altarik.toolbox.pagination.api.PaginationApi;
|
||||||
import net.minecraft.server.command.ServerCommandSource;
|
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.argument;
|
||||||
import static net.minecraft.server.command.CommandManager.literal;
|
import static net.minecraft.server.command.CommandManager.literal;
|
||||||
@ -20,7 +21,7 @@ public class CommandsRegister {
|
|||||||
this.api = instance.getApi();
|
this.api = instance.getApi();
|
||||||
}
|
}
|
||||||
|
|
||||||
public void register(CommandDispatcher<ServerCommandSource> dispatcher, boolean dedicated) {
|
public void register(CommandDispatcher<ServerCommandSource> dispatcher) {
|
||||||
dispatcher.register(literal("table")
|
dispatcher.register(literal("table")
|
||||||
.then(literal("page")
|
.then(literal("page")
|
||||||
.then(argument("page", IntegerArgumentType.integer())
|
.then(argument("page", IntegerArgumentType.integer())
|
||||||
@ -60,8 +61,8 @@ public class CommandsRegister {
|
|||||||
try {
|
try {
|
||||||
int page = IntegerArgumentType.getInteger(context, "page");
|
int page = IntegerArgumentType.getInteger(context, "page");
|
||||||
api.display(context.getSource().getPlayerOrThrow(), page);
|
api.display(context.getSource().getPlayerOrThrow(), page);
|
||||||
} catch(NullPointerException | IllegalArgumentException e) {
|
} catch(PageIndexOutOfBoundException e) {
|
||||||
context.getSource().sendFeedback(Text.literal("Error: " + e.getMessage()), false);
|
throw new CommandSyntaxException(new SimpleCommandExceptionType(e.getText()), e.getText());
|
||||||
}
|
}
|
||||||
return 0;
|
return 0;
|
||||||
}
|
}
|
||||||
|
@ -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"
|
||||||
|
}
|
Loading…
Reference in New Issue
Block a user