forked from Zoinkwiz/quest-helper
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
* feat: Added Potion Storage as container * feat: Filter non helper items on bank filter * feat: Potion Storage works with sidebar * feat: Potion storage works for bank Also adds performance improvements by making all items rather than finding items in the bank, like the core tags Layouts now do. Additionally, only renders the quest items and not the entire bank to further speed things up. * fix: Added more dynamic bank trimming * fix: Add null check for quest bank trimming * fix: Remove duplicate code in QuestHelperBankTagService * fix: Use right bank item name and details * fix: Apply item finding to all items for bank * feat: Added vial support for potion storage * fix: Set Interface to null on potionStorage unregistering * feat: Update potion storage caching This now only considers 1. The script running to update the UI of the potion storage, and 2. The vial quantity changing. This hopefully should catch all scenarios for knowing to change the known state. * fix: Ensure potion storage properly closed when entering quest bank tab * fix: Count both bank and potion storage for quest tab Items are removed first from the bank then potion storage, so worth having both shown in total quantity. * fix: checkWithBank -> checkWithAllContainers * fix: Removed redundant function for item check * Updated copyright on PotionStorage * feat: Remove item filtering for quest bank tab This was fairly redundant to do with the new way of simply replacing widgets rather than finding matching widgets, and messed with the scrollbar.
- Loading branch information
1 parent
1c8f097
commit aad0801
Showing
12 changed files
with
668 additions
and
362 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
262 changes: 262 additions & 0 deletions
262
src/main/java/com/questhelper/bank/banktab/PotionStorage.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,262 @@ | ||
/* | ||
* Copyright (c) 2024, Adam <[email protected]> | ||
* Copyright (c) 2025, Zoinkwiz <https://github.com/Zoinkwiz> | ||
* 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.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 BankSearch bankSearch; | ||
|
||
private Potion[] potions; | ||
|
||
/* represents that something has occurred which means we should update the values of potions **/ | ||
public boolean updateCachedPotions; | ||
|
||
private boolean updateBankLayout; | ||
|
||
@Setter | ||
private QuestBankTabInterface questBankTabInterface; | ||
|
||
@Subscribe | ||
public void onClientTick(ClientTick event) | ||
{ | ||
if (updateCachedPotions) | ||
{ | ||
log.debug("Rebuilding potions"); | ||
updateCachedPotions = false; | ||
rebuildPotions(); | ||
|
||
if (updateBankLayout) | ||
{ | ||
updateBankLayout = 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()) | ||
{ | ||
updateCachedPotions = true; | ||
updateBankLayout = 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; | ||
|
||
Item[] newTrackedPotions = getItems(); | ||
|
||
Map<Integer, Integer> newPotionsAsMap = itemArrayToMap(newTrackedPotions); | ||
Map<Integer, Integer> oldPotionsAsMap = itemArrayToMap(QuestContainerManager.getPotionData().getItems()); | ||
if (!oldPotionsAsMap.equals(newPotionsAsMap)) | ||
{ | ||
updateBankLayout = true; | ||
} | ||
|
||
QuestContainerManager.getPotionData().update(client.getTickCount(), newTrackedPotions); | ||
} | ||
|
||
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); | ||
} | ||
} | ||
} | ||
} | ||
|
||
private Map<Integer, Integer> itemArrayToMap(Item[] items) | ||
{ | ||
if (items == null) return new HashMap<>(); | ||
Map<Integer, Integer> quantityMap = new HashMap<>(); | ||
for (Item item : items) | ||
{ | ||
quantityMap.put( | ||
item.getId(), | ||
quantityMap.getOrDefault(item.getId(), 0) + item.getQuantity() | ||
); | ||
} | ||
return quantityMap; | ||
} | ||
} |
Oops, something went wrong.