Skip to content

Commit 3e598f8

Browse files
committed
feat: enhance pagination management with max page tracking and UI adjustments
1 parent e6d73dc commit 3e598f8

5 files changed

Lines changed: 84 additions & 5 deletions

File tree

API/src/main/java/fr/maxlego08/menu/api/button/GenericPaginationButton.java

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -43,10 +43,12 @@ public final void onRender(@NotNull Player player, @NotNull InventoryEngine inve
4343
int pageSize = slots.size();
4444
int currentPage = getCurrentPageOneIndexed(player);
4545

46-
int maxPage = getMaxPage(player, pageSize) + 1;
46+
int maxPage = getMaxPage(player, pageSize);
47+
48+
getPaginationManager().setMaxPage(player.getUniqueId(), getContextId(player), maxPage);
4749

4850
Pagination<T> pagination = new Pagination<>();
49-
List<T> paginatedElements = pagination.paginate(elements, pageSize, currentPage - 1);
51+
List<T> paginatedElements = pagination.paginate(elements, pageSize, currentPage);
5052

5153
int slotIndex = 0;
5254
for (Integer slot : slots) {
@@ -55,7 +57,7 @@ public final void onRender(@NotNull Player player, @NotNull InventoryEngine inve
5557
T element = paginatedElements.get(slotIndex);
5658
Placeholders placeholders = new Placeholders();
5759
placeholders.register("page", String.valueOf(currentPage));
58-
placeholders.register("max_page", String.valueOf(maxPage));
60+
placeholders.register("max_page", String.valueOf(maxPage + 1));
5961

6062
renderElement(player, inventory, slot, element, placeholders);
6163
slotIndex++;

API/src/main/java/fr/maxlego08/menu/api/pagination/PaginationManager.java

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -95,6 +95,24 @@ default boolean hasState(@NotNull UUID playerId, @NotNull String contextId) {
9595
*/
9696
void removePlayerStates(@NotNull UUID playerId);
9797

98+
/**
99+
* Gets the maximum page for a player and context.
100+
*
101+
* @param playerId the player's UUID
102+
* @param contextId the context identifier
103+
* @return the maximum page (0-based index), or 0 if not found
104+
*/
105+
int getMaxPage(@NotNull UUID playerId, @NotNull String contextId);
106+
107+
/**
108+
* Sets the maximum page for a player and context.
109+
*
110+
* @param playerId the player's UUID
111+
* @param contextId the context identifier
112+
* @param maxPage the maximum page to set (0-based index)
113+
*/
114+
void setMaxPage(@NotNull UUID playerId, @NotNull String contextId, int maxPage);
115+
98116
/**
99117
* Clears all pagination states.
100118
*/

API/src/main/java/fr/maxlego08/menu/api/pagination/PaginationState.java

Lines changed: 29 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@
22

33
public class PaginationState {
44
private int currentPage;
5+
private int maxPage = 0;
56

67
public PaginationState() {
78
this(0);
@@ -39,8 +40,35 @@ public void previousPage() {
3940
}
4041
}
4142

43+
/**
44+
* Gets the maximum page number (0-based index).
45+
*
46+
* @return the maximum page
47+
*/
48+
public int getMaxPage() {
49+
return maxPage;
50+
}
51+
52+
/**
53+
* Sets the maximum page number (0-based index).
54+
*
55+
* @param maxPage the maximum page to set
56+
*/
57+
public void setMaxPage(int maxPage) {
58+
this.maxPage = Math.max(0, maxPage);
59+
}
60+
61+
/**
62+
* Gets the maximum page number (1-based index for UI purposes).
63+
*
64+
* @return the maximum page (1-based)
65+
*/
66+
public int getMaxPageOneIndexed() {
67+
return this.maxPage + 1;
68+
}
69+
4270
@Override
4371
public String toString() {
44-
return String.format("PaginationState{currentPage=%d}", currentPage);
72+
return String.format("PaginationState{currentPage=%d, maxPage=%d}", currentPage, maxPage);
4573
}
4674
}

src/main/java/fr/maxlego08/menu/pagination/ZPaginationManager.java

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -45,6 +45,17 @@ public void reset(@NotNull UUID playerId, @NotNull String contextId) {
4545
removeState(playerId, contextId);
4646
}
4747

48+
@Override
49+
public int getMaxPage(@NotNull UUID playerId, @NotNull String contextId) {
50+
PaginationState state = getState(playerId, contextId);
51+
return state != null ? state.getMaxPage() : 0;
52+
}
53+
54+
@Override
55+
public void setMaxPage(@NotNull UUID playerId, @NotNull String contextId, int maxPage) {
56+
getOrCreateState(playerId, contextId).setMaxPage(maxPage);
57+
}
58+
4859
@Override
4960
public @Nullable PaginationState getState(@NotNull UUID playerId, @NotNull String contextId) {
5061
Map<String, PaginationState> playerStates = paginationStates.get(playerId);

src/main/java/fr/maxlego08/menu/placeholder/MenuPlaceholders.java

Lines changed: 21 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -41,7 +41,7 @@ public void register(MenuPlugin plugin) {
4141

4242
placeholder.register("pagination_next_page_", (player, contextId) -> {
4343
if (paginationManager == null) return "0";
44-
return String.valueOf(paginationManager.getPage(player.getUniqueId(), contextId) + 1);
44+
return String.valueOf(paginationManager.getPage(player.getUniqueId(), contextId) + 2);
4545
});
4646

4747
placeholder.register("pagination_previous_page_", (player, contextId) -> {
@@ -50,6 +50,26 @@ public void register(MenuPlugin plugin) {
5050
return String.valueOf(Math.max(0, currentPage - 1));
5151
});
5252

53+
placeholder.register("pagination_max_page_", (player, contextId) -> {
54+
if (paginationManager == null) return "1";
55+
56+
String actualContextId = contextId;
57+
int defaultMaxPage = 0;
58+
59+
if (contextId.contains(":")) {
60+
String[] parts = contextId.split(":", 2);
61+
actualContextId = parts[0];
62+
try {
63+
defaultMaxPage = Integer.parseInt(parts[1]);
64+
} catch (NumberFormatException ignored) {
65+
}
66+
}
67+
68+
int storedMaxPage = paginationManager.getMaxPage(player.getUniqueId(), actualContextId);
69+
int maxPage = Math.max(storedMaxPage, defaultMaxPage);
70+
return String.valueOf(maxPage + 1);
71+
});
72+
5373
placeholder.register("player_previous_inventories", (playeofflinePlayer, s) -> {
5474
if (playeofflinePlayer.isOnline()) {
5575
Player player = playeofflinePlayer.getPlayer();

0 commit comments

Comments
 (0)