Pagination - hamza-cskn/obliviate-invs GitHub Wiki
Pagination
All GUI objects should have a special pagination manager.
TL;DR
Allows you to show tons of items part by part. Just copy the code blocks.
How to use
Define Pagination Manager of the GUI
public class TestGUI extends GUI {
private final PaginationManager pagination = new PaginationManager(this);
}
Registering Page Slots
this.pagination.registerItemSlots(10, 11, 12, 13, 14, 15);
//or
this.pagination.registerSlotsBetween(10, 15);
this.pagination.getSlots();
Registering Icons
You must add all icons for all pages.
for (Material material : Material.values()) {
this.pagination.addItem(new Icon(material));
}
Show
this.pagination.update(); // puts page icons
this.pagination.goFirstPage(); // sets page to 0
this.pagination.goLastPage(); // sets page to last page
this.pagination.isFirstPage();
this.pagination.isLastPage();
this.pagination.goNextPage(); // sets page to page+1
this.pagination.goPreviousPage(); // sets page to page-1
this.pagination.setPage(3); // sets page to 3
this.pagination.getPage();
this.pagination.getLastPage();
this.pagination.getItems();
Example Gif
Real World Example
- XMaterials.
- Page navigators.
- Custom gui titles.
- Formatted lores and display names.
package mc.obliviate.supershops.gui;
import mc.obliviate.inventory.Gui;
import mc.obliviate.inventory.Icon;
import mc.obliviate.inventory.pagination.PaginationManager;
import mc.obliviate.supershops.product.ShopProduct;
import mc.obliviate.supershops.shop.category.Category;
import mc.obliviate.supershops.user.ShopUser;
import mc.obliviate.supershops.utils.XMaterial;
import mc.obliviate.supershops.utils.message.MessageUtils;
import mc.obliviate.supershops.utils.message.placeholder.PlaceholderUtil;
import org.bukkit.Material;
import org.bukkit.enchantments.Enchantment;
import org.bukkit.entity.Player;
import org.bukkit.event.inventory.InventoryOpenEvent;
import org.bukkit.inventory.ItemFlag;
public class CategoryGUI extends Gui {
private final PaginationManager paginationManager = new PaginationManager(this);
private final Category category;
private final ShopUser user;
//handles initialization of the gui. registers gui title and pagination slots.
public CategoryGUI(Player player, Category category) {
super(player, "category-gui", category.getShop().getShopDisplayName() + " > " + category.getName(), 6);
this.category = category;
this.user = ShopUser.getUser(player.getUniqueId());
paginationManager.registerPageSlotsBetween(9, 44);
}
//handles onOpen event and inserts static items like stained glasses.
@Override
public void onOpen(InventoryOpenEvent event) {
fillRow(new Icon(XMaterial.GRAY_STAINED_GLASS_PANE.parseItem()), 0);
fillRow(new Icon(XMaterial.BLACK_STAINED_GLASS_PANE.parseItem()), 5);
calculateAndUpdatePagination();
if (paginationManager.getCurrentPage() != 0) {
addItem(0, new Icon(XMaterial.ARROW.parseItem()).setName("§aPrevious").onClick(e -> {
paginationManager.goPreviousPage();
calculateAndUpdatePagination();
}));
}
if (!paginationManager.isLastPage()) {
addItem(8, new Icon(XMaterial.ARROW.parseItem()).setName("§aNext").onClick(e -> {
paginationManager.goNextPage();
calculateAndUpdatePagination();
}));
}
putStackSizeIcons();
}
// makes glowed selected stack size icon.
private void putStackSizeIcon(int slot, int stackSize) {
final Icon icon = new Icon(XMaterial.CHEST_MINECART.parseItem()).onClick(e -> {
user.setStackSize(stackSize);
open();
})
.setName(MessageUtils.formatName(new PlaceholderUtil().add("{stack}", stackSize + ""), "stack-size-selector"))
.setLore(MessageUtils.formatLore(new PlaceholderUtil().add("{stack}", stackSize + ""), "stack-size-selector"))
.setAmount(stackSize);
if (user.getStackSize() == stackSize) {
icon.enchant(Enchantment.PROTECTION_ENVIRONMENTAL, 1);
icon.hideFlags(ItemFlag.HIDE_ENCHANTS);
}
addItem(slot, icon);
}
//updates stack size bar.
private void putStackSizeIcons() {
putStackSizeIcon(2, 1);
putStackSizeIcon(3, 8);
putStackSizeIcon(4, 16);
putStackSizeIcon(5, 32);
putStackSizeIcon(6, 64);
}
//calculates content of the shop gui.
private void calculateProducts() {
paginationManager.getItems().clear();
for (final ShopProduct product : category.getProducts()) {
Icon icon = new Icon(product.getIconItem(user.getStackSize()));
int amount = Math.min(user.getStackSize(), icon.getItem().getMaxStackSize());
paginationManager.addItem(new Icon(product.getIconItem(user.getStackSize())).setAmount(amount).onClick(e -> {
if (product.isBuyable() && !product.isSellable()) {
product.buy(player, amount);
} else if (!product.isBuyable() && product.isSellable()) {
product.sell(player, amount);
} else if (product.isSellable() && product.isBuyable()) {
if (e.isLeftClick()) {
product.buy(player, amount);
} else if (e.isRightClick()) {
product.sell(player, amount);
}
}
}));
}
}
// calculates and updates content of the shop gui.
private void calculateAndUpdatePagination() {
calculateProducts();
paginationManager.update();
}
}