Jump to content
View in the app

A better way to browse. Learn more.

OSBot :: 2007 OSRS Botting

A full-screen app on your home screen with push notifications, badges and more.

To install this app on iOS and iPadOS
  1. Tap the Share icon in Safari
  2. Scroll the menu and tap Add to Home Screen.
  3. Tap Add in the top-right corner.
To install this app on Android
  1. Tap the 3-dot menu (⋮) in the top-right corner of the browser.
  2. Tap Add to Home screen or Install app.
  3. Confirm by tapping Install.

Checking for items in inventory

Featured Replies

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

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

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

  • Author
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

// 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.

  • Author
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

Recently Browsing 0

  • No registered users viewing this page.

Account

Navigation

Search

Configure browser push notifications

Chrome (Android)
  1. Tap the lock icon next to the address bar.
  2. Tap Permissions → Notifications.
  3. Adjust your preference.
Chrome (Desktop)
  1. Click the padlock icon in the address bar.
  2. Select Site settings.
  3. Find Notifications and adjust your preference.