Added comments. fixed a bug where it wouldn't recognize an item if there is another item with the search term in its name.

This commit is contained in:
l2-
2017-07-17 21:07:37 -04:00
committed by Adam
parent f9b52e2472
commit d6208133b6

View File

@@ -27,9 +27,11 @@ package net.runelite.client.plugins.pricecommands;
import com.google.common.eventbus.Subscribe;
import java.io.IOException;
import java.util.List;
import java.util.concurrent.ScheduledExecutorService;
import net.runelite.api.Client;
import net.runelite.api.GameState;
import net.runelite.http.api.item.Item;
import net.runelite.api.MessageNode;
import net.runelite.client.game.ItemManager;
import net.runelite.client.RuneLite;
@@ -61,6 +63,11 @@ public class PriceCommands extends Plugin
{
}
/**
* Checks if the chat message is a command.
*
* @param setMessage The chat message
*/
@Subscribe
public void onSetMessage(SetMessage setMessage)
{
@@ -92,6 +99,13 @@ public class PriceCommands extends Plugin
}
}
/**
* Looks up the item price and changes the original message to the
* reponse.
*
* @param messageNode The chat message containing the command.
* @param search The item given with the command.
*/
private void lookup(MessageNode messageNode, String search)
{
SearchResult result;
@@ -106,9 +120,16 @@ public class PriceCommands extends Plugin
return;
}
if (result != null && result.getItems().size() == 1)
if (result != null && !result.getItems().isEmpty())
{
int itemId = result.getItems().get(0).getId();
Item item = retrieveFromList(result.getItems(), search);
if (item == null)
{
logger.debug("Unable to find item {} in result {}", search, result);
return;
}
int itemId = item.getId();
ItemPrice itemPrice;
try
@@ -122,7 +143,7 @@ public class PriceCommands extends Plugin
}
int cost = itemPrice.getPrice();
String response = "Price of " + result.getItems().get(0).getName() + ": GE average " + String.format("%,d", cost);
String response = "Price of " + item.getName() + ": GE average " + String.format("%,d", cost);
logger.debug("Setting response {}", response);
@@ -131,4 +152,25 @@ public class PriceCommands extends Plugin
client.refreshChat();
}
}
/**
* Compares the names of the items in the list with the original input.
* returns the item if its name is equal to the original input or null
* if it can't find the item.
*
* @param items List of items.
* @param originalInput String with the original input.
* @return Item which has a name equal to the original input.
*/
private Item retrieveFromList(List<Item> items, String originalInput)
{
for (Item item : items)
{
if (item.getName().toLowerCase().equals(originalInput.toLowerCase()))
{
return item;
}
}
return null;
}
}