From bf956abcad838b43c2a63047ce8244974d4e690b Mon Sep 17 00:00:00 2001 From: Sam Date: Tue, 17 Oct 2017 00:04:05 +0100 Subject: [PATCH] Paginate TreasurePage --- .../core/treasure/ui/TreasurePage.java | 56 +++++++++++++++---- 1 file changed, 44 insertions(+), 12 deletions(-) diff --git a/Plugins/Mineplex.Core/src/mineplex/core/treasure/ui/TreasurePage.java b/Plugins/Mineplex.Core/src/mineplex/core/treasure/ui/TreasurePage.java index 50d9eb917..c20958475 100644 --- a/Plugins/Mineplex.Core/src/mineplex/core/treasure/ui/TreasurePage.java +++ b/Plugins/Mineplex.Core/src/mineplex/core/treasure/ui/TreasurePage.java @@ -4,6 +4,7 @@ import java.util.ArrayList; import java.util.List; import org.bukkit.ChatColor; +import org.bukkit.Material; import org.bukkit.entity.Player; import org.bukkit.event.inventory.ClickType; import org.bukkit.inventory.ItemStack; @@ -12,18 +13,28 @@ import org.bukkit.inventory.meta.ItemMeta; import mineplex.core.common.util.C; import mineplex.core.common.util.F; import mineplex.core.common.util.UtilUI; +import mineplex.core.itemstack.ItemBuilder; import mineplex.core.shop.item.IButton; import mineplex.core.shop.page.ShopPageBase; import mineplex.core.treasure.TreasureLocation; import mineplex.core.treasure.TreasureManager; -import mineplex.core.treasure.reward.TreasureRewardManager; import mineplex.core.treasure.types.Treasure; public class TreasurePage extends ShopPageBase { + private static final int ELEMENTS_PER_PAGE = 9; + private static final ItemStack PREVIOUS_PAGE = new ItemBuilder(Material.ARROW) + .setTitle(C.cGreen + "Previous Page") + .build(); + private static final ItemStack NEXT_PAGE = new ItemBuilder(Material.ARROW) + .setTitle(C.cGreen + "Next Page") + .build(); + private final TreasureLocation _treasureLocation; + private int _page; + TreasurePage(TreasureManager plugin, TreasureShop shop, Player player, TreasureLocation treasureLocation) { super(plugin, shop, plugin.getClientManager(), plugin.getDonationManager(), "Treasure Chest", player); @@ -37,13 +48,34 @@ public class TreasurePage extends ShopPageBase protected void buildPage() { List treasures = _treasureLocation.getManager().getTreasures(); - int[] slots = UtilUI.getIndicesFor(treasures.size(), 2, 5, 1); + int[] slots = UtilUI.getIndicesFor(ELEMENTS_PER_PAGE, 1, 5, 1); int index = 0; + int start = _page * ELEMENTS_PER_PAGE; - for (Treasure treasure : _treasureLocation.getManager().getTreasures()) + for (int treasureIndex = start; treasureIndex < start + ELEMENTS_PER_PAGE && treasureIndex < treasures.size(); treasureIndex++) { + Treasure treasure = treasures.get(treasureIndex); + addButton(slots[index++], prettifyItem(getPlayer(), treasure), new TreasureButton(treasure)); } + + if (_page != 0) + { + addButton(45, PREVIOUS_PAGE, (player, clickType) -> + { + _page--; + refresh(); + }); + } + + if (start + ELEMENTS_PER_PAGE < treasures.size()) + { + addButton(53, NEXT_PAGE, (player, clickType) -> + { + _page++; + refresh(); + }); + } } private ItemStack prettifyItem(Player player, Treasure treasure) @@ -69,15 +101,15 @@ public class TreasurePage extends ShopPageBase lore.addAll(treasure.getPurchaseMethods()); } - if (!treasure.isDuplicates()) - { - TreasureRewardManager rewardManager = _treasureLocation.getManager().getRewardManager(); - int total = rewardManager.getTotalItems(treasure); - int available = total - rewardManager.getOwnedItems(player, treasure); - - lore.add(""); - lore.add(C.cGray + "Available Items: " + F.elem(available) + "/" + F.elem(total)); - } +// if (!treasure.isDuplicates()) +// { +// TreasureRewardManager rewardManager = _treasureLocation.getManager().getRewardManager(); +// int total = rewardManager.getTotalItems(treasure); +// int available = total - rewardManager.getOwnedItems(player, treasure); +// +// lore.add(""); +// lore.add(C.cGray + "Available Items: " + F.elem(available) + "/" + F.elem(total)); +// } if (canOpen) {