From d6208133b617aec585e05590c5b6ccd8bacb7554 Mon Sep 17 00:00:00 2001 From: l2- Date: Mon, 17 Jul 2017 21:07:37 -0400 Subject: [PATCH] Added comments. fixed a bug where it wouldn't recognize an item if there is another item with the search term in its name. --- .../plugins/pricecommands/PriceCommands.java | 48 +++++++++++++++++-- 1 file changed, 45 insertions(+), 3 deletions(-) diff --git a/runelite-client/src/main/java/net/runelite/client/plugins/pricecommands/PriceCommands.java b/runelite-client/src/main/java/net/runelite/client/plugins/pricecommands/PriceCommands.java index a9e329e06c..8698e1aa2f 100644 --- a/runelite-client/src/main/java/net/runelite/client/plugins/pricecommands/PriceCommands.java +++ b/runelite-client/src/main/java/net/runelite/client/plugins/pricecommands/PriceCommands.java @@ -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 items, String originalInput) + { + for (Item item : items) + { + if (item.getName().toLowerCase().equals(originalInput.toLowerCase())) + { + return item; + } + } + return null; + } }