Skip to content

Commit 855e1c2

Browse files
committed
add Menu#setTitle
1 parent c6c99d5 commit 855e1c2

1 file changed

Lines changed: 23 additions & 3 deletions

File tree

  • core/src/main/java/net/j4c0b3y/api/menu

core/src/main/java/net/j4c0b3y/api/menu/Menu.java

Lines changed: 23 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -50,12 +50,12 @@ public abstract class Menu {
5050
/**
5151
* The underlying bukkit inventory the player sees.
5252
*/
53-
private final Inventory inventory;
53+
private Inventory inventory;
5454

5555
/**
5656
* The title shown at the top of the inventory.
5757
*/
58-
private final String title;
58+
private String title;
5959

6060
/**
6161
* The menu size in rows.
@@ -110,10 +110,14 @@ public Menu(String title, MenuSize size, Player player, MenuHandler handler) {
110110
throw new IllegalStateException("No menu handler instance found.");
111111
}
112112

113-
this.inventory = handler.getInventoryCreator().apply(player, getTotalSlots(), this.title);
113+
this.inventory = createInventory();
114114
this.async = getClass().isAnnotationPresent(Async.class);
115115
}
116116

117+
private Inventory createInventory() {
118+
return handler.getInventoryCreator().apply(player, getTotalSlots(), this.title);
119+
}
120+
117121
/**
118122
* Creates a new menu, initializing the underlying bukkit inventory.
119123
*
@@ -176,6 +180,22 @@ public void open() {
176180
}, async);
177181
}
178182

183+
public void setTitle(String title) {
184+
this.title = title;
185+
186+
// Create an inventory with the new title.
187+
Inventory inventory = createInventory();
188+
189+
// Copy the contents of the old inventory
190+
inventory.setContents(this.inventory.getContents());
191+
this.inventory = inventory;
192+
193+
// Open the new inventory for the player.
194+
handler.schedule(() ->
195+
player.openInventory(this.inventory)
196+
);
197+
}
198+
179199
/**
180200
* Removes the player from the open menus map.
181201
*

0 commit comments

Comments
 (0)