bank value: use ItemContainer instead of WidgetItemQuery

This commit is contained in:
trimbe
2019-01-04 22:03:10 -05:00
committed by Adam
parent ecd0f7dce3
commit 60aa135fe5
3 changed files with 100 additions and 36 deletions

View File

@@ -24,30 +24,45 @@
*/
package net.runelite.client.plugins.bankvalue;
import com.google.common.collect.ImmutableList;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import javax.inject.Inject;
import lombok.Getter;
import lombok.extern.slf4j.Slf4j;
import net.runelite.api.Client;
import net.runelite.api.InventoryID;
import net.runelite.api.Item;
import net.runelite.api.ItemComposition;
import net.runelite.api.ItemContainer;
import net.runelite.api.ItemID;
import static net.runelite.api.ItemID.COINS_995;
import static net.runelite.api.ItemID.PLATINUM_TOKEN;
import net.runelite.api.queries.BankItemQuery;
import net.runelite.api.widgets.WidgetItem;
import net.runelite.api.Varbits;
import net.runelite.client.game.ItemManager;
import net.runelite.client.util.QueryRunner;
@Slf4j
class BankCalculation
{
private static final float HIGH_ALCHEMY_CONSTANT = 0.6f;
private static final ImmutableList<Varbits> TAB_VARBITS = ImmutableList.of(
Varbits.BANK_TAB_ONE_COUNT,
Varbits.BANK_TAB_TWO_COUNT,
Varbits.BANK_TAB_THREE_COUNT,
Varbits.BANK_TAB_FOUR_COUNT,
Varbits.BANK_TAB_FIVE_COUNT,
Varbits.BANK_TAB_SIX_COUNT,
Varbits.BANK_TAB_SEVEN_COUNT,
Varbits.BANK_TAB_EIGHT_COUNT,
Varbits.BANK_TAB_NINE_COUNT
);
private final QueryRunner queryRunner;
private final BankValueConfig config;
private final ItemManager itemManager;
private final Client client;
// Used to avoid extra calculation if the bank has not changed
private int itemsHash;
@@ -59,11 +74,11 @@ class BankCalculation
private long haPrice;
@Inject
BankCalculation(QueryRunner queryRunner, ItemManager itemManager, BankValueConfig config)
BankCalculation(ItemManager itemManager, BankValueConfig config, Client client)
{
this.queryRunner = queryRunner;
this.itemManager = itemManager;
this.config = config;
this.client = client;
}
/**
@@ -71,9 +86,30 @@ class BankCalculation
*/
void calculate()
{
WidgetItem[] widgetItems = queryRunner.runQuery(new BankItemQuery());
ItemContainer bankInventory = client.getItemContainer(InventoryID.BANK);
if (widgetItems.length == 0 || !isBankDifferent(widgetItems))
if (bankInventory == null)
{
return;
}
Item[] items = bankInventory.getItems();
int currentTab = client.getVar(Varbits.CURRENT_BANK_TAB);
if (currentTab > 0)
{
int startIndex = 0;
for (int i = currentTab - 1; i > 0; i--)
{
startIndex += client.getVar(TAB_VARBITS.get(i - 1));
}
int itemCount = client.getVar(TAB_VARBITS.get(currentTab - 1));
items = Arrays.copyOfRange(items, startIndex, startIndex + itemCount);
}
if (items.length == 0 || !isBankDifferent(items))
{
return;
}
@@ -85,34 +121,34 @@ class BankCalculation
List<Integer> itemIds = new ArrayList<>();
// Generate our lists (and do some quick price additions)
for (WidgetItem widgetItem : widgetItems)
for (Item item : items)
{
int quantity = widgetItem.getQuantity();
int quantity = item.getQuantity();
if (widgetItem.getId() <= 0 || quantity == 0)
if (item.getId() <= 0 || quantity == 0)
{
continue;
}
if (widgetItem.getId() == COINS_995)
if (item.getId() == COINS_995)
{
gePrice += quantity;
haPrice += quantity;
continue;
}
if (widgetItem.getId() == PLATINUM_TOKEN)
if (item.getId() == PLATINUM_TOKEN)
{
gePrice += quantity * 1000L;
haPrice += quantity * 1000L;
continue;
}
final ItemComposition itemComposition = itemManager.getItemComposition(widgetItem.getId());
final ItemComposition itemComposition = itemManager.getItemComposition(item.getId());
if (config.showGE())
{
itemIds.add(widgetItem.getId());
itemIds.add(item.getId());
}
if (config.showHA())
@@ -130,10 +166,10 @@ class BankCalculation
// Now do the calculations
if (config.showGE() && !itemIds.isEmpty())
{
for (WidgetItem widgetItem : widgetItems)
for (Item item : items)
{
int itemId = widgetItem.getId();
int quantity = widgetItem.getQuantity();
int itemId = item.getId();
int quantity = item.getQuantity();
if (itemId <= 0 || quantity == 0
|| itemId == ItemID.COINS_995 || itemId == ItemID.PLATINUM_TOKEN)
@@ -146,13 +182,13 @@ class BankCalculation
}
}
private boolean isBankDifferent(WidgetItem[] widgetItems)
private boolean isBankDifferent(Item[] items)
{
Map<Integer, Integer> mapCheck = new HashMap<>();
for (WidgetItem widgetItem : widgetItems)
for (Item item : items)
{
mapCheck.put(widgetItem.getId(), widgetItem.getQuantity());
mapCheck.put(item.getId(), item.getQuantity());
}
int curHash = mapCheck.hashCode();