Add Fancy Shmancy MultiPageManager

This commit is contained in:
Spencer 2018-01-04 22:14:54 -05:00 committed by Alexander Meech
parent 0664ea0884
commit db66e166fa

View File

@ -0,0 +1,86 @@
package mineplex.core.shop.page;
import java.util.List;
import java.util.function.Consumer;
import java.util.function.Predicate;
import java.util.function.Supplier;
import org.bukkit.Material;
import mineplex.core.common.Pair;
import mineplex.core.common.util.C;
import mineplex.core.shop.item.ShopItem;
public class MultiPageManager<ItemType>
{
private int ELEMENTS_PER_PAGE = 28;
private int _page;
private Predicate<Pair<ItemType, Integer>> _addItem;
private Supplier<List<ItemType>> _getItems;
private ShopPageBase _shopPage;
public MultiPageManager(ShopPageBase shopPage, Supplier<List<ItemType>> getItems, Predicate<Pair<ItemType, Integer>> addItem)
{
_shopPage = shopPage;
_getItems = getItems;
_addItem = addItem;
}
public MultiPageManager(ShopPageBase shopPage, Supplier<List<ItemType>> getItems, Consumer<Pair<ItemType, Integer>> addItem)
{
this(shopPage, getItems, (Pair<ItemType, Integer> pair)-> {
addItem.accept(pair);
return true;
});
}
public void setElementsPerPage(int elements)
{
this.ELEMENTS_PER_PAGE = elements;
}
public void setPage(int page)
{
_page = page;
_shopPage.refresh();
}
public void buildPage()
{
int slot = 10;
int startIndex = _page * ELEMENTS_PER_PAGE;
int endIndex = startIndex + ELEMENTS_PER_PAGE;
for (ItemType item : _getItems.get())
{
if (!_addItem.test(Pair.create(item, slot)))
{
continue;
}
if (++slot % 9 == 8)
{
slot += 2;
}
}
if (_page != 0)
{
_shopPage.addButton(45, new ShopItem(Material.ARROW, C.cGreen + "Previous Page", new String[0], 1, false), (player, clickType) ->
{
_page--;
_shopPage.refresh();
});
}
if (endIndex <= _getItems.get().size())
{
_shopPage.addButton(53, new ShopItem(Material.ARROW, C.cGreen + "Next Page", new String[0], 1, false), (player, clickType) ->
{
_page++;
_shopPage.refresh();
});
}
}
}