Skip to content

Commit

Permalink
feat: Potion Storage works with sidebar
Browse files Browse the repository at this point in the history
  • Loading branch information
Zoinkwiz committed Nov 26, 2024
1 parent bd502eb commit 17bbdac
Show file tree
Hide file tree
Showing 3 changed files with 59 additions and 41 deletions.
1 change: 1 addition & 0 deletions src/main/java/com/questhelper/QuestHelperPlugin.java
Original file line number Diff line number Diff line change
Expand Up @@ -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();
Expand Down
41 changes: 7 additions & 34 deletions src/main/java/com/questhelper/bank/banktab/PotionStorage.java
Original file line number Diff line number Diff line change
Expand Up @@ -28,16 +28,15 @@
import javax.inject.Inject;
import javax.inject.Singleton;

import com.questhelper.managers.QuestContainerManager;
import lombok.RequiredArgsConstructor;
import lombok.Setter;
import lombok.extern.slf4j.Slf4j;
import net.runelite.api.*;
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;
Expand All @@ -57,15 +56,15 @@ 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;
private final ItemManager itemManager;
private final BankSearch bankSearch;

private Potion[] potions;
boolean cachePotions;
public boolean cachePotions;
private boolean layout;
private Set<Integer> potionStoreVars;

Expand All @@ -77,7 +76,7 @@ public void onClientTick(ClientTick event)
{
if (cachePotions)
{
// log.debug("Rebuilding potions");
log.debug("Rebuilding potions");
cachePotions = false;
rebuildPotions();

Expand Down Expand Up @@ -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);
Expand All @@ -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()
Expand All @@ -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);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -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;

Expand All @@ -48,28 +48,72 @@ public class QuestHelperBankTagService
private QuestHelperPlugin plugin;

@Inject
private QuestBank questBank;
private Client client;

ArrayList<Integer> taggedItems;

ArrayList<Integer> taggedItemsForBank;

int lastTickUpdated = 0;

int lastTickUpdatedForBank = 0;

public ArrayList<Integer> itemsToTagForBank()
{
if (client.getTickCount() <= lastTickUpdatedForBank)
{
return taggedItemsForBank;
}

lastTickUpdatedForBank = client.getTickCount();

ArrayList<BankTabItems> 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<Integer> itemsToTag()
{
if (client.getTickCount() <= lastTickUpdated)
{
return taggedItems;
}

lastTickUpdated = client.getTickCount();

ArrayList<BankTabItems> sortedItems = getPluginBankTagItemsForSections(true);

if (sortedItems == null)
{
return null;
}

ArrayList<Integer> flattenedList = new ArrayList<>();
taggedItems = 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 -> !flattenedList.contains(id))
.forEach(flattenedList::add);
return flattenedList;
.filter(id -> !taggedItems.contains(id))
.forEach(taggedItems::add);
return taggedItems;
}

public ArrayList<BankTabItems> getPluginBankTagItemsForSections(boolean onlyGetMissingItems)
Expand Down

0 comments on commit 17bbdac

Please sign in to comment.