Paginate TreasurePage

This commit is contained in:
Sam 2017-10-17 00:04:05 +01:00 committed by Alexander Meech
parent f76e30d177
commit bf956abcad

View File

@ -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<TreasureManager, TreasureShop>
{
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<TreasureManager, TreasureShop>
protected void buildPage()
{
List<Treasure> 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<TreasureManager, TreasureShop>
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)
{