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 and toggling shift dropping

Featured Replies

import org.osbot.rs07.api.filter.Filter;
import org.osbot.rs07.api.ui.RS2Widget;
import org.osbot.rs07.api.ui.Tab;
import org.osbot.rs07.event.Event;

public final class ToggleShiftDropEvent extends Event {

    @Override
    public final int execute() throws InterruptedException {
        if (Tab.SETTINGS.isDisabled(getBot())) {
            setFailed();
        } else if (getTabs().getOpen() != Tab.SETTINGS) {
            getTabs().open(Tab.SETTINGS);
        } else if(getShiftDropWidget() == null || !getShiftDropWidget().isVisible()) {
            getGameSettingsWidget().interact();
        } else {
            boolean enabled = getSettings().isShiftDropActive();
            if (getShiftDropWidget().interact()) {
                Sleep.sleepUntil(() -> getSettings().isShiftDropActive() != enabled, 3000);
                setFinished();
            }
        }
        return random(100, 200);
    }

    private RS2Widget getShiftDropWidget() {
        return getWidgets().singleFilter(getWidgets().getAll(), new WidgetActionFilter("Toggle shift click to drop"));
    }

    private RS2Widget getGameSettingsWidget() {
        return getWidgets().singleFilter(getWidgets().getAll(), new WidgetActionFilter("Controls"));
    }
}

class WidgetActionFilter implements Filter<RS2Widget> {

    private final String action;

    WidgetActionFilter(final String action) {
        this.action = action;
    }

    @Override
    public boolean match(RS2Widget rs2Widget) {
        if (rs2Widget == null) {
            return false;
        }
        if (rs2Widget.getInteractActions() == null) {
            return false;
        }
        for (String action : rs2Widget.getInteractActions()) {
            if (this.action.equals(action)) {
                return true;
            }
        }
        return false;
    }
}

 

Edited by Explv

  • 1 month later...

Thanks for the snippet, I changed the match code around a bit. Using try/catch avoids needing to check rs2Widget/getInteractions for null, and using Arrays.asList avoid needing to iterate the array. Since we are talking about <5 actions 99% of the time, it's kinda insignificant, but still looks better.

	@Override
    public boolean match(RS2Widget rs2Widget) {
        try {
            return Arrays.asList(rs2Widget.getInteractActions()).contains(action);
        } catch (Exception e) {
            return false;
        }
    }

Edited by Trees

  • Author
4 hours ago, Trees said:

Thanks for the snippet, I changed the match code around a bit. Using try/catch avoids needing to check rs2Widget/getInteractions for null, and using Arrays.asList avoid needing to iterate the array. Since we are talking about <5 actions 99% of the time, it's kinda insignificant, but still looks better.


	@Override
    public boolean match(RS2Widget rs2Widget) {
        try {
            return Arrays.asList(rs2Widget.getInteractActions()).contains(action);
        } catch (Exception e) {
            return false;
        }
    }

Exceptions are supposed to be used in exceptional circumstances, not for normal flow control. It is the best practice in my opinion to null check here.

Edited by Explv

6 hours ago, Explv said:

Exceptions are supposed to be used in exceptional circumstances, not for normal flow control. It is the best practice in my opinion to null check here.

They can be used for normal flow in order to avoid excessive nested if statements. Maybe not needed here though. Would recommend creating a var to hold the interact options though so you don't call it twice.

  • Author
10 hours ago, Trees said:

They can be used for normal flow in order to avoid excessive nested if statements. Maybe not needed here though. Would recommend creating a var to hold the interact options though so you don't call it twice.

Yes they can, but they shouldn't, it's bad practice. When you know that a null value is possible, you should use control flow statements to handle it. Again, exceptions should be used for exceptional circumstances.

Using try & catch for program flow in a non-trivial scenario can even decrease performance due to the overhead of garbage collection.

If you want to create a variable to hold the interact options, go ahead, I personally did not feel the need to micro-optimise every little bit of this code, which will likely only ever be called once in a script.

 

Edited by Explv

On 4/29/2017 at 10:24 AM, Explv said:

Yes they can, but they shouldn't, it's bad practice. When you know that a null value is possible, you should use control flow statements to handle it. Again, exceptions should be used for exceptional circumstances.

Using try & catch for program flow in a non-trivial scenario can even decrease performance due to the overhead of garbage collection.

If you want to create a variable to hold the interact options, go ahead, I personally did not feel the need to micro-optimise every little bit of this code, which will likely only ever be called once in a script.

 

Actually in most c-based programming languages, using a try-catch flow can actually provide better performance (not sure if that's regarding exceptions or not)

  • 4 weeks later...

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.