Jump to content

[Snippet] CSleep, Syntactic Sugar for ConditionalSleep


Botre

Recommended Posts

Quick snippet that allows you to pass lambda expressions in order to evaluate ConditionalSleep.

 

Syntactic sugar:

CSleep.create(2400, 200, () -> !myPlayer().getPosition().equals(cache)).sleep();

vs.

new ConditionalSleep(2400,200) {@Override public boolean condition() throws InterruptedException { return !myPlayer().getPosition().equals(cache); }}.sleep();

Snippet:

package org.botre;

import java.util.function.Function;

import org.osbot.rs07.utility.ConditionalSleep;

public final class CSleep {

	private CSleep() {
	
	}
	
	public static ConditionalSleep create(int maximumMs, int deviationMs, BooleanSupplier condition) {
		return new ConditionalSleep(maximumMs, deviationMs) {
			@Override
			public boolean condition() throws InterruptedException {
				return condition.getAsBoolean();
			}
		};
	}
	
}
Edited by Botre
  • Like 4
Link to comment
Share on other sites

  • 2 weeks later...
package org.botre;

import java.util.function.Function;

import org.osbot.rs07.utility.ConditionalSleep;

public final class CSleep {

	private CSleep() {
	
	}
	
	public static ConditionalSleep create(int maximumMs, int deviationMs, BooleanSupplier condition) {
		return new ConditionalSleep(maximumMs, deviationMs) {
			@Override
			public boolean condition() throws InterruptedException {
				return condition.getAsBoolean();
			}
		};
	}
	
}

 

Alternatively you could just extend the ConditionalSleep class:

import org.osbot.rs07.utility.ConditionalSleep;
import java.util.function.BooleanSupplier;

public final class FConditionalSleep extends ConditionalSleep {

    private final BooleanSupplier condition;

    public FConditionalSleep(final BooleanSupplier condition, int timeout) {
        super(timeout);
        this.condition = condition;
    }

    @Override
    public boolean condition() throws InterruptedException {
        return condition.getAsBoolean();
    }
}
new FConditionalSleep(() -> myPlayer().isAnimating(), 5000).sleep();
Edited by Explv
  • Like 3
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...