examine plugin: fix overflow in computing alch price

This commit is contained in:
Adam
2020-06-27 13:52:14 -04:00
parent 71d89af946
commit f9aea0d958
2 changed files with 28 additions and 4 deletions

View File

@@ -24,6 +24,7 @@
*/ */
package net.runelite.client.plugins.examine; package net.runelite.client.plugins.examine;
import com.google.common.annotations.VisibleForTesting;
import com.google.common.cache.Cache; import com.google.common.cache.Cache;
import com.google.common.cache.CacheBuilder; import com.google.common.cache.CacheBuilder;
import java.time.Instant; import java.time.Instant;
@@ -313,11 +314,12 @@ public class ExaminePlugin extends Plugin
return null; return null;
} }
private void getItemPrice(int id, ItemComposition itemComposition, int quantity) @VisibleForTesting
void getItemPrice(int id, ItemComposition itemComposition, int quantity)
{ {
// quantity is at least 1 // quantity is at least 1
quantity = Math.max(1, quantity); quantity = Math.max(1, quantity);
final long gePrice = itemManager.getItemPrice(id); final int gePrice = itemManager.getItemPrice(id);
final int alchPrice = itemComposition.getHaPrice(); final int alchPrice = itemComposition.getHaPrice();
if (gePrice > 0 || alchPrice > 0) if (gePrice > 0 || alchPrice > 0)
@@ -345,7 +347,7 @@ public class ExaminePlugin extends Plugin
.append(ChatColorType.NORMAL) .append(ChatColorType.NORMAL)
.append(" GE average ") .append(" GE average ")
.append(ChatColorType.HIGHLIGHT) .append(ChatColorType.HIGHLIGHT)
.append(QuantityFormatter.formatNumber(gePrice * quantity)); .append(QuantityFormatter.formatNumber((long) gePrice * quantity));
if (quantity > 1) if (quantity > 1)
{ {
@@ -365,7 +367,7 @@ public class ExaminePlugin extends Plugin
.append(ChatColorType.NORMAL) .append(ChatColorType.NORMAL)
.append(" HA value ") .append(" HA value ")
.append(ChatColorType.HIGHLIGHT) .append(ChatColorType.HIGHLIGHT)
.append(QuantityFormatter.formatNumber(alchPrice * quantity)); .append(QuantityFormatter.formatNumber((long) alchPrice * quantity));
if (quantity > 1) if (quantity > 1)
{ {

View File

@@ -37,11 +37,14 @@ import net.runelite.api.events.ChatMessage;
import net.runelite.api.events.MenuOptionClicked; import net.runelite.api.events.MenuOptionClicked;
import net.runelite.api.widgets.Widget; import net.runelite.api.widgets.Widget;
import net.runelite.client.chat.ChatMessageManager; import net.runelite.client.chat.ChatMessageManager;
import net.runelite.client.chat.QueuedMessage;
import net.runelite.client.game.ItemManager; import net.runelite.client.game.ItemManager;
import net.runelite.http.api.examine.ExamineClient; import net.runelite.http.api.examine.ExamineClient;
import static org.junit.Assert.assertEquals;
import org.junit.Before; import org.junit.Before;
import org.junit.Test; import org.junit.Test;
import org.junit.runner.RunWith; import org.junit.runner.RunWith;
import org.mockito.ArgumentCaptor;
import static org.mockito.ArgumentMatchers.anyInt; import static org.mockito.ArgumentMatchers.anyInt;
import static org.mockito.ArgumentMatchers.anyString; import static org.mockito.ArgumentMatchers.anyString;
import org.mockito.Mock; import org.mockito.Mock;
@@ -115,4 +118,23 @@ public class ExaminePluginTest
verify(examineClient, never()).submitItem(anyInt(), anyString()); verify(examineClient, never()).submitItem(anyInt(), anyString());
} }
@Test
public void testGetItemPrice()
{
ItemComposition itemComposition = mock(ItemComposition.class);
when(itemComposition.getName()).thenReturn("Abyssal whip");
when(itemComposition.getHaPrice()).thenReturn(2);
when(itemManager.getItemPrice(ItemID.ABYSSAL_WHIP)).thenReturn(3);
examinePlugin.getItemPrice(ItemID.ABYSSAL_WHIP, itemComposition, 2_000_000_000);
ArgumentCaptor<QueuedMessage> argumentCaptor = ArgumentCaptor.forClass(QueuedMessage.class);
verify(chatMessageManager).queue(argumentCaptor.capture());
QueuedMessage queuedMessage = argumentCaptor.getValue();
assertEquals(
"<colNORMAL>Price of <colHIGHLIGHT>2,000,000,000 x Abyssal whip<colNORMAL>:<colNORMAL> GE average <colHIGHLIGHT>6,000,000,000<colNORMAL> (<colHIGHLIGHT>3<colNORMAL>ea)<colNORMAL> HA value <colHIGHLIGHT>4,000,000,000<colNORMAL> (<colHIGHLIGHT>2<colNORMAL>ea)",
queuedMessage.getRuneLiteFormattedMessage()
);
}
} }