Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Bank improvements #1873

Open
wants to merge 10 commits into
base: master
Choose a base branch
from
7 changes: 7 additions & 0 deletions src/main/java/com/questhelper/QuestHelperPlugin.java
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,7 @@
import com.google.inject.Injector;
import com.google.inject.Provides;
import com.questhelper.bank.banktab.BankTabItems;
import com.questhelper.bank.banktab.PotionStorage;
import com.questhelper.managers.*;
import com.questhelper.panel.QuestHelperPanel;
import com.questhelper.questhelpers.QuestHelper;
Expand Down Expand Up @@ -72,6 +73,7 @@
import net.runelite.client.plugins.Plugin;
import net.runelite.client.plugins.PluginDescriptor;
import net.runelite.client.plugins.bank.BankSearch;
import net.runelite.client.plugins.banktags.tabs.Layout;
import net.runelite.client.ui.ClientToolbar;
import net.runelite.client.ui.NavigationButton;
import net.runelite.client.ui.components.colorpicker.ColorPickerManager;
Expand Down Expand Up @@ -154,6 +156,9 @@ public class QuestHelperPlugin extends Plugin
@Inject
PlayerStateManager playerStateManager;

@Inject
PotionStorage potionStorage;

@Inject
public SkillIconManager skillIconManager;

Expand All @@ -177,6 +182,7 @@ protected void startUp() throws IOException
{
questBankManager.startUp(injector, eventBus);
QuestContainerManager.getBankData().setSpecialMethodToObtainItems(() -> questBankManager.getBankItems().toArray(new Item[0]));
QuestContainerManager.getPotionData().setSpecialMethodToObtainItems(() -> potionStorage.getItems());
eventBus.register(worldMapAreaManager);

injector.injectMembers(playerStateManager);
Expand Down Expand Up @@ -283,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
Original file line number Diff line number Diff line change
Expand Up @@ -67,7 +67,7 @@ public BankTabItem(ItemRequirement item)
this.text = item.getName();
this.itemIDs = Collections.singletonList(item.getId());
this.details = item.getTooltip();
this.displayID = null;
this.displayID = -1;
this.itemRequirement = item;
}
}
247 changes: 247 additions & 0 deletions src/main/java/com/questhelper/bank/banktab/PotionStorage.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,247 @@
/*
* Copyright (c) 2024, Adam <Adam@sigterm.info>
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Add yourself too - looks like you've done enough to warrant it

* All rights reserved.
*
* Redistribution and use in source and binary forms, with or without
* modification, are permitted provided that the following conditions are met:
*
* 1. Redistributions of source code must retain the above copyright notice, this
* list of conditions and the following disclaimer.
* 2. Redistributions in binary form must reproduce the above copyright notice,
* this list of conditions and the following disclaimer in the documentation
* and/or other materials provided with the distribution.
*
* THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND
* ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
* WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
* DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR
* ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
* (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
* LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
* ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
* (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
* SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
*/
package com.questhelper.bank.banktab;

import java.util.*;
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.VarbitChanged;
import net.runelite.api.widgets.ComponentID;
import net.runelite.api.widgets.Widget;
import net.runelite.api.widgets.WidgetType;
import net.runelite.client.eventbus.Subscribe;
import net.runelite.client.game.ItemManager;
import net.runelite.client.plugins.bank.BankSearch;

class Potion
{
EnumComposition potionEnum;
int itemId;
int doses;
int withdrawDoses;
}

@Singleton
@RequiredArgsConstructor(onConstructor = @__(@Inject))
@Slf4j
public class PotionStorage
{
static final int TOTAL_POTIONS_VARBIT = 4286;
static final int VIAL_IDX = 514;
static final int COMPONENTS_PER_POTION = 5;

private final Client client;
private final ItemManager itemManager;
private final BankSearch bankSearch;

private Potion[] potions;
public boolean cachePotions;
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Naming/documentation

If I understand it correctly, it's a hint to the PotionStorage's onClientTick function that we should look for the potion storage widget & note down what types of potions are stored in it.

private boolean layout;
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Naming - as with the cachePotions, maybe an additional verb saying that "hey, this variable denotes that we'd like something to happen".

private Set<Integer> potionStoreVars;
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

List of potion ItemIDs(?), or varbits(?) that are interested to us?


@Setter
private QuestBankTabInterface questBankTabInterface;

@Subscribe
public void onClientTick(ClientTick event)
{
if (cachePotions)
{
log.debug("Rebuilding potions");
cachePotions = false;
rebuildPotions();

Widget w = client.getWidget(ComponentID.BANK_POTIONSTORE_CONTENT);
if (w != null && potionStoreVars == null)
{
// cache varps that the potion store rebuilds on
int[] trigger = w.getVarTransmitTrigger();
potionStoreVars = new HashSet<>();
Arrays.stream(trigger).forEach(potionStoreVars::add);
}

if (layout)
{
layout = false;
if (questBankTabInterface.isQuestTabActive())
{
bankSearch.layoutBank();
}
}
}
}

// Use varp change event instead of a widget change listener so that we can recache the potions prior to
// the cs2 vm running.
@Subscribe
public void onVarbitChanged(VarbitChanged varbitChanged)
{
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
}
}

private void rebuildPotions()
{
var potionStorePotions = client.getEnum(EnumID.POTIONSTORE_POTIONS);
var potionStoreUnfinishedPotions = client.getEnum(EnumID.POTIONSTORE_UNFINISHED_POTIONS);
potions = new Potion[potionStorePotions.size() + potionStoreUnfinishedPotions.size() + 1];
int potionsIdx = 0;
for (EnumComposition e : new EnumComposition[]{potionStorePotions, potionStoreUnfinishedPotions})
{
for (int potionEnumId : e.getIntVals())
{
var potionEnum = client.getEnum(potionEnumId);
client.runScript(ScriptID.POTIONSTORE_DOSES, potionEnumId);
int doses = client.getIntStack()[0];
client.runScript(ScriptID.POTIONSTORE_WITHDRAW_DOSES, potionEnumId);
int withdrawDoses = client.getIntStack()[0];

if (doses > 0 && withdrawDoses > 0)
{
Potion p = new Potion();
p.potionEnum = potionEnum;
p.itemId = potionEnum.getIntValue(withdrawDoses);
p.doses = doses;
p.withdrawDoses = withdrawDoses;
potions[potionsIdx] = p;
}

++potionsIdx;
}
}

// Add vial
Potion p = new Potion();
p.potionEnum = null;
p.itemId = ItemID.VIAL;
p.doses = client.getVarpValue(4286);
p.withdrawDoses = 0;
potions[potions.length - 1] = p;

QuestContainerManager.getPotionData().update(client.getTickCount(), getItems());
}

public Item[] getItems()
{
if (potions == null)
{
return new Item[0];
}

List<Item> items = new ArrayList<>();

for (Potion potion : potions)
{
if (potion == null) continue;
var potionEnum = potion.potionEnum;
if (potionEnum != null)
{
int potionItemId = potionEnum.getIntValue(potion.withdrawDoses);
int quantity = potion.doses / potion.withdrawDoses;
items.add(new Item(potionItemId, quantity));
}
else
{
items.add(new Item(potion.itemId, potion.doses));
}
}

return items.toArray(new Item[0]);
}

int count(int itemId)
{
if (potions == null)
{
return 0;
}

for (Potion potion : potions)
{
if (potion != null && potion.itemId == itemId)
{
if (potion.withdrawDoses != 0)
{
return potion.doses / potion.withdrawDoses;
}

return potion.doses;
}
}
return 0;
}

int find(int itemId)
{
if (potions == null)
{
return -1;
}

if (itemId == ItemID.VIAL)
{
return VIAL_IDX;
}

int potionIdx = 0;
for (Potion potion : potions)
{
++potionIdx;
if (potion != null && potion.itemId == itemId)
{
return potionIdx - 1;
}
}
return -1;
}

public void prepareWidgets()
{
// if the potion store hasn't been opened yet, the client components won't have been made yet.
// they need to exist for the click to work correctly.
Widget potStoreContent = client.getWidget(ComponentID.BANK_POTIONSTORE_CONTENT);
if (potStoreContent.getChildren() == null)
{
int childIdx = 0;
for (int i = 0; i < potions.length; ++i) // NOPMD: ForLoopCanBeForeach
{
for (int j = 0; j < COMPONENTS_PER_POTION; ++j)
{
potStoreContent.createChild(childIdx++, WidgetType.GRAPHIC);
}
}
}
}
}
Loading