-
-
Notifications
You must be signed in to change notification settings - Fork 418
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
base: master
Are you sure you want to change the base?
Bank improvements #1873
Changes from all commits
f6f66cd
bd502eb
17bbdac
43542e9
a8b5b31
16fee01
36f2e92
8b292a3
5bd1a71
316b6bf
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,247 @@ | ||
/* | ||
* Copyright (c) 2024, Adam <Adam@sigterm.info> | ||
* 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; | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe 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; | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Naming - as with the |
||
private Set<Integer> potionStoreVars; | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe 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); | ||
} | ||
} | ||
} | ||
} | ||
} |
There was a problem hiding this comment.
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