Skip to content

Commit

Permalink
* Introduced paginated menu sizing.
Browse files Browse the repository at this point in the history
  • Loading branch information
Hempfest committed Mar 12, 2021
1 parent 63e06b1 commit bbeca00
Show file tree
Hide file tree
Showing 6 changed files with 189 additions and 34 deletions.
2 changes: 1 addition & 1 deletion pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@

<groupId>com.github.sanctum.Labyrinth</groupId>
<artifactId>Labyrinth</artifactId>
<version>1.3.7-SNAP</version>
<version>1.3.8</version>
<packaging>jar</packaging>

<name>Labyrinth</name>
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
package com.github.sanctum.labyrinth.gui.builder;

/**
* Helpful enum to define inventory size with.
*/
public enum InventoryRows {
ONE(9),
TWO(18),
THREE(27),
FOUR(36),
FIVE(45),
SIX(54);

/**
* Number of slots in an Inventory of these rows.
*/
private final int slotCount;

InventoryRows(int slots) {
this.slotCount = slots;
}

public int getSlotCount() {
return slotCount;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,7 @@ public final class PaginatedBuilder {
protected int amountPer;
protected int index;
protected int page;
protected int size;
protected final UUID id;
protected String title;
protected String alreadyFirstPage;
Expand Down Expand Up @@ -99,6 +100,28 @@ public PaginatedBuilder limit(int amountPer) {
return this;
}

/**
* Define how large the inventory will be
*
* @param size The size of the inventory
* @return The same menu builder.
*/
public PaginatedBuilder setSize(int size) {
this.size = size;
return this;
}

/**
* Define how large the inventory will be
*
* @param rows The size of the inventory
* @return The same menu builder.
*/
public PaginatedBuilder setSize(InventoryRows rows) {
this.size = rows.getSlotCount();
return this;
}

/**
* Set the message to be displayed when a player attempts to switch to a previous page on the initial page.
*
Expand Down Expand Up @@ -172,20 +195,67 @@ public SpareElement newItem() {
protected PaginatedBuilder adjust(int desiredPage) {
page = desiredPage;
if (border != null) {
int j;
for (j = 0; j < 10; j++) {
if (inv.getItem(j) == null)
inv.setItem(j, border);
}
inv.setItem(17, border);
inv.setItem(18, border);
inv.setItem(26, border);
inv.setItem(27, border);
inv.setItem(35, border);
inv.setItem(36, border);
for (j = 44; j < 54; j++) {
if (inv.getItem(j) == null)
inv.setItem(j, border);
switch (size) {
case 27:
int f;
for (f = 0; f < 10; f++) {
if (inv.getItem(f) == null)
inv.setItem(f, border);
}
inv.setItem(17, border);
for (f = 18; f < 27; f++) {
if (inv.getItem(f) == null)
inv.setItem(f, border);
}
break;
case 36:
int h;
for (h = 0; h < 10; h++) {
if (inv.getItem(h) == null)
inv.setItem(h, border);
}
inv.setItem(17, border);
inv.setItem(18, border);
inv.setItem(26, border);
for (h = 27; h < 36; h++) {
if (inv.getItem(h) == null)
inv.setItem(h, border);
}
break;
case 45:
int o;
for (o = 0; o < 10; o++) {
if (inv.getItem(o) == null)
inv.setItem(o, border);
}
inv.setItem(17, border);
inv.setItem(18, border);
inv.setItem(26, border);
inv.setItem(27, border);
inv.setItem(35, border);
inv.setItem(36, border);
for (o = 36; o < 45; o++) {
if (inv.getItem(o) == null)
inv.setItem(o, border);
}
break;
case 54:
int j;
for (j = 0; j < 10; j++) {
if (inv.getItem(j) == null)
inv.setItem(j, border);
}
inv.setItem(17, border);
inv.setItem(18, border);
inv.setItem(26, border);
inv.setItem(27, border);
inv.setItem(35, border);
inv.setItem(36, border);
for (j = 44; j < 54; j++) {
if (inv.getItem(j) == null)
inv.setItem(j, border);
}
break;
}
}
if (collection == null) {
Expand Down Expand Up @@ -226,7 +296,7 @@ protected PaginatedBuilder adjust(int desiredPage) {
}
if (fill != null) {
Schedule.sync(() -> {
for (int l = 0; l < 54; l++) {
for (int l = 0; l < size; l++) {
if (inv.getItem(l) == null) {
inv.setItem(l, fill);
}
Expand Down Expand Up @@ -256,20 +326,67 @@ protected PaginatedBuilder adjust(int desiredPage) {
*/
protected PaginatedBuilder adjust() {
if (border != null) {
int j;
for (j = 0; j < 10; j++) {
if (inv.getItem(j) == null)
inv.setItem(j, border);
}
inv.setItem(17, border);
inv.setItem(18, border);
inv.setItem(26, border);
inv.setItem(27, border);
inv.setItem(35, border);
inv.setItem(36, border);
for (j = 44; j < 54; j++) {
if (inv.getItem(j) == null)
inv.setItem(j, border);
switch (size) {
case 27:
int f;
for (f = 0; f < 10; f++) {
if (inv.getItem(f) == null)
inv.setItem(f, border);
}
inv.setItem(17, border);
for (f = 18; f < 27; f++) {
if (inv.getItem(f) == null)
inv.setItem(f, border);
}
break;
case 36:
int h;
for (h = 0; h < 10; h++) {
if (inv.getItem(h) == null)
inv.setItem(h, border);
}
inv.setItem(17, border);
inv.setItem(18, border);
inv.setItem(26, border);
for (h = 27; h < 36; h++) {
if (inv.getItem(h) == null)
inv.setItem(h, border);
}
break;
case 45:
int o;
for (o = 0; o < 10; o++) {
if (inv.getItem(o) == null)
inv.setItem(o, border);
}
inv.setItem(17, border);
inv.setItem(18, border);
inv.setItem(26, border);
inv.setItem(27, border);
inv.setItem(35, border);
inv.setItem(36, border);
for (o = 36; o < 45; o++) {
if (inv.getItem(o) == null)
inv.setItem(o, border);
}
break;
case 54:
int j;
for (j = 0; j < 10; j++) {
if (inv.getItem(j) == null)
inv.setItem(j, border);
}
inv.setItem(17, border);
inv.setItem(18, border);
inv.setItem(26, border);
inv.setItem(27, border);
inv.setItem(35, border);
inv.setItem(36, border);
for (j = 44; j < 54; j++) {
if (inv.getItem(j) == null)
inv.setItem(j, border);
}
break;
}
}
if (collection == null) {
Expand Down Expand Up @@ -309,7 +426,7 @@ protected PaginatedBuilder adjust() {
}
if (fill != null) {
Schedule.sync(() -> {
for (int l = 0; l < 54; l++) {
for (int l = 0; l < size; l++) {
if (inv.getItem(l) == null) {
inv.setItem(l, fill);
}
Expand Down Expand Up @@ -475,7 +592,7 @@ public void onProcess(SyncMenuItemPreProcessEvent e) {
public void onClose(InventoryCloseEvent e) {
if (!(e.getPlayer() instanceof Player))
return;
if (e.getView().getTopInventory().getSize() < 54)
if (e.getView().getTopInventory().getSize() < builder.size)
return;
if (builder.getInventory() == e.getInventory()) {
if (builder.closeAction != null) {
Expand All @@ -490,7 +607,7 @@ public void onClose(InventoryCloseEvent e) {
public void onClick(InventoryClickEvent e) {
if (!(e.getWhoClicked() instanceof Player))
return;
if (e.getView().getTopInventory().getSize() < 54)
if (e.getView().getTopInventory().getSize() < builder.size)
return;

if (e.getHotbarButton() != -1) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,7 @@ public void run() {
if (fallback) {
if (Bukkit.getOnlinePlayers().size() == 0) {
this.cancel();
return;
}
}
if (map != null) {
Expand All @@ -36,6 +37,7 @@ public void run() {
if (debug) {
Labyrinth.getInstance().getLogger().info("Closing un-used task, target player in-activity.");
}
return;
}
}
if (check) {
Expand All @@ -44,6 +46,7 @@ public void run() {
Labyrinth.getInstance().getLogger().info("Closing un-used task, target player in-activity.");
}
this.cancel();
return;
}
}
applicable.apply();
Expand All @@ -57,6 +60,7 @@ public void run() {
if (fallback) {
if (Bukkit.getOnlinePlayers().size() == 0) {
this.cancel();
return;
}
}
if (map != null) {
Expand All @@ -65,6 +69,7 @@ public void run() {
if (debug) {
Labyrinth.getInstance().getLogger().info("Closing un-used task, target player in-activity.");
}
return;
}
}
if (count > 0) {
Expand All @@ -74,6 +79,7 @@ public void run() {
Labyrinth.getInstance().getLogger().info("Closing un-used task, target player in-activity.");
}
this.cancel();
return;
}
}
applicable.apply();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,7 @@ public void run() {
if (fallback) {
if (Bukkit.getOnlinePlayers().size() == 0) {
cancelTask();
return;
}
}
if (map != null) {
Expand All @@ -37,6 +38,7 @@ public void run() {
if (debug) {
Labyrinth.getInstance().getLogger().info("Closing un-used task, target player in-activity.");
}
return;
}
}
if (check) {
Expand All @@ -45,6 +47,7 @@ public void run() {
Labyrinth.getInstance().getLogger().info("Closing un-used task, target player in-activity.");
}
cancelTask();
return;
}
}
applicable.apply();
Expand All @@ -58,6 +61,7 @@ public void run() {
if (fallback) {
if (Bukkit.getOnlinePlayers().size() == 0) {
cancelTask();
return;
}
}
if (map != null) {
Expand All @@ -66,6 +70,7 @@ public void run() {
if (debug) {
Labyrinth.getInstance().getLogger().info("Closing un-used task, target player in-activity.");
}
return;
}
}
if (count > 0) {
Expand All @@ -75,6 +80,7 @@ public void run() {
Labyrinth.getInstance().getLogger().info("Closing un-used task, target player in-activity.");
}
cancelTask();
return;
}
}
applicable.apply();
Expand Down
2 changes: 1 addition & 1 deletion src/main/resources/plugin.yml
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
name: Labyrinth
version: 1.3.7
version: 1.3.8
main: com.github.sanctum.labyrinth.Labyrinth
prefix: Labyrinth
api-version: 1.13
Expand Down

0 comments on commit bbeca00

Please sign in to comment.