Jump to content

A Better Way To Handle Widgets (CachedWidget)


Explv

Recommended Posts

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 by Explv
  • Like 8
Link to comment
Share on other sites

  • 1 month later...

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