Welcome to TRiBot Forums

Register now to gain access to all of our features. Once registered and logged in, you will be able to contribute to this site by submitting your own content or replying to existing content. You'll be able to customize your profile, receive reputation points as a reward for submitting content, while also communicating with other members via your own private inbox, plus much more! This message will be removed once you have signed in.

Beg

[TUTORIAL] Functional Condition

3 posts in this topic

One method classes such as Condition, https://tribot.org/doc/org/tribot/api/types/generic/Condition.html , can now be replaced by functional interfaces since Java 8 release.

@FunctionalInterface
public interface Condition07 {

    boolean active();

}

 

Why is this useful? 
We can take advantage of lambda expressions to write a much more compact version of anonymous inner classes.

Timing.waitCondition(new Condition() {
    @Override
    public boolean active() {
       General.sleep(100);
       return !isFletching();
    }
}, General.random(30000, 35000));

VS

Timing07.waitCondition(() -> !isFletching(), General.random(30000, 35000));

You can also have more than one statement on your lambda expression:

Timing07.waitCondition(() -> {
    AntiBan.timedActions();
    return !isFletching();
}, General.random(30000, 35000));

 

Setbacks
You would have to re implement API methods which use Condition such as Timing#waitCondition, which is widely used on most scripts.

import org.tribot.api.General;
import org.tribot.api.Timing;

public class Timing07 {

    public static boolean waitCondition(Condition07 condition, long timeout) {
        long t = Timing.currentTimeMillis() + timeout;
        while (!condition.active()) {
            if (Timing.currentTimeMillis() >= t)
                return false;
            General.sleep(100);
        }
        return true;
    }

}

 

@TRiLeZ It wouldn't take that long to replace Condition abstract class with Condition functional interface. You can still use the old fashion way (new Condition() ...) with functional interfaces, so, it's compatible with current existing scripts. 

2 people like this

Share this post


Link to post
Share on other sites

lool trilez knows he can make an interface but that would mean changing all of the parameters and code for methods that use conditions because they would have to look something like:

Timing07.waitCondition(() -> isSmithing(), 100, 8000)

Edited by Flamo353

Share this post


Link to post
Share on other sites

Actually, its not hard at all because he's probably using something like if(condition.active()) in the methods, which means he could swap the classes without having to change too much. He'll have to add in parameters for the frequency and timeout of the condition, which is probably the only reason why he won't do this. The other problem is that this update would break scripts if he were to remove the abstract class. So it's best to keep the abstract class + add the conditional interface to the api. It's a little bit tedious to update, but people will still be able to write conditions using 4 redundant lines of code and the abstract class, or they'll have the option to make use of lambdas like so:

WebWalking.walkTo(LUMBER_YARD_TILE, () -> LUMBER_YARD_TILE.getDistanceTo(Player.getPosition()) <= maxDist);

 

Edited by Flamo353

Share this post


Link to post
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!


Register a new account

Sign in

Already have an account? Sign in here.


Sign In Now

  • Recently Browsing   0 members

    No registered users viewing this page.