add simple paged gui handler

This commit is contained in:
jascotty2 2019-08-31 12:26:45 -05:00
parent 9bf3239279
commit 4f669bb360
21 changed files with 248 additions and 57 deletions

View File

@ -72,6 +72,10 @@ public class Gui {
}
}
public Gui(Gui parent) {
this.parent = parent;
}
public Gui(int rows) {
this.rows = Math.max(1, Math.min(6, rows));
}
@ -476,7 +480,8 @@ public class Gui {
updatePageNavigation();
// push new inventory to the view inventory
update();
// shouldn't be needed since adding inventory update to setItem
//update();
}
}
}

View File

@ -91,6 +91,7 @@ public class GuiUtils {
public static ItemStack createButtonItem(LegacyMaterials mat, String title, String... lore) {
ItemStack item = mat.getItem();
ItemMeta meta = item.getItemMeta();
if (meta != null) {
meta.setDisplayName(title);
if (lore != null) {
meta.setLore(getSafeLore(lore));
@ -98,18 +99,21 @@ public class GuiUtils {
meta.setLore(Collections.EMPTY_LIST);
}
item.setItemMeta(meta);
}
return item;
}
public static ItemStack createButtonItem(ItemStack from, String title, String... lore) {
ItemStack item = from.clone();
ItemMeta meta = item.getItemMeta();
if (meta != null) {
meta.setDisplayName(title);
if (lore != null) {
meta.setLore(getSafeLore(lore));
} else {
meta.setLore(Collections.EMPTY_LIST);
}
}
item.setItemMeta(meta);
return item;
}
@ -117,12 +121,14 @@ public class GuiUtils {
public static ItemStack createButtonItem(LegacyMaterials mat, String title, List<String> lore) {
ItemStack item = mat.getItem();
ItemMeta meta = item.getItemMeta();
if (meta != null) {
meta.setDisplayName(title);
if (lore != null) {
meta.setLore(getSafeLore(lore));
} else {
meta.setLore(Collections.EMPTY_LIST);
}
}
item.setItemMeta(meta);
return item;
}
@ -130,24 +136,28 @@ public class GuiUtils {
public static ItemStack createButtonItem(ItemStack from, String title, List<String> lore) {
ItemStack item = from.clone();
ItemMeta meta = item.getItemMeta();
if (meta != null) {
meta.setDisplayName(title);
if (lore != null) {
meta.setLore(getSafeLore(lore));
} else {
meta.setLore(Collections.EMPTY_LIST);
}
}
item.setItemMeta(meta);
return item;
}
public static ItemStack updateItem(ItemStack item, String title, String... lore) {
ItemMeta meta = item.getItemMeta();
if (meta != null) {
meta.setDisplayName(title);
if (lore != null) {
meta.setLore(getSafeLore(lore));
} else {
meta.setLore(Collections.EMPTY_LIST);
}
}
item.setItemMeta(meta);
return item;
}
@ -157,12 +167,14 @@ public class GuiUtils {
item = matTo.getItem();
}
ItemMeta meta = item.getItemMeta();
if (meta != null) {
meta.setDisplayName(title);
if (lore != null) {
meta.setLore(getSafeLore(lore));
} else {
meta.setLore(Collections.EMPTY_LIST);
}
}
item.setItemMeta(meta);
return item;
}
@ -172,24 +184,28 @@ public class GuiUtils {
item = to.clone();
}
ItemMeta meta = item.getItemMeta();
if (meta != null) {
meta.setDisplayName(title);
if (lore != null) {
meta.setLore(getSafeLore(lore));
} else {
meta.setLore(Collections.EMPTY_LIST);
}
}
item.setItemMeta(meta);
return item;
}
public static ItemStack updateItem(ItemStack item, String title, List<String> lore) {
ItemMeta meta = item.getItemMeta();
if (meta != null) {
meta.setDisplayName(title);
if (lore != null) {
meta.setLore(getSafeLore(lore));
} else {
meta.setLore(Collections.EMPTY_LIST);
}
}
item.setItemMeta(meta);
return item;
}
@ -199,12 +215,14 @@ public class GuiUtils {
item = matTo.getItem();
}
ItemMeta meta = item.getItemMeta();
if (meta != null) {
meta.setDisplayName(title);
if (lore != null) {
meta.setLore(getSafeLore(lore));
} else {
meta.setLore(Collections.EMPTY_LIST);
}
}
item.setItemMeta(meta);
return item;
}
@ -214,12 +232,14 @@ public class GuiUtils {
item = to.clone();
}
ItemMeta meta = item.getItemMeta();
if (meta != null) {
meta.setDisplayName(title);
if (lore != null) {
meta.setLore(getSafeLore(lore));
} else {
meta.setLore(Collections.EMPTY_LIST);
}
}
item.setItemMeta(meta);
return item;
}

View File

@ -0,0 +1,166 @@
package com.songoda.core.gui;
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.Map;
import org.bukkit.Bukkit;
import org.bukkit.ChatColor;
import org.bukkit.entity.Player;
import org.bukkit.event.inventory.ClickType;
import org.bukkit.event.inventory.InventoryClickEvent;
import org.bukkit.inventory.Inventory;
import org.bukkit.inventory.ItemStack;
/**
* Paged GUI for when you aren't going to be making too many pages
*
* @since 2019-08-31
* @author jascotty2
*/
public class SimplePagedGui extends Gui {
protected boolean useHeader;
private int rowsPerPage, maxCellSlot;
protected ItemStack headerBackItem;
protected ItemStack footerBackItem;
final int nextPageIndex = -4, prevPageIndex = -6;
public SimplePagedGui() {
this(null);
}
public SimplePagedGui(Gui parent) {
super(parent);
nextPage = GuiUtils.createButtonItem(LegacyMaterials.ARROW, "Next Page");
prevPage = GuiUtils.createButtonItem(LegacyMaterials.ARROW, "Previous Page");
}
public SimplePagedGui setUseHeader(boolean useHeader) {
this.useHeader = useHeader;
return this;
}
public SimplePagedGui setNextPage(ItemStack item) {
nextPage = item;
return this;
}
public SimplePagedGui setPrevPage(ItemStack item) {
prevPage = item;
return this;
}
@Override
public void nextPage() {
if (page < pages) {
++page;
showPage();
}
}
@Override
public void prevPage() {
if (page > 1) {
--page;
showPage();
}
}
public void showPage() {
int startCell = useHeader ? 9 : 0;
int cellIndex = startCell + (page - 1) * (rowsPerPage * 9);
for (int i = startCell; i < (rows - 1) * 9; ++i) {
final ItemStack item = cellItems.get(cellIndex++);
inventory.setItem(i, item != null ? item : blankItem);
}
// page markers
updatePageNavigation();
}
@Override
protected void updatePageNavigation() {
if (page > 1) {
inventory.setItem((rows * 9) - 6, prevPage);
this.setButton(prevPageIndex, prevPage, ClickType.LEFT, (event) -> this.prevPage());
} else {
inventory.setItem((rows * 9) - 6, footerBackItem != null ? footerBackItem : blankItem);
this.setItem(prevPageIndex, null);
this.clearActions(prevPageIndex);
}
if (pages > 1 && page != pages) {
inventory.setItem((rows * 9) - 4, nextPage);
this.setButton(nextPageIndex, nextPage, ClickType.LEFT, (event) -> this.nextPage());
} else {
inventory.setItem((rows * 9) - 4, footerBackItem != null ? footerBackItem : blankItem);
this.setItem(nextPageIndex, null);
this.clearActions(nextPageIndex);
}
}
@Override
protected Inventory generateInventory(GuiManager manager) {
// calculate pages here
rowsPerPage = useHeader ? 4 : 5;
maxCellSlot = this.cellItems.isEmpty() ? 0 : this.cellItems.keySet().stream().max(Integer::compare).get();
int maxRows = (int) Math.ceil(maxCellSlot / 9.);
pages = (int) Math.ceil(maxRows / rowsPerPage);
page = 1;
this.setRows(maxRows + 1);
// create inventory view
final int cells = rows * 9;
inventory = Bukkit.getServer().createInventory(new GuiHolder(manager, this), cells,
title == null ? "" : trimTitle(ChatColor.translateAlternateColorCodes('&', title)));
// populate initial inventory
if (useHeader) {
for (int i = 0; i < 9; ++i) {
final ItemStack item = cellItems.get(i);
inventory.setItem(i, item != null ? item : (headerBackItem != null ? headerBackItem : blankItem));
}
}
for (int i = useHeader ? 9 : 0; i < cells - 9; ++i) {
final ItemStack item = cellItems.get(i);
inventory.setItem(i, item != null ? item : blankItem);
}
// last row is dedicated to pagation
for (int i = cells - 9; i < cells; ++i) {
inventory.setItem(i, footerBackItem != null ? footerBackItem : blankItem);
}
updatePageNavigation();
return inventory;
}
@Override
protected boolean onClick(GuiManager manager, Player player, Inventory inventory, InventoryClickEvent event) {
int cell = event.getSlot();
Map<ClickType, Clickable> conditionals;
if (useHeader && cell < 9) {
conditionals = conditionalButtons.get(cell);
} else if (cell >= (rows - 1) * 9) {
// footer row
conditionals = conditionalButtons.get(cell - (rows * 9));
} else {
int startCell = useHeader ? 9 : 0;
int cellIndex = startCell + (page - 1) * rowsPerPage;
conditionals = conditionalButtons.get(cellIndex);
}
Clickable button;
if (conditionals != null
&& ((button = conditionals.get(event.getClick())) != null || (button = conditionals.get(null)) != null)) {
button.onClick(new GuiClickEvent(manager, this, player, event, cell, true));
} else {
// no event for this button
return false;
}
return true;
}
}