Skip to content

Commit 8aa4b42

Browse files
committed
Improve the menus module
1 parent 6a1b8b0 commit 8aa4b42

12 files changed

Lines changed: 333 additions & 116 deletions

File tree

Menu Library/src/main/java/org/broken/arrow/menu/library/MenuUtility.java

Lines changed: 112 additions & 80 deletions
Large diffs are not rendered by default.

Menu Library/src/main/java/org/broken/arrow/menu/library/holder/HolderUtility.java

Lines changed: 7 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -23,7 +23,6 @@
2323
import javax.annotation.Nullable;
2424
import java.util.ArrayList;
2525
import java.util.List;
26-
import java.util.Map;
2726
import java.util.Set;
2827
import java.util.stream.Collectors;
2928
import java.util.stream.IntStream;
@@ -49,7 +48,7 @@ protected HolderUtility() {
4948
* Create menu instance.
5049
*
5150
* @param shallCacheItems Set this to false items and slots should be cached in this class,
52-
* other case override {@link #retrieveMenuButtons(int, Map)} to cache
51+
* other case override {@link #retrieveMenuButtons(int, MenuDataUtility)} to cache
5352
* this in own implementation.
5453
*/
5554
protected HolderUtility(final boolean shallCacheItems) {
@@ -61,7 +60,7 @@ protected HolderUtility(final boolean shallCacheItems) {
6160
*
6261
* @param fillSlots The slots to be filled with items.
6362
* @param shallCacheItems Set this to false items and slots should be cached in this class,
64-
* other case override {@link #retrieveMenuButtons(int, Map)} to cache
63+
* other case override {@link #retrieveMenuButtons(int, MenuDataUtility)} to cache
6564
* this in own implementation.
6665
*/
6766
protected HolderUtility(List<Integer> fillSlots, boolean shallCacheItems) {
@@ -77,7 +76,7 @@ protected HolderUtility(List<Integer> fillSlots, boolean shallCacheItems) {
7776
* @param menuAPI The instance of RegisterMenuAPI where you have registered your plugin.
7877
* @param fillSlots The slots to be filled with items. Can be null if not filling specific slots.
7978
* @param shallCacheItems Set this to false items and slots should be cached in this class,
80-
* other case override {@link #retrieveMenuButtons(int, Map)} to cache
79+
* other case override {@link #retrieveMenuButtons(int, MenuDataUtility)} to cache
8180
* this in own implementation.
8281
*/
8382
protected HolderUtility(RegisterMenuAPI menuAPI, List<Integer> fillSlots, boolean shallCacheItems) {
@@ -97,7 +96,7 @@ public void menuClose(final InventoryCloseEvent event, final MenuUtility<?> menu
9796

9897
/**
9998
* open menu and make one instance in cache.
100-
* Will be clered on server restart.
99+
* Will be cleared on server restart.
101100
*
102101
* @param player some open menu.
103102
* @param location location you open menu.
@@ -145,7 +144,8 @@ public void menuOpen(@Nonnull final Player player, @Nullable final Location loca
145144

146145
if (!getButtonsToUpdate().isEmpty())
147146
updateButtonsInList();
148-
Bukkit.getScheduler().runTaskLater(menuAPI.getPlugin(), this::updateTitle, 1);
147+
148+
Bukkit.getScheduler().runTaskLater(menuAPI.getPlugin(), ()->this.updateTitle(), 1);
149149
}
150150

151151
/**
@@ -404,16 +404,6 @@ public boolean setPage(final int page) {
404404
return true;
405405
}
406406

407-
/**
408-
* get the number it currently fill
409-
* items in.
410-
*
411-
* @return curent number it will fill with one item.
412-
*/
413-
public int getSlotIndex() {
414-
return this.slotIndex;
415-
}
416-
417407
/**
418408
* get previous page if this menu has several pages
419409
*/
@@ -451,7 +441,7 @@ public void updateButton(final MenuButton menuButton) {
451441

452442
final ItemStack menuItem = getMenuItem(menuButton, buttonData, slot, true);
453443
menu.setItem(slot, menuItem);
454-
menuDataUtility.putButton(menuSlot, buttonData.copy(menuItem) /*new ButtonData<>(menuItem, buttonData.getMenuButton(), buttonData.getObject())*/);
444+
menuDataUtility.putButton(menuSlot, buttonData.copy(menuItem));
455445
}
456446
} else {
457447
final int buttonSlot = this.getButtonSlot(menuButton);

Menu Library/src/main/java/org/broken/arrow/menu/library/holder/MenuHolder.java

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,8 @@
11
package org.broken.arrow.menu.library.holder;
22

3+
import org.broken.arrow.menu.library.builders.MenuDataUtility;
4+
35
import java.util.List;
4-
import java.util.Map;
56

67
/**
78
* This class handles single-page menus or paged menus if you implement the logic yourself.
@@ -75,7 +76,7 @@ protected MenuHolder(final List<Integer> fillSlots) {
7576
* Constructs a menu instance with specified caching option.
7677
*
7778
* @param shallCacheItems Set this to false if items and slots should be cached in this class.
78-
* Otherwise, override {@link #retrieveMenuButtons(int, Map)} to cache
79+
* Otherwise, override {@link #retrieveMenuButtons(int, MenuDataUtility)} to cache
7980
* them in your own implementation.
8081
*/
8182
protected MenuHolder(final boolean shallCacheItems) {
@@ -90,7 +91,7 @@ protected MenuHolder(final boolean shallCacheItems) {
9091
* @param fillSlots The slots you want to fill with items, and you need to set the amount of pages if your plan
9192
* to use mor than one page.
9293
* @param shallCacheItems Set this to false if items and slots should be cached in this class.
93-
* Otherwise, override {@link #retrieveMenuButtons(int, Map)} to cache
94+
* Otherwise, override {@link #retrieveMenuButtons(int, MenuDataUtility)} to cache
9495
* them in your own implementation.
9596
*/
9697
protected MenuHolder(final List<Integer> fillSlots, boolean shallCacheItems) {

Menu Library/src/main/java/org/broken/arrow/menu/library/holder/MenuHolderPage.java

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -138,7 +138,7 @@ protected MenuHolderPage(final List<Integer> fillSlots, final List<T> fillItems)
138138
* @param fillSlots The slots to be filled with items on each page.
139139
* @param fillItems The list of items to be displayed inside the GUI.
140140
* @param shallCacheItems Set this to false if items and slots should be cached in this class;
141-
* otherwise, override {@link #retrieveMenuButtons(int, Map)} to cache
141+
* otherwise, override {@link #retrieveMenuButtons(int, MenuDataUtility)} to cache
142142
* them in your own implementation.
143143
*/
144144
protected MenuHolderPage(@Nullable List<Integer> fillSlots, @Nullable List<T> fillItems, boolean shallCacheItems) {
@@ -154,7 +154,7 @@ protected MenuHolderPage(@Nullable List<Integer> fillSlots, @Nullable List<T> fi
154154
* @param fillSlots The slots to be filled with items on each page.
155155
* @param fillItems The list of items to be displayed inside the GUI.
156156
* @param shallCacheItems Set this to false if items and slots should be cached in this class;
157-
* otherwise, override {@link #retrieveMenuButtons(int, Map)} to cache
157+
* otherwise, override {@link #retrieveMenuButtons(int, MenuDataUtility)} to cache
158158
* them in your own implementation.
159159
*/
160160
protected MenuHolderPage(@Nonnull RegisterMenuAPI menuAPI, @Nullable List<Integer> fillSlots, @Nullable List<T> fillItems, boolean shallCacheItems) {
@@ -283,7 +283,7 @@ protected final double amountOfPages() {
283283
}
284284

285285
@Override
286-
protected void setButton(final int pageNumber, final MenuDataUtility<T> menuDataUtility, final int slot, final int fillSlotIndex, final boolean isLastFillSlot) {
286+
public void setButton(final int pageNumber, final MenuDataUtility<T> menuDataUtility, final int slot, final int fillSlotIndex, final boolean isLastFillSlot) {
287287
final int fillSlot = isLastFillSlot ? -1 : fillSlotIndex;
288288

289289
final MenuButton menuButton = getMenuButtonAtSlot(slot, fillSlot);
@@ -353,7 +353,7 @@ private MenuButtonPage<T> getPagedMenuButton(MenuButton menuButton) {
353353
}
354354

355355
private boolean isFillSlot(final int slot) {
356-
final List<Integer> fillSlots = this.fillSpace == null ? this.getFillSpace() : this.fillSpace;
356+
final List<Integer> fillSlots = this.getFillSpace();
357357
return !fillSlots.isEmpty() && fillSlots.contains(slot);
358358
}
359359
}
Lines changed: 54 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,54 @@
1+
package org.broken.arrow.menu.library.holder.utility;
2+
3+
import org.broken.arrow.menu.library.MenuUtility;
4+
import org.broken.arrow.menu.library.utility.ServerVersion;
5+
import org.bukkit.Bukkit;
6+
import org.bukkit.scheduler.BukkitRunnable;
7+
8+
public class AnimateTitleTask<T> extends BukkitRunnable {
9+
10+
private final MenuUtility<T> menuUtility;
11+
private int taskId;
12+
private volatile boolean cancelled = false;
13+
14+
public AnimateTitleTask(MenuUtility<T> menuUtility) {
15+
this.menuUtility = menuUtility;
16+
}
17+
18+
public void runTask(long delay) {
19+
taskId = runTaskTimerAsynchronously(menuUtility.getPlugin(), 1L, delay).getTaskId();
20+
}
21+
22+
public boolean isRunning() {
23+
return taskId > 0 &&
24+
(Bukkit.getScheduler().isCurrentlyRunning(taskId) ||
25+
Bukkit.getScheduler().isQueued(taskId));
26+
}
27+
28+
public void stopTask() {
29+
if (this.isRunning()) {
30+
this.cancelled = true;
31+
Bukkit.getScheduler().cancelTask(this.taskId);
32+
}
33+
}
34+
35+
@Override
36+
public void run() {
37+
if(this.cancelled) return;
38+
39+
Object text = menuUtility.getAnimateTitle().apply();
40+
if (text == null || (ServerVersion.atLeast(ServerVersion.V1_9) && this.isCancelled())) {
41+
this.cancelled = true;
42+
this.cancel();
43+
menuUtility.updateTitle();
44+
return;
45+
}
46+
if (!text.equals("")) {
47+
menuUtility.updateTitle(text);
48+
} else {
49+
this.cancelled = true;
50+
this.cancel();
51+
}
52+
}
53+
54+
}
Lines changed: 70 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,70 @@
1+
package org.broken.arrow.menu.library.holder.utility;
2+
3+
import org.broken.arrow.logging.library.Logging;
4+
import org.broken.arrow.menu.library.MenuUtility;
5+
import org.broken.arrow.menu.library.builders.ButtonData;
6+
import org.bukkit.Bukkit;
7+
import org.bukkit.Material;
8+
import org.bukkit.event.inventory.InventoryType;
9+
import org.bukkit.inventory.Inventory;
10+
import org.bukkit.inventory.ItemStack;
11+
12+
import javax.annotation.Nonnull;
13+
import java.util.List;
14+
import java.util.Map;
15+
import java.util.Optional;
16+
import java.util.logging.Level;
17+
18+
public class InventoryRenderer<T> {
19+
20+
private final MenuUtility<T> utility;
21+
private final Logging logger = new Logging(MenuUtility.class);
22+
23+
public InventoryRenderer(MenuUtility<T> utility) {
24+
this.utility = utility;
25+
}
26+
27+
@Nonnull
28+
public Inventory redraw() {
29+
Inventory menu = utility.getMenu();
30+
int size = utility.getInventorySize();
31+
List<Integer> fillSpace = utility.getFillSpace();
32+
33+
if (menu == null || size > menu.getSize()) {
34+
menu = createInventory();
35+
}
36+
37+
int fillSlots = !fillSpace.isEmpty() ? fillSpace.size() : menu.getSize();
38+
39+
for (int i = fillSpace.stream().findFirst().orElse(0); i < fillSlots; i++) {
40+
menu.setItem(i, new ItemStack(Material.AIR));
41+
}
42+
43+
Map<Integer, ButtonData<T>> buttons = utility.getMenuButtons(utility.getPageNumber());
44+
if (buttons != null && !buttons.isEmpty()) {
45+
for (int i = 0; i < menu.getSize(); i++) {
46+
ButtonData<?> data = buttons.get(utility.getPageNumber() * size + i);
47+
menu.setItem(i, data != null ? data.getItemStack() : null);
48+
}
49+
}
50+
51+
return menu;
52+
}
53+
54+
@Nonnull
55+
private Inventory createInventory() {
56+
String title = Optional.ofNullable(utility.getTitle()).map(Object::toString).orElse(" ");
57+
InventoryType type = utility.getInventoryType();
58+
int size = utility.getInventorySize();
59+
60+
if (type != null) return Bukkit.createInventory(null, type, title);
61+
62+
if (!(size == 5 || size % 9 == 0)) {
63+
this.logger.log(Level.WARNING, () -> Logging.of("Wrong inventory size , you has put in " + size + " it need to be valid number."));
64+
}
65+
66+
if (size == 5) return Bukkit.createInventory(null, InventoryType.HOPPER, title);
67+
68+
return Bukkit.createInventory(null, size % 9 == 0 ? size : 9, title);
69+
}
70+
}
Lines changed: 51 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,51 @@
1+
package org.broken.arrow.menu.library.holder.utility;
2+
3+
import org.broken.arrow.menu.library.MenuUtility;
4+
import org.broken.arrow.menu.library.builders.MenuDataUtility;
5+
6+
import java.util.List;
7+
8+
public class MenuRenderer<T> {
9+
10+
private final MenuUtility<T> utility;
11+
private int itemIndex;
12+
private int lastFillSlot;
13+
14+
public MenuRenderer(MenuUtility<T> utility) {
15+
this.utility = utility;
16+
}
17+
18+
public int getStartItemIndex() {
19+
return itemIndex;
20+
}
21+
22+
public void setStartItemIndex(final int startItemIndex) {
23+
this.itemIndex = startItemIndex;
24+
}
25+
26+
public void resetStartItemIndex() {
27+
this.itemIndex = 0;
28+
}
29+
30+
public void setHighestFillSlot(final int highestFillSlot) {
31+
this.lastFillSlot = highestFillSlot;
32+
}
33+
34+
public MenuDataUtility<T> renderPage(final int pageNumber) {
35+
MenuDataUtility<T> data = new MenuDataUtility<>();
36+
List<Integer> fillSlots = this.utility.getFillSpace();
37+
38+
for (int slot = 0; slot < this.utility.getInventorySize(); slot++) {
39+
boolean isFillButton = fillSlots.contains(slot);
40+
41+
this.utility.setButton(pageNumber, data, slot, this.itemIndex, slot > this.lastFillSlot);
42+
if (isFillButton) incrementItemIndex();
43+
}
44+
return data;
45+
}
46+
47+
private void incrementItemIndex() {
48+
itemIndex++;
49+
}
50+
51+
}

Menu Library/src/main/java/org/broken/arrow/menu/library/runnable/RunButtonAnimation.java renamed to Menu Library/src/main/java/org/broken/arrow/menu/library/runnable/ButtonAnimation.java

Lines changed: 21 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@
44
import org.broken.arrow.menu.library.builders.ButtonData;
55
import org.broken.arrow.menu.library.builders.MenuDataUtility;
66
import org.broken.arrow.menu.library.button.MenuButton;
7+
import org.bukkit.Bukkit;
78
import org.bukkit.inventory.Inventory;
89
import org.bukkit.inventory.ItemStack;
910
import org.bukkit.scheduler.BukkitRunnable;
@@ -17,21 +18,34 @@
1718
import java.util.Objects;
1819
import java.util.Set;
1920

20-
public class RunButtonAnimation<T> extends BukkitRunnable {
21+
public class ButtonAnimation<T> extends BukkitRunnable {
2122
private final Map<Integer, Long> timeWhenUpdatesButtons = new HashMap<>();
2223
private final MenuUtility<T> menuUtility;
2324
private final Inventory menu;
2425
private final int inventorySize;
2526
private int counter = 0;
27+
private int taskid;
2628

27-
public RunButtonAnimation(MenuUtility<T> menuUtility) {
29+
public ButtonAnimation(MenuUtility<T> menuUtility) {
2830
this.menuUtility = menuUtility;
2931
this.menu = menuUtility.getMenu();
3032
this.inventorySize = menuUtility.getInventorySize();
3133
}
3234

33-
public int runTask(long delay) {
34-
return runTaskTimer(menuUtility.getPlugin(), 1L, delay).getTaskId();
35+
public void runTask(long delay) {
36+
taskid = runTaskTimer(menuUtility.getPlugin(), 1L, delay).getTaskId();
37+
}
38+
39+
public boolean isRunning() {
40+
return taskid > 0 &&
41+
(Bukkit.getScheduler().isCurrentlyRunning(taskid) ||
42+
Bukkit.getScheduler().isQueued(taskid));
43+
}
44+
45+
public void stopTask() {
46+
if (this.isRunning()) {
47+
Bukkit.getScheduler().cancelTask(this.taskid);
48+
}
3549
}
3650

3751
@Override
@@ -58,7 +72,7 @@ else if (counter >= timeLeft) {
5872
}
5973

6074
private boolean updateButtonsData(final MenuButton menuButton, final MenuDataUtility<T> menuDataUtility, final Set<Integer> itemSlots) {
61-
if (!itemSlots.isEmpty()){
75+
if (!itemSlots.isEmpty()) {
6276
final Iterator<Integer> slotList = itemSlots.iterator();
6377
setButtons(menuButton, menuDataUtility, slotList);
6478
}
@@ -75,7 +89,7 @@ private void setButtons(final MenuButton menuButton, final MenuDataUtility<T> me
7589
if (buttonData == null) continue;
7690

7791
final ItemStack menuItem = getMenuItemStack(menuButton, buttonData, slot);
78-
final ButtonData<T> newButtonData = buttonData.copy(menuItem);
92+
final ButtonData<T> newButtonData = buttonData.copy(menuItem);
7993

8094
menuDataUtility.putButton(slotPageCalculated, newButtonData);
8195
menu.setItem(slot, menuItem);
@@ -132,4 +146,5 @@ private Set<Integer> getItemSlotsMap(final MenuDataUtility<T> menuDataMap, final
132146
}
133147
return slotList;
134148
}
149+
135150
}

0 commit comments

Comments
 (0)