grandexchange: Shows the quantity you can buy on the GE WIP (#1042)

* Add measures to ensure positions of strings when togg:ling configItem

change method of gathering price per item

change math.round to math.floor, errors when certain numbers are rounded fix

* implement geBuilt script event, clear values when item isn't visible

* Update

* Update pos number

* Update
This commit is contained in:
James
2019-07-19 02:13:45 -07:00
committed by sdburns1998
parent d08d80c468
commit 3675061f21
3 changed files with 87 additions and 19 deletions

View File

@@ -49,6 +49,11 @@ public enum Varbits
*/
CHAT_SCROLLBAR_ON_LEFT(6374),
/**
* Grand Exchange
*/
GRAND_EXCHANGE_PRICE_PER_ITEM(4398),
/**
* Runepouch
*/

View File

@@ -96,4 +96,15 @@ public interface GrandExchangeConfig extends Config
{
return false;
}
@ConfigItem(
position = 7,
keyName = "enableAfford",
name = "Enable Afford quantity on GE",
description = "Shows the quantity you can buy on the GE"
)
default boolean enableAfford()
{
return true;
}
}

View File

@@ -48,14 +48,18 @@ import net.runelite.api.Client;
import net.runelite.api.GameState;
import net.runelite.api.GrandExchangeOffer;
import net.runelite.api.GrandExchangeOfferState;
import net.runelite.api.InventoryID;
import net.runelite.api.Item;
import net.runelite.api.ItemContainer;
import net.runelite.api.ItemDefinition;
import static net.runelite.api.ItemID.COINS_995;
import net.runelite.api.MenuAction;
import net.runelite.api.MenuEntry;
import net.runelite.api.Varbits;
import net.runelite.api.events.ChatMessage;
import net.runelite.api.events.ConfigChanged;
import net.runelite.api.events.FocusChanged;
import net.runelite.api.events.GameStateChanged;
import net.runelite.api.events.GameTick;
import net.runelite.api.events.GrandExchangeOfferChanged;
import net.runelite.api.events.MenuEntryAdded;
import net.runelite.api.events.ScriptCallbackEvent;
@@ -99,7 +103,8 @@ public class GrandExchangePlugin extends Plugin
private static final OSBGrandExchangeClient CLIENT = new OSBGrandExchangeClient();
private static final String OSB_GE_TEXT = "<br>OSBuddy Actively traded price: ";
private static final String BUY_LIMIT_GE_TEXT = "<br>Buy limit: ";
private static final String BUY_LIMIT_GE_TEXT = "Buy limit: ";
private static final String AFFORD_GE_TEXT = "<br>Afford: ";
private static final Gson GSON = new Gson();
private static final TypeToken<Map<Integer, Integer>> BUY_LIMIT_TOKEN = new TypeToken<Map<Integer, Integer>>()
{
@@ -162,6 +167,13 @@ public class GrandExchangePlugin extends Plugin
private GrandExchangeClient grandExchangeClient;
private int coins = 0;
private int lastAmount = -1;
private int lastItem = -1;
private int osbItem = -1;
private String osbText = "";
private SavedOffer getOffer(int slot)
{
String offer = configManager.getConfiguration("geoffer." + client.getUsername().toLowerCase(), Integer.toString(slot));
@@ -186,6 +198,7 @@ public class GrandExchangePlugin extends Plugin
private boolean enableNotifications;
private boolean enableOsbPrices;
private boolean enableGELimits;
private boolean enableAfford;
@Provides
GrandExchangeConfig provideConfig(ConfigManager configManager)
@@ -244,7 +257,6 @@ public class GrandExchangePlugin extends Plugin
private void addSubscriptions()
{
eventBus.subscribe(ConfigChanged.class, this, this::onConfigChanged);
eventBus.subscribe(GameTick.class, this, this::onGameTick);
eventBus.subscribe(ChatMessage.class, this, this::onChatMessage);
eventBus.subscribe(SessionOpen.class, this, this::onSessionOpen);
eventBus.subscribe(SessionClose.class, this, this::onSessionClose);
@@ -254,7 +266,6 @@ public class GrandExchangePlugin extends Plugin
eventBus.subscribe(FocusChanged.class, this, this::onFocusChanged);
eventBus.subscribe(WidgetLoaded.class, this, this::onWidgetLoaded);
eventBus.subscribe(ScriptCallbackEvent.class, this, this::onScriptCallbackEvent);
eventBus.subscribe(GameTick.class, this, this::onGameTick);
}
private void onSessionOpen(SessionOpen sessionOpen)
@@ -276,6 +287,7 @@ public class GrandExchangePlugin extends Plugin
this.enableNotifications = config.enableNotifications();
this.enableOsbPrices = config.enableOsbPrices();
this.enableGELimits = config.enableGELimits();
this.enableAfford = config.enableAfford();
}
private void onSessionClose(SessionClose sessionClose)
@@ -465,6 +477,11 @@ public class GrandExchangePlugin extends Plugin
private void onScriptCallbackEvent(ScriptCallbackEvent event)
{
if (event.getEventName().equals("geBuilt"))
{
rebuildGeText();
}
if (!event.getEventName().equals("setGETitle") || !config.showTotal())
{
return;
@@ -505,7 +522,7 @@ public class GrandExchangePlugin extends Plugin
stringStack[stringStackSize - 1] += titleBuilder.toString();
}
private void onGameTick(GameTick event)
public void rebuildGeText()
{
if (grandExchangeText == null || grandExchangeItem == null || grandExchangeItem.isHidden())
{
@@ -513,34 +530,74 @@ public class GrandExchangePlugin extends Plugin
}
final Widget geText = grandExchangeText;
final String geTextString = geText.getText();
final int itemId = grandExchangeItem.getItemId();
if (itemId == OFFER_DEFAULT_ITEM_ID || itemId == -1)
{
lastAmount = osbItem = lastItem = -1;
// This item is invalid/nothing has been searched for
return;
}
if (this.enableGELimits && itemGELimits != null && !geTextString.contains(BUY_LIMIT_GE_TEXT))
final int currentItemPrice = client.getVar(Varbits.GRAND_EXCHANGE_PRICE_PER_ITEM);
if (lastItem == itemId && lastAmount == currentItemPrice )
{
return;
}
lastItem = itemId;
lastAmount = currentItemPrice;
String[] texts = geText.getText().split("<br>");
String text = texts[0];
if (this.enableAfford)
{
final ItemContainer itemContainer = client.getItemContainer(InventoryID.INVENTORY);
final Item[] items = itemContainer.getItems();
for (Item item : items)
{
if (item.getId() == COINS_995)
{
coins = item.getQuantity();
break;
}
}
text += AFFORD_GE_TEXT + StackFormatter.formatNumber(coins / currentItemPrice) + " ";
}
if (this.enableGELimits && itemGELimits != null)
{
final Integer itemLimit = itemGELimits.get(itemId);
// If we have item buy limit, append it
if (itemLimit != null)
{
final String text = geText.getText() + BUY_LIMIT_GE_TEXT + StackFormatter.formatNumber(itemLimit);
geText.setText(text);
text += (!this.enableAfford ? "<br>" : "") + BUY_LIMIT_GE_TEXT + StackFormatter.formatNumber(itemLimit);
}
}
if (!this.enableOsbPrices || geTextString.contains(OSB_GE_TEXT))
if (!this.enableOsbPrices)
{
// OSB prices are disabled or price was already looked up, so no need to set it again
geText.setText(text);
return;
}
geText.setText(text + osbText);
log.debug("Looking up OSB item price {}", itemId);
if (osbItem == lastItem)
{
// OSB Item was already looked up
return;
}
osbItem = lastItem;
final String str = text;
executorService.submit(() ->
{
@@ -550,13 +607,8 @@ public class GrandExchangePlugin extends Plugin
.subscribe(
(osbresult) ->
{
final String text = geText.getText() + OSB_GE_TEXT + StackFormatter.formatNumber(osbresult.getOverall_average());
if (geText.getText().contains(OSB_GE_TEXT))
{
// If there are multiple tasks queued and one of them have already added the price
return;
}
geText.setText(text);
osbText = OSB_GE_TEXT + StackFormatter.formatNumber(osbresult.getOverall_average());
geText.setText(str + osbText);
},
(e) -> log.debug("Error getting price of item {}", itemId, e)
);
@@ -570,4 +622,4 @@ public class GrandExchangePlugin extends Plugin
log.debug("Loaded {} limits", itemGELimits.size());
return itemGELimits;
}
}
}