Jump to content

Checking and toggling shift dropping


Recommended Posts

Posted (edited)
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
  • Like 11
  • 1 month later...
Posted (edited)

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
Posted (edited)
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
Posted
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.

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

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