forked from Maxlego08/zMenu
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathGenericPaginationButton.java
More file actions
68 lines (54 loc) · 2.2 KB
/
GenericPaginationButton.java
File metadata and controls
68 lines (54 loc) · 2.2 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
package fr.maxlego08.menu.api.button;
import fr.maxlego08.menu.api.engine.InventoryEngine;
import fr.maxlego08.menu.api.engine.Pagination;
import fr.maxlego08.menu.api.utils.Placeholders;
import org.bukkit.entity.Player;
import org.jetbrains.annotations.NotNull;
import java.util.Collection;
import java.util.List;
public abstract class GenericPaginationButton <T> extends GenericPaginateButton {
/**
* Gets the list of elements to paginate.
*
* @param player the player
* @return the list of elements
*/
@NotNull
protected abstract List<T> getElements(@NotNull Player player);
/**
* Renders a single element at the given slot.
*
* @param player the player
* @param inventory the inventory engine
* @param slot the inventory slot
* @param element the element to render
* @param placeholders the placeholders
*/
protected abstract void renderElement(
@NotNull Player player,
@NotNull InventoryEngine inventory,
int slot,
@NotNull T element,
@NotNull Placeholders placeholders);
@Override
public final void onRender(@NotNull Player player, @NotNull InventoryEngine inventory) {
List<T> elements = getElements(player);
Collection<Integer> slots = getSlots();
int pageSize = slots.size();
int currentPage = getCurrentPageOneIndexed(player);
int maxPage = getMaxPage(player, pageSize);
getPaginationManager().setMaxPage(player.getUniqueId(), getContextId(player), maxPage);
Pagination<T> pagination = new Pagination<>();
List<T> paginatedElements = pagination.paginate(elements, pageSize, currentPage);
int slotIndex = 0;
for (Integer slot : slots) {
if (slotIndex >= paginatedElements.size()) break;
T element = paginatedElements.get(slotIndex);
Placeholders placeholders = new Placeholders();
placeholders.register("page", String.valueOf(currentPage));
placeholders.register("max_page", String.valueOf(maxPage + 1));
renderElement(player, inventory, slot, element, placeholders);
slotIndex++;
}
}
}