Explv Posted August 5, 2017 Share Posted August 5, 2017 (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 October 28, 2017 by Explv 6 Quote Link to comment Share on other sites More sharing options...
Tom Posted August 5, 2017 Share Posted August 5, 2017 noooooooooooooooooooooooooooooooooooooooooooooooooooo Quote Link to comment Share on other sites More sharing options...
Explv Posted October 28, 2017 Author Share Posted October 28, 2017 Updated the code here, previously could cause NPE if RS2Widget.getInteractActions() contains a null value. Changed if (widgetAction.equals(matchAction)) To if (matchAction.equals(widgetAction)) Thanks for the bug report @The Undefeated Quote Link to comment Share on other sites More sharing options...
liverare Posted October 28, 2017 Share Posted October 28, 2017 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; } Quote Link to comment Share on other sites More sharing options...
Explv Posted October 28, 2017 Author Share Posted October 28, 2017 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. Quote Link to comment Share on other sites More sharing options...