use of net.runelite.http.api.item.ItemPrice in project runelite by runelite.
the class BankCalculation method calculate.
/**
* 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) {
// cached no price
continue;
}
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();
}
}
}
}
use of net.runelite.http.api.item.ItemPrice in project runelite by runelite.
the class ExaminePlugin method getItemPrice.
private void getItemPrice(ItemComposition itemComposition, int quantity) {
// convert to unnoted id
final boolean note = itemComposition.getNote() != -1;
final int id = note ? itemComposition.getLinkedNoteId() : itemComposition.getId();
ItemPrice itemPrice;
try {
itemPrice = itemManager.getItemPrice(id);
} catch (IOException e) {
log.warn("Error looking up item price", e);
return;
}
int itemCompositionPrice = itemComposition.getPrice();
final int gePrice = itemPrice == null ? 0 : itemPrice.getPrice();
final int alchPrice = itemCompositionPrice <= 0 ? 0 : Math.round(itemCompositionPrice * HIGH_ALCHEMY_CONSTANT);
if (gePrice > 0 || alchPrice > 0) {
final ChatMessageBuilder message = new ChatMessageBuilder().append(ChatColorType.NORMAL).append("Price of ").append(ChatColorType.HIGHLIGHT);
if (quantity > 1) {
message.append(StackFormatter.formatNumber(quantity)).append(" x ");
}
message.append(itemComposition.getName()).append(ChatColorType.NORMAL).append(":");
if (gePrice > 0) {
message.append(ChatColorType.NORMAL).append(" GE average ").append(ChatColorType.HIGHLIGHT).append(StackFormatter.formatNumber(gePrice * quantity));
}
if (quantity > 1) {
message.append(ChatColorType.NORMAL).append(" (").append(ChatColorType.HIGHLIGHT).append(StackFormatter.formatNumber(gePrice)).append(ChatColorType.NORMAL).append("ea)");
}
if (alchPrice > 0) {
message.append(ChatColorType.NORMAL).append(" HA value ").append(ChatColorType.HIGHLIGHT).append(StackFormatter.formatNumber(alchPrice * quantity));
}
if (quantity > 1) {
message.append(ChatColorType.NORMAL).append(" (").append(ChatColorType.HIGHLIGHT).append(StackFormatter.formatNumber(alchPrice)).append(ChatColorType.NORMAL).append("ea)");
}
chatMessageManager.queue(QueuedMessage.builder().type(ChatMessageType.EXAMINE_ITEM).runeLiteFormattedMessage(message.build()).build());
}
}
use of net.runelite.http.api.item.ItemPrice in project runelite by runelite.
the class ItemManager method getItemPriceBatch.
/**
* Look up bulk item prices asynchronously
*
* @param itemIds array of item Ids
* @return a future called with the looked up prices
*/
public CompletableFuture<ItemPrice[]> getItemPriceBatch(List<Integer> itemIds) {
final List<Integer> lookup = new ArrayList<>();
final List<ItemPrice> 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<ItemPrice[]> 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;
}
use of net.runelite.http.api.item.ItemPrice in project runelite by runelite.
the class ItemPricesOverlay method getItemStackValueText.
private String getItemStackValueText(Item item) {
int id = item.getId();
int qty = item.getQuantity();
// Special case for coins and platinum tokens
if (id == ItemID.COINS_995) {
return StackFormatter.formatNumber(qty) + " gp";
} else if (id == ItemID.PLATINUM_TOKEN) {
return StackFormatter.formatNumber(qty * 1000) + " gp";
}
final ItemComposition itemDef = itemManager.getItemComposition(id);
// Only check prices for things with store prices
if (itemDef.getPrice() <= 0) {
return null;
}
int gePrice = 0;
int haPrice = 0;
if (config.showGEPrice()) {
final ItemPrice price = itemManager.getItemPriceAsync(id);
if (price != null) {
gePrice = price.getPrice();
}
}
if (config.showHAValue()) {
haPrice = Math.round(itemDef.getPrice() * HIGH_ALCHEMY_CONSTANT);
}
if (gePrice > 0 || haPrice > 0) {
return stackValueText(qty, gePrice, haPrice);
}
return null;
}
use of net.runelite.http.api.item.ItemPrice in project runelite by runelite.
the class ItemController method itemPrice.
@RequestMapping("/{itemId}/price")
public ResponseEntity<ItemPrice> itemPrice(@PathVariable int itemId, @RequestParam(required = false) Instant time) {
if (cachedEmpty.getIfPresent(itemId) != null) {
return ResponseEntity.notFound().header(RUNELITE_CACHE, "HIT").build();
}
Instant now = Instant.now();
boolean hit = true;
if (time != null && time.isAfter(now)) {
time = now;
}
ItemEntry item = itemService.getItem(itemId);
if (item == null) {
item = itemService.fetchItem(itemId);
hit = false;
if (item == null) {
cachedEmpty.put(itemId, itemId);
return ResponseEntity.notFound().header(RUNELITE_CACHE, "MISS").build();
}
}
PriceEntry priceEntry = itemService.getPrice(itemId, time);
if (time != null) {
if (priceEntry == null) {
// we maybe can't backfill this
return ResponseEntity.notFound().header(RUNELITE_CACHE, "MISS").build();
}
} else if (priceEntry == null) {
// Price is unknown
List<PriceEntry> prices = itemService.fetchPrice(itemId);
if (prices == null || prices.isEmpty()) {
cachedEmpty.put(itemId, itemId);
return ResponseEntity.notFound().header(RUNELITE_CACHE, "MISS").build();
}
// Get the most recent price
priceEntry = prices.get(prices.size() - 1);
hit = false;
} else {
Instant cacheTime = now.minus(CACHE_DUATION);
if (priceEntry.getFetched_time().isBefore(cacheTime)) {
// Queue a check for the price
itemService.queuePriceLookup(itemId);
}
}
ItemPrice itemPrice = new ItemPrice();
itemPrice.setItem(item.toItem());
itemPrice.setPrice(priceEntry.getPrice());
itemPrice.setTime(priceEntry.getTime());
return ResponseEntity.ok().header(RUNELITE_CACHE, hit ? "HIT" : "MISS").cacheControl(CacheControl.maxAge(30, TimeUnit.MINUTES).cachePublic()).body(itemPrice);
}
Aggregations