From 17bbdacee3a368c92def6777647c5d4db60931d3 Mon Sep 17 00:00:00 2001 From: Zoinkwiz Date: Tue, 26 Nov 2024 17:13:09 +0000 Subject: [PATCH] feat: Potion Storage works with sidebar --- .../com/questhelper/QuestHelperPlugin.java | 1 + .../bank/banktab/PotionStorage.java | 41 +++---------- .../banktab/QuestHelperBankTagService.java | 58 ++++++++++++++++--- 3 files changed, 59 insertions(+), 41 deletions(-) diff --git a/src/main/java/com/questhelper/QuestHelperPlugin.java b/src/main/java/com/questhelper/QuestHelperPlugin.java index a738c54ce4..bcf4d8b4e2 100644 --- a/src/main/java/com/questhelper/QuestHelperPlugin.java +++ b/src/main/java/com/questhelper/QuestHelperPlugin.java @@ -289,6 +289,7 @@ public void onGameStateChanged(final GameStateChanged event) GlobalFakeObjects.createNpcs(client, runeliteObjectManager, configManager, config); newVersionManager.updateChatWithNotificationIfNewVersion(); questBankManager.setUnknownInitialState(); + potionStorage.cachePotions = true; clientThread.invokeAtTickEnd(() -> { questManager.setupRequirements(); questManager.setupOnLogin(); diff --git a/src/main/java/com/questhelper/bank/banktab/PotionStorage.java b/src/main/java/com/questhelper/bank/banktab/PotionStorage.java index 2479cac349..02f407c367 100644 --- a/src/main/java/com/questhelper/bank/banktab/PotionStorage.java +++ b/src/main/java/com/questhelper/bank/banktab/PotionStorage.java @@ -28,6 +28,7 @@ import javax.inject.Inject; import javax.inject.Singleton; +import com.questhelper.managers.QuestContainerManager; import lombok.RequiredArgsConstructor; import lombok.Setter; import lombok.extern.slf4j.Slf4j; @@ -35,9 +36,7 @@ import net.runelite.api.events.ClientTick; import net.runelite.api.events.MenuOptionClicked; import net.runelite.api.events.VarbitChanged; -import net.runelite.api.events.WidgetClosed; import net.runelite.api.widgets.ComponentID; -import net.runelite.api.widgets.InterfaceID; import net.runelite.api.widgets.Widget; import net.runelite.api.widgets.WidgetType; import net.runelite.client.eventbus.Subscribe; @@ -57,7 +56,7 @@ class Potion @Slf4j public class PotionStorage { - static final int BANKTAB_POTIONSTORE = 15; + static final int TOTAL_POTIONS_VARBIT = 4286; static final int COMPONENTS_PER_POTION = 5; private final Client client; @@ -65,7 +64,7 @@ public class PotionStorage private final BankSearch bankSearch; private Potion[] potions; - boolean cachePotions; + public boolean cachePotions; private boolean layout; private Set potionStoreVars; @@ -77,7 +76,7 @@ public void onClientTick(ClientTick event) { if (cachePotions) { -// log.debug("Rebuilding potions"); + log.debug("Rebuilding potions"); cachePotions = false; rebuildPotions(); @@ -106,23 +105,13 @@ public void onClientTick(ClientTick event) @Subscribe public void onVarbitChanged(VarbitChanged varbitChanged) { - if (potionStoreVars != null && potionStoreVars.contains(varbitChanged.getVarpId())) + if (TOTAL_POTIONS_VARBIT == varbitChanged.getVarpId() || potionStoreVars != null && potionStoreVars.contains(varbitChanged.getVarpId())) { cachePotions = true; layout = true; // trigger a bank rebuild as the qty has changed } } - @Subscribe - public void onWidgetClosed(WidgetClosed event) - { - if (event.getGroupId() == InterfaceID.BANK && event.isUnload()) - { - log.debug("Invalidating potions"); - potions = null; - } - } - private void rebuildPotions() { var potionStorePotions = client.getEnum(EnumID.POTIONSTORE_POTIONS); @@ -147,30 +136,13 @@ private void rebuildPotions() p.doses = doses; p.withdrawDoses = withdrawDoses; potions[potionsIdx] = p; - - if (log.isDebugEnabled()) - { -// log.debug("Potion store has {} doses of {}", p.doses, itemManager.getItemComposition(p.itemId).getName()); - } } ++potionsIdx; } } - for (Potion potion : potions) - { - if (potion != null) - { - String name = itemManager.getItemComposition(potion.itemId).getName(); - if (name.contains("Hunter")) - { -// System.out.println(itemManager.getItemComposition(potion.itemId).getName()); -// System.out.println(potion.doses); - } - } - } -// System.out.println(Arrays.toString(potions)); + QuestContainerManager.getPotionData().update(client.getTickCount(), getItems()); } public Item[] getItems() @@ -184,6 +156,7 @@ public Item[] getItems() for (Potion potion : potions) { + if (potion == null) continue; var potionEnum = potion.potionEnum; // TODO: An issue due to potentially wanting a specific potion, or to default to full potion int potionItemId = potionEnum.getIntValue(potion.withdrawDoses); diff --git a/src/main/java/com/questhelper/bank/banktab/QuestHelperBankTagService.java b/src/main/java/com/questhelper/bank/banktab/QuestHelperBankTagService.java index ca935ffcb3..fa8a0380a8 100644 --- a/src/main/java/com/questhelper/bank/banktab/QuestHelperBankTagService.java +++ b/src/main/java/com/questhelper/bank/banktab/QuestHelperBankTagService.java @@ -24,9 +24,7 @@ */ package com.questhelper.bank.banktab; -import com.questhelper.bank.QuestBank; import com.questhelper.QuestHelperPlugin; -import com.questhelper.managers.QuestContainerManager; import com.questhelper.panel.PanelDetails; import com.questhelper.requirements.item.ItemRequirement; import com.questhelper.requirements.item.ItemRequirements; @@ -38,6 +36,8 @@ import java.util.stream.Collectors; import javax.inject.Inject; import javax.inject.Singleton; + +import net.runelite.api.Client; import net.runelite.api.InventoryID; import net.runelite.api.ItemContainer; @@ -48,10 +48,54 @@ public class QuestHelperBankTagService private QuestHelperPlugin plugin; @Inject - private QuestBank questBank; + private Client client; + + ArrayList taggedItems; + + ArrayList taggedItemsForBank; + + int lastTickUpdated = 0; + + int lastTickUpdatedForBank = 0; + + public ArrayList itemsToTagForBank() + { + if (client.getTickCount() <= lastTickUpdatedForBank) + { + return taggedItemsForBank; + } + + lastTickUpdatedForBank = client.getTickCount(); + + ArrayList sortedItems = getPluginBankTagItemsForSections(false); + + if (sortedItems == null) + { + return null; + } + + taggedItemsForBank = new ArrayList<>(); + + sortedItems.stream() + .map(BankTabItems::getItems) + .flatMap(Collection::stream) + .map(BankTabItem::getItemIDs) + .flatMap(Collection::stream) + .filter(Objects::nonNull) // filter non-null just in case any Integer get in the list + .filter(id -> !taggedItemsForBank.contains(id)) + .forEach(taggedItemsForBank::add); + return taggedItemsForBank; + } public ArrayList itemsToTag() { + if (client.getTickCount() <= lastTickUpdated) + { + return taggedItems; + } + + lastTickUpdated = client.getTickCount(); + ArrayList sortedItems = getPluginBankTagItemsForSections(true); if (sortedItems == null) @@ -59,7 +103,7 @@ public ArrayList itemsToTag() return null; } - ArrayList flattenedList = new ArrayList<>(); + taggedItems = new ArrayList<>(); sortedItems.stream() .map(BankTabItems::getItems) @@ -67,9 +111,9 @@ public ArrayList itemsToTag() .map(BankTabItem::getItemIDs) .flatMap(Collection::stream) .filter(Objects::nonNull) // filter non-null just in case any Integer get in the list - .filter(id -> !flattenedList.contains(id)) - .forEach(flattenedList::add); - return flattenedList; + .filter(id -> !taggedItems.contains(id)) + .forEach(taggedItems::add); + return taggedItems; } public ArrayList getPluginBankTagItemsForSections(boolean onlyGetMissingItems)