From 149e1225c1659140b8e332302f23af8e207787d0 Mon Sep 17 00:00:00 2001 From: Amaury Carrade Date: Thu, 10 Sep 2015 01:39:38 +0200 Subject: [PATCH] Improved the pagination buttons of the explorer GUI. * NEW: updated the pagination button to more clean ones, and through an overrideable method, to allow a more in-depth personalization if needed. The way used to implement this may be improved, but it's not very important. * NEW: added a version of `ActionGui.updateAction` with only the action and an ItemStack as argument. --- .../imageonmap/guiproko/core/ActionGui.java | 18 +++- .../imageonmap/guiproko/core/ExplorerGui.java | 91 +++++++++++++++---- 2 files changed, 87 insertions(+), 22 deletions(-) diff --git a/src/main/java/fr/moribus/imageonmap/guiproko/core/ActionGui.java b/src/main/java/fr/moribus/imageonmap/guiproko/core/ActionGui.java index 3ca2a93..fd18ec1 100644 --- a/src/main/java/fr/moribus/imageonmap/guiproko/core/ActionGui.java +++ b/src/main/java/fr/moribus/imageonmap/guiproko/core/ActionGui.java @@ -180,13 +180,25 @@ abstract public class ActionGui extends Gui */ protected void updateAction(String name, ItemStack item, String title) { - Action action = getAction(name); - action.item = item; + updateAction(name, item); + ItemMeta meta = item.getItemMeta(); meta.setDisplayName(title); item.setItemMeta(meta); } - + + /** + * Updates the action represented by the given name. + * + * @param name The name of the action to update. + * @param item The new item to affect to the action. + * @throws IllegalArgumentException If no action has the given name. + */ + protected void updateAction(String name, ItemStack item) + { + getAction(name).item = item; + } + /** * Retrieves the action represented by the given name. * diff --git a/src/main/java/fr/moribus/imageonmap/guiproko/core/ExplorerGui.java b/src/main/java/fr/moribus/imageonmap/guiproko/core/ExplorerGui.java index a2f8909..c48de53 100644 --- a/src/main/java/fr/moribus/imageonmap/guiproko/core/ExplorerGui.java +++ b/src/main/java/fr/moribus/imageonmap/guiproko/core/ExplorerGui.java @@ -18,13 +18,16 @@ package fr.moribus.imageonmap.guiproko.core; -import org.bukkit.Material; +import org.bukkit.*; import org.bukkit.event.inventory.ClickType; import org.bukkit.event.inventory.InventoryAction; import org.bukkit.event.inventory.InventoryClickEvent; import org.bukkit.event.inventory.InventoryDragEvent; import org.bukkit.inventory.Inventory; import org.bukkit.inventory.ItemStack; +import org.bukkit.inventory.meta.*; + +import java.util.*; /** @@ -118,28 +121,14 @@ abstract public class ExplorerGui extends ActionGui { if(pageCountX > 1) { - if(canGoNext()) - updateAction("next", Material.ARROW, "Next page"); - else - updateAction("next", Material.STICK, "No next page"); - - if(canGoPrevious()) - updateAction("previous", Material.ARROW, "Previous page"); - else - updateAction("previous", Material.STICK, "No previous page"); + updateAction("next", getPageItem("next", canGoNext())); + updateAction("previous", getPageItem("previous", canGoPrevious())); } if(pageCountY > 1) { - if(canGoUp()) - updateAction("up", Material.ARROW, "Go Up"); - else - updateAction("up", Material.STICK, "Top page"); - - if(canGoDown()) - updateAction("down", Material.ARROW, "Go Down"); - else - updateAction("down", Material.STICK, "Bottom page"); + updateAction("up", getPageItem("up", canGoUp())); + updateAction("down", getPageItem("down", canGoDown())); } if(!isData2D) @@ -453,6 +442,70 @@ abstract public class ExplorerGui extends ActionGui */ protected ItemStack getPickedUpItem(T data) { return getViewItem(data); } + /** + * Returns the item to use to display the pagination buttons. + * + * @param paginationButtonType The type of button (either "next", "previous", "up", "down"). + * @param canUse {@code true} if the button is usable (i.e. not in the last or first page of + * its kind. + * @return The item. + */ + protected ItemStack getPageItem(String paginationButtonType, boolean canUse) + { + ItemStack icon = new ItemStack(canUse ? Material.ARROW : Material.STICK); + ItemMeta meta = icon.getItemMeta(); + + String title; + Integer newPage; + Integer lastPage; + + switch (paginationButtonType) + { + case "next": + title = canUse ? "Next page" : "No next page"; + + newPage = currentPageX + 1; + lastPage = getPageCount(); + break; + + case "previous": + title = canUse ? "Previous page" : "No previous page"; + + newPage = currentPageX - 1; + lastPage = getPageCount(); + break; + + case "up": + title = canUse ? "Go up" : "Top page"; + + newPage = currentPageY + 1; + lastPage = getVerticalPageCount(); + break; + + case "down": + title = canUse ? "Go down" : "Bottom page"; + + newPage = currentPageY - 1; + lastPage = getVerticalPageCount(); + break; + + default: + return null; // invalid page type + } + + meta.setDisplayName((canUse ? ChatColor.WHITE : ChatColor.GRAY) + title); + + if(canUse) + { + meta.setLore(Collections.singletonList( + ChatColor.GRAY + "Go to page " + ChatColor.WHITE + (newPage) + ChatColor.GRAY + " of " + ChatColor.WHITE + lastPage + )); + } + + icon.setItemMeta(meta); + return icon; + } + /** * Triggered when the player right-clicks an item on the GUI. *