Add bank value plugin

This commit is contained in:
Jeremy Plsek
2018-03-18 11:33:41 -04:00
committed by Adam
parent b55fba573f
commit 53cf956547
7 changed files with 466 additions and 0 deletions

View File

@@ -33,6 +33,8 @@ public enum Setting
{
ATTACK_STYLE(43),
BANK_TAB(115),
SPECIAL_ATTACK_PERCENT(300),
SPECIAL_ATTACK_ENABLED(301),

View File

@@ -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

View File

@@ -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),

View File

@@ -0,0 +1,219 @@
/*
* 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 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<ItemComposition> itemCompositions = new ArrayList<>();
Map<Integer, WidgetItem> itemMap = new HashMap<>();
List<Integer> 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<ItemPrice[]> 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<Integer, Integer> 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;
}
}

View File

@@ -0,0 +1,106 @@
/*
* 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: " + StackFormatter.quantityToStackSize(gePrice) + ")";
}
if (config.showHA() && haPrice != 0)
{
strCurrentTab += " (HA: " + StackFormatter.quantityToStackSize(haPrice) + ")";
}
log.debug("Setting bank title: {}", bankTitle + strCurrentTab);
widgetBankTitleBar.setText(bankTitle + strCurrentTab);
}
}

View File

@@ -0,0 +1,55 @@
/*
* Copyright (c) 2018, TheLonelyDev <https://github.com/TheLonelyDev>
* 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 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;
}
}

View File

@@ -0,0 +1,80 @@
/*
* Copyright (c) 2018, TheLonelyDev <https://github.com/TheLonelyDev>
* 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.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());
}
}
}