Jump to content

Widget Action Filter


Explv

Recommended Posts

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
Link to comment
Share on other sites

  • 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;
}

 

Link to comment
Share on other sites

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.

Link to comment
Share on other sites

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.
Note: Your post will require moderator approval before it will be visible.

Guest
Reply to this topic...

×   Pasted as rich text.   Paste as plain text instead

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

  • Recently Browsing   0 members

    • No registered users viewing this page.
×
×
  • Create New...