Skip to content

Commit ea2a2be

Browse files
committed
trying to improve paginated menu implementation
1 parent 6f7f8e4 commit ea2a2be

File tree

3 files changed

+16
-30
lines changed

3 files changed

+16
-30
lines changed

pom.xml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@
66

77
<groupId>me.kodysimpson</groupId>
88
<artifactId>SimpAPI</artifactId>
9-
<version>4.5.5</version>
9+
<version>4.5.6</version>
1010
<packaging>jar</packaging>
1111

1212
<name>SimpAPI</name>

src/main/java/me/kodysimpson/simpapi/menu/Menu.java

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -23,16 +23,16 @@ public abstract class Menu implements InventoryHolder {
2323

2424
//Protected values that can be accessed in the menus
2525
protected PlayerMenuUtility playerMenuUtility;
26-
protected Player p;
26+
protected Player player;
2727
protected Inventory inventory;
2828
protected ItemStack FILLER_GLASS = makeItem(Material.GRAY_STAINED_GLASS_PANE, " ");
2929

3030
//Constructor for Menu. Pass in a PlayerMenuUtility so that
31-
// we have information on who's menu this is and
31+
// we have information on whose menu this is and
3232
// what info is to be transferred
3333
public Menu(PlayerMenuUtility playerMenuUtility) {
3434
this.playerMenuUtility = playerMenuUtility;
35-
this.p = playerMenuUtility.getOwner();
35+
this.player = playerMenuUtility.getOwner();
3636
}
3737

3838
//let each menu decide their name
@@ -76,8 +76,8 @@ protected void reloadItems() {
7676
}
7777

7878
protected void reload() throws MenuManagerException, MenuManagerNotSetupException {
79-
p.closeInventory();
80-
MenuManager.openMenu(this.getClass(), p);
79+
player.closeInventory();
80+
MenuManager.openMenu(this.getClass(), player);
8181
}
8282

8383
//Overridden method from the InventoryHolder interface

src/main/java/me/kodysimpson/simpapi/menu/PaginatedMenu.java

Lines changed: 10 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,6 @@
33
import org.bukkit.ChatColor;
44
import org.bukkit.Material;
55
import org.bukkit.inventory.ItemStack;
6-
import org.bukkit.plugin.Plugin;
76
import org.jetbrains.annotations.Nullable;
87

98
import java.util.HashMap;
@@ -19,23 +18,16 @@ public abstract class PaginatedMenu extends Menu {
1918
//28 is max items because with the border set below,
2019
//28 empty slots are remaining.
2120
protected int maxItemsPerPage = 28;
22-
//the index represents the index of the slot
23-
//that the loop is on
24-
protected int index = 0;
2521

2622
public PaginatedMenu(PlayerMenuUtility playerMenuUtility) {
2723
super(playerMenuUtility);
2824
}
2925

3026
/**
31-
* @return A list of the data being paginated. usually this is a list of items but it can be anything
27+
* @return A list of ItemStacks that you want to be placed in the menu. This is the data that will be paginated
28+
* You can also use this as a way to convert your data to items if you need to
3229
*/
33-
public abstract List<?> getData();
34-
35-
/**
36-
* @param object A single element of the data list that you do something with. It is recommended that you turn this into an item if it is not already and then put it into the inventory as you would with a normal Menu. You can execute any other logic in here as well.
37-
*/
38-
public abstract void loopCode(Object object);
30+
public abstract List<ItemStack> dataToItems();
3931

4032
/**
4133
* @return A hashmap of items you want to be placed in the paginated menu border. This will override any items already placed by default. Key = slot, Value = Item
@@ -86,20 +78,14 @@ public void setMenuItems() {
8678

8779
addMenuBorder();
8880

89-
List<Object> data = (List<Object>) getData();
90-
91-
if (data != null && !data.isEmpty()) {
92-
for (int i = 0; i < getMaxItemsPerPage(); i++) {
93-
index = getMaxItemsPerPage() * page + i;
94-
System.out.println(index);
95-
if (index >= data.size()) break;
96-
if (data.get(index) != null) {
97-
loopCode(data.get(index)); //run the code defined by the user
98-
}
99-
}
81+
//add the items to the inventory based on the current page and max items per page
82+
List<ItemStack> items = dataToItems();
83+
for (int i = 0; i < maxItemsPerPage; i++) {
84+
int index = maxItemsPerPage * page + i;
85+
if (index >= items.size()) break;
86+
inventory.addItem(items.get(index));
10087
}
10188

102-
10389
}
10490

10591
/**
@@ -119,7 +105,7 @@ public boolean prevPage() {
119105
* @return true if successful, false if already on the last page
120106
*/
121107
public boolean nextPage() {
122-
if (!((index + 1) >= getData().size())) {
108+
if (!((page + 1) * maxItemsPerPage >= dataToItems().size())) {
123109
page = page + 1;
124110
reloadItems();
125111
return true;

0 commit comments

Comments
 (0)