bank plugin: Remove ContainerCalculation class
This commit removes the hashing feature from container calculation which alone would make obsolete the ContainerCalculation class, so it additionally inlines the `calculate()` method into the plugin class. Additionally, this commit adds null handling for the items parameter of `calculate()` to prevent NPEs.
This commit is contained in:
@@ -36,9 +36,11 @@ import java.util.Arrays;
|
||||
import java.util.List;
|
||||
import java.util.regex.Matcher;
|
||||
import java.util.regex.Pattern;
|
||||
import javax.annotation.Nullable;
|
||||
import javax.inject.Inject;
|
||||
import lombok.extern.slf4j.Slf4j;
|
||||
import net.runelite.api.Client;
|
||||
import net.runelite.api.Constants;
|
||||
import static net.runelite.api.Constants.HIGH_ALCHEMY_MULTIPLIER;
|
||||
import net.runelite.api.InventoryID;
|
||||
import net.runelite.api.Item;
|
||||
@@ -115,12 +117,6 @@ public class BankPlugin extends Plugin
|
||||
@Inject
|
||||
private BankSearch bankSearch;
|
||||
|
||||
@Inject
|
||||
private ContainerCalculation bankCalculation;
|
||||
|
||||
@Inject
|
||||
private ContainerCalculation seedVaultCalculation;
|
||||
|
||||
private boolean forceRightClickFlag;
|
||||
private Multiset<Integer> itemQuantities; // bank item quantities for bank value search
|
||||
private String searchString;
|
||||
@@ -184,7 +180,7 @@ public class BankPlugin extends Plugin
|
||||
switch (event.getEventName())
|
||||
{
|
||||
case "setBankTitle":
|
||||
final ContainerPrices prices = bankCalculation.calculate(getBankTabItems());
|
||||
final ContainerPrices prices = calculate(getBankTabItems());
|
||||
if (prices == null)
|
||||
{
|
||||
return;
|
||||
@@ -375,7 +371,7 @@ public class BankPlugin extends Plugin
|
||||
return;
|
||||
}
|
||||
|
||||
final ContainerPrices prices = seedVaultCalculation.calculate(getSeedVaultItems());
|
||||
final ContainerPrices prices = calculate(getSeedVaultItems());
|
||||
if (prices == null)
|
||||
{
|
||||
return;
|
||||
@@ -493,4 +489,47 @@ public class BankPlugin extends Plugin
|
||||
}
|
||||
return set;
|
||||
}
|
||||
|
||||
@Nullable
|
||||
ContainerPrices calculate(@Nullable Item[] items)
|
||||
{
|
||||
if (items == null)
|
||||
{
|
||||
return null;
|
||||
}
|
||||
|
||||
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 += (long) itemManager.getItemPrice(id) * qty;
|
||||
break;
|
||||
}
|
||||
}
|
||||
|
||||
return new ContainerPrices(ge, alch);
|
||||
}
|
||||
}
|
||||
|
||||
@@ -1,111 +0,0 @@
|
||||
/*
|
||||
* 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 += (long) 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();
|
||||
}
|
||||
}
|
||||
@@ -25,6 +25,7 @@
|
||||
*/
|
||||
package net.runelite.client.plugins.bank;
|
||||
|
||||
import com.google.common.collect.ImmutableList;
|
||||
import com.google.inject.Guice;
|
||||
import com.google.inject.testing.fieldbinder.Bind;
|
||||
import com.google.inject.testing.fieldbinder.BoundFieldModule;
|
||||
@@ -37,6 +38,7 @@ import net.runelite.api.ItemContainer;
|
||||
import net.runelite.api.ItemID;
|
||||
import net.runelite.client.game.ItemManager;
|
||||
import static org.junit.Assert.assertFalse;
|
||||
import static org.junit.Assert.assertNotNull;
|
||||
import static org.junit.Assert.assertTrue;
|
||||
import org.junit.Before;
|
||||
import org.junit.Test;
|
||||
@@ -105,4 +107,31 @@ public class BankPluginTest
|
||||
|
||||
assertFalse(bankPlugin.valueSearch(itemId, "1000k"));
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testCalculate()
|
||||
{
|
||||
Item coins = new Item(ItemID.COINS_995, Integer.MAX_VALUE);
|
||||
|
||||
Item whip = new Item(ItemID.ABYSSAL_WHIP, 1_000_000_000);
|
||||
|
||||
Item[] items = ImmutableList.of(
|
||||
coins,
|
||||
whip
|
||||
).toArray(new Item[0]);
|
||||
|
||||
ItemComposition whipComp = mock(ItemComposition.class);
|
||||
when(whipComp.getPrice())
|
||||
.thenReturn(7); // 7 * .6 = 4, 4 * 1m overflows
|
||||
when(itemManager.getItemComposition(ItemID.ABYSSAL_WHIP))
|
||||
.thenReturn(whipComp);
|
||||
when(itemManager.getItemPrice(ItemID.ABYSSAL_WHIP))
|
||||
.thenReturn(3); // 1b * 3 overflows
|
||||
|
||||
final ContainerPrices prices = bankPlugin.calculate(items);
|
||||
assertNotNull(prices);
|
||||
|
||||
assertTrue(prices.getHighAlchPrice() > Integer.MAX_VALUE);
|
||||
assertTrue(prices.getGePrice() > Integer.MAX_VALUE);
|
||||
}
|
||||
}
|
||||
@@ -1,93 +0,0 @@
|
||||
/*
|
||||
* 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 com.google.common.collect.ImmutableList;
|
||||
import com.google.inject.Guice;
|
||||
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.Item;
|
||||
import net.runelite.api.ItemComposition;
|
||||
import net.runelite.api.ItemID;
|
||||
import net.runelite.client.game.ItemManager;
|
||||
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;
|
||||
import org.mockito.Mock;
|
||||
import static org.mockito.Mockito.mock;
|
||||
import static org.mockito.Mockito.when;
|
||||
import org.mockito.junit.MockitoJUnitRunner;
|
||||
|
||||
@RunWith(MockitoJUnitRunner.class)
|
||||
public class ContainerCalculationTest
|
||||
{
|
||||
@Mock
|
||||
@Bind
|
||||
private Client client;
|
||||
|
||||
@Mock
|
||||
@Bind
|
||||
private ItemManager itemManager;
|
||||
|
||||
@Inject
|
||||
private ContainerCalculation containerCalculation;
|
||||
|
||||
@Before
|
||||
public void before()
|
||||
{
|
||||
Guice.createInjector(BoundFieldModule.of(this)).injectMembers(this);
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testCalculate()
|
||||
{
|
||||
Item coins = new Item(ItemID.COINS_995, Integer.MAX_VALUE);
|
||||
|
||||
Item whip = new Item(ItemID.ABYSSAL_WHIP, 1_000_000_000);
|
||||
|
||||
Item[] items = ImmutableList.of(
|
||||
coins,
|
||||
whip
|
||||
).toArray(new Item[0]);
|
||||
|
||||
ItemComposition whipComp = mock(ItemComposition.class);
|
||||
when(whipComp.getPrice())
|
||||
.thenReturn(7); // 7 * .6 = 4, 4 * 1m overflows
|
||||
when(itemManager.getItemComposition(ItemID.ABYSSAL_WHIP))
|
||||
.thenReturn(whipComp);
|
||||
when(itemManager.getItemPrice(ItemID.ABYSSAL_WHIP))
|
||||
.thenReturn(3); // 1b * 3 overflows
|
||||
|
||||
final ContainerPrices prices = containerCalculation.calculate(items);
|
||||
assertNotNull(prices);
|
||||
|
||||
assertTrue(prices.getHighAlchPrice() > Integer.MAX_VALUE);
|
||||
assertTrue(prices.getGePrice() > Integer.MAX_VALUE);
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user