Skip to content

Commit 73885ee

Browse files
committed
Support both InventoryType and inventory size when instantiating menus
1 parent 5a0a5ba commit 73885ee

5 files changed

Lines changed: 57 additions & 5 deletions

File tree

pom.xml

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

77
<groupId>com.github.Fupery</groupId>
88
<artifactId>InvMenu</artifactId>
9-
<version>1.2.2</version>
9+
<version>1.2.3</version>
1010
<description>A Simple Inventory Menu Library</description>
1111

1212
<build>

src/main/java/com/github/Fupery/InvMenu/API/Handler/CacheableMenu.java

Lines changed: 11 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@
33
import com.github.Fupery.InvMenu.API.Button.Button;
44
import com.github.Fupery.InvMenu.API.Event.MenuCloseReason;
55
import com.github.Fupery.InvMenu.API.Templates.MenuTemplate;
6-
import org.bukkit.Bukkit;
6+
import com.github.Fupery.InvMenu.Utils.MenuType;
77
import org.bukkit.Material;
88
import org.bukkit.entity.Player;
99
import org.bukkit.event.inventory.ClickType;
@@ -15,11 +15,19 @@ public abstract class CacheableMenu implements MenuTemplate {
1515

1616
protected MenuHandler handler;
1717
protected String heading;
18-
protected InventoryType type;
18+
protected MenuType type;
1919
private Button[] buttons;
2020
private boolean open = false;
2121

2222
protected CacheableMenu(MenuHandler handler, String heading, InventoryType type) {
23+
this(handler, heading, new MenuType(type));
24+
}
25+
26+
protected CacheableMenu(MenuHandler handler, String heading, int size) {
27+
this(handler, heading, new MenuType(size));
28+
}
29+
30+
protected CacheableMenu(MenuHandler handler, String heading, MenuType type) {
2331
this.handler = handler;
2432
this.heading = (heading.length() > 32) ? this.heading = heading.substring(0, 29) + "..." : heading;
2533
this.type = type;
@@ -34,7 +42,7 @@ private void loadButtons(Inventory inventory) {
3442
}
3543

3644
void open(Player player) {
37-
Inventory inventory = Bukkit.createInventory(player, type, heading);
45+
Inventory inventory = type.createInventory(player, heading);
3846
loadButtons(inventory);
3947
player.openInventory(inventory);
4048
onMenuOpenEvent(player);

src/main/java/com/github/Fupery/InvMenu/API/Templates/BasicMenu.java

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,10 @@ protected BasicMenu(MenuHandler handler, String heading, InventoryType type) {
1818
super(handler, heading, type);
1919
}
2020

21+
protected BasicMenu(MenuHandler handler, String heading, int size) {
22+
super(handler, heading, size);
23+
}
24+
2125
@Override
2226
public void onMenuOpenEvent(Player viewer) {
2327
}

src/main/java/com/github/Fupery/InvMenu/API/Templates/ListMenu.java

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@
55
import com.github.Fupery.InvMenu.API.Event.MenuCloseReason;
66
import com.github.Fupery.InvMenu.API.Handler.CacheableMenu;
77
import com.github.Fupery.InvMenu.API.Handler.MenuHandler;
8+
import com.github.Fupery.InvMenu.Utils.MenuType;
89
import com.github.Fupery.InvMenu.Utils.SoundCompat;
910
import org.bukkit.Material;
1011
import org.bukkit.entity.Player;
@@ -21,7 +22,11 @@ public ListMenu(MenuHandler handler, String heading, int page) {
2122
}
2223

2324
public ListMenu(MenuHandler handler, String heading, int listStartIndex, int page) {
24-
super(handler, heading, InventoryType.CHEST);
25+
this(handler, heading, new MenuType(InventoryType.CHEST), listStartIndex, page);
26+
}
27+
28+
public ListMenu(MenuHandler handler, String heading, MenuType menuType, int listStartIndex, int page) {
29+
super(handler, heading, menuType);
2530
this.heading = heading;
2631
this.listStart = listStartIndex;
2732
this.page = page;
Lines changed: 35 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,35 @@
1+
package com.github.Fupery.InvMenu.Utils;
2+
3+
import org.bukkit.Bukkit;
4+
import org.bukkit.entity.Player;
5+
import org.bukkit.event.inventory.InventoryType;
6+
import org.bukkit.inventory.Inventory;
7+
8+
/**
9+
* Wrapper for menu types that supports both InventoryType and inventory size
10+
*/
11+
public class MenuType {
12+
13+
private final InventoryType inventoryType;
14+
private final int size;
15+
16+
public MenuType(InventoryType inventoryType) {
17+
this.inventoryType = inventoryType;
18+
this.size = (inventoryType != null) ? inventoryType.getDefaultSize() : -1;
19+
}
20+
21+
public MenuType(int size) {
22+
this.size = size;
23+
this.inventoryType = null;
24+
}
25+
26+
public Inventory createInventory(Player player, String heading) {
27+
if (inventoryType != null) return Bukkit.createInventory(player, inventoryType, heading);
28+
else if (size > 0) return Bukkit.createInventory(player, size, heading);
29+
else return null;
30+
}
31+
32+
public int getDefaultSize() {
33+
return size;
34+
}
35+
}

0 commit comments

Comments
 (0)