update inventory if title updated

This commit is contained in:
jascotty2 2019-09-08 07:25:18 -05:00
parent aac1813506
commit 9860484061
2 changed files with 48 additions and 23 deletions

View File

@ -18,7 +18,6 @@ import java.util.List;
import java.util.Map; import java.util.Map;
import java.util.stream.Collectors; import java.util.stream.Collectors;
import org.bukkit.Bukkit; import org.bukkit.Bukkit;
import org.bukkit.ChatColor;
import org.bukkit.Material; import org.bukkit.Material;
import org.bukkit.entity.Player; import org.bukkit.entity.Player;
import org.bukkit.event.inventory.ClickType; import org.bukkit.event.inventory.ClickType;
@ -40,7 +39,7 @@ public class Gui {
protected Inventory inventory; protected Inventory inventory;
protected String title; protected String title;
protected GuiType inventoryType = GuiType.STANDARD; protected GuiType inventoryType = GuiType.STANDARD;
protected int rows, page, pages; protected int rows, page = 1, pages = 1;
protected boolean acceptsItems = false; protected boolean acceptsItems = false;
protected boolean allowDropItems = true; protected boolean allowDropItems = true;
protected boolean allowClose = true; protected boolean allowClose = true;
@ -206,7 +205,22 @@ public class Gui {
@NotNull @NotNull
public Gui setTitle(String title) { 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<Player> toUpdate = getPlayers();
boolean isAllowClose = allowClose;
exit();
Inventory oldInv = inventory;
createInventory();
inventory.setContents(oldInv.getContents());
toUpdate.forEach(player -> player.openInventory(inventory));
allowClose = isAllowClose;
}
}
return this; return this;
} }
@ -657,19 +671,10 @@ public class Gui {
@NotNull @NotNull
protected Inventory generateInventory(@NotNull GuiManager manager) { protected Inventory generateInventory(@NotNull GuiManager manager) {
this.guiManager = manager;
final int cells = rows * 9; 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) { for (int i = 0; i < cells; ++i) {
final ItemStack item = cellItems.get(i); final ItemStack item = cellItems.get(i);
inventory.setItem(i, item != null ? item : (unlockedCells.getOrDefault(i, false) ? AIR : blankItem)); inventory.setItem(i, item != null ? item : (unlockedCells.getOrDefault(i, false) ? AIR : blankItem));
@ -678,6 +683,20 @@ public class Gui {
return inventory; 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 @Nullable
public Gui getParent() { public Gui getParent() {
return parent; return parent;
@ -741,11 +760,12 @@ public class Gui {
manager.showGUI(player, this); manager.showGUI(player, this);
return; return;
} }
boolean showParent = open && parent != null;
if (open && closer != null) { if (open && closer != null) {
open = inventory.getViewers().isEmpty(); open = !inventory.getViewers().isEmpty();
closer.onClose(new GuiCloseEvent(manager, this, player)); closer.onClose(new GuiCloseEvent(manager, this, player));
} }
if (parent != null) { if (showParent) {
manager.showGUI(player, parent); manager.showGUI(player, parent);
} }
} }

View File

@ -129,6 +129,7 @@ public class SimplePagedGui extends Gui {
@Override @Override
protected Inventory generateInventory(GuiManager manager) { protected Inventory generateInventory(GuiManager manager) {
this.guiManager = manager;
// calculate pages here // calculate pages here
rowsPerPage = useHeader ? 4 : 5; rowsPerPage = useHeader ? 4 : 5;
maxCellSlot = (this.cellItems.isEmpty() ? 0 : this.cellItems.keySet().stream().max(Integer::compare).get()) + 1; 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)); this.setRows(maxRows + (useHeader ? 1 : 0));
// create inventory view // create inventory view
final int cells = rows * 9; createInventory();
inventory = Bukkit.getServer().createInventory(new GuiHolder(manager, this), cells,
title == null ? "" : trimTitle(ChatColor.translateAlternateColorCodes('&', title)));
// populate and return the display inventory // populate and return the display inventory
page = 1; page = Math.min(page, pages);
update(); update();
return inventory; 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 @Override
public void update() { public void update() {
if (inventory == null) { if (inventory == null) {
@ -164,8 +170,7 @@ public class SimplePagedGui extends Gui {
if (Math.min(54, (maxRows + (useHeader ? 1 : 0)) * 9) != inventory.getSize()) { if (Math.min(54, (maxRows + (useHeader ? 1 : 0)) * 9) != inventory.getSize()) {
toUpdate = getPlayers(); toUpdate = getPlayers();
this.setRows(maxRows + (useHeader ? 1 : 0)); this.setRows(maxRows + (useHeader ? 1 : 0));
inventory = Bukkit.getServer().createInventory(inventory.getHolder(), rows * 9, createInventory();
title == null ? "" : trimTitle(ChatColor.translateAlternateColorCodes('&', title)));
} }
// populate header // populate header
@ -189,7 +194,7 @@ public class SimplePagedGui extends Gui {
if(toUpdate != null) { if(toUpdate != null) {
// whoopsie! // whoopsie!
exit(); exit();
toUpdate.forEach(player -> ((GuiHolder) inventory.getHolder()).manager.showGUI(player, this)); toUpdate.forEach(player -> guiManager.showGUI(player, this));
} }
} }