Guest User

Untitled

a guest
May 2nd, 2025
4
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 5.44 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.event.inventory.InventoryOpenEvent;
  9. import org.bukkit.inventory.Inventory;
  10. import org.bukkit.inventory.ItemStack;
  11.  
  12. import java.util.ArrayList;
  13. import java.util.List;
  14.  
  15. public abstract class Paginator<T> extends InventoryGui {
  16.  
  17. protected final int itemsPerPage;
  18. protected List<T> itemList;
  19. protected int currentPage = 0;
  20.  
  21. // This inventory is created by the InventoryGui constructor (super())
  22.  
  23. public Paginator(ArrayList<T> itemList) {
  24. super(); // Creates the inventory via InventoryGui constructor -> createInventory()
  25. // Ensure createInventory() in your Paginator implementation creates the right size (e.g., 54 slots)
  26. this.itemsPerPage = getInventory().getSize() - 9; // Calculate based on actual inv size minus one row for controls
  27. this.itemList = itemList;
  28. updateInventoryContent(); // Initial population of buttonMap. Decoration happens in onOpen.
  29. }
  30.  
  31. protected void updateInventoryContent() {
  32. FloorIsLava.getInstance().getPluginLogger().info("Updating inventory content map for page " + currentPage);
  33.  
  34. if (this.inventory != null) {
  35. this.inventory.clear();
  36. }
  37.  
  38. // *** 3. Clear the button map to prepare for the new page's buttons ***
  39. this.buttonMap.clear();
  40.  
  41. // --- Add Item Buttons ---
  42. int startIndex = currentPage * itemsPerPage;
  43. int endIndex = Math.min(startIndex + itemsPerPage, itemList.size());
  44.  
  45. FloorIsLava.getInstance().getPluginLogger().debug(
  46. "Calculating loop bounds: currentPage=" + currentPage +
  47. ", itemsPerPage=" + itemsPerPage +
  48. ", calculatedStartIndex=" + startIndex +
  49. ", itemList.size=" + (itemList == null ? "null" : itemList.size()) + // Check for null list
  50. ", calculatedEndIndex=" + endIndex
  51. );
  52. for (int i = startIndex; i < endIndex; i++) {
  53. int itemSlot = i - startIndex; // Slot index within the item display area (0 to itemsPerPage-1)
  54. InventoryButton button = getUnitButton(itemList.get(i));
  55. FloorIsLava.getInstance().getPluginLogger().info("Mapping button for item index " + i + " to slot " + itemSlot);
  56. if (button != null) {
  57. // Add button logic to map. Visual item placement is done by decorate().
  58. this.addButton(itemSlot, button);
  59. }
  60. }
  61.  
  62. // --- Add Navigation Buttons ---
  63. int inventorySize = getInventory().getSize();
  64. int previousButtonSlot = inventorySize - 9; // e.g., slot 45 in a 54-slot inventory
  65. int nextButtonSlot = inventorySize - 1; // e.g., slot 53 in a 54-slot inventory
  66.  
  67. if (currentPage > 0) {
  68. this.addButton(previousButtonSlot, new InventoryButton()
  69. .creator(player -> new ItemStack(Material.ARROW)) // Creator used by decorate
  70. .consumer(event -> this.previousPage((Player) event.getWhoClicked())));
  71. } else {
  72. // Optional: Add a placeholder if you want the slot filled when disabled
  73. //addButton(previousButtonSlot, createStaleButton()); // Example using InventoryGui's method
  74. }
  75.  
  76. if (itemList != null && (currentPage + 1) * itemsPerPage < itemList.size()) {
  77. this.addButton(nextButtonSlot, new InventoryButton()
  78. .creator(p -> new ItemStack(Material.ARROW))
  79. .consumer(event -> {
  80. // *** ADD TRY-CATCH HERE ***
  81. FloorIsLava.getInstance().getPluginLogger().info("Next Page Button CLICKED - Attempting to call nextPage()..."); // Log right before call
  82. this.nextPage((Player) event.getWhoClicked());
  83. FloorIsLava.getInstance().getPluginLogger().info("Next Page Button CLICKED - Call to nextPage() completed."); // Log right after call
  84. }));
  85. } else {
  86. // Optional: Add a placeholder
  87. // addButton(nextButtonSlot, createStaleButton()); // Example
  88. }
  89.  
  90. // *** 4. DO NOT call decorate() here. It runs in onOpen or after page change + reopen ***
  91. }
  92.  
  93.  
  94. public void nextPage(Player player) {
  95. boolean condition = (itemList != null && (currentPage + 1) * itemsPerPage < itemList.size());
  96. if (condition) {
  97. currentPage++;
  98. updateInventoryContent();
  99. GuiManager manager = FloorIsLava.getInstance().getGuiManager();
  100. manager.openGUI(this, player);
  101. }
  102. }
  103.  
  104. public void previousPage(Player player) {
  105. if (currentPage > 0) {
  106. currentPage--;
  107. updateInventoryContent();
  108. FloorIsLava.getInstance().getGuiManager().openGUI(this, player);
  109. }
  110. }
  111.  
  112. protected abstract InventoryButton getUnitButton(T item);
  113.  
  114.  
  115. @Override
  116. public void onOpen(InventoryOpenEvent event) {
  117. FloorIsLava.getInstance().getPluginLogger().info("onOpen inventory for page " + currentPage);
  118.  
  119. this.decorate((Player) event.getPlayer());
  120.  
  121. ((Player) event.getPlayer()).updateInventory();
  122. }
  123. }
Add Comment
Please, Sign In to add comment