Created
March 13, 2018 02:59
-
-
Save Exerosis/1dcb0cec20e355210f1ec8a041e49d97 to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| package me.bhop.guide; | |
| import org.bukkit.event.EventHandler; | |
| import org.bukkit.event.player.PlayerJoinEvent; | |
| import org.bukkit.inventory.Inventory; | |
| import org.bukkit.plugin.Plugin; | |
| import org.bukkit.plugin.java.JavaPlugin; | |
| import java.util.ArrayList; | |
| import java.util.List; | |
| import java.util.function.Consumer; | |
| import java.util.function.Supplier; | |
| import static com.google.common.collect.Lists.partition; | |
| import static com.google.common.collect.Streams.mapWithIndex; | |
| import static java.util.stream.Collectors.toList; | |
| import static me.bhop.guide.MiniWindow.Item; | |
| import static me.bhop.guide.MiniWindow.Page; | |
| import static org.bukkit.DyeColor.*; | |
| import static org.bukkit.Material.STAINED_GLASS_PANE; | |
| public class Main extends JavaPlugin { | |
| private final Guide guide; | |
| public Main() { | |
| guide = new Guide(this, 3); | |
| } | |
| @EventHandler | |
| void onJoin(PlayerJoinEvent event) { | |
| event.getPlayer().openInventory(guide.getPage(0)); | |
| } | |
| public static Supplier<Inventory> cache(Supplier<Inventory> creator) { | |
| return new Supplier<Inventory>() { | |
| Inventory inventory = null; | |
| @Override | |
| public Inventory get() { | |
| if (inventory == null) | |
| inventory = creator.get(); | |
| return inventory; | |
| } | |
| }; | |
| } | |
| class Guide { | |
| final List<Category> categories = new ArrayList<>(); | |
| final List<Supplier<Inventory>> pages = new ArrayList<>(); | |
| public Guide(Plugin plugin, int rows) { | |
| MiniWindow window = new MiniWindow(plugin); | |
| List<List<Category>> partitions = partition(categories, rows * 9); | |
| pages.addAll(mapWithIndex(partitions.stream(), (categories, index) -> | |
| cache(() -> window.page(page -> { | |
| //Title the page. | |
| page.title("Page " + index); | |
| //Add items for each category on the page. | |
| categories.forEach(category -> { | |
| //Create a new item for the category. | |
| Item item = page.element(); | |
| //Let the category style it. | |
| category.iconLike().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 (index != 0 && i == 2) | |
| item.data(RED.getDyeData()).onClick(player -> | |
| //Navigate back one page. | |
| player.openInventory(pages.get((int) (index - 1)).get()) | |
| ); | |
| else if (index != partitions.size() - 1 && i == 6) | |
| item.data(GREEN.getDyeData()).onClick(player -> | |
| //Navigate forward one page. | |
| player.openInventory(pages.get((int) (index + 1)).get()) | |
| ); | |
| else | |
| item.data(GRAY.getDyeData()); | |
| } | |
| })) | |
| ).collect(toList())); | |
| } | |
| public Inventory getPage(int index) { | |
| return pages.get(index).get(); | |
| } | |
| } | |
| interface Category { | |
| Consumer<Page> pageLike(); | |
| Consumer<Item> iconLike(); | |
| } | |
| } |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment