From c12dc9b6c7024cf751e5e4c382fb0f557d746e52 Mon Sep 17 00:00:00 2001 From: Jeremy Plsek Date: Sun, 18 Mar 2018 11:32:33 -0400 Subject: [PATCH 1/5] http-api: add batch lookup to ItemClient --- .../runelite/http/api/item/ItemClient.java | 37 +++++++++++++++++++ 1 file changed, 37 insertions(+) diff --git a/http-api/src/main/java/net/runelite/http/api/item/ItemClient.java b/http-api/src/main/java/net/runelite/http/api/item/ItemClient.java index 5350d427da..39ca8e2e90 100644 --- a/http-api/src/main/java/net/runelite/http/api/item/ItemClient.java +++ b/http-api/src/main/java/net/runelite/http/api/item/ItemClient.java @@ -29,6 +29,7 @@ import java.awt.image.BufferedImage; import java.io.IOException; import java.io.InputStream; import java.io.InputStreamReader; +import java.util.Arrays; import javax.imageio.ImageIO; import net.runelite.http.api.RuneLiteAPI; import okhttp3.HttpUrl; @@ -72,6 +73,42 @@ public class ItemClient } } + public ItemPrice[] lookupItemPrice(Integer[] itemIds) throws IOException + { + HttpUrl.Builder urlBuilder = RuneLiteAPI.getApiBase().newBuilder() + .addPathSegment("item") + .addPathSegment("price"); + + for (int itemId : itemIds) + { + urlBuilder.addQueryParameter("id", String.valueOf(itemId)); + } + + HttpUrl url = urlBuilder.build(); + + logger.debug("Built URI: {}", url); + + Request request = new Request.Builder() + .url(url) + .build(); + + try (Response response = RuneLiteAPI.CLIENT.newCall(request).execute()) + { + if (!response.isSuccessful()) + { + logger.debug("Error looking up items {}: {}", Arrays.toString(itemIds), response.message()); + return null; + } + + InputStream in = response.body().byteStream(); + return RuneLiteAPI.GSON.fromJson(new InputStreamReader(in), ItemPrice[].class); + } + catch (JsonParseException ex) + { + throw new IOException(ex); + } + } + public BufferedImage getIcon(int itemId) throws IOException { HttpUrl url = RuneLiteAPI.getApiBase().newBuilder() From adc5fe2b26370389d6e09f4fa6df116dd7f26612 Mon Sep 17 00:00:00 2001 From: Adam Date: Sun, 18 Mar 2018 11:32:48 -0400 Subject: [PATCH 2/5] item manager: add batch item price lookup --- .../net/runelite/client/game/ItemManager.java | 86 +++++++++++++++++-- 1 file changed, 77 insertions(+), 9 deletions(-) diff --git a/runelite-client/src/main/java/net/runelite/client/game/ItemManager.java b/runelite-client/src/main/java/net/runelite/client/game/ItemManager.java index 199e73f473..edffbf23b2 100644 --- a/runelite-client/src/main/java/net/runelite/client/game/ItemManager.java +++ b/runelite-client/src/main/java/net/runelite/client/game/ItemManager.java @@ -29,6 +29,10 @@ import com.google.common.cache.CacheLoader; import com.google.common.cache.LoadingCache; import java.awt.image.BufferedImage; import java.io.IOException; +import java.util.ArrayList; +import java.util.Arrays; +import java.util.List; +import java.util.concurrent.CompletableFuture; import java.util.concurrent.ExecutionException; import java.util.concurrent.ScheduledExecutorService; import java.util.concurrent.TimeUnit; @@ -36,6 +40,7 @@ import javax.annotation.Nullable; import javax.inject.Inject; import javax.inject.Singleton; import lombok.Value; +import lombok.extern.slf4j.Slf4j; import net.runelite.api.Client; import static net.runelite.api.Constants.CLIENT_DEFAULT_ZOOM; import net.runelite.api.ItemComposition; @@ -45,6 +50,7 @@ import net.runelite.http.api.item.ItemPrice; import net.runelite.http.api.item.SearchResult; @Singleton +@Slf4j public class ItemManager { @Value @@ -58,17 +64,19 @@ public class ItemManager /** * not yet looked up */ - static final ItemPrice EMPTY = new ItemPrice(); + /** * has no price */ static final ItemPrice NONE = new ItemPrice(); private final Client client; + private final ScheduledExecutorService scheduledExecutorService; + private final ItemClient itemClient = new ItemClient(); private final LoadingCache itemSearches; - private final LoadingCache itemPrices; + private final LoadingCache itemPriceCache; private final LoadingCache itemImages; private final LoadingCache itemCompositions; @@ -76,8 +84,10 @@ public class ItemManager public ItemManager(@Nullable Client client, ScheduledExecutorService executor) { this.client = client; - itemPrices = CacheBuilder.newBuilder() - .maximumSize(512L) + this.scheduledExecutorService = executor; + + itemPriceCache = CacheBuilder.newBuilder() + .maximumSize(1024L) .expireAfterAccess(1, TimeUnit.HOURS) .build(new ItemPriceLoader(executor, itemClient)); @@ -126,16 +136,74 @@ public class ItemManager */ public ItemPrice getItemPriceAsync(int itemId) { - ItemPrice itemPrice = itemPrices.getIfPresent(itemId); + ItemPrice itemPrice = itemPriceCache.getIfPresent(itemId); if (itemPrice != null && itemPrice != EMPTY) { return itemPrice == NONE ? null : itemPrice; } - itemPrices.refresh(itemId); + itemPriceCache.refresh(itemId); return null; } + /** + * Look up bulk item prices asynchronously + * + * @param itemIds array of item Ids + * @return a future called with the looked up prices + */ + public CompletableFuture getItemPriceBatch(List itemIds) + { + final List lookup = new ArrayList<>(); + final List existing = new ArrayList<>(); + for (int itemId : itemIds) + { + ItemPrice itemPrice = itemPriceCache.getIfPresent(itemId); + if (itemPrice != null) + { + existing.add(itemPrice); + } + else + { + lookup.add(itemId); + } + } + // All cached? + if (lookup.isEmpty()) + { + return CompletableFuture.completedFuture(existing.toArray(new ItemPrice[existing.size()])); + } + + final CompletableFuture future = new CompletableFuture<>(); + scheduledExecutorService.execute(() -> + { + try + { + // Do a query for the items not in the cache + ItemPrice[] itemPrices = itemClient.lookupItemPrice(lookup.toArray(new Integer[lookup.size()])); + if (itemPrices != null) + { + for (int itemId : lookup) + { + itemPriceCache.put(itemId, NONE); + } + for (ItemPrice itemPrice : itemPrices) + { + itemPriceCache.put(itemPrice.getItem().getId(), itemPrice); + } + // Append these to the already cached items + Arrays.stream(itemPrices).forEach(existing::add); + } + future.complete(existing.toArray(new ItemPrice[existing.size()])); + } + catch (Exception ex) + { + future.completeExceptionally(ex); + } + }); + return future; + } + /** * Look up an item's price synchronously * @@ -145,7 +213,7 @@ public class ItemManager */ public ItemPrice getItemPrice(int itemId) throws IOException { - ItemPrice itemPrice = itemPrices.getIfPresent(itemId); + ItemPrice itemPrice = itemPriceCache.getIfPresent(itemId); if (itemPrice != null && itemPrice != EMPTY) { return itemPrice == NONE ? null : itemPrice; @@ -154,11 +222,11 @@ public class ItemManager itemPrice = itemClient.lookupItemPrice(itemId); if (itemPrice == null) { - itemPrices.put(itemId, NONE); + itemPriceCache.put(itemId, NONE); return null; } - itemPrices.put(itemId, itemPrice); + itemPriceCache.put(itemId, itemPrice); return itemPrice; } From 254c62bfabada58c26c0a3a39684213bbeb2e7af Mon Sep 17 00:00:00 2001 From: Jeremy Plsek Date: Sun, 18 Mar 2018 11:33:10 -0400 Subject: [PATCH 3/5] stack formatter: support larger stack sizes --- .../net/runelite/client/util/StackFormatter.java | 14 +++++++------- .../runelite/client/util/StackFormatterTest.java | 4 +++- 2 files changed, 10 insertions(+), 8 deletions(-) diff --git a/runelite-client/src/main/java/net/runelite/client/util/StackFormatter.java b/runelite-client/src/main/java/net/runelite/client/util/StackFormatter.java index f970187419..093055658e 100644 --- a/runelite-client/src/main/java/net/runelite/client/util/StackFormatter.java +++ b/runelite-client/src/main/java/net/runelite/client/util/StackFormatter.java @@ -58,12 +58,12 @@ public class StackFormatter * @return A condensed version, with commas, K, M or B * as needed to 3 significant figures. */ - public static String quantityToStackSize(int quantity) + public static String quantityToStackSize(long quantity) { if (quantity < 0) { - // Integer.MIN_VALUE = -1 * Integer.MIN_VALUE so we need to correct for it. - return "-" + quantityToStackSize(quantity == Integer.MIN_VALUE ? Integer.MAX_VALUE : -quantity); + // Long.MIN_VALUE = -1 * Long.MIN_VALUE so we need to correct for it. + return "-" + quantityToStackSize(quantity == Long.MIN_VALUE ? Long.MAX_VALUE : -quantity); } else if (quantity < 10_000) { @@ -71,14 +71,14 @@ public class StackFormatter } String suffix = SUFFIXES[0]; - int divideBy = 1; + long divideBy = 1; // determine correct suffix by iterating backward through the list // of suffixes until the suffix results in a value >= 1 for (int i = (SUFFIXES.length - 1); i >= 0; i--) { - divideBy = (int) Math.pow(10, i * 3); - if ((float) quantity / divideBy >= 1) + divideBy = (long) Math.pow(10, i * 3); + if ((double) quantity / divideBy >= 1) { suffix = SUFFIXES[i]; break; @@ -86,7 +86,7 @@ public class StackFormatter } // get locale formatted string - String formattedString = NUMBER_FORMATTER.format((float) quantity / divideBy); + String formattedString = NUMBER_FORMATTER.format((double) quantity / divideBy); // strip down any digits past the 4 first formattedString = (formattedString.length() > 4 ? formattedString.substring(0, 4) : formattedString); diff --git a/runelite-client/src/test/java/net/runelite/client/util/StackFormatterTest.java b/runelite-client/src/test/java/net/runelite/client/util/StackFormatterTest.java index 79be1cba7d..9c344a909f 100644 --- a/runelite-client/src/test/java/net/runelite/client/util/StackFormatterTest.java +++ b/runelite-client/src/test/java/net/runelite/client/util/StackFormatterTest.java @@ -61,12 +61,14 @@ public class StackFormatterTest assertEquals("100K", StackFormatter.quantityToStackSize(100_000)); assertEquals("10M", StackFormatter.quantityToStackSize(10_000_000)); assertEquals(NumberFormat.getNumberInstance().format(2.14) + "B", StackFormatter.quantityToStackSize(Integer.MAX_VALUE)); + assertEquals("100B", StackFormatter.quantityToStackSize(100_000_000_000L)); assertEquals("0", StackFormatter.quantityToStackSize(-0)); assertEquals("-400", StackFormatter.quantityToStackSize(-400)); assertEquals("-400K", StackFormatter.quantityToStackSize(-400_000)); assertEquals("-40M", StackFormatter.quantityToStackSize(-40_000_000)); assertEquals(NumberFormat.getNumberInstance().format(-2.14) + "B", StackFormatter.quantityToStackSize(Integer.MIN_VALUE)); + assertEquals("-400B", StackFormatter.quantityToStackSize(-400_000_000_000L)); } @Test @@ -103,4 +105,4 @@ public class StackFormatterTest { } } -} \ No newline at end of file +} From b55fba573fa722901674db7db29422923630818f Mon Sep 17 00:00:00 2001 From: Jeremy Plsek Date: Sun, 18 Mar 2018 11:33:31 -0400 Subject: [PATCH 4/5] bank item query: only search items in current tab --- .../src/main/java/net/runelite/api/queries/BankItemQuery.java | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/runelite-api/src/main/java/net/runelite/api/queries/BankItemQuery.java b/runelite-api/src/main/java/net/runelite/api/queries/BankItemQuery.java index 96ddf0f454..a4e11a3690 100644 --- a/runelite-api/src/main/java/net/runelite/api/queries/BankItemQuery.java +++ b/runelite-api/src/main/java/net/runelite/api/queries/BankItemQuery.java @@ -61,7 +61,7 @@ public class BankItemQuery extends WidgetItemQuery Widget[] children = bank.getDynamicChildren(); for (int i = 0; i < children.length; i++) { - if (children[i].getItemId() == ITEM_EMPTY) + if (children[i].getItemId() == ITEM_EMPTY || children[i].isHidden()) { continue; } From 53cf9565477a39e425981f7ce75b3a60f93c6a2e Mon Sep 17 00:00:00 2001 From: Jeremy Plsek Date: Sun, 18 Mar 2018 11:33:41 -0400 Subject: [PATCH 5/5] Add bank value plugin --- .../main/java/net/runelite/api/Setting.java | 2 + .../net/runelite/api/widgets/WidgetID.java | 2 + .../net/runelite/api/widgets/WidgetInfo.java | 2 + .../plugins/bankvalue/BankCalculation.java | 219 ++++++++++++++++++ .../client/plugins/bankvalue/BankTitle.java | 106 +++++++++ .../plugins/bankvalue/BankValueConfig.java | 55 +++++ .../plugins/bankvalue/BankValuePlugin.java | 80 +++++++ 7 files changed, 466 insertions(+) create mode 100644 runelite-client/src/main/java/net/runelite/client/plugins/bankvalue/BankCalculation.java create mode 100644 runelite-client/src/main/java/net/runelite/client/plugins/bankvalue/BankTitle.java create mode 100644 runelite-client/src/main/java/net/runelite/client/plugins/bankvalue/BankValueConfig.java create mode 100644 runelite-client/src/main/java/net/runelite/client/plugins/bankvalue/BankValuePlugin.java diff --git a/runelite-api/src/main/java/net/runelite/api/Setting.java b/runelite-api/src/main/java/net/runelite/api/Setting.java index f049024ac3..c7ef11e97b 100644 --- a/runelite-api/src/main/java/net/runelite/api/Setting.java +++ b/runelite-api/src/main/java/net/runelite/api/Setting.java @@ -33,6 +33,8 @@ public enum Setting { ATTACK_STYLE(43), + BANK_TAB(115), + SPECIAL_ATTACK_PERCENT(300), SPECIAL_ATTACK_ENABLED(301), diff --git a/runelite-api/src/main/java/net/runelite/api/widgets/WidgetID.java b/runelite-api/src/main/java/net/runelite/api/widgets/WidgetID.java index 3f51196bf0..9083d91c47 100644 --- a/runelite-api/src/main/java/net/runelite/api/widgets/WidgetID.java +++ b/runelite-api/src/main/java/net/runelite/api/widgets/WidgetID.java @@ -132,6 +132,8 @@ public class WidgetID { static final int ITEM_CONTAINER = 12; static final int INVENTORY_ITEM_CONTAINER = 3; + static final int BANK_TITLE_BAR = 4; + static final int BANK_ITEM_COUNT = 5; } static class GrandExchange diff --git a/runelite-api/src/main/java/net/runelite/api/widgets/WidgetInfo.java b/runelite-api/src/main/java/net/runelite/api/widgets/WidgetInfo.java index 5e84d3b431..5275ed3733 100644 --- a/runelite-api/src/main/java/net/runelite/api/widgets/WidgetInfo.java +++ b/runelite-api/src/main/java/net/runelite/api/widgets/WidgetInfo.java @@ -87,6 +87,8 @@ public enum WidgetInfo BANK_ITEM_CONTAINER(WidgetID.BANK_GROUP_ID, WidgetID.Bank.ITEM_CONTAINER), BANK_INVENTORY_ITEMS_CONTAINER(WidgetID.BANK_INVENTORY_GROUP_ID, WidgetID.Bank.INVENTORY_ITEM_CONTAINER), + BANK_TITLE_BAR(WidgetID.BANK_GROUP_ID, WidgetID.Bank.BANK_TITLE_BAR), + BANK_ITEM_COUNT(WidgetID.BANK_GROUP_ID, WidgetID.Bank.BANK_ITEM_COUNT), GRAND_EXCHANGE_INVENTORY_ITEMS_CONTAINER(WidgetID.GRAND_EXCHANGE_INVENTORY_GROUP_ID, WidgetID.GrandExchange.INVENTORY_ITEM_CONTAINER), diff --git a/runelite-client/src/main/java/net/runelite/client/plugins/bankvalue/BankCalculation.java b/runelite-client/src/main/java/net/runelite/client/plugins/bankvalue/BankCalculation.java new file mode 100644 index 0000000000..ad4f9744fe --- /dev/null +++ b/runelite-client/src/main/java/net/runelite/client/plugins/bankvalue/BankCalculation.java @@ -0,0 +1,219 @@ +/* + * Copyright (c) 2018, Jeremy Plsek + * 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 java.util.ArrayList; +import java.util.HashMap; +import java.util.List; +import java.util.Map; +import java.util.concurrent.CompletableFuture; +import javax.inject.Inject; +import lombok.Getter; +import lombok.extern.slf4j.Slf4j; +import net.runelite.api.Client; +import net.runelite.api.ItemComposition; +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.Widget; +import net.runelite.api.widgets.WidgetInfo; +import net.runelite.api.widgets.WidgetItem; +import net.runelite.client.game.ItemManager; +import net.runelite.http.api.item.ItemPrice; + +@Slf4j +class BankCalculation +{ + private static final float HIGH_ALCHEMY_CONSTANT = 0.6f; + + private final Client client; + private final BankValueConfig config; + private final ItemManager itemManager; + + // Used to avoid extra calculation if the bank has not changed + private int itemsHash; + + @Getter + private long gePrice; + + @Getter + private long haPrice; + + @Getter + private boolean finished; + + @Inject + BankCalculation(Client client, ItemManager itemManager, BankValueConfig config) + { + this.client = client; + this.itemManager = itemManager; + this.config = config; + } + + /** + * Calculate the bank based on the cache, price can be 0 if bank not active, or cache not set + */ + void calculate() + { + 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")) + { + return; + } + + WidgetItem[] widgetItems = new BankItemQuery().result(client); + + if (widgetItems.length == 0 || !isBankDifferent(widgetItems)) + { + return; + } + + log.debug("Calculating new bank value..."); + + gePrice = haPrice = 0; + finished = false; + + List itemCompositions = new ArrayList<>(); + Map itemMap = new HashMap<>(); + List itemIds = new ArrayList<>(); + + // Generate our lists (and do some quick price additions) + for (WidgetItem widgetItem : widgetItems) + { + if (widgetItem.getId() <= 0 || widgetItem.getQuantity() == 0) + { + continue; + } + + if (widgetItem.getId() == COINS_995) + { + gePrice += widgetItem.getQuantity(); + haPrice += widgetItem.getQuantity(); + continue; + } + + if (widgetItem.getId() == PLATINUM_TOKEN) + { + gePrice += widgetItem.getQuantity() * 1000; + haPrice += widgetItem.getQuantity() * 1000; + continue; + } + + ItemComposition itemComposition = itemManager.getItemComposition(widgetItem.getId()); + itemCompositions.add(itemComposition); + itemMap.put(widgetItem.getId(), widgetItem); + + if (config.showGE()) + { + itemIds.add(widgetItem.getId()); + } + } + + // Now do the calculations + if (config.showGE() && !itemIds.isEmpty()) + { + CompletableFuture future = itemManager.getItemPriceBatch(itemIds); + future.whenComplete((ItemPrice[] itemPrices, Throwable ex) -> + { + if (ex != null) + { + log.debug("Error looking up item prices", ex); + return; + } + + if (itemPrices == null) + { + log.debug("Error looking up item prices"); + return; + } + + log.debug("Price lookup is complete. {} prices.", itemPrices.length); + + try + { + for (ItemPrice itemPrice : itemPrices) + { + if (itemPrice.getItem() == null) + { + continue; // cached no price + } + + gePrice += itemPrice.getPrice() * itemMap.get(itemPrice.getItem().getId()).getQuantity(); + } + } + catch (Exception ex2) + { + log.warn("error calculating price", ex2); + } + finally + { + finished = true; + } + }); + } + else + { + finished = true; + } + + if (config.showHA()) + { + for (ItemComposition itemComposition : itemCompositions) + { + int price = itemComposition.getPrice(); + + if (price > 0) + { + haPrice += Math.round(price * HIGH_ALCHEMY_CONSTANT) * + itemMap.get(itemComposition.getId()).getQuantity(); + } + } + } + } + + private boolean isBankDifferent(WidgetItem[] widgetItems) + { + Map mapCheck = new HashMap<>(); + + for (WidgetItem widgetItem : widgetItems) + { + mapCheck.put(widgetItem.getId(), widgetItem.getQuantity()); + } + + int curHash = mapCheck.hashCode(); + + if (curHash != itemsHash) + { + itemsHash = curHash; + return true; + } + + return false; + } +} diff --git a/runelite-client/src/main/java/net/runelite/client/plugins/bankvalue/BankTitle.java b/runelite-client/src/main/java/net/runelite/client/plugins/bankvalue/BankTitle.java new file mode 100644 index 0000000000..48e509c508 --- /dev/null +++ b/runelite-client/src/main/java/net/runelite/client/plugins/bankvalue/BankTitle.java @@ -0,0 +1,106 @@ +/* + * Copyright (c) 2018, Jeremy Plsek + * 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: " + StackFormatter.quantityToStackSize(gePrice) + ")"; + } + + if (config.showHA() && haPrice != 0) + { + strCurrentTab += " (HA: " + StackFormatter.quantityToStackSize(haPrice) + ")"; + } + + log.debug("Setting bank title: {}", bankTitle + strCurrentTab); + widgetBankTitleBar.setText(bankTitle + strCurrentTab); + } +} diff --git a/runelite-client/src/main/java/net/runelite/client/plugins/bankvalue/BankValueConfig.java b/runelite-client/src/main/java/net/runelite/client/plugins/bankvalue/BankValueConfig.java new file mode 100644 index 0000000000..707dfccb7d --- /dev/null +++ b/runelite-client/src/main/java/net/runelite/client/plugins/bankvalue/BankValueConfig.java @@ -0,0 +1,55 @@ +/* + * Copyright (c) 2018, TheLonelyDev + * Copyright (c) 2018, Jeremy Plsek + * 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 net.runelite.client.config.Config; +import net.runelite.client.config.ConfigGroup; +import net.runelite.client.config.ConfigItem; + +@ConfigGroup( + keyName = "bankvalue", + name = "Bank Value", + description = "Shows the value of your bank and/or current tab") +public interface BankValueConfig extends Config +{ + @ConfigItem( + keyName = "showGE", + name = "Show Grand Exchange price", + description = "Show grand exchange price total (GE)") + default boolean showGE() + { + return true; + } + + @ConfigItem( + keyName = "showHA", + name = "Show high alchemy price", + description = "Show high alchemy price total (HA)") + default boolean showHA() + { + return false; + } +} diff --git a/runelite-client/src/main/java/net/runelite/client/plugins/bankvalue/BankValuePlugin.java b/runelite-client/src/main/java/net/runelite/client/plugins/bankvalue/BankValuePlugin.java new file mode 100644 index 0000000000..6f03df7133 --- /dev/null +++ b/runelite-client/src/main/java/net/runelite/client/plugins/bankvalue/BankValuePlugin.java @@ -0,0 +1,80 @@ +/* + * Copyright (c) 2018, TheLonelyDev + * Copyright (c) 2018, Jeremy Plsek + * 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.common.eventbus.Subscribe; +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.client.config.ConfigManager; +import net.runelite.client.plugins.Plugin; +import net.runelite.client.plugins.PluginDescriptor; + +@PluginDescriptor(name = "Bank Value") +public class BankValuePlugin extends Plugin +{ + @Inject + private Client client; + + @Inject + private BankCalculation bankCalculation; + + @Inject + private BankTitle bankTitle; + + @Provides + BankValueConfig getConfig(ConfigManager configManager) + { + return configManager.getConfig(BankValueConfig.class); + } + + @Override + protected void shutDown() + { + bankTitle.reset(); + } + + @Subscribe + public void onGameTick(GameTick event) + { + Widget widgetBankTitleBar = client.getWidget(WidgetInfo.BANK_TITLE_BAR); + + if (widgetBankTitleBar == null || widgetBankTitleBar.isHidden()) + { + return; + } + + bankTitle.save(); + bankCalculation.calculate(); + if (bankCalculation.isFinished()) + { + bankTitle.update(bankCalculation.getGePrice(), bankCalculation.getHaPrice()); + } + } +}