bank plugin: refactor to allow pricing multiple containers

Co-authored-by: TheStonedTurtle <mgoodwin559@gmail.com>
This commit is contained in:
Adam
2019-07-16 13:54:16 -04:00
committed by Adam
parent 4ac7c33f47
commit 2b2983242f
6 changed files with 232 additions and 236 deletions

View File

@@ -1,204 +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.bank;
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.Constants;
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.Varbits;
import net.runelite.client.game.ItemManager;
@Slf4j
class BankCalculation
{
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 BankConfig config;
private final ItemManager itemManager;
private final Client client;
// Used to avoid extra calculation if the bank has not changed
private int itemsHash;
@Getter
private long gePrice;
@Getter
private long haPrice;
@Inject
BankCalculation(ItemManager itemManager, BankConfig config, Client client)
{
this.itemManager = itemManager;
this.config = config;
this.client = client;
}
/**
* Calculate the bank based on the cache, price can be 0 if bank not active, or cache not set
*/
void calculate()
{
ItemContainer bankInventory = client.getItemContainer(InventoryID.BANK);
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;
}
log.debug("Calculating new bank value...");
gePrice = haPrice = 0;
List<Integer> itemIds = new ArrayList<>();
// Generate our lists (and do some quick price additions)
for (Item item : items)
{
int quantity = item.getQuantity();
if (item.getId() <= 0 || quantity == 0)
{
continue;
}
if (item.getId() == COINS_995)
{
gePrice += quantity;
haPrice += quantity;
continue;
}
if (item.getId() == PLATINUM_TOKEN)
{
gePrice += quantity * 1000L;
haPrice += quantity * 1000L;
continue;
}
final ItemComposition itemComposition = itemManager.getItemComposition(item.getId());
if (config.showGE())
{
itemIds.add(item.getId());
}
if (config.showHA())
{
int price = itemComposition.getPrice();
if (price > 0)
{
haPrice += (long) Math.round(price * Constants.HIGH_ALCHEMY_MULTIPLIER) *
(long) quantity;
}
}
}
// Now do the calculations
if (config.showGE() && !itemIds.isEmpty())
{
for (Item item : items)
{
int itemId = item.getId();
int quantity = item.getQuantity();
if (itemId <= 0 || quantity == 0
|| itemId == ItemID.COINS_995 || itemId == ItemID.PLATINUM_TOKEN)
{
continue;
}
gePrice += (long) itemManager.getItemPrice(itemId) * quantity;
}
}
}
private boolean isBankDifferent(Item[] items)
{
Map<Integer, Integer> mapCheck = new HashMap<>();
for (Item item : items)
{
mapCheck.put(item.getId(), item.getQuantity());
}
int curHash = mapCheck.hashCode();
if (curHash != itemsHash)
{
itemsHash = curHash;
return true;
}
return false;
}
}

View File

@@ -97,4 +97,15 @@ public interface BankConfig extends Config
{
return false;
}
@ConfigItem(
keyName = "seedVaultValue",
name = "Show seed vault value",
description = "Adds the total value of all seeds inside the seed vault to the title",
position = 7
)
default boolean seedVaultValue()
{
return true;
}
}

View File

@@ -26,10 +26,17 @@
*/
package net.runelite.client.plugins.bank;
import com.google.common.collect.ImmutableList;
import com.google.inject.Provides;
import java.util.Arrays;
import java.util.List;
import javax.inject.Inject;
import net.runelite.api.Client;
import net.runelite.api.InventoryID;
import net.runelite.api.Item;
import net.runelite.api.ItemContainer;
import net.runelite.api.MenuEntry;
import net.runelite.api.Varbits;
import net.runelite.api.events.MenuEntryAdded;
import net.runelite.api.events.MenuShouldLeftClick;
import net.runelite.api.events.ScriptCallbackEvent;
@@ -48,6 +55,18 @@ import net.runelite.client.util.StackFormatter;
)
public class BankPlugin extends Plugin
{
private static final List<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 static final String DEPOSIT_WORN = "Deposit worn items";
private static final String DEPOSIT_INVENTORY = "Deposit inventory";
private static final String DEPOSIT_LOOT = "Deposit loot";
@@ -58,15 +77,15 @@ public class BankPlugin extends Plugin
@Inject
private ClientThread clientThread;
@Inject
private BankCalculation bankCalculation;
@Inject
private BankConfig config;
@Inject
private BankSearch bankSearch;
@Inject
private ContainerCalculation bankCalculation;
private boolean forceRightClickFlag;
@Provides
@@ -123,11 +142,26 @@ public class BankPlugin extends Plugin
return;
}
String strCurrentTab = "";
bankCalculation.calculate();
long gePrice = bankCalculation.getGePrice();
long haPrice = bankCalculation.getHaPrice();
final ContainerPrices prices = bankCalculation.calculate(getBankTabItems());
if (prices == null)
{
return;
}
final String strCurrentTab = createValueText(prices);
String[] stringStack = client.getStringStack();
int stringStackSize = client.getStringStackSize();
stringStack[stringStackSize - 1] += strCurrentTab;
}
private String createValueText(final ContainerPrices prices)
{
final long gePrice = prices.getGePrice();
final long haPrice = prices.getHighAlchPrice();
String strCurrentTab = "";
if (config.showGE() && gePrice != 0)
{
strCurrentTab += " (";
@@ -166,9 +200,33 @@ public class BankPlugin extends Plugin
}
}
String[] stringStack = client.getStringStack();
int stringStackSize = client.getStringStackSize();
return strCurrentTab;
}
stringStack[stringStackSize - 1] += strCurrentTab;
private Item[] getBankTabItems()
{
final ItemContainer container = client.getItemContainer(InventoryID.BANK);
if (container == null)
{
return null;
}
final Item[] items = container.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));
return Arrays.copyOfRange(items, startIndex, startIndex + itemCount);
}
return items;
}
}

View File

@@ -0,0 +1,111 @@
/*
* Copyright (c) 2019, TheStonedTurtle <https://github.com/TheStonedTurtle>
* Copyright (c) 2019, 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 net.runelite.client.plugins.bank;
import java.util.HashMap;
import java.util.Map;
import javax.annotation.Nullable;
import javax.inject.Inject;
import net.runelite.api.Constants;
import net.runelite.api.Item;
import net.runelite.api.ItemID;
import net.runelite.client.game.ItemManager;
class ContainerCalculation
{
private final ItemManager itemManager;
private int hash;
private ContainerPrices containerPrices;
@Inject
private ContainerCalculation(ItemManager itemManager)
{
this.itemManager = itemManager;
}
@Nullable
ContainerPrices calculate(Item[] items)
{
// Returns last calculation if inventory hasn't changed
final int newHash = hashItems(items);
if (containerPrices != null && hash == newHash)
{
return containerPrices;
}
hash = newHash;
long ge = 0;
long alch = 0;
for (final Item item : items)
{
final int qty = item.getQuantity();
final int id = item.getId();
if (id <= 0 || qty == 0)
{
continue;
}
switch (id)
{
case ItemID.COINS_995:
ge += qty;
alch += qty;
break;
case ItemID.PLATINUM_TOKEN:
ge += qty * 1000L;
alch += qty * 1000L;
break;
default:
final long storePrice = itemManager.getItemComposition(id).getPrice();
final long alchPrice = (long) (storePrice * Constants.HIGH_ALCHEMY_MULTIPLIER);
alch += alchPrice * qty;
ge += itemManager.getItemPrice(id) * qty;
break;
}
}
ContainerPrices prices = new ContainerPrices(ge, alch);
containerPrices = prices;
return prices;
}
private int hashItems(final Item[] items)
{
final Map<Integer, Integer> mapCheck = new HashMap<>(items.length);
for (Item item : items)
{
mapCheck.put(item.getId(), item.getQuantity());
}
return mapCheck.hashCode();
}
}

View File

@@ -0,0 +1,34 @@
/*
* Copyright (c) 2019, TheStonedTurtle <https://github.com/TheStonedTurtle>
* 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.bank;
import lombok.Value;
@Value
class ContainerPrices
{
private long gePrice;
private long highAlchPrice;
}

View File

@@ -1,5 +1,5 @@
/*
* Copyright (c) 2018, Adam <Adam@sigterm.info>
* Copyright (c) 2019, Adam <Adam@sigterm.info>
* All rights reserved.
*
* Redistribution and use in source and binary forms, with or without
@@ -30,13 +30,12 @@ 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.client.game.ItemManager;
import static org.junit.Assert.*;
import static org.junit.Assert.assertNotNull;
import static org.junit.Assert.assertTrue;
import org.junit.Before;
import org.junit.Test;
import org.junit.runner.RunWith;
@@ -46,7 +45,7 @@ import static org.mockito.Mockito.when;
import org.mockito.runners.MockitoJUnitRunner;
@RunWith(MockitoJUnitRunner.class)
public class BankCalculationTest
public class ContainerCalculationTest
{
@Mock
@Bind
@@ -56,12 +55,8 @@ public class BankCalculationTest
@Bind
private ItemManager itemManager;
@Mock
@Bind
private BankConfig bankConfig;
@Inject
private BankCalculation bankCalculation;
private ContainerCalculation containerCalculation;
@Before
public void before()
@@ -72,9 +67,6 @@ public class BankCalculationTest
@Test
public void testCalculate()
{
when(bankConfig.showHA())
.thenReturn(true);
Item coins = mock(Item.class);
when(coins.getId())
.thenReturn(ItemID.COINS_995);
@@ -92,13 +84,6 @@ public class BankCalculationTest
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);
@@ -107,9 +92,10 @@ public class BankCalculationTest
when(itemManager.getItemComposition(ItemID.ABYSSAL_WHIP))
.thenReturn(whipComp);
bankCalculation.calculate();
final ContainerPrices prices = containerCalculation.calculate(items);
assertNotNull(prices);
long value = bankCalculation.getHaPrice();
long value = prices.getHighAlchPrice();
assertTrue(value > Integer.MAX_VALUE);
}
}