|
| 1 | +package fr.maxlego08.menu.api.button; |
| 2 | + |
| 3 | +import fr.maxlego08.menu.api.pagination.PaginationManager; |
| 4 | +import org.bukkit.entity.Player; |
| 5 | +import org.jetbrains.annotations.NotNull; |
| 6 | + |
| 7 | +public abstract class GenericPaginateButton extends PaginateButton { |
| 8 | + |
| 9 | + @NotNull |
| 10 | + public abstract String getContextId(@NotNull Player player); |
| 11 | + |
| 12 | + |
| 13 | + @NotNull |
| 14 | + public abstract PaginationManager getPaginationManager(); |
| 15 | + |
| 16 | + /** |
| 17 | + * Gets the current page for this button (0-based index). |
| 18 | + * |
| 19 | + * @param player the player |
| 20 | + * @return the current page |
| 21 | + */ |
| 22 | + public final int getCurrentPage(@NotNull Player player) { |
| 23 | + return getPaginationManager().getPage(player.getUniqueId(), getContextId(player)); |
| 24 | + } |
| 25 | + |
| 26 | + /** |
| 27 | + * Gets the current page (1-based index) for UI purposes. |
| 28 | + * |
| 29 | + * @param player the player |
| 30 | + * @return the current page (1-based) |
| 31 | + */ |
| 32 | + public final int getCurrentPageOneIndexed(@NotNull Player player) { |
| 33 | + return getCurrentPage(player) + 1; |
| 34 | + } |
| 35 | + |
| 36 | + /** |
| 37 | + * Sets the current page for this button. |
| 38 | + * |
| 39 | + * @param player the player |
| 40 | + * @param page the page to set (0-based index) |
| 41 | + */ |
| 42 | + public final void setCurrentPage(@NotNull Player player, int page) { |
| 43 | + getPaginationManager().setPage(player.getUniqueId(), getContextId(player), page); |
| 44 | + } |
| 45 | + |
| 46 | + /** |
| 47 | + * Advances to the next page if available. |
| 48 | + * |
| 49 | + * @param player the player |
| 50 | + * @return true if advanced, false if already at the last page |
| 51 | + */ |
| 52 | + public final boolean nextPage(@NotNull Player player) { |
| 53 | + int currentPage = getCurrentPage(player); |
| 54 | + int maxPage = getMaxPage(player); |
| 55 | + if (currentPage < maxPage) { |
| 56 | + getPaginationManager().nextPage(player.getUniqueId(), getContextId(player)); |
| 57 | + return true; |
| 58 | + } |
| 59 | + return false; |
| 60 | + } |
| 61 | + |
| 62 | + /** |
| 63 | + * Goes to the previous page if available. |
| 64 | + * |
| 65 | + * @param player the player |
| 66 | + * @return true if went back, false if already at the first page |
| 67 | + */ |
| 68 | + public final boolean previousPage(@NotNull Player player) { |
| 69 | + int currentPage = getCurrentPage(player); |
| 70 | + if (currentPage > 0) { |
| 71 | + getPaginationManager().previousPage(player.getUniqueId(), getContextId(player)); |
| 72 | + return true; |
| 73 | + } |
| 74 | + return false; |
| 75 | + } |
| 76 | + |
| 77 | + /** |
| 78 | + * Resets pagination to the first page. |
| 79 | + * |
| 80 | + * @param player the player |
| 81 | + */ |
| 82 | + public final void resetPagination(@NotNull Player player) { |
| 83 | + getPaginationManager().reset(player.getUniqueId(), getContextId(player)); |
| 84 | + } |
| 85 | + |
| 86 | + /** |
| 87 | + * Calculates the maximum page number (0-based index). |
| 88 | + * This caches the page size to avoid multiple getSlots() calls. |
| 89 | + * |
| 90 | + * @param player the player |
| 91 | + * @return the maximum page |
| 92 | + */ |
| 93 | + public final int getMaxPage(@NotNull Player player) { |
| 94 | + int totalSize = getPaginationSize(player); |
| 95 | + int pageSize = getSlots().size(); |
| 96 | + if (pageSize <= 0) return 0; |
| 97 | + return Math.max(0, (totalSize - 1) / pageSize); |
| 98 | + } |
| 99 | + |
| 100 | + /** |
| 101 | + * Calculates the maximum page number with pre-calculated page size (0-based index). |
| 102 | + * Use this when page size is already available to avoid redundant getSlots() calls. |
| 103 | + * |
| 104 | + * @param player the player |
| 105 | + * @param pageSize the page size |
| 106 | + * @return the maximum page |
| 107 | + */ |
| 108 | + public final int getMaxPage(@NotNull Player player, int pageSize) { |
| 109 | + if (pageSize <= 0) return 0; |
| 110 | + int totalSize = getPaginationSize(player); |
| 111 | + return Math.max(0, (totalSize - 1) / pageSize); |
| 112 | + } |
| 113 | + |
| 114 | + /** |
| 115 | + * Checks if there's a next page available. |
| 116 | + * |
| 117 | + * @param player the player |
| 118 | + * @return true if there's a next page |
| 119 | + */ |
| 120 | + public final boolean hasNextPage(@NotNull Player player) { |
| 121 | + int currentPage = getCurrentPage(player); |
| 122 | + return currentPage < getMaxPage(player); |
| 123 | + } |
| 124 | + |
| 125 | + /** |
| 126 | + * Checks if there's a previous page available. |
| 127 | + * |
| 128 | + * @param player the player |
| 129 | + * @return true if there's a previous page |
| 130 | + */ |
| 131 | + public final boolean hasPreviousPage(@NotNull Player player) { |
| 132 | + return getCurrentPage(player) > 0; |
| 133 | + } |
| 134 | + |
| 135 | +} |
| 136 | + |
0 commit comments