Jump to content

Checking for items in inventory


Recommended Posts

Posted (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 by Mephisto
Posted (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 by jca
  • Like 1
Posted (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 by NoxMerc
  • Like 1
Posted (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 by Mephisto
Posted
// 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.

  • Like 1
Posted
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 :) 

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now
  • Recently Browsing   0 members

    • No registered users viewing this page.
×
×
  • Create New...