Explv Posted April 30, 2016 Share Posted April 30, 2016 (edited) import org.osbot.rs07.api.Widgets; import org.osbot.rs07.api.filter.Filter; import org.osbot.rs07.api.ui.RS2Widget; import java.util.Optional; public class CachedWidget { private int rootID = -1, secondLevelID = -1, thirdLevelID = -1; private String[] widgetTexts; private Filter<RS2Widget> filter; public CachedWidget(final int rootID, final int secondLevelID){ this.rootID = rootID; this.secondLevelID = secondLevelID; } public CachedWidget(final int rootID, final int secondLevelID, final int thirdLevelID){ this.rootID = rootID; this.secondLevelID = secondLevelID; this.thirdLevelID = thirdLevelID; } public CachedWidget(final int rootID, final String... widgetTexts) { this.rootID = rootID; this.widgetTexts = widgetTexts; } public CachedWidget(final String... widgetTexts){ this.widgetTexts = widgetTexts; } public CachedWidget(final int rootID, final Filter<RS2Widget> filter) { this.rootID = rootID; this.filter = filter; } public CachedWidget(final Filter<RS2Widget> filter) { this.filter = filter; } public Optional<RS2Widget> getParent(final Widgets widgets) { return get(widgets).map(widget -> { if (widget.isSecondLevel()) { return widget; } return widgets.get(widget.getRootId(), widget.getSecondLevelId()); }); } public Optional<RS2Widget> get(final Widgets widgets){ if(rootID != -1 && secondLevelID != -1 && thirdLevelID != -1) { return Optional.ofNullable(widgets.get(rootID, secondLevelID, thirdLevelID)); } else if(rootID != -1 && secondLevelID != -1) { return getSecondLevelWidget(widgets); } else if (widgetTexts != null) { return getWidgetWithText(widgets); } else { return getWidgetUsingFilter(widgets); } } private Optional<RS2Widget> getSecondLevelWidget(final Widgets widgets){ RS2Widget rs2Widget = widgets.get(rootID, secondLevelID); if(rs2Widget != null && rs2Widget.isThirdLevel()){ thirdLevelID = rs2Widget.getThirdLevelId(); } return Optional.ofNullable(rs2Widget); } private Optional<RS2Widget> getWidgetWithText(final Widgets widgets){ RS2Widget rs2Widget; if (rootID != -1) { rs2Widget = widgets.getWidgetContainingText(rootID, widgetTexts); } else { rs2Widget = widgets.getWidgetContainingText(widgetTexts); } setWidgetIDs(rs2Widget); return Optional.ofNullable(rs2Widget); } private Optional<RS2Widget> getWidgetUsingFilter(final Widgets widgets) { RS2Widget rs2Widget; if (rootID != -1) { rs2Widget = widgets.singleFilter(rootID, filter); } else { rs2Widget = widgets.singleFilter(widgets.getAll(), filter); } setWidgetIDs(rs2Widget); return Optional.ofNullable(rs2Widget); } private void setWidgetIDs(final RS2Widget rs2Widget) { if (rs2Widget == null) { return; } rootID = rs2Widget.getRootId(); secondLevelID = rs2Widget.getSecondLevelId(); if (rs2Widget.isThirdLevel()) { thirdLevelID = rs2Widget.getThirdLevelId(); } } @Override public String toString() { return rootID + ", " + secondLevelID + ", " + thirdLevelID; } } Usage: private final CachedWidget exampleWidget = new CachedWidget("Blah"); public void someMethod() { exampleWidget.get(getWidgets()).ifPresent(widget -> widget.interact()); } Edited May 24, 2018 by Explv 8 Quote Link to comment Share on other sites More sharing options...
Explv Posted May 24, 2018 Author Share Posted May 24, 2018 Updated to include more constructors 1 Quote Link to comment Share on other sites More sharing options...
Shaft Posted June 29, 2018 Share Posted June 29, 2018 Nice work! Quote Link to comment Share on other sites More sharing options...