Guest User

Untitled

a guest
May 3rd, 2025
5
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 4.29 KB | None | 0 0
  1. package com.bluenova.floorislava.util.gui.objects;
  2.  
  3. import com.bluenova.floorislava.FloorIsLava;
  4. import com.bluenova.floorislava.util.gui.GuiManager;
  5. import com.bluenova.floorislava.util.gui.InventoryButton;
  6. import org.bukkit.Material;
  7. import org.bukkit.entity.Player;
  8. import org.bukkit.inventory.ItemStack;
  9.  
  10. import java.util.ArrayList;
  11. import java.util.List;
  12.  
  13. public abstract class Paginator<T> extends InventoryGui {
  14.  
  15. protected final int itemsPerPage;
  16. protected List<T> itemList;
  17. protected int currentPage = 0;
  18.  
  19. // This inventory is created by the InventoryGui constructor (super())
  20.  
  21. public Paginator(ArrayList<T> itemList) {
  22. super(); // Creates the inventory via InventoryGui constructor -> createInventory()
  23. // Ensure createInventory() in your Paginator implementation creates the right size (e.g., 54 slots)
  24. this.itemsPerPage = getInventory().getSize() - 9; // Calculate based on actual inv size minus one row for controls
  25. this.itemList = itemList;
  26. updateInventoryContent(); // Initial population of buttonMap. Decoration happens in onOpen.
  27. }
  28.  
  29. protected void updateInventoryContent() {
  30. FloorIsLava.getInstance().getPluginLogger().info("Updating inventory content map for page " + currentPage);
  31.  
  32. if (this.inventory != null) {
  33. this.inventory.clear();
  34. }
  35.  
  36. // *** 3. Clear the button map to prepare for the new page's buttons ***
  37. this.buttonMap.clear();
  38.  
  39. // --- Add Item Buttons ---
  40. int startIndex = currentPage * itemsPerPage;
  41. int endIndex = Math.min(startIndex + itemsPerPage, itemList.size());
  42.  
  43. FloorIsLava.getInstance().getPluginLogger().debug(
  44. "Calculating loop bounds: currentPage=" + currentPage +
  45. ", itemsPerPage=" + itemsPerPage +
  46. ", calculatedStartIndex=" + startIndex +
  47. ", itemList.size=" + (itemList == null ? "null" : itemList.size()) + // Check for null list
  48. ", calculatedEndIndex=" + endIndex
  49. );
  50. for (int i = startIndex; i < endIndex; i++) {
  51. int itemSlot = i - startIndex; // Slot index within the item display area (0 to itemsPerPage-1)
  52. InventoryButton button = getUnitButton(itemList.get(i));
  53. if (button != null) {
  54. this.addButton(itemSlot, button);
  55. }
  56. }
  57.  
  58. // --- Add Navigation Buttons ---
  59. int inventorySize = getInventory().getSize();
  60. int previousButtonSlot = inventorySize - 9; // e.g., slot 45 in a 54-slot inventory
  61. int nextButtonSlot = inventorySize - 1; // e.g., slot 53 in a 54-slot inventory
  62.  
  63. if (currentPage > 0) {
  64. this.addButton(previousButtonSlot, new InventoryButton()
  65. .creator(player -> new ItemStack(Material.ARROW)) // Creator used by decorate
  66. .consumer(event -> this.previousPage((Player) event.getWhoClicked())));
  67. }
  68.  
  69. if (itemList != null && (currentPage + 1) * itemsPerPage < itemList.size()) {
  70. this.addButton(nextButtonSlot, new InventoryButton()
  71. .creator(p -> new ItemStack(Material.ARROW))
  72. .consumer(event -> {
  73. // *** ADD TRY-CATCH HERE ***
  74. FloorIsLava.getInstance().getPluginLogger().info("Next Page Button CLICKED - Attempting to call nextPage()..."); // Log right before call
  75. this.nextPage((Player) event.getWhoClicked());
  76. FloorIsLava.getInstance().getPluginLogger().info("Next Page Button CLICKED - Call to nextPage() completed."); // Log right after call
  77. }));
  78. }
  79. }
  80.  
  81.  
  82. public void nextPage(Player player) {
  83. boolean condition = (itemList != null && (currentPage + 1) * itemsPerPage < itemList.size());
  84. if (condition) {
  85. currentPage++;
  86. updateInventoryContent();
  87. this.decorate(player);
  88. }
  89. }
  90.  
  91. public void previousPage(Player player) {
  92. if (currentPage > 0) {
  93. currentPage--;
  94. updateInventoryContent();
  95. this.decorate(player);
  96. }
  97. }
  98.  
  99. protected abstract InventoryButton getUnitButton(T item);
  100.  
  101. }
Add Comment
Please, Sign In to add comment