Skip to content

Commit b373cca

Browse files
committed
change how time is stored
It now use a id instead of the class, to remove inconsistency and also not store the menu button in more places than it needs to.
1 parent e2c5c66 commit b373cca

File tree

2 files changed

+30
-11
lines changed

2 files changed

+30
-11
lines changed

src/main/java/org/brokenarrow/menu/library/CreateMenus.java

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -61,7 +61,7 @@ public CreateMenus(final @Nullable List<Integer> fillSlots, @Nullable final List
6161
private final List<MenuButton> buttons = new ArrayList<>();
6262
private final List<MenuButton> buttonsToUpdate = new ArrayList<>();
6363
private final Map<Integer, Map<Integer, MenuData>> addedButtons = new HashMap<>();
64-
private final Map<MenuButton, Long> timeWhenUpdatesButtons = new HashMap<>();
64+
private final Map<Integer, Long> timeWhenUpdatesButtons = new HashMap<>();
6565

6666
protected List<Integer> fillSpace;
6767
private final List<?> listOfFillItems;
@@ -164,14 +164,13 @@ public InventoryType getInventoryType() {
164164
}
165165

166166
@Nonnull
167-
protected Map<MenuButton, Long> getTimeWhenUpdatesButtons() {
167+
protected Map<Integer, Long> getTimeWhenUpdatesButtons() {
168168
return timeWhenUpdatesButtons;
169169
}
170170

171171
@Nullable
172172
protected Long getTimeWhenUpdatesButton(MenuButton menuButton) {
173-
System.out.println("menuButton " + menuButton.getId());
174-
return getTimeWhenUpdatesButtons().getOrDefault(menuButton, null);
173+
return getTimeWhenUpdatesButtons().getOrDefault(menuButton.getId(), null);
175174
}
176175

177176
/**
@@ -525,7 +524,7 @@ protected void putAddedButtonsCache(Integer pageNumber, Map<Integer, MenuData> m
525524
}
526525

527526
protected void putTimeWhenUpdatesButtons(MenuButton menuButton, Long time) {
528-
this.getTimeWhenUpdatesButtons().put(menuButton, time);
527+
this.getTimeWhenUpdatesButtons().put(menuButton.getId(), time);
529528
}
530529

531530
protected void changePage(final boolean nextPage) {
@@ -786,6 +785,7 @@ private Map<Integer, MenuData> cacheMenuData(final int pageNumber) {
786785
}
787786
final MenuButton menuButton = getMenuButtonAtSlot(slot, slotIndexOld, objectFromlistOfFillItems);
788787
final ItemStack result = getItemAtSlot(menuButton, slot, slotIndexOld, objectFromlistOfFillItems);
788+
789789
if (menuButton != null) {
790790
if (menuButton.shouldUpdateButtons())
791791
this.buttonsToUpdate.add(menuButton);

src/main/java/org/brokenarrow/menu/library/MenuButton.java

Lines changed: 25 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,13 @@
88

99
public abstract class MenuButton {
1010

11+
private static int counter = 0;
12+
private final int id;
13+
14+
public MenuButton() {
15+
this.id = counter++;
16+
}
17+
1118
/**
1219
* when you click inside the menu.
1320
*
@@ -52,21 +59,33 @@ public ItemStack getItem(int slot, @Nullable Object object) {
5259
}
5360

5461
/**
55-
* Set own time it shall update buttons in seconds.
62+
* Set your own time, if and when it shall update buttons. If this is set to -1
63+
* It will use the global from {@link CreateMenus#getUpdateTime()}
64+
* <p>
65+
* You also need set this to true {@link #shouldUpdateButtons()}
5666
*
57-
* @return seconds.
67+
* @return -1 or seconds between updates.
5868
*/
59-
public long updateTime() {
69+
public long setUpdateTime() {
6070
return -1;
6171
}
6272

6373
/**
64-
* Set this to true if you want to update buttons.
74+
* Returns true if the buttons should be updated, when menu is open and no buttons are pushed. By default, this method
75+
* returns false. If you want to update the buttons, override this method and return true in your implementation.
6576
*
66-
* @return true if it shall update button.
77+
* @return true if the buttons should be updated, false otherwise.
6778
*/
68-
public boolean updateButton() {
79+
public boolean shouldUpdateButtons() {
6980
return false;
7081
}
7182

83+
/**
84+
* The unique id for this instance.
85+
*
86+
* @return the id for this instance.
87+
*/
88+
public int getId() {
89+
return id;
90+
}
7291
}

0 commit comments

Comments
 (0)