diff --git a/src/main/java/com/songoda/core/gui/Gui.java b/src/main/java/com/songoda/core/gui/Gui.java index 8b1639f7..db8be9bf 100644 --- a/src/main/java/com/songoda/core/gui/Gui.java +++ b/src/main/java/com/songoda/core/gui/Gui.java @@ -18,7 +18,6 @@ import java.util.List; import java.util.Map; import java.util.stream.Collectors; import org.bukkit.Bukkit; -import org.bukkit.ChatColor; import org.bukkit.Material; import org.bukkit.entity.Player; import org.bukkit.event.inventory.ClickType; @@ -40,7 +39,7 @@ public class Gui { protected Inventory inventory; protected String title; protected GuiType inventoryType = GuiType.STANDARD; - protected int rows, page, pages; + protected int rows, page = 1, pages = 1; protected boolean acceptsItems = false; protected boolean allowDropItems = true; protected boolean allowClose = true; @@ -206,7 +205,22 @@ public class Gui { @NotNull public Gui setTitle(String title) { - this.title = title; + System.out.println("Change title " + this.title + " -> " + title); + if(title == null) title = ""; + if(!title.equals(this.title)) { + this.title = title; + if(inventory != null) {System.out.println("Update!"); + // update active inventory + List toUpdate = getPlayers(); + boolean isAllowClose = allowClose; + exit(); + Inventory oldInv = inventory; + createInventory(); + inventory.setContents(oldInv.getContents()); + toUpdate.forEach(player -> player.openInventory(inventory)); + allowClose = isAllowClose; + } + } return this; } @@ -657,19 +671,10 @@ public class Gui { @NotNull protected Inventory generateInventory(@NotNull GuiManager manager) { + this.guiManager = manager; final int cells = rows * 9; - InventoryType t = inventoryType == null ? InventoryType.CHEST : inventoryType.type; - switch (t) { - case DISPENSER: - case HOPPER: - inventory = Bukkit.getServer().createInventory(new GuiHolder(manager, this), t, - title == null ? "" : trimTitle(ChatColor.translateAlternateColorCodes('&', title))); - break; - default: - inventory = Bukkit.getServer().createInventory(new GuiHolder(manager, this), cells, - title == null ? "" : trimTitle(ChatColor.translateAlternateColorCodes('&', title))); - } + createInventory(); for (int i = 0; i < cells; ++i) { final ItemStack item = cellItems.get(i); inventory.setItem(i, item != null ? item : (unlockedCells.getOrDefault(i, false) ? AIR : blankItem)); @@ -678,6 +683,20 @@ public class Gui { return inventory; } + protected void createInventory() { + final InventoryType t = inventoryType == null ? InventoryType.CHEST : inventoryType.type; + switch (t) { + case DISPENSER: + case HOPPER: + inventory = Bukkit.getServer().createInventory(new GuiHolder(guiManager, this), t, + title == null ? "" : trimTitle(title)); + break; + default: + inventory = Bukkit.getServer().createInventory(new GuiHolder(guiManager, this), rows * 9, + title == null ? "" : trimTitle(title)); + } + } + @Nullable public Gui getParent() { return parent; @@ -741,11 +760,12 @@ public class Gui { manager.showGUI(player, this); return; } + boolean showParent = open && parent != null; if (open && closer != null) { - open = inventory.getViewers().isEmpty(); + open = !inventory.getViewers().isEmpty(); closer.onClose(new GuiCloseEvent(manager, this, player)); } - if (parent != null) { + if (showParent) { manager.showGUI(player, parent); } } diff --git a/src/main/java/com/songoda/core/gui/SimplePagedGui.java b/src/main/java/com/songoda/core/gui/SimplePagedGui.java index 6f4eabb9..c636a8c6 100644 --- a/src/main/java/com/songoda/core/gui/SimplePagedGui.java +++ b/src/main/java/com/songoda/core/gui/SimplePagedGui.java @@ -129,6 +129,7 @@ public class SimplePagedGui extends Gui { @Override protected Inventory generateInventory(GuiManager manager) { + this.guiManager = manager; // calculate pages here rowsPerPage = useHeader ? 4 : 5; maxCellSlot = (this.cellItems.isEmpty() ? 0 : this.cellItems.keySet().stream().max(Integer::compare).get()) + 1; @@ -137,16 +138,21 @@ public class SimplePagedGui extends Gui { this.setRows(maxRows + (useHeader ? 1 : 0)); // create inventory view - final int cells = rows * 9; - inventory = Bukkit.getServer().createInventory(new GuiHolder(manager, this), cells, - title == null ? "" : trimTitle(ChatColor.translateAlternateColorCodes('&', title))); + createInventory(); // populate and return the display inventory - page = 1; + page = Math.min(page, pages); update(); return inventory; } + @Override + protected void createInventory() { + final int cells = rows * 9; + inventory = Bukkit.getServer().createInventory(new GuiHolder(guiManager, this), cells, + title == null ? "" : trimTitle(title)); + } + @Override public void update() { if (inventory == null) { @@ -164,8 +170,7 @@ public class SimplePagedGui extends Gui { if (Math.min(54, (maxRows + (useHeader ? 1 : 0)) * 9) != inventory.getSize()) { toUpdate = getPlayers(); this.setRows(maxRows + (useHeader ? 1 : 0)); - inventory = Bukkit.getServer().createInventory(inventory.getHolder(), rows * 9, - title == null ? "" : trimTitle(ChatColor.translateAlternateColorCodes('&', title))); + createInventory(); } // populate header @@ -189,7 +194,7 @@ public class SimplePagedGui extends Gui { if(toUpdate != null) { // whoopsie! exit(); - toUpdate.forEach(player -> ((GuiHolder) inventory.getHolder()).manager.showGUI(player, this)); + toUpdate.forEach(player -> guiManager.showGUI(player, this)); } }