Merge pull request #6908 from trimbe/bank-value-itemcontainer

Bank Value: Use ItemContainer instead of BankItemQuery
This commit is contained in:
Adam
2019-01-05 19:56:56 -05:00
committed by GitHub
6 changed files with 152 additions and 184 deletions

View File

@@ -437,7 +437,20 @@ public enum Varbits
/**
* Spell cooldowns
*/
VENGEANCE_COOLDOWN(2451);
VENGEANCE_COOLDOWN(2451),
/**
* Amount of items in each bank tab
*/
BANK_TAB_ONE_COUNT(4171),
BANK_TAB_TWO_COUNT(4172),
BANK_TAB_THREE_COUNT(4173),
BANK_TAB_FOUR_COUNT(4174),
BANK_TAB_FIVE_COUNT(4175),
BANK_TAB_SIX_COUNT(4176),
BANK_TAB_SEVEN_COUNT(4177),
BANK_TAB_EIGHT_COUNT(4178),
BANK_TAB_NINE_COUNT(4179);
/**
* The raw varbit ID.

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();

View File

@@ -1,124 +0,0 @@
/*
* Copyright (c) 2018, Jeremy Plsek <https://github.com/jplsek>
* 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 net.runelite.client.plugins.bankvalue;
import com.google.inject.Inject;
import lombok.extern.slf4j.Slf4j;
import net.runelite.api.Client;
import net.runelite.api.widgets.Widget;
import net.runelite.api.widgets.WidgetInfo;
import net.runelite.client.util.StackFormatter;
@Slf4j
class BankTitle
{
private final Client client;
private final BankValueConfig config;
private String bankTitle;
@Inject
BankTitle(Client client, BankValueConfig config)
{
this.client = client;
this.config = config;
}
void reset()
{
Widget widgetBankTitleBar = client.getWidget(WidgetInfo.BANK_TITLE_BAR);
if (widgetBankTitleBar == null || widgetBankTitleBar.isHidden())
{
return;
}
widgetBankTitleBar.setText(bankTitle);
}
void save()
{
Widget widgetBankTitleBar = client.getWidget(WidgetInfo.BANK_TITLE_BAR);
// Only save if the title hasn't been modified
// Don't update on a search because rs seems to constantly update the title
if (widgetBankTitleBar == null ||
widgetBankTitleBar.isHidden() ||
widgetBankTitleBar.getText().contains("(") ||
widgetBankTitleBar.getText().contains("Showing"))
{
return;
}
bankTitle = widgetBankTitleBar.getText();
}
void update(long gePrice, long haPrice)
{
Widget widgetBankTitleBar = client.getWidget(WidgetInfo.BANK_TITLE_BAR);
// Don't update on a search because rs seems to constantly update the title
if (widgetBankTitleBar == null ||
widgetBankTitleBar.isHidden() ||
widgetBankTitleBar.getText().contains("Showing") ||
widgetBankTitleBar.getText().contains("("))
{
return;
}
String strCurrentTab = "";
if (config.showGE() && gePrice != 0)
{
strCurrentTab += " (EX: ";
if (config.showExact())
{
strCurrentTab += StackFormatter.formatNumber(gePrice) + ")";
}
else
{
strCurrentTab += StackFormatter.quantityToStackSize(gePrice) + ")";
}
}
if (config.showHA() && haPrice != 0)
{
strCurrentTab += " (HA: ";
if (config.showExact())
{
strCurrentTab += StackFormatter.formatNumber(haPrice) + ")";
}
else
{
strCurrentTab += StackFormatter.quantityToStackSize(haPrice) + ")";
}
}
log.debug("Setting bank title: {}", bankTitle + strCurrentTab);
widgetBankTitleBar.setText(bankTitle + strCurrentTab);
}
}

View File

@@ -28,14 +28,14 @@ package net.runelite.client.plugins.bankvalue;
import com.google.inject.Provides;
import javax.inject.Inject;
import net.runelite.api.Client;
import net.runelite.api.events.GameTick;
import net.runelite.api.widgets.Widget;
import net.runelite.api.widgets.WidgetInfo;
import net.runelite.api.events.ScriptCallbackEvent;
import net.runelite.client.callback.ClientThread;
import net.runelite.client.config.ConfigManager;
import net.runelite.client.eventbus.Subscribe;
import net.runelite.client.plugins.Plugin;
import net.runelite.client.plugins.PluginDescriptor;
import net.runelite.client.plugins.banktags.tabs.BankSearch;
import net.runelite.client.util.StackFormatter;
@PluginDescriptor(
name = "Bank Value",
@@ -54,7 +54,10 @@ public class BankValuePlugin extends Plugin
private BankCalculation bankCalculation;
@Inject
private BankTitle bankTitle;
private BankValueConfig config;
@Inject
private BankSearch bankSearch;
@Provides
BankValueConfig getConfig(ConfigManager configManager)
@@ -65,34 +68,53 @@ public class BankValuePlugin extends Plugin
@Override
protected void shutDown()
{
clientThread.invokeLater(bankTitle::reset);
clientThread.invokeLater(() -> bankSearch.reset(false));
}
@Subscribe
public void onGameTick(GameTick event)
public void onScriptCallbackEvent(ScriptCallbackEvent event)
{
Widget widgetBankTitleBar = client.getWidget(WidgetInfo.BANK_TITLE_BAR);
if (widgetBankTitleBar == null || widgetBankTitleBar.isHidden())
{
return;
}
bankTitle.save();
calculate(widgetBankTitleBar);
bankTitle.update(bankCalculation.getGePrice(), bankCalculation.getHaPrice());
}
private void calculate(Widget bankTitleBar)
{
// Don't update on a search because rs seems to constantly update the title
if (bankTitleBar == null ||
bankTitleBar.isHidden() ||
bankTitleBar.getText().contains("Showing"))
if (!event.getEventName().equals("setBankTitle"))
{
return;
}
String strCurrentTab = "";
bankCalculation.calculate();
long gePrice = bankCalculation.getGePrice();
long haPrice = bankCalculation.getHaPrice();
if (config.showGE() && gePrice != 0)
{
strCurrentTab += " (EX: ";
if (config.showExact())
{
strCurrentTab += StackFormatter.formatNumber(gePrice) + ")";
}
else
{
strCurrentTab += StackFormatter.quantityToStackSize(gePrice) + ")";
}
}
if (config.showHA() && haPrice != 0)
{
strCurrentTab += " (HA: ";
if (config.showExact())
{
strCurrentTab += StackFormatter.formatNumber(haPrice) + ")";
}
else
{
strCurrentTab += StackFormatter.quantityToStackSize(haPrice) + ")";
}
}
String[] stringStack = client.getStringStack();
int stringStackSize = client.getStringStackSize();
stringStack[stringStackSize - 1] += strCurrentTab;
}
}

View File

@@ -702,6 +702,8 @@ LABEL637:
jump LABEL641
LABEL638:
load_string "The Bank of Gielinor"
load_string "setBankTitle" ;
runelite_callback ;
iload 6
widget_put_text_widget
LABEL641:
@@ -853,6 +855,8 @@ LABEL765:
get_varbit 4150
get_enum_value
string_append 2
load_string "setBankTitle" ;
runelite_callback ;
iload 6
widget_put_text_widget
jump LABEL781
@@ -861,6 +865,8 @@ LABEL775:
get_varbit 4150
int_to_string
string_append 2
load_string "setBankTitle" ;
runelite_callback ;
iload 6
widget_put_text_widget
LABEL781:

View File

@@ -30,7 +30,10 @@ import com.google.inject.testing.fieldbinder.Bind;
import com.google.inject.testing.fieldbinder.BoundFieldModule;
import javax.inject.Inject;
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 net.runelite.api.queries.BankItemQuery;
import net.runelite.api.widgets.WidgetItem;
@@ -53,10 +56,6 @@ public class BankCalculationTest
@Bind
private Client client;
@Mock
@Bind
private QueryRunner queryRunner;
@Mock
@Bind
private ItemManager itemManager;
@@ -80,21 +79,37 @@ public class BankCalculationTest
when(bankValueConfig.showHA())
.thenReturn(true);
WidgetItem[] widgetItems = ImmutableList.of(
new WidgetItem(ItemID.COINS_995, Integer.MAX_VALUE, -1, null),
new WidgetItem(ItemID.ABYSSAL_WHIP, 1_000_000_000, -1, null)
).toArray(new WidgetItem[0]);
Item coins = mock(Item.class);
when(coins.getId())
.thenReturn(ItemID.COINS_995);
when(coins.getQuantity())
.thenReturn(Integer.MAX_VALUE);
when(queryRunner.runQuery(any(BankItemQuery.class)))
.thenReturn(widgetItems);
ItemComposition whip = mock(ItemComposition.class);
Item whip = mock(Item.class);
when(whip.getId())
.thenReturn(ItemID.ABYSSAL_WHIP);
when(whip.getPrice())
when(whip.getQuantity())
.thenReturn(1_000_000_000);
Item[] items = ImmutableList.of(
coins,
whip
).toArray(new Item[0]);
ItemContainer bankContainer = mock(ItemContainer.class);
when(bankContainer.getItems())
.thenReturn(items);
when(client.getItemContainer(InventoryID.BANK))
.thenReturn(bankContainer);
ItemComposition whipComp = mock(ItemComposition.class);
when(whipComp.getId())
.thenReturn(ItemID.ABYSSAL_WHIP);
when(whipComp.getPrice())
.thenReturn(7); // 7 * .6 = 4, 4 * 1m overflows
when(itemManager.getItemComposition(ItemID.ABYSSAL_WHIP))
.thenReturn(whip);
.thenReturn(whipComp);
bankCalculation.calculate();