Skip to content

Instantly share code, notes, and snippets.

@Exerosis
Created March 13, 2018 03:32
Show Gist options
  • Select an option

  • Save Exerosis/9299e44b17a71caa238b4b46bac73ae8 to your computer and use it in GitHub Desktop.

Select an option

Save Exerosis/9299e44b17a71caa238b4b46bac73ae8 to your computer and use it in GitHub Desktop.
public static IntFunction<Inventory> createGuide(List<Category> categories, Plugin plugin, int rows) {
final MiniWindow window = new MiniWindow(plugin);
final List<Supplier<Inventory>> pages = new ArrayList<>();
//Split our list up into pages.
final List<List<Category>> partitions = partition(categories, rows * 9);
//Loop over our category lists with an index.
for (int index = 0; index < partitions.size(); index++) {
//Get the list of categories for this page.
final List<Category> categoryList = partitions.get(index);
int pageNumber = index; //This is just because java is stupid af...
pages.add(index, cache(() -> {
//Create a new page
Page page = window.page();
//Give it a title
page.title("Page " + pageNumber);
//Add items for each category on the page.
for (Category category : categoryList) {//Create a new item for the category.
Item item = page.element();
//Let the category style it.
category.itemLike().accept(item);
//Cache the category's page as well.
Supplier<Inventory> categoryPage = cache(() ->
window.page(category.pageLike())
);
//Open the category's page when it's icon is clicked.
item.onClick(player ->
player.openInventory(categoryPage.get())
);
}
//Footer time.
for (int i = 0; i < 9; i++) {
Item item = page.element(rows + 1).icon(STAINED_GLASS_PANE);
if (pageNumber != 0 && i == 2)
item.data(RED.getDyeData()).onClick(player ->
//Navigate back one page.
player.openInventory(pages.get((int) (pageNumber - 1)).get())
);
else if (pageNumber != partitions.size() - 1 && i == 6)
item.data(GREEN.getDyeData()).onClick(player ->
//Navigate forward one page.
player.openInventory(pages.get((int) (pageNumber + 1)).get())
);
else
item.data(GRAY.getDyeData());
}
return page.toInventory();
}));
}
return index -> pages.get(index).get();
}
interface Category {
static Category create(Consumer<Page> page, Consumer<Item> item) {
return new Category() {
@Override
public Consumer<Page> pageLike() {
return page;
}
@Override
public Consumer<Item> itemLike() {
return item;
}
};
}
Consumer<Page> pageLike();
Consumer<Item> itemLike();
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment