Mephisto Posted December 13, 2018 Share Posted December 13, 2018 (edited) Hi! I'm trying to write my first script & have some questions. I want my script to check if my bot's inventory has some items I selected & teleport if all the items are in its inventory. So I made this: if (getInventory().contains("Tinderbox","Falador teleport","Varrock teleport","Lumbridge teleport","Steel pickaxe")){ inventory.getItem("Lumbridge teleport").interact(); log("Teleporting to Lumbridge"); }else{ log("We don't have all the items we need"); } The bot now teleports when it has one of these items instead of all of them. (So if one item is missing the getInventory().contains() statement is still true) Do I have to write a long list "Ifs & elses" containing every item seperatley to make it work or are there other solutions that are less messy & less work? Thanks in Advance! Edited December 13, 2018 by Mephisto Quote Link to comment Share on other sites More sharing options...
jca Posted December 13, 2018 Share Posted December 13, 2018 (edited) 14 minutes ago, Mephisto said: Hi! I'm trying to write my first script & have some questions. I want my script to check if my bot's inventory has some items I selected & teleport if all the items are in its inventory. So I made this: if (getInventory().contains("Tinderbox","Falador teleport","Varrock teleport","Lumbridge teleport","Steel pickaxe")){ inventory.getItem("Lumbridge teleport").interact(); log("Teleporting to Lumbridge"); }else{ log("We don't have all the items we need"); } The bot now teleports when it has one of these items instead of all of them. (So if one item is missing the getInventory().contains() statement is still true) Do I have to write a long list "Ifs & elses" containing every item seperatley to make it work or are there other solutions that are less messy & less work? Thanks in Advance! inventory#contains will check if any item in the provided array is in the inventory. There's probably a better way, but I use a separate function private boolean inventoryContainsAllItems(String... items){ for( String item : items ){ if ( !getInventory().contains(item) ) return false; } return true; } Edited December 13, 2018 by jca 1 Quote Link to comment Share on other sites More sharing options...
NoxMerc Posted December 13, 2018 Share Posted December 13, 2018 (edited) List<String> itemsToFind = Arrays.asList("Tinderbox", "Falador teleport", "..."); List<String> inventoryItems = Arrays.stream(ctx.getInventory().getItems()).map(Item::getName).collect(Collectors.toList()); boolean containsAll = inventoryItems.containsAll(itemsToFind); Might be more efficient to get the items once rather than constantly querying. Otherwise.. List<String> itemsToFind = Arrays.asList("Tinderbox", "Falador teleport", "..."); boolean contains = itemsToFind.stream().allMatch(getInventory()::contains); Edited December 13, 2018 by NoxMerc 1 Quote Link to comment Share on other sites More sharing options...
Mephisto Posted December 13, 2018 Author Share Posted December 13, 2018 (edited) 11 minutes ago, NoxMerc said: List<String> itemsToFind = Arrays.asList("Tinderbox", "Falador teleport", "..."); List<String> inventoryItems = Arrays.stream(ctx.getInventory().getItems()).map(Item::getName).collect(Collectors.toList()); boolean containsAll = inventoryItems.containsAll(itemsToFind); Might be more efficient to get the items once rather than constantly querying. Otherwise.. List<String> itemsToFind = Arrays.asList("Tinderbox", "Falador teleport", "..."); boolean contains = itemsToFind.stream().allMatch(getInventory()::contains); How would I implement this in my script? I try to understand everything you wrote but I can't figure out everything of what you wrote yet Sorry for beeing such a newb... Edited December 13, 2018 by Mephisto Quote Link to comment Share on other sites More sharing options...
NoxMerc Posted December 13, 2018 Share Posted December 13, 2018 (edited) There's two concepts that java8 introduces, Streams and Lambdas. People here have written tutorials, and there are infinitely more tutorials on the internet. The double-colon is called a Method Reference. https://www.oracle.com/webfolder/technetwork/tutorials/obe/java/Lambda-QuickStart/index.html Edited December 13, 2018 by NoxMerc Quote Link to comment Share on other sites More sharing options...
NoxMerc Posted December 13, 2018 Share Posted December 13, 2018 // Create a list of items we want to find List<String> itemsToFind = Arrays.asList("Tinderbox", "Falador teleport", "..."); // .stream -> Create a stream from our List. Streams a way of processing lists of data in a functional manner // .allMatch -> Returns true if and only if all items of the stream pass this test // getInventory::contains -> Method Reference to our local function getInventory().contains(). // The method reference implicitly passes in our lambda expression parmaeter, which is a string. // To write this our long-form, you would do ".allMatch(itemName -> getInventory().contains(itemName))" boolean contains = itemsToFind.stream().allMatch(getInventory()::contains); Commented it a bit for you as well. Streams, Lambdas, and Method References offer more concise ways to execute code. Bear in mind that it's not faster to use Streams in most cases, but they often offer more clarity. 1 Quote Link to comment Share on other sites More sharing options...
Mephisto Posted December 13, 2018 Author Share Posted December 13, 2018 18 minutes ago, NoxMerc said: There's two concepts that java8 introduces, Streams and Lambdas. People here have written tutorials, and there are infinitely more tutorials on the internet. The double-colon is called a Method Reference. https://www.oracle.com/webfolder/technetwork/tutorials/obe/java/Lambda-QuickStart/index.html Thanks I'm going to look into this Quote Link to comment Share on other sites More sharing options...