Skip to content

Commit

Permalink
fix CME on AddonMenu
Browse files Browse the repository at this point in the history
  • Loading branch information
HSGamer committed Nov 26, 2022
1 parent be744be commit 8481415
Showing 1 changed file with 26 additions and 17 deletions.
43 changes: 26 additions & 17 deletions src/main/java/me/hsgamer/bettergui/menu/AddonMenu.java
Original file line number Diff line number Diff line change
Expand Up @@ -44,29 +44,33 @@ public AddonMenu(Config config) {
protected ButtonMap createButtonMap(Config config) {
Map<String, Object> itemMap = config.getNormalizedValues("button", false);
return new ButtonMap() {
private final Map<Button, List<Integer>> buttonMap = new HashMap<>();
private Map<Button, List<Integer>> buttonMap;
private final Object lock = new Object();

@Override
public Map<Button, List<Integer>> getButtons(UUID uuid) {
Collection<DownloadInfo> downloadInfos = getInstance().getAddonDownloader().getLoadedDownloadInfo().values();
int slot = 0;
for (DownloadInfo info : downloadInfos) {
ItemBuilder itemBuilder = new ItemBuilder();
ItemModifierBuilder.INSTANCE.build(itemMap).forEach(itemBuilder::addItemModifier);
buttonMap.put(new AddonButton(info, itemBuilder), Collections.singletonList(slot++));
synchronized (lock) {
if (buttonMap == null) {
buttonMap = new HashMap<>();
Collection<DownloadInfo> downloadInfos = getInstance().getAddonDownloader().getLoadedDownloadInfo().values();
int slot = 0;
for (DownloadInfo info : downloadInfos) {
ItemBuilder itemBuilder = new ItemBuilder();
ItemModifierBuilder.INSTANCE.build(itemMap).forEach(itemBuilder::addItemModifier);
buttonMap.put(new AddonButton(info, itemBuilder), Collections.singletonList(slot++));
}
buttonMap.keySet().forEach(Initializable::init);
}
}
return buttonMap;
}

@Override
public void init() {
buttonMap.keySet().forEach(Initializable::init);
}

@Override
public void stop() {
buttonMap.keySet().forEach(Initializable::stop);
buttonMap.clear();
if (buttonMap != null) {
buttonMap.keySet().forEach(Initializable::stop);
buttonMap.clear();
}
}
};
}
Expand All @@ -89,8 +93,15 @@ private class AddonButton implements Button {
itemBuilder.addStringReplacer("colorize", StringReplacerApplier.COLORIZE);
}

private void updateStatus() {
status = Optional.ofNullable(getInstance().getAddonManager().getAddon(downloadInfo.getName()))
.map(addon -> addon.getDescription().getVersion().equals(downloadInfo.getVersion()) ? upToDateStatus : outdatedStatus)
.orElse(availableStatus);
}

@Override
public ItemStack getItemStack(UUID uuid) {
updateStatus();
return itemBuilder.build(uuid);
}

Expand Down Expand Up @@ -127,9 +138,7 @@ public void handleAction(UUID uuid, InventoryClickEvent event) {

@Override
public void init() {
status = Optional.ofNullable(getInstance().getAddonManager().getAddon(downloadInfo.getName()))
.map(addon -> addon.getDescription().getVersion().equals(downloadInfo.getVersion()) ? upToDateStatus : outdatedStatus)
.orElse(availableStatus);
updateStatus();
}
}
}

0 comments on commit 8481415

Please sign in to comment.