Jump to content

Checking for items in inventory


Mephisto

Recommended Posts

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
Link to comment
Share on other sites

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
Link to comment
Share on other sites

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
Link to comment
Share on other sites

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
Link to comment
Share on other sites

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 by NoxMerc
Link to comment
Share on other sites

// 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
Link to comment
Share on other sites

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 :) 

Link to comment
Share on other sites

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.
Note: Your post will require moderator approval before it will be visible.

Guest
Reply to this topic...

×   Pasted as rich text.   Paste as plain text instead

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

  • Recently Browsing   0 members

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