SimplePage update size

This commit is contained in:
jascotty2 2019-09-03 12:49:59 -05:00
parent 76e7e8cd11
commit 0b72aebe96
3 changed files with 27 additions and 10 deletions

View File

@ -1158,6 +1158,19 @@ public enum LegacyMaterials {
return name == null ? null : lookupMap.get(name.toUpperCase());
}
/**
* Lookup a Legacy Material by its modern id name. <br />
* This also can grab materials by their legacy, but only if there is no
* modern material by that name.
*
* @param name item to lookup
* @param def default item if this is not a valid material
* @return LegacyMaterial or null if none found
*/
public static LegacyMaterials getMaterial(String name, LegacyMaterials def) {
return name == null ? def : lookupMap.getOrDefault(name.toUpperCase(), def);
}
/**
* Lookup a Legacy Material by bukkit material.
*

View File

@ -243,7 +243,7 @@ public class Gui {
public Gui setItem(int cell, ItemStack item) {
cellItems.put(cell, item);
if (open && cell >= 0 && cell < inventory.getSize()) {
if (inventory != null && cell >= 0 && cell < inventory.getSize()) {
inventory.setItem(cell, item);
}
return this;
@ -252,7 +252,7 @@ public class Gui {
public Gui setItem(int row, int col, ItemStack item) {
final int cell = col + row * 9;
cellItems.put(cell, item);
if (open && cell >= 0 && cell < inventory.getSize()) {
if (inventory != null && cell >= 0 && cell < inventory.getSize()) {
inventory.setItem(cell, item);
}
return this;

View File

@ -4,6 +4,7 @@ import com.songoda.core.compatibility.LegacyMaterials;
import static com.songoda.core.gui.Gui.trimTitle;
import com.songoda.core.gui.events.GuiClickEvent;
import com.songoda.core.gui.methods.Clickable;
import java.util.List;
import java.util.Map;
import org.bukkit.Bukkit;
import org.bukkit.ChatColor;
@ -169,13 +170,12 @@ public class SimplePagedGui extends Gui {
pages = (int) Math.ceil(maxRows / rowsPerPage);
// create a new inventory if needed
final int cells = rows * 9;
boolean isNew = false;
if (cells != inventory.getSize()) {
this.setRows(maxRows + (useHeader ? 2 : 1));
inventory = Bukkit.getServer().createInventory(inventory.getHolder(), cells,
List<Player> toUpdate = null;
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)));
isNew = true;
}
// populate header
@ -185,17 +185,21 @@ public class SimplePagedGui extends Gui {
inventory.setItem(i, item != null ? item : (headerBackItem != null ? headerBackItem : blankItem));
}
}
// last row is dedicated to pagation
final int cells = rows * 9;
for (int i = cells - 9; i < cells; ++i) {
inventory.setItem(i, footerBackItem != null ? footerBackItem : blankItem);
}
// fill out the rest of the page
showPage();
if(isNew) {
// did we need to change the display window size?
if(toUpdate != null) {
// whoopsie!
exit();
getPlayers().forEach(player -> ((GuiHolder) inventory.getHolder()).manager.showGUI(player, this));
toUpdate.forEach(player -> ((GuiHolder) inventory.getHolder()).manager.showGUI(player, this));
}
}