Skip to content

Commit

Permalink
Merge pull request #2 from focamacho/dev
Browse files Browse the repository at this point in the history
fixes and additions for pageable menus
  • Loading branch information
focamacho authored Mar 13, 2023
2 parents 9c528cf + c0a7eab commit d15c6be
Show file tree
Hide file tree
Showing 4 changed files with 59 additions and 6 deletions.
2 changes: 1 addition & 1 deletion build.gradle
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
allprojects {
group = 'com.focamacho'
version = '1.0.9'
version = '1.0.10'
}

subprojects {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -109,6 +109,7 @@ public List<MenuItem> getPageableItems() {
public PageableChestMenu setPageableItems(List<MenuItem> items) {
this.pageableItems.clear();
pageableItems.addAll(items);
this.page = 0;
requireUpdate(null);
return this;
}
Expand Down Expand Up @@ -144,12 +145,34 @@ public Map.Entry<Integer, Integer> getPageableItemSlot(MenuItem item) {
* Get the quantity of pages of this menu.
* The quantity is defined by the amount
* of items inserted.
*
* @return the quantity of pages of this menu.
*/
public int getPageCount() {
return (int) Math.max(1, Math.ceil(this.pageableItems.size() / (double) itemSlots.length));
}

/**
* Get the current page the player
* is in.
*
* @param player the player to check the
* page.
* @return the current page index.
*/
public int getCurrentPage(Player player) {
if(this.fatherMenu != null && this.fatherMenu.inventory != null && this.fatherMenu.inventory.getViewers().contains(player)) {
return this.fatherMenu.page;
} else if(this.inventory != null && this.inventory.getViewers().contains(player)) {
return this.page;
} else {
Optional<PageableChestMenu> menu = mirrorMenus.stream().filter(m -> m.inventory != null && m.inventory.getViewers().contains(player)).findFirst();
if(menu.isPresent()) return menu.get().page;
}

return 0;
}

/**
* Set the item used to go to the next page.
* This item is only shown if there is enough items
Expand Down Expand Up @@ -281,11 +304,15 @@ public MenuItem getItem(Integer slot) {

@Override
public void open(Player player) {
open(player, 0);
}

public void open(Player player, int page) {
if(this.inventory == null || !super.hasViewers()) {
this.page = 0;
this.page = Math.min(getPageCount() - 1, page);
requireUpdate(null);
super.open(player);
} else new PageableChestMenu(this).open(player);
} else new PageableChestMenu(this).open(player, page);
}

@Override
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -290,7 +290,6 @@ public void update(int slot) {
for (Inventory inventorySlot : this.inventory.slots()) {
Integer slotIndex = inventorySlot.getInventoryProperty(SlotIndex.class).get().getValue();
if(Objects.equals(slot, slotIndex)) {
System.out.println("atualizando slot");
if(containsItem(slot)) {
ItemStack stack = getItem(slot).getItem();
if (inventorySlot.peek().orElse(ItemStack.empty()) != stack)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -107,6 +107,7 @@ public List<MenuItem> getPageableItems() {
public PageableChestMenu setPageableItems(List<MenuItem> items) {
this.pageableItems.clear();
pageableItems.addAll(items);
this.page = 0;
requireUpdate(null);
return this;
}
Expand Down Expand Up @@ -142,12 +143,34 @@ public Map.Entry<Integer, Integer> getPageableItemSlot(MenuItem item) {
* Get the quantity of pages of this menu.
* The quantity is defined by the amount
* of items inserted.
*
* @return the quantity of pages of this menu.
*/
public int getPageCount() {
return (int) Math.max(1, Math.ceil(this.pageableItems.size() / (double) itemSlots.length));
}

/**
* Get the current page the player
* is in.
*
* @param player the player to check the
* page.
* @return the current page index.
*/
public int getCurrentPage(Player player) {
if(this.fatherMenu != null && this.fatherMenu.playersViewing.contains(player)) {
return this.fatherMenu.page;
} else if(playersViewing.contains(player)) {
return this.page;
} else {
Optional<PageableChestMenu> menu = mirrorMenus.stream().filter(m -> m.playersViewing.contains(player)).findFirst();
if(menu.isPresent()) return menu.get().page;
}

return 0;
}

/**
* Set the item used to go to the next page.
* This item is only shown if there is enough items
Expand Down Expand Up @@ -279,11 +302,15 @@ public MenuItem getItem(Integer slot) {

@Override
public void open(Player player) {
open(player, 0);
}

public void open(Player player, int page) {
if(this.inventory == null || !super.hasViewers()) {
this.page = 0;
this.page = Math.min(getPageCount() - 1, page);
requireUpdate(null);
super.open(player);
} else new PageableChestMenu(this).open(player);
} else new PageableChestMenu(this).open(player, page);
}

@Override
Expand Down

0 comments on commit d15c6be

Please sign in to comment.