328 lines
11 KiB
Java
328 lines
11 KiB
Java
package eu.univento.core.api.items;
|
|
|
|
import eu.univento.core.api.items.events.PagesClickEvent;
|
|
import eu.univento.core.api.items.events.PagesTurnEvent;
|
|
import eu.univento.core.api.player.CustomPlayer;
|
|
import org.bukkit.Bukkit;
|
|
import org.bukkit.Material;
|
|
import org.bukkit.event.inventory.InventoryClickEvent;
|
|
import org.bukkit.inventory.ItemStack;
|
|
|
|
import java.util.ArrayList;
|
|
import java.util.Arrays;
|
|
import java.util.HashMap;
|
|
|
|
/**
|
|
* @author joethei
|
|
* @version 0.1
|
|
*/
|
|
public class PageInventory extends ClickInventory{
|
|
|
|
protected ItemStack backAPage;
|
|
protected ItemStack forwardsAPage;
|
|
protected ItemStack exitInventory;
|
|
protected int currentPage;
|
|
protected boolean dynamicInventorySize;
|
|
private int inventorySize;
|
|
protected boolean pageDisplayedInTitle;
|
|
protected HashMap<Integer, ItemStack[]> pages;
|
|
protected String title;
|
|
private String titleFormat;
|
|
|
|
public PageInventory(final CustomPlayer player) {
|
|
this(null, player);
|
|
}
|
|
|
|
public PageInventory(final CustomPlayer player, final boolean dynamicInventory) {
|
|
this(null, player, dynamicInventory);
|
|
}
|
|
|
|
public PageInventory(final CustomPlayer player, final int inventorySize) {
|
|
this(null, player, inventorySize);
|
|
}
|
|
|
|
public PageInventory(final String inventoryName, final CustomPlayer player) {
|
|
super(inventoryName, player);
|
|
this.dynamicInventorySize = true;
|
|
this.inventorySize = 54;
|
|
this.pages = new HashMap<>();
|
|
this.title = "Inventory";
|
|
this.titleFormat = "%Title% - Page %Page%";
|
|
}
|
|
|
|
public PageInventory(final String inventoryName, final CustomPlayer player, final boolean dynamicInventorySize) {
|
|
super(inventoryName, player);
|
|
this.dynamicInventorySize = true;
|
|
this.inventorySize = 54;
|
|
this.pages = new HashMap<>();
|
|
this.title = "Inventory";
|
|
this.titleFormat = "%Title% - Page %Page%";
|
|
this.dynamicInventorySize = dynamicInventorySize;
|
|
}
|
|
|
|
public PageInventory(final String inventoryName, final CustomPlayer player, final int inventorySize) {
|
|
super(inventoryName, player);
|
|
this.dynamicInventorySize = true;
|
|
this.inventorySize = 54;
|
|
this.pages = new HashMap<>();
|
|
this.title = "Inventory";
|
|
this.titleFormat = "%Title% - Page %Page%";
|
|
this.inventorySize = Math.min(54, (int)Math.ceil(inventorySize / 9.0) * 9);
|
|
this.dynamicInventorySize = false;
|
|
this.pages.put(0, new ItemStack[0]);
|
|
}
|
|
|
|
public ItemStack getExitInventory() {
|
|
return this.exitInventory;
|
|
}
|
|
|
|
public void setExitInventory(final ItemStack item) {
|
|
this.exitInventory = item;
|
|
}
|
|
|
|
public ItemStack getBackPage() {
|
|
if (this.backAPage == null) {
|
|
this.backAPage = new ItemBuilder(Material.SIGN).name("Zurück").make();
|
|
}
|
|
return this.backAPage;
|
|
}
|
|
|
|
public int getCurrentPage() {
|
|
return this.currentPage;
|
|
}
|
|
|
|
public ItemStack getForwardsPage() {
|
|
if (this.forwardsAPage == null) {
|
|
this.forwardsAPage = new ItemBuilder(Material.SIGN).name("Vorwärts").make();
|
|
}
|
|
return this.forwardsAPage;
|
|
}
|
|
|
|
@Override
|
|
public String getTitle() {
|
|
return this.getPageTitle();
|
|
}
|
|
|
|
public ItemStack[] getPage(final int pageNumber) {
|
|
if (this.pages.containsKey(pageNumber)) {
|
|
return this.pages.get(pageNumber);
|
|
}
|
|
return null;
|
|
}
|
|
|
|
public HashMap<Integer, ItemStack[]> getPages() {
|
|
return this.pages;
|
|
}
|
|
|
|
protected String getPageTitle() {
|
|
return this.isPageDisplayedInTitle() ? this.titleFormat.replace("%Title%", this.title).replace("%Page%", this.getCurrentPage() + 1 + "") : this.title;
|
|
}
|
|
|
|
public boolean isPageDisplayedInTitle() {
|
|
return this.pageDisplayedInTitle;
|
|
}
|
|
|
|
@Override
|
|
protected void onInventoryClick(final InventoryClickEvent event) {
|
|
final ItemStack item = event.getCurrentItem();
|
|
if (this.checkInMenu(event.getRawSlot())) {
|
|
if (item != null) {
|
|
int newPage = 0;
|
|
if (item.equals(this.getBackPage())) {
|
|
newPage = -1;
|
|
}
|
|
else if (item.equals(this.getForwardsPage())) {
|
|
newPage = 1;
|
|
}
|
|
if (newPage != 0) {
|
|
final PagesTurnEvent newEvent = new PagesTurnEvent(this, event.getSlot(), event, this.getCurrentPage() + newPage);
|
|
Bukkit.getPluginManager().callEvent(newEvent);
|
|
if (!newEvent.isCancelled()) {
|
|
this.setPage(this.getCurrentPage() + newPage);
|
|
}
|
|
event.setCancelled(true);
|
|
return;
|
|
}
|
|
}
|
|
int slot = event.getSlot();
|
|
if (this.isPlayerInventory()) {
|
|
slot -= 9;
|
|
if (slot < 0) {
|
|
slot += 36;
|
|
}
|
|
}
|
|
final PagesClickEvent itemClickEvent = new PagesClickEvent(this, slot, event);
|
|
if (!this.isModifiable()) {
|
|
itemClickEvent.setCancelled(true);
|
|
}
|
|
Bukkit.getPluginManager().callEvent(itemClickEvent);
|
|
if (itemClickEvent.isCancelled()) {
|
|
event.setCancelled(true);
|
|
}
|
|
}
|
|
else if (!this.isModifiable() && event.isShiftClick() && item != null && item.getType() != Material.AIR) {
|
|
for (int slot = 0; slot < this.currentInventory.getSize(); ++slot) {
|
|
final ItemStack invItem = this.currentInventory.getItem(slot);
|
|
if (invItem == null || invItem.getType() == Material.AIR || (invItem.isSimilar(item) && item.getAmount() < item.getMaxStackSize())) {
|
|
event.setCancelled(true);
|
|
break;
|
|
}
|
|
}
|
|
}
|
|
}
|
|
|
|
public void openInventory() {
|
|
if (this.isInUse()) {
|
|
return;
|
|
}
|
|
this.saveContents();
|
|
final ItemStack[] pageItems = this.getItemsForPage();
|
|
if (this.currentInventory == null) {
|
|
if (this.isPlayerInventory()) {
|
|
this.currentInventory = this.getPlayer().getInventory();
|
|
}
|
|
else {
|
|
this.currentInventory = Bukkit.createInventory(null, pageItems.length, this.getPageTitle());
|
|
}
|
|
}
|
|
this.setItems(pageItems);
|
|
this.openInventory();
|
|
}
|
|
|
|
private ItemStack[] getItemsForPage() {
|
|
ItemStack[] pageItems = this.pages.get(Math.max(this.getCurrentPage(), 0));
|
|
int pageSize = pageItems.length;
|
|
if (this.pages.size() > 1 || this.getExitInventory() != null) {
|
|
pageSize += 9;
|
|
}
|
|
if (!this.dynamicInventorySize || this.isPlayerInventory()) {
|
|
pageSize = (this.isPlayerInventory() ? 36 : this.inventorySize);
|
|
}
|
|
else {
|
|
pageSize = (pageSize + 8) / 9 * 9;
|
|
}
|
|
pageItems = Arrays.copyOf(pageItems, pageSize);
|
|
if (this.getCurrentPage() > 0 || this.getExitInventory() != null) {
|
|
pageItems[pageItems.length - 9] = ((this.getCurrentPage() == 0) ? this.getExitInventory() : this.getBackPage());
|
|
}
|
|
if (this.pages.size() - 1 > this.getCurrentPage()) {
|
|
pageItems[pageItems.length - 1] = this.getForwardsPage();
|
|
}
|
|
return pageItems;
|
|
}
|
|
|
|
public void setBackPage(final ItemStack newBack) {
|
|
this.backAPage = newBack;
|
|
}
|
|
|
|
public void setForwardsPage(final ItemStack newForwards) {
|
|
this.forwardsAPage = newForwards;
|
|
}
|
|
|
|
public ArrayList<ItemStack> getItems() {
|
|
final ArrayList<ItemStack> items = new ArrayList<>();
|
|
for (int i = 0; i < this.pages.size(); ++i) {
|
|
final ItemStack[] itemArray = this.pages.get(i);
|
|
for (int a = 0; a < itemArray.length - ((this.pages.size() > 1) ? 9 : 0); ++a) {
|
|
items.add(itemArray[a]);
|
|
}
|
|
}
|
|
return items;
|
|
}
|
|
|
|
public void setPage(final int newPage) {
|
|
if (this.pages.containsKey(newPage)) {
|
|
this.currentPage = newPage;
|
|
if (this.isInUse()) {
|
|
final ItemStack[] pageItems = this.getItemsForPage();
|
|
if (!this.isPlayerInventory() && (pageItems.length != this.currentInventory.getSize() || !this.currentInventory.getTitle().equalsIgnoreCase(this.getPageTitle()))) {
|
|
(this.currentInventory = Bukkit.createInventory(null, pageItems.length, this.getPageTitle())).setContents(pageItems);
|
|
this.openInventory();
|
|
}
|
|
else {
|
|
this.setItems(pageItems);
|
|
}
|
|
}
|
|
}
|
|
}
|
|
|
|
public void setPageDisplayedInTitle(final boolean displayPage) {
|
|
if (this.isPageDisplayedInTitle() != displayPage) {
|
|
this.pageDisplayedInTitle = displayPage;
|
|
if (this.isInUse()) {
|
|
this.setPage(this.getCurrentPage());
|
|
}
|
|
}
|
|
}
|
|
|
|
public void setPageDisplayTitleFormat(final String titleFormat) {
|
|
this.titleFormat = titleFormat;
|
|
if (this.isInUse()) {
|
|
this.setPage(this.getCurrentPage());
|
|
}
|
|
}
|
|
|
|
public void setPages(final ArrayList<ItemStack> allItems) {
|
|
this.setPages((ItemStack[])allItems.toArray(new ItemStack[allItems.size()]));
|
|
}
|
|
|
|
public void setPages(final ItemStack... allItems) {
|
|
this.pages.clear();
|
|
int invPage = 0;
|
|
final boolean usePages = this.getExitInventory() != null || allItems.length > this.inventorySize;
|
|
ItemStack[] items = null;
|
|
int currentSlot = 0;
|
|
final int baseSize = this.isPlayerInventory() ? 36 : this.inventorySize;
|
|
for (int currentItem = 0; currentItem < allItems.length; ++currentItem) {
|
|
if (items == null) {
|
|
int newSize = allItems.length - currentItem;
|
|
if (usePages && newSize + 9 > baseSize) {
|
|
newSize = baseSize - 9;
|
|
}
|
|
else if (newSize > baseSize) {
|
|
newSize = baseSize;
|
|
}
|
|
items = new ItemStack[newSize];
|
|
}
|
|
final ItemStack item = allItems[currentItem];
|
|
items[currentSlot++] = item;
|
|
if (currentSlot == items.length) {
|
|
this.pages.put(invPage, items);
|
|
++invPage;
|
|
currentSlot = 0;
|
|
items = null;
|
|
}
|
|
}
|
|
if (this.pages.keySet().size() < this.getCurrentPage()) {
|
|
this.currentPage = this.pages.keySet().size() - 1;
|
|
}
|
|
if (allItems.length == 0) {
|
|
int size = this.isPlayerInventory() ? 36 : this.inventorySize;
|
|
if (!this.isPlayerInventory() && this.dynamicInventorySize) {
|
|
size = 9;
|
|
}
|
|
items = InventoryManager.generateEmptyPage(size);
|
|
if (this.getExitInventory() != null) {
|
|
items[0] = this.getExitInventory();
|
|
}
|
|
this.pages.put(0, items);
|
|
}
|
|
this.setPage(this.getCurrentPage());
|
|
}
|
|
|
|
@Override
|
|
public PageInventory setPlayerInventory() {
|
|
super.setPlayerInventory();
|
|
return this;
|
|
}
|
|
|
|
@Override
|
|
public void setTitle(final String newTitle) {
|
|
if (!this.getTitle().equals(newTitle)) {
|
|
this.title = newTitle;
|
|
if (this.isInUse()) {
|
|
this.setPage(this.getCurrentPage());
|
|
}
|
|
}
|
|
}
|
|
} |