Skip to content

Commit

Permalink
change how time is stored
Browse files Browse the repository at this point in the history
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.
  • Loading branch information
broken1arrow committed Apr 29, 2023
1 parent e2c5c66 commit b373cca
Show file tree
Hide file tree
Showing 2 changed files with 30 additions and 11 deletions.
10 changes: 5 additions & 5 deletions src/main/java/org/brokenarrow/menu/library/CreateMenus.java
Original file line number Diff line number Diff line change
Expand Up @@ -61,7 +61,7 @@ public CreateMenus(final @Nullable List<Integer> fillSlots, @Nullable final List
private final List<MenuButton> buttons = new ArrayList<>();
private final List<MenuButton> buttonsToUpdate = new ArrayList<>();
private final Map<Integer, Map<Integer, MenuData>> addedButtons = new HashMap<>();
private final Map<MenuButton, Long> timeWhenUpdatesButtons = new HashMap<>();
private final Map<Integer, Long> timeWhenUpdatesButtons = new HashMap<>();

protected List<Integer> fillSpace;
private final List<?> listOfFillItems;
Expand Down Expand Up @@ -164,14 +164,13 @@ public InventoryType getInventoryType() {
}

@Nonnull
protected Map<MenuButton, Long> getTimeWhenUpdatesButtons() {
protected Map<Integer, Long> getTimeWhenUpdatesButtons() {
return timeWhenUpdatesButtons;
}

@Nullable
protected Long getTimeWhenUpdatesButton(MenuButton menuButton) {
System.out.println("menuButton " + menuButton.getId());
return getTimeWhenUpdatesButtons().getOrDefault(menuButton, null);
return getTimeWhenUpdatesButtons().getOrDefault(menuButton.getId(), null);
}

/**
Expand Down Expand Up @@ -525,7 +524,7 @@ protected void putAddedButtonsCache(Integer pageNumber, Map<Integer, MenuData> m
}

protected void putTimeWhenUpdatesButtons(MenuButton menuButton, Long time) {
this.getTimeWhenUpdatesButtons().put(menuButton, time);
this.getTimeWhenUpdatesButtons().put(menuButton.getId(), time);
}

protected void changePage(final boolean nextPage) {
Expand Down Expand Up @@ -786,6 +785,7 @@ private Map<Integer, MenuData> cacheMenuData(final int pageNumber) {
}
final MenuButton menuButton = getMenuButtonAtSlot(slot, slotIndexOld, objectFromlistOfFillItems);
final ItemStack result = getItemAtSlot(menuButton, slot, slotIndexOld, objectFromlistOfFillItems);

if (menuButton != null) {
if (menuButton.shouldUpdateButtons())
this.buttonsToUpdate.add(menuButton);
Expand Down
31 changes: 25 additions & 6 deletions src/main/java/org/brokenarrow/menu/library/MenuButton.java
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,13 @@

public abstract class MenuButton {

private static int counter = 0;
private final int id;

public MenuButton() {
this.id = counter++;
}

/**
* when you click inside the menu.
*
Expand Down Expand Up @@ -52,21 +59,33 @@ public ItemStack getItem(int slot, @Nullable Object object) {
}

/**
* Set own time it shall update buttons in seconds.
* Set your own time, if and when it shall update buttons. If this is set to -1
* It will use the global from {@link CreateMenus#getUpdateTime()}
* <p>
* You also need set this to true {@link #shouldUpdateButtons()}
*
* @return seconds.
* @return -1 or seconds between updates.
*/
public long updateTime() {
public long setUpdateTime() {
return -1;
}

/**
* Set this to true if you want to update buttons.
* Returns true if the buttons should be updated, when menu is open and no buttons are pushed. By default, this method
* returns false. If you want to update the buttons, override this method and return true in your implementation.
*
* @return true if it shall update button.
* @return true if the buttons should be updated, false otherwise.
*/
public boolean updateButton() {
public boolean shouldUpdateButtons() {
return false;
}

/**
* The unique id for this instance.
*
* @return the id for this instance.
*/
public int getId() {
return id;
}
}

0 comments on commit b373cca

Please sign in to comment.