Jump to content
Search In
  • More options...
Find results that contain...
Find results in...
Buy OSRS Gold

Sell OSRS Gold
Sign in to follow this  
Jamie

[Snippet] Membership Checker

Recommended Posts

This is a snippet that will observe your membership status based on the interface found on the login screen. Any suggestions to improve this are welcome. :)

Code:

Interface:

public interface MembershipListener {

    void needsMembers();

}

Observer:

public class MembershipObserver extends Thread {
    ArrayList<MembershipListener> listeners;
    boolean shouldRun = true;

    public MembershipObserver() {
        this.listeners = new ArrayList<MembershipListener>();
    }

    @Override
    public void run() {
        General.println("[Membership Handler] : Thread Started.");
        while (shouldRun) {
            RSInterfaceChild membershipInterface = Interfaces.get(378, 74);
            if (membershipInterface != null) {
                if (getMembershipCount(membershipInterface) < 1) {
                    triggerMembershipRequest();
                }
                General.sleep(15000);
            }
            General.sleep(50);
        }
    }

    public void endThread() {
        General.println("[Membership Handler] : Thread Stopped.");
        shouldRun = false;
    }


    private int getMembershipCount(RSInterfaceChild rsInterfaceChild) {
       
        String text = rsInterfaceChild.getText() != null ? rsInterfaceChild.getText().replace("<col=00ff00>", "").replace("<col=ffff00>", "") : "0";
        
        text = text.replaceAll("[^0-9]", "");

        General.println("[Membership Handler] : Members days remaining : " + Integer.parseInt(text));

       return Integer.parseInt(text);
       
    }

    public void addListener(MembershipListener membershipListener) {
        listeners.add(membershipListener);
    }

    private void triggerMembershipRequest() {
        for (MembershipListener l : listeners) {
            l.needsMembers();
        }
    }

Add to your script as follows:

public class MembershipTestScript extends Script implements MembershipListener, Ending {

	private MembershipObserver membershipObserver = new MembershipObserver(); // I set this as a global variable as we need to set the thread as a daemon so it's not stopped when the login bot is executed. Because of this, we must end the thread on script exit, so we also need to implement the Ending interface.

	@Override
	public void run() {

		// your script

	}
	
	@Override
	public void onEnd() {

	membershipObserver.endThread(); // I've already made this method for you in the Observer class.

	}
	
	private void initiateObserver(){ // You can do this in onStart or as you run your script. I keep it in it's own method just for preference.
		membershipObserver.addListener(this);
        membershipObserver.setDaemon(true);
        membershipObserver.start();
	}
	
	 @Override
    	public void needsMembers() {
		// This is run once when the observer detects the account does not have membership. I set a boolean to true so I can then buy a bond etc within my script.
        General.println("[Membership Handler] : Needs Membership");
        Vars.get().needsMembership = true;
    }

 

Edited by HeyImJamie
  • Like 4

Share this post


Link to post
Share on other sites
 RSInterfaceChild membershipInterface = Interfaces.get(378, 74);
            if (membershipInterface != null) {
                if (getMembershipCount() < 1) {
 private int getMembershipCount() {
        RSInterfaceChild membershipInterface = Interfaces.get(378, 74);

        if (membershipInterface == null)
            return 0;

 

Instead of grabbing the interface again from scratch you could simply pass it as an argument to this method. If it passed the null check, it's good to go and it doesn't require additional checks:

RSInterfaceChild i = Interfaces.get(378, 74);
          if (i != null) {
             getMembershipCount(i);
          }
...
  
private int getMembershipCount(RSInterfaceChild i) { ... }

 

 

Speaking of NPEs:

String text = membershipInterface.getText().replace("<col=00ff00>", "").replace("<col=ffff00>", "");
        if (text == null)
            return 0;

You called replace() on the returned value of getText() - which can be null, so the first line can throw a NPE.

Null checking text on line 2 is not required. If the program didn't crash at this point because of the previous line of code, text is guaranteed to be non-null.

 

Edited by Einstein
  • Like 1

Share this post


Link to post
Share on other sites
5 minutes ago, Einstein said:
 RSInterfaceChild membershipInterface = Interfaces.get(378, 74);
            if (membershipInterface != null) {
                if (getMembershipCount() < 1) {
 private int getMembershipCount() {
        RSInterfaceChild membershipInterface = Interfaces.get(378, 74);

        if (membershipInterface == null)
            return 0;

 

Instead of grabbing the interface again from scratch you could simply pass it as an argument to this method. If it passed the null check, it's good to go and it doesn't require additional checks:

RSInterfaceChild i = Interfaces.get(378, 74);
          if (i != null) {
             getMembershipCount(i);
          }
...
  
private int getMembershipCount(RSInterfaceChild i) { ... }

 

 

Speaking of NPEs:

String text = membershipInterface.getText().replace("<col=00ff00>", "").replace("<col=ffff00>", "");
        if (text == null)
            return 0;

You called replace() on the returned value of getText() - which can be null, so the first line can throw a NPE.

Null checking text on line 2 is not required. If the program didn't crash at this point because of the previous line of code, text is guaranteed to be non-null.

 

Noted, thanks. I'll amend shortly. :) 

Share this post


Link to post
Share on other sites

Can't edit the above post, here's the updated getMembershipCount code:

  private int getMembershipCount(RSInterfaceChild rsInterfaceChild) {

        String text = rsInterfaceChild.getText() != null ? rsInterfaceChild.getText().replace("<col=00ff00>", "").replace("<col=ffff00>", "") : "0";

        text = text.replaceAll("[^0-9]", "");

        General.println("[Membership Handler] : Members days remaining : " + Integer.parseInt(text));

       return Integer.parseInt(text);

    }

 

  • Like 1

Share this post


Link to post
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.

Sign in to follow this  

  • Our picks

    • Hello everyone,

      Last week we tried to roll out Auth0 Login, but we lost that battle. Now it's time to win the war!

      Important changes

      When logging into the client, you'll now have to enter your Auth0 account credentials instead of your forums credentials

      Note: 2FA is still handled through your forums account (for the time being)



      Changes for existing users

      You'll have to link your Auth0 account to your forums account here: https://tribot.org/forums/settings/login/?service=11


      Auth0 accounts have been created for most existing users. Please use your forums email address and password to login.



      Important notes

      Make sure to verify your email address upon creating a new Auth0 account


      When we mention your Auth0 account, we mean your account used for auth.tribot.org as displayed below
      • 69 replies
    • To better support the upcoming changes (TRiBot X, new repository), we're switching our login handler to Auth0. Instead of logging in with the standard form, you'll now be required to login through our Auth0 application.

      All existing accounts which have been used within approximately the past year have been imported into Auth0 using the same email and password combination which has been stored on the forums.

      What does this mean for users?

      Your account credentials are now even more securely stored


      You'll be able to login via Facebook, Google, and others in the future


      Is there anything users have to do differently now?

      Existing users: You'll have to login with the standard login, open your Account Settings, then link your Auth0 account


      New users: You'll be redirected to our Auth0 app (auth.tribot.org) where you'll be able to create an account


      Why was this change made?

      The new apps we are creating (such as the new repository) aren't able to use the forums to handle user logins


      To centralize all user accounts in one area


      To ensure that the client login doesn't go down when the forums are having problems


      To speed up our development


      Other considerations

      There's no documentation or official support for using Invision Community combined with Auth0, so there are still a few kinks we're working out


      We're in the works of creating an account management panel specifically for Auth0 accounts (ETA August)


      It's not possible to change email addresses for the time being (this will be resolved this August)


      Changing passwords is a weird process for the time being. To change your password, you'll have to use the "Don't remember your password" tool on the Auth0 login page
        • Like
      • 11 replies
    • Over the past month, we've been working hard on TRiBot's new repository - a much needed update. This change has been deemed necessary for TRiBot X, and will allow us to really speed up development of all aspects of TRiBot.

      Today we are going to share what we've been working on!


      Now you must be wondering what kind of features the new repository will have.... well, you'll have to be patient for a little while longer. We're still figuring out various technical aspects so we can't provide answers to all possible questions. We're also focusing on development rather than writing about it so that everyone can get access to our latest developments at lightning speed. I will however answer a few users' questions.

      We're planning on a release of this early to mid August, giving users some goodies before TRiBot X's release.

      Thank you all for being patient. I hope everyone is excited as much as I am!
        • Like
      • 17 replies
    • Over the past few months, I’ve been working diligently on a new project - TRiBot X. Everything has been written from the ground up, with all of the best practices of software engineering. Every aspect of TRiBot has been re-imagined to support three main goals: flexibility, useability, and reliability.
        • Like
      • 50 replies
    • Come give us feedback on the next version of TRiBot!
        • Thanks
        • Like
      • 86 replies
  • Recently Browsing   0 members

    No registered users viewing this page.

×
×
  • Create New...