From 6ccaf8876c3fcd97b27ad91653ce6949315f29ee Mon Sep 17 00:00:00 2001 From: Jordan Atwood Date: Wed, 24 Jun 2020 17:28:47 -0700 Subject: [PATCH] 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. --- .../client/plugins/bank/BankPlugin.java | 55 +++++++-- .../plugins/bank/ContainerCalculation.java | 111 ------------------ .../client/plugins/bank/BankPluginTest.java | 29 +++++ .../bank/ContainerCalculationTest.java | 93 --------------- 4 files changed, 76 insertions(+), 212 deletions(-) delete mode 100644 runelite-client/src/main/java/net/runelite/client/plugins/bank/ContainerCalculation.java delete mode 100644 runelite-client/src/test/java/net/runelite/client/plugins/bank/ContainerCalculationTest.java diff --git a/runelite-client/src/main/java/net/runelite/client/plugins/bank/BankPlugin.java b/runelite-client/src/main/java/net/runelite/client/plugins/bank/BankPlugin.java index 0153dbb5e2..fbbd834ed8 100644 --- a/runelite-client/src/main/java/net/runelite/client/plugins/bank/BankPlugin.java +++ b/runelite-client/src/main/java/net/runelite/client/plugins/bank/BankPlugin.java @@ -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 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); + } } diff --git a/runelite-client/src/main/java/net/runelite/client/plugins/bank/ContainerCalculation.java b/runelite-client/src/main/java/net/runelite/client/plugins/bank/ContainerCalculation.java deleted file mode 100644 index ca383bc81a..0000000000 --- a/runelite-client/src/main/java/net/runelite/client/plugins/bank/ContainerCalculation.java +++ /dev/null @@ -1,111 +0,0 @@ -/* - * Copyright (c) 2019, TheStonedTurtle - * Copyright (c) 2019, Adam - * 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 mapCheck = new HashMap<>(items.length); - for (Item item : items) - { - mapCheck.put(item.getId(), item.getQuantity()); - } - - return mapCheck.hashCode(); - } -} diff --git a/runelite-client/src/test/java/net/runelite/client/plugins/bank/BankPluginTest.java b/runelite-client/src/test/java/net/runelite/client/plugins/bank/BankPluginTest.java index bc9caff9da..03424d7d01 100644 --- a/runelite-client/src/test/java/net/runelite/client/plugins/bank/BankPluginTest.java +++ b/runelite-client/src/test/java/net/runelite/client/plugins/bank/BankPluginTest.java @@ -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); + } } \ No newline at end of file diff --git a/runelite-client/src/test/java/net/runelite/client/plugins/bank/ContainerCalculationTest.java b/runelite-client/src/test/java/net/runelite/client/plugins/bank/ContainerCalculationTest.java deleted file mode 100644 index 1f220f4b64..0000000000 --- a/runelite-client/src/test/java/net/runelite/client/plugins/bank/ContainerCalculationTest.java +++ /dev/null @@ -1,93 +0,0 @@ -/* - * Copyright (c) 2019, Adam - * 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); - } -}