Jump to content

Widget Action Filter


Recommended Posts

Posted (edited)
import org.osbot.rs07.api.filter.Filter;
import org.osbot.rs07.api.ui.RS2Widget;

public final class WidgetActionFilter implements Filter<RS2Widget> {

    private final String[] actions;

    public WidgetActionFilter(final String... actions) {
        this.actions = actions;
    }

    @Override
    public final boolean match(final RS2Widget rs2Widget) {
        if (rs2Widget == null || !rs2Widget.isVisible() || rs2Widget.getInteractActions() == null) {
            return false;
        }
        for (final String widgetAction : rs2Widget.getInteractActions()) {
            for (final String matchAction : actions) {
                if (matchAction.equals(widgetAction)) {
                    return true;
                }
            }
        }
        return false;
    }
}

 

Edited by Explv
  • Like 6
Posted
  • Only valid widgets would be passed into the match function, so you don't need to check for that.
  • Be sure to check the array length to make sure it's not an empty array.
  • You should only have one return statement in a function.
  • You should store the widget actions in a variable and reuse them.
@Override
public final boolean match(RS2Widget widget) {
	boolean match = false;
	String widgetActions = rs2Widget.getInteractActions();
		if (widgetActions != null && widgetActions.length > 0) {
		for (String action : actions) {
			for (String widgetAction : widgetActions) {
				if (widgetAction.equals(action)) {
					match = true;
					break;
				}
			}
		}
	}
	return match;
}

 

Posted
45 minutes ago, liverare said:
  • Only valid widgets would be passed into the match function, so you don't need to check for that.
  • Be sure to check the array length to make sure it's not an empty array.
  • You should only have one return statement in a function.
  • You should store the widget actions in a variable and reuse them.

@Override
public final boolean match(RS2Widget widget) {
	boolean match = false;
	String widgetActions = rs2Widget.getInteractActions();
		if (widgetActions != null && widgetActions.length > 0) {
		for (String action : actions) {
			for (String widgetAction : widgetActions) {
				if (widgetAction.equals(action)) {
					match = true;
					break;
				}
			}
		}
	}
	return match;
}

 

 

1. Not necessarily true, you could call the match function yourself with an invalid widget outside of the OSBot methods

2. I am using a foreach loop, I don't need to check if the array is empty

3. I personally think that this convention is purely personal preference and ancient history. I find code much more legible if I have early return conditions.

If your code has many many return statements in a single function, then probably it is not as object oriented as it should be, in this case I do not think it applies.

4. If you look at how my loops are structured I only loop the widget's actions once, and the match actions are stored in a variable.

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