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.

Return true if there are 10 dropped buckets on the floor

Featured Replies

How would I go about doing this? Don't ask why lol...

 

Thanks :)

Edited by mlrkey

If the player dropped 10 buckets, or if there are 10 buckets on the ground and you dont care where they came from?

What he said

  • Author

If the player dropped 10 buckets, or if there are 10 buckets on the ground and you dont care where they came from?

 

Dont care where they came from.

Dont care where they came from.

 

 

You can either use the stream api afforded in Java 8, or run a for loop of all ground items.

 

Going with the second method, here is a simplistic easy to follow way (completely untested and just for demonstration, but it should work):

    public int getAmountOf(String groundItemName){
        int numberOfItems = 0;
        java.util.List<GroundItem> allGroundItems = script.getGroundItems().getAll();
        if(allGroundItems != null && allGroundItems.size() > 0){
            for(GroundItem groundItem : allGroundItems){
                if(groundItem != null && groundItem.getName() != null && groundItem.getName().equals(groundItemName)){
                    numberOfItems++;
                }
            }
        }
        return numberOfItems;
    }
  • Author

 

Going with the second method, here is a simplistic easy to follow way (completely untested and just for demonstration, but it should work):

    public int getAmountOf(String groundItemName){
        int numberOfItems = 0;
        java.util.List<GroundItem> allGroundItems = script.getGroundItems().getAll();
        if(allGroundItems != null && allGroundItems.size() > 0){
            for(GroundItem groundItem : allGroundItems){
                if(groundItem != null && groundItem.getName() != null && groundItem.getName().equals(groundItemName)){
                    numberOfItems++;
                }
            }
        }
        return numberOfItems;
    }

 

Thanks a lot :) Will try soon

 

Going with the second method, here is a simplistic easy to follow way (completely untested and just for demonstration, but it should work):

    public int getAmountOf(String groundItemName){
        int numberOfItems = 0;
        java.util.List<GroundItem> allGroundItems = script.getGroundItems().getAll();
        if(allGroundItems != null && allGroundItems.size() > 0){
            for(GroundItem groundItem : allGroundItems){
                if(groundItem != null && groundItem.getName() != null && groundItem.getName().equals(groundItemName)){
                    numberOfItems++;
                }
            }
        }
        return numberOfItems;
    }

No need for

allGroundItems.size() > 0

The for loop already does this kind of check. Also, you could switch

groundItem.getName() != null && groundItem.getName().equals(groundItemName)

to

groundItemName.equals(groundItem.getName())

Allowing the equals call to perform the null-check on getName.

 

__________________________________

 

Using the Stream API:

public long getAmountOf(String itemName) {
     java.util.List<GroundItem> groundItems = script.getGroundItems().getAll();
     long numberOfItems = 0L;

     if(grounditems != null)
          numberOfItems = groundItems.stream().filter(item -> itemName.equals(item.getName())).count();

     return numberOfItems;
}

This is based off Mysteryyy's use of script.getGroundItems().getAll(); I've never actually used this myself (yet). If you want to return an int, simply cast when you return. Although, you could just have a boolean method:

public boolean near10Buckets() {
     return getAmountOf("Bucket") > 10L;
}

To prevent casting overhead.

No need for

allGroundItems.size() > 0

The for loop already does this kind of check. Also, you could switch

groundItem.getName() != null && groundItem.getName().equals(groundItemName)

to

groundItemName.equals(groundItem.getName())

Allowing the equals call to perform the null-check on getName.

 

 

 

1) Yes, the loop will take care of this. The way I had it before, I was allocating the int inside of that if statement, so it actually would save the allocation of the int if the length was 0. After I decided to give a proper snippet I just decided to make it a method and forgot to change it after I moved things around.

 

2) You are correct, but I always feel it is good to do my own null checks. I have learned that you can never be too careful. Where I work, we are constantly writing safety critical applications, and you cant assume anything, and can never be too safe. 

The null check could be considered redundant, but it is good practice to use defensive programming, especially when scripting.

Defensive programming is in my blood. happy.png

 

Edit: Also, in your example, I dont think you will ever need a long to store the number of buckets on the ground. :p

Edited by Mysteryy

I have learned that you can never be too careful. Where I work, we are constantly writing safety critical applications, and you cant assume anything, and can never be too safe.

Edit: Also, in your example, I dont think you will ever need a long to store the number of buckets on the ground. :p

It's not really about safety measures. It's the exact same execution; the null check is just performed in the equals method, hidden away.

And count() returns a long, which is why I suggested casting. But depending on the situation, you could avoid the overhead of casting (trading memory for runtime performance) by keeping it a long.

Edited by fixthissite

  • Author

"script" cannot be resolved in:

 

java.util.List<GroundItem> allGroundItems = script.getGroundItems().getAll();

"script" cannot be resolved in:

java.util.List<GroundItem> allGroundItems = script.getGroundItems().getAll();

After checking out the API, it seems there is no script field. But it seems getGroundItems() is declared in the Script class, so use that knowledge to fix up your code

Edited by fixthissite

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.